BlobController.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Configuration;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.Context.Configuration;
  9. using TEAMModelOS.SDK;
  10. using TEAMModelOS.SDK.Helper.Common.JsonHelper;
  11. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  12. using TEAMModelOS.SDK.DI;
  13. using System.Net.Http;
  14. using TEAMModelOS.SDK.Helper.Security.ShaHash;
  15. using TEAMModelOS.SDK.Extension;
  16. using System.IdentityModel.Tokens.Jwt;
  17. using Microsoft.AspNetCore.Authorization;
  18. using TEAMModelOS.Filter;
  19. using StackExchange.Redis;
  20. using Azure.Messaging.ServiceBus;
  21. using static TEAMModelOS.SDK.DI.AzureStorageBlobExtensions;
  22. using System.Linq;
  23. using Microsoft.AspNetCore.Http;
  24. using HTEXLib.COMM.Helpers;
  25. using TEAMModelOS.Models;
  26. using Microsoft.Extensions.Options;
  27. using TEAMModelOS.SDK.Models;
  28. using Azure.Cosmos;
  29. using Azure;
  30. using System.IO;
  31. using Azure.Storage.Blobs.Models;
  32. using Azure.Storage.Sas;
  33. using ContentTypeDict = TEAMModelOS.SDK.ContentTypeDict;
  34. namespace TEAMModelOS.Controllers.Core
  35. {
  36. [Route("blob")]
  37. [ApiController]
  38. public class BlobController : ControllerBase
  39. {
  40. private readonly AzureStorageFactory _azureStorage;
  41. private readonly IHttpClientFactory _clientFactory;
  42. private readonly AzureRedisFactory _azureRedis;
  43. private readonly AzureServiceBusFactory _serviceBus;
  44. private readonly DingDing _dingDing;
  45. private readonly Option _option;
  46. private readonly AzureCosmosFactory _azureCosmos;
  47. public IConfiguration _configuration { get; set; }
  48. public BlobController(AzureStorageFactory azureStorage, AzureServiceBusFactory serviceBus, IHttpClientFactory clientFactory, AzureRedisFactory azureRedis, IConfiguration configuration,
  49. DingDing dingDing,
  50. IOptionsSnapshot<Option> option, AzureCosmosFactory azureCosmos)
  51. {
  52. _azureStorage = azureStorage;
  53. _clientFactory = clientFactory;
  54. _serviceBus = serviceBus;
  55. _azureRedis = azureRedis;
  56. _configuration = configuration;
  57. _dingDing = dingDing;
  58. _option = option?.Value;
  59. _azureCosmos = azureCosmos;
  60. }
  61. /// <summary>
  62. /// 上传文件到指定的0-public
  63. /// </summary>
  64. /// <param name="request"></param>
  65. /// <returns></returns>
  66. [HttpPost("public-upload")]
  67. //[AuthToken(Roles = "teacher,admin")]
  68. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  69. public async Task<IActionResult> PublicUpload([FromForm] IFormFile file)
  70. {
  71. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  72. string fileExt = FileType.GetExtention(file.FileName).ToLower();
  73. if (ContentTypeDict.dict.ContainsKey($".{fileExt}"))
  74. {
  75. var url= await _azureStorage.UploadFileByContainer("0-public", file.OpenReadStream(), "school", $"{Guid.NewGuid()}.{fileExt}", false);
  76. return Ok(new { url });
  77. }
  78. else {
  79. return BadRequest();
  80. }
  81. }
  82. /// <summary>
  83. /// 获取某个容器的只读权限
  84. /// </summary>
  85. /// <param name="request"></param>
  86. /// <returns></returns>
  87. [HttpPost("sas-r")]
  88. public IActionResult BlobSasR(BlobSas request)
  89. {
  90. ///返回金钥过期时间
  91. // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
  92. // dict.Add(d.Key, d.Value);
  93. return Ok(_azureStorage.GetContainerSasUri(request, true));
  94. }
  95. /// <summary>
  96. /// 某个文件的上传SAS rcw权限
  97. /// </summary>
  98. /// <param name="request"></param>
  99. /// <returns></returns>
  100. [HttpPost("sas-rcwld")]
  101. public IActionResult BlobSasRCW(BlobSas request)
  102. {
  103. ///返回金钥过期时间
  104. // Dictionary<string,object> dict= await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
  105. // Dictionary<string, object> dict = ;
  106. //dict.Add(d.Key, d.Value);
  107. return Ok(_azureStorage.GetContainerSasUri(request, false));
  108. }
  109. /// <summary>
  110. /// 链接只读(读)
  111. /// </summary>
  112. /// <param name="azureBlobSASDto"></param>
  113. /// <returns></returns>
  114. [HttpPost("sas-url-r")]
  115. public IActionResult GetContainerSASRead(JsonElement azureBlobSASDto)
  116. {
  117. azureBlobSASDto.TryGetProperty("url", out JsonElement azureBlobSAS);
  118. //string azureBlobSAS = azureBlobSASDto;
  119. (string, string) a = BlobUrlString(azureBlobSAS.ToString());
  120. string ContainerName = a.Item1;
  121. string BlobName = a.Item2;
  122. bool flg = IsBlobName(BlobName);
  123. if (flg)
  124. {
  125. return Ok(_azureStorage.GetBlobSasUriRead(ContainerName, BlobName));
  126. }
  127. else
  128. {
  129. return BadRequest("文件名错误");
  130. };
  131. }
  132. /// <summary>
  133. /// 获取文件内容
  134. /// </summary>
  135. /// <param name="azureBlobSASDto"></param>
  136. /// <returns></returns>
  137. [HttpPost("get-text")]
  138. public async Task<IActionResult> GetText(JsonElement request)
  139. {
  140. request.TryGetProperty("code", out JsonElement code);
  141. string azureBlobSAS = System.Web.HttpUtility.UrlDecode(code.ToString(), Encoding.UTF8);
  142. (string, string) a = BlobUrlString(azureBlobSAS);
  143. string ContainerName = a.Item1;
  144. string BlobName = a.Item2;
  145. bool flg = IsBlobName(BlobName);
  146. if (flg)
  147. {
  148. //TODO 需驗證
  149. BlobAuth blobAuth= _azureStorage.GetBlobSasUriRead(ContainerName, BlobName);
  150. var response= await _clientFactory.CreateClient().GetAsync(new Uri(blobAuth.url + blobAuth.sas));
  151. response.EnsureSuccessStatusCode();
  152. using var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
  153. return Ok(json.RootElement);
  154. }
  155. else
  156. {
  157. return BadRequest("文件名错误");
  158. };
  159. }
  160. /// <summary>
  161. /// {"containerName":"hbcn","cache":true,"scope":"school"}
  162. /// 获取容器的 分类及总量
  163. /// </summary>
  164. /// <param name="azureBlobSASDto"></param>
  165. /// <returns></returns>
  166. [HttpPost("used-space")]
  167. public async Task<ActionResult> UsedSpace(JsonElement request)
  168. {
  169. try
  170. {
  171. //学校已经分配给所有教师的空间大小GB。
  172. long teach = 0;
  173. request.TryGetProperty("scope", out JsonElement _scope);
  174. request.TryGetProperty("containerName", out JsonElement containerName);
  175. if (_scope.ValueKind.Equals(JsonValueKind.String) && _scope.GetString().Equals("school")) {
  176. var client = _azureCosmos.GetCosmosClient();
  177. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
  178. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{containerName}") })) {
  179. var json = await JsonDocument.ParseAsync(item.ContentStream);
  180. foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
  181. {
  182. if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
  183. {
  184. teach = _size.GetInt32();
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. var name =containerName.GetString();
  191. long blobsize = 0;
  192. RedisValue value = default;
  193. value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", name);
  194. if (value != default && !value.IsNullOrEmpty)
  195. {
  196. JsonElement record = value.ToString().ToObject<JsonElement>();
  197. if (record.TryGetInt64(out blobsize))
  198. {
  199. }
  200. }
  201. else
  202. {
  203. var client = _azureStorage.GetBlobContainerClient(name);
  204. var size = await client.GetBlobsCatalogSize();
  205. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  206. foreach (var key in size.Item2.Keys)
  207. {
  208. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  209. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  210. }
  211. return Ok(new { size = size.Item1, catalog = size.Item2 ,teach });
  212. }
  213. Dictionary<string, double> catalog = new Dictionary<string, double>();
  214. SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{name}");
  215. if (Scores != null)
  216. {
  217. foreach (var score in Scores)
  218. {
  219. double val = score.Score;
  220. string key = score.Element.ToString();
  221. catalog.Add(key, val);
  222. }
  223. return Ok(new { size = blobsize, catalog = catalog, teach });
  224. }
  225. else {
  226. var client = _azureStorage.GetBlobContainerClient(name);
  227. var size = await client.GetBlobsCatalogSize();
  228. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  229. foreach (var key in size.Item2.Keys)
  230. {
  231. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  232. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  233. }
  234. return Ok(new { size = size.Item1, catalog = size.Item2, teach });
  235. }
  236. }
  237. catch (Exception ex){
  238. await _dingDing.SendBotMsg($"IES5,{_option.Location},blon/get-blobsize()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  239. }
  240. return BadRequest();
  241. }
  242. private static (string, string) BlobUrlString(string sasUrl)
  243. {
  244. sasUrl = sasUrl.Substring(8);
  245. string[] sasUrls = sasUrl.Split("/");
  246. string ContainerName;
  247. ContainerName = sasUrls[1].Clone().ToString();
  248. string item = sasUrls[0] + "/" + sasUrls[1] + "/";
  249. string blob = sasUrl.Replace(item, "");
  250. return (ContainerName, blob);
  251. }
  252. public static bool IsBlobName(string BlobName)
  253. {
  254. return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
  255. @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
  256. }
  257. /// <summary>
  258. ///
  259. /// </summary>
  260. /// <param name="request"></param>
  261. /// <returns></returns>
  262. [HttpPost("bloblog-list")]
  263. public async Task<ActionResult> BloblogList(JsonElement request)
  264. {
  265. List<Bloblog> bloblogs = new List<Bloblog>();
  266. try
  267. {
  268. request.TryGetProperty("name", out JsonElement name);
  269. request.TryGetProperty("type", out JsonElement type);
  270. request.TryGetProperty("scope", out JsonElement scope);
  271. request.TryGetProperty("periodId", out JsonElement periodId);
  272. var client = _azureCosmos.GetCosmosClient();
  273. if (scope.GetString().Equals("school"))
  274. {
  275. var queryslt = new StringBuilder($"SELECT value(c) FROM c join A1 in c.periodId WHERE c.type='{type}' and A1 in ('{periodId}') ");
  276. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  277. {
  278. bloblogs.Add(item);
  279. }
  280. }
  281. else if (scope.GetString().Equals("private"))
  282. {
  283. var queryslt = new StringBuilder($"SELECT value(c) FROM c WHERE c.type='{type}' ");
  284. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  285. {
  286. bloblogs.Add(item);
  287. }
  288. }
  289. return Ok(new { bloblogs = bloblogs });
  290. }
  291. catch (Exception ex)
  292. {
  293. return Ok(new { bloblogs = bloblogs });
  294. }
  295. }
  296. /// <summary>
  297. /// 重命名
  298. /// {"scope":"school","cntr":"hbcn","id":"bbf24ca7-487e-4196-8c99-b2c418a2d1b1","newName":"video/核能1.mp4"}
  299. /// </summary>
  300. /// <param name="json"></param>
  301. /// <returns></returns>
  302. [HttpPost("bloblog-rename")]
  303. public async Task<ActionResult> BloblogRename(JsonElement request)
  304. {
  305. var client = _azureCosmos.GetCosmosClient();
  306. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  307. if(!request.TryGetProperty("cntr", out JsonElement _cntr))return BadRequest();
  308. if (!request.TryGetProperty("id", out JsonElement _id))return BadRequest();
  309. if (!request.TryGetProperty("newName", out JsonElement _newName))return BadRequest();
  310. string tbname= $"{_scope}".Equals("school",StringComparison.OrdinalIgnoreCase) ? "School": "Teacher";
  311. try
  312. {
  313. Bloblog bloblog = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<Bloblog>($"{_id}", new PartitionKey($"Bloblog-{_cntr}"));
  314. string oldUrl = bloblog.url;
  315. if (oldUrl.Equals($"{_newName}")) {
  316. return Ok();
  317. }
  318. if (oldUrl.StartsWith("res", StringComparison.OrdinalIgnoreCase) && oldUrl.EndsWith(".htex", StringComparison.OrdinalIgnoreCase)) {
  319. oldUrl = oldUrl.Replace(".htex", "", StringComparison.OrdinalIgnoreCase);
  320. }
  321. string newName = $"{_newName}";
  322. if (newName.StartsWith("res", StringComparison.OrdinalIgnoreCase) && newName.EndsWith(".htex", StringComparison.OrdinalIgnoreCase))
  323. {
  324. newName = newName.Replace(".htex", "", StringComparison.OrdinalIgnoreCase);
  325. }
  326. bloblog.name = $"{_newName}";
  327. bloblog.url = $"{_newName}";
  328. var bcc = _azureStorage.GetBlobContainerClient($"{_cntr}");
  329. string px = oldUrl;
  330. if (oldUrl.StartsWith("/"))
  331. {
  332. px = oldUrl.Substring(1);
  333. }
  334. List<BlobItem> blobItems = new List<BlobItem>();
  335. await foreach (var item in bcc.GetBlobsAsync(BlobTraits.None, BlobStates.None, px)) {
  336. blobItems.Add(item);
  337. }
  338. foreach (var item in blobItems)
  339. {
  340. if (item.Name.StartsWith("image") || item.Name.StartsWith("video"))
  341. {
  342. var thum = $"thum{ item.Name.Substring(5)}";
  343. var tnewName = $"thum{ newName.Substring(5)}";
  344. var tpx = $"thum{ px.Substring(5)}";
  345. string tname = thum.Replace(tpx, tnewName);
  346. if (item.Name.StartsWith("video")) {
  347. string fileexturl = thum.Substring(thum.LastIndexOf(".") > 0 ? thum.LastIndexOf(".") : 0);
  348. thum = thum.Replace(fileexturl, ".png");
  349. string fileextname = tname.Substring(tname.LastIndexOf(".") > 0 ? tname.LastIndexOf(".") : 0);
  350. tname = tname.Replace(fileextname, ".png");
  351. }
  352. var turl = _azureStorage.GetBlobSAS($"{_cntr}", thum, BlobSasPermissions.Read | BlobSasPermissions.List);
  353. bcc.GetBlobClient(tname).SyncCopyFromUri(new Uri(turl));
  354. await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { thum });
  355. }
  356. string targetName = item.Name.Replace(px, newName);
  357. var url = _azureStorage.GetBlobSAS($"{_cntr}", item.Name, BlobSasPermissions.Read | BlobSasPermissions.List);
  358. bcc.GetBlobClient(targetName).SyncCopyFromUri(new Uri(url));
  359. };
  360. await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { px });
  361. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<Bloblog>(bloblog, $"{_id}", new PartitionKey($"Bloblog-{_cntr}"));
  362. string u = "";
  363. string[] uls = System.Web.HttpUtility.UrlDecode(px, Encoding.UTF8).Split("/");
  364. if (uls != null)
  365. {
  366. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  367. }
  368. if (!string.IsNullOrEmpty(u)) {
  369. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = u, name = $"{_cntr}" }.ToJsonString()); ;
  370. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  371. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  372. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  373. }
  374. return Ok();
  375. }
  376. catch (CosmosException ex)
  377. {
  378. return BadRequest();
  379. }
  380. catch (Exception ex )
  381. {
  382. return BadRequest();
  383. }
  384. }
  385. /*
  386. 新增 编辑接口
  387. {
  388. "periodId": "",
  389. "scope": "school",
  390. "name": "hbcn",
  391. "url": "video/xxx.png",
  392. "opt": "add",
  393. }
  394. */
  395. /*
  396. {
  397. "scope": "school",
  398. "name": "hbcn",
  399. "opt": "del",
  400. "id": "19ccce98-c524-4ea7-aabc-887d1391e551"
  401. }
  402. */
  403. /// <summary>
  404. ///
  405. /// </summary>
  406. /// <param name="request"></param>
  407. /// <returns></returns>
  408. [HttpPost("bloblog-upsert")]
  409. public async Task<ActionResult> BloblogOpt(JsonElement request) {
  410. try
  411. {
  412. request.TryGetProperty("periodId", out JsonElement periodId);
  413. request.TryGetProperty("subjectId", out JsonElement subjectId);
  414. request.TryGetProperty("gradeId", out JsonElement gradeId);
  415. request.TryGetProperty("scope", out JsonElement scope);
  416. request.TryGetProperty("name", out JsonElement name);
  417. request.TryGetProperty("url", out JsonElement jurls);
  418. request.TryGetProperty("id", out JsonElement ids);
  419. //获取文件的大小
  420. var client = _azureCosmos.GetCosmosClient();
  421. List<string> urls = jurls.ToObject<List<string>>();
  422. List<Bloblog> bloblog = new List<Bloblog>();
  423. foreach (var uri in urls) {
  424. var url = System.Web.HttpUtility.UrlDecode(uri, Encoding.UTF8);
  425. string[] uls = url.Split("/");
  426. var u = "";
  427. if (uls != null)
  428. {
  429. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  430. }
  431. var size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(url);
  432. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  433. //地址相同的,直接更新
  434. bool exsit = false;
  435. try
  436. {
  437. var queryslt = $"SELECT value(c) FROM c WHERE c.url='{url}'";
  438. if (scope.GetString().Equals("school"))
  439. {
  440. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Bloblog>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  441. {
  442. item.time = now;
  443. item.size = size != null && size.HasValue ? size.Value : 0;
  444. item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" };
  445. item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" };
  446. item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" };
  447. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
  448. bloblog.Add(item);
  449. exsit = true;
  450. }
  451. }
  452. else if (scope.GetString().Equals("private"))
  453. {
  454. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Bloblog>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  455. {
  456. item.time = now;
  457. item.size = size != null && size.HasValue ? size.Value : 0;
  458. item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" };
  459. item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" };
  460. item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" };
  461. await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
  462. bloblog.Add(item);
  463. exsit = true;
  464. }
  465. }
  466. }
  467. catch (Exception ex)
  468. {
  469. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  470. }
  471. if (!exsit)
  472. {
  473. var blob = new Bloblog
  474. {
  475. id = Guid.NewGuid().ToString(),
  476. pk = "Bloblog",
  477. code = $"Bloblog-{name}",
  478. url = url,
  479. time = now,
  480. size = size != null && size.HasValue ? size.Value : 0,
  481. periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" },
  482. subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" },
  483. gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" },
  484. type = u
  485. };
  486. if (scope.GetString().Equals("school"))
  487. {
  488. await client.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
  489. }
  490. else if (scope.GetString().Equals("private"))
  491. {
  492. await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
  493. }
  494. bloblog.Add(blob);
  495. }
  496. }
  497. return Ok(new { bloblog, status = 200 });
  498. }
  499. catch (Exception ex)
  500. {
  501. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}{ex.StackTrace}\n{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  502. }
  503. return Ok(new { status = 200 });
  504. }
  505. /// <summary>
  506. /// 删除prefix 不管是内容 模块还是其他试题试卷 评测 问卷投票等都在使用。
  507. ///
  508. /// {"cntr":"","prefix":"res/test"}
  509. /// </summary>
  510. /// <param name="request"></param>
  511. /// <returns></returns>
  512. [HttpPost("delete-prefix")]
  513. [AuthToken(Roles = "teacher,admin")]
  514. public async Task<IActionResult> DeletePrefix(JsonElement json)
  515. {
  516. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  517. string blobContainerName = null;
  518. string prefix = null;
  519. if (json.TryGetProperty("cntr", out JsonElement cntr) && cntr.ValueKind.Equals(JsonValueKind.String))
  520. {
  521. var cntrs = cntr.GetString();
  522. if (cntrs.Equals(id))
  523. {
  524. blobContainerName = id;
  525. }
  526. else if (cntrs.Equals(school))
  527. {
  528. blobContainerName = school;
  529. }
  530. else
  531. {
  532. return BadRequest("只能删除本人管理的文件夹");
  533. }
  534. }
  535. if (json.TryGetProperty("prefix", out JsonElement prefixjson) && prefixjson.ValueKind.Equals(JsonValueKind.String))
  536. {
  537. prefix = prefixjson.GetString();
  538. }
  539. if (prefix != null && blobContainerName != null)
  540. {
  541. var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, new List<string> { prefix });
  542. string u = "";
  543. string[] uls = System.Web.HttpUtility.UrlDecode($"{prefixjson}", Encoding.UTF8).Split("/");
  544. if (uls != null)
  545. {
  546. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  547. }
  548. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = u, name = $"{blobContainerName}" }.ToJsonString()); ;
  549. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  550. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  551. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  552. return Ok(new { status });
  553. }
  554. else
  555. {
  556. return BadRequest();
  557. }
  558. }
  559. public record ContBlob {
  560. public string path { get; set; }
  561. public string id { get; set; }
  562. }
  563. /// <summary>
  564. /// 删除多个Url,只会在内容模块使用该接口
  565. ///
  566. ///
  567. /// {"scope":"school","cntr":"hbcn","blobs":[{"path":"other/test/1.json","id":"c107069d-4553-46c2-8c81-b3e6b4599393"},{"path":"res/test","id":"09d59b87-68c0-45fa-8221-9931a4190a2f"}]}
  568. /// </summary>
  569. /// <param name="request"></param>
  570. /// <returns></returns>
  571. [HttpPost("bloblog-delete")]
  572. [AuthToken(Roles = "teacher,admin")]
  573. public async Task<IActionResult> DeleteBlobs(JsonElement json)
  574. {
  575. ///BlobBaseClient copy
  576. try {
  577. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  578. string blobContainerName = null;
  579. if (!json.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  580. if (!json.TryGetProperty("blobs", out JsonElement _blobs)) return BadRequest();
  581. if (json.TryGetProperty("cntr", out JsonElement cntr))
  582. {
  583. var cntrs = cntr.ToString();
  584. if (cntrs.Equals(id))
  585. {
  586. blobContainerName = id;
  587. }
  588. else if (cntrs.Equals(school))
  589. {
  590. blobContainerName = school;
  591. }
  592. else
  593. {
  594. return BadRequest("只能删除本人管理的文件");
  595. }
  596. }
  597. bool flag = true;
  598. List<ContBlob> blobs = _blobs.ToObject<List<ContBlob>>();
  599. try
  600. {
  601. var client = _azureCosmos.GetCosmosClient();
  602. List<string> ids = blobs.Select(x => x.id).ToList();
  603. string containerId = "School";
  604. if (_scope.GetString().Equals("school"))
  605. {
  606. containerId = "School";
  607. }
  608. else if (_scope.GetString().Equals("private"))
  609. {
  610. containerId = "Teacher";
  611. }
  612. await client.GetContainer(Constant.TEAMModelOS, containerId).DeleteItemsAsync<Bloblog>(ids, $"Bloblog-{blobContainerName}");
  613. }
  614. catch (CosmosException ex )
  615. {
  616. //仅处理 cosmos不存在 但容器又存在的
  617. }
  618. if (flag)
  619. {
  620. var urls = blobs.Select(x => x.path).ToList();
  621. List<string> deleteUrl = new List<string>();
  622. urls.ForEach(x => {
  623. string delUrl = x;
  624. if (x.StartsWith("res") && x.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
  625. {
  626. delUrl = x.Substring(0, x.Length - 4);
  627. }
  628. //自动删除视频和图片的缩略图
  629. if (x.StartsWith("image")||x.StartsWith("video")) {
  630. var thum = $"thum{ x.Substring(5)}" ;
  631. if (x.StartsWith("video"))
  632. {
  633. string fileexturl = thum.Substring(thum.LastIndexOf(".") > 0 ? thum.LastIndexOf(".") : 0);
  634. thum = thum.Replace(fileexturl, ".png");
  635. }
  636. deleteUrl.Add(thum);
  637. }
  638. deleteUrl.Add(delUrl);
  639. });
  640. var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, deleteUrl);
  641. //释放的空间
  642. HashSet<string> root = new HashSet<string>();
  643. foreach (var x in deleteUrl)
  644. {
  645. string url = System.Web.HttpUtility.UrlDecode(x, Encoding.UTF8);
  646. string[] uls = url.Split("/");
  647. if (uls != null)
  648. {
  649. string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  650. root.Add(u);
  651. }
  652. }
  653. root.ToList().ForEach(async x => {
  654. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = x, name = $"{blobContainerName}" }.ToJsonString()); ;
  655. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  656. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  657. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  658. });
  659. return Ok(new { status });
  660. }
  661. else
  662. {
  663. return BadRequest("只能删除本人管理的文件");
  664. }
  665. } catch (Exception ex) {
  666. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/delete-blobs\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  667. return BadRequest();
  668. }
  669. }
  670. /// <summary>
  671. /// 列出blob的
  672. ///
  673. /// {"cntr":"","urls":["res/test/1.json","res/test/2.json"]}
  674. /// {"scope":"school","cntr":"hbcn","blobs":[{"path":"other/test/1.json","id":"c107069d-4553-46c2-8c81-b3e6b4599393"},{"path":"res/test","id":"09d59b87-68c0-45fa-8221-9931a4190a2f"}]}
  675. /// </summary>
  676. /// <param name="request"></param>
  677. /// <returns></returns>
  678. [HttpPost("blob-list")]
  679. [AuthToken(Roles = "teacher,admin,student")]
  680. public async Task<IActionResult> BlobList(JsonElement json) {
  681. var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  682. List<string> paths = new List<string>();
  683. //文件的容器
  684. if (!json.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
  685. //业务存取类型 exam,vote,survey,item,paper,syllabus,records,content(doc,image,res,video,audio,other),thum,train,temp,jyzx
  686. if (!json.TryGetProperty("type", out JsonElement _type)) return BadRequest();
  687. //文件前缀prefix
  688. switch (true)
  689. {
  690. case bool when $"{_type}".Equals("exam", StringComparison.OrdinalIgnoreCase)
  691. || $"{_type}".Equals("vote", StringComparison.OrdinalIgnoreCase)
  692. || $"{_type}".Equals("survey", StringComparison.OrdinalIgnoreCase)
  693. || $"{_type}".Equals("item", StringComparison.OrdinalIgnoreCase)
  694. || $"{_type}".Equals("paper", StringComparison.OrdinalIgnoreCase):
  695. string type = $"{_type}".Substring(0, 1).ToUpper() + $"{_type}".Substring(1);
  696. //业务存取id
  697. if (!json.TryGetProperty("id", out JsonElement _aid)) return BadRequest();
  698. //业务存取分区键
  699. if (!json.TryGetProperty("code", out JsonElement _acode)) return BadRequest();
  700. //业务存取分区键
  701. if (!json.TryGetProperty("scope", out JsonElement _ascope)) return BadRequest();
  702. break;
  703. case bool when $"{_type}".Equals("doc", StringComparison.OrdinalIgnoreCase)
  704. || $"{_type}".Equals("image", StringComparison.OrdinalIgnoreCase)
  705. || $"{_type}".Equals("res", StringComparison.OrdinalIgnoreCase)
  706. || $"{_type}".Equals("video", StringComparison.OrdinalIgnoreCase)
  707. || $"{_type}".Equals("audio", StringComparison.OrdinalIgnoreCase)
  708. || $"{_type}".Equals("other", StringComparison.OrdinalIgnoreCase):
  709. //业务存取id
  710. if (!json.TryGetProperty("id", out JsonElement _bid)) return BadRequest();
  711. //业务存取分区键
  712. if (!json.TryGetProperty("code", out JsonElement _bcode)) return BadRequest();
  713. //业务存取分区键
  714. if (!json.TryGetProperty("scope", out JsonElement _bscope)) return BadRequest();
  715. break;
  716. case bool when $"{_type}".Equals("records", StringComparison.OrdinalIgnoreCase)
  717. || $"{_type}".Equals("syllabus", StringComparison.OrdinalIgnoreCase)
  718. || $"{_type}".Equals("thum", StringComparison.OrdinalIgnoreCase)
  719. || $"{_type}".Equals("temp", StringComparison.OrdinalIgnoreCase)
  720. || $"{_type}".Equals("jyzx", StringComparison.OrdinalIgnoreCase)
  721. || $"{_type}".Equals("train", StringComparison.OrdinalIgnoreCase):
  722. break;
  723. default:
  724. break;
  725. }
  726. var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { "aaa" });
  727. return Ok(new { paths ,status=1});
  728. }
  729. }
  730. }