BlobController.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 TEAMModelOS.SDK.Context.Constant.Common;
  25. using HTEXLib.COMM.Helpers;
  26. using TEAMModelOS.Models;
  27. using Microsoft.Extensions.Options;
  28. using TEAMModelOS.SDK.Models;
  29. using Azure.Cosmos;
  30. using Azure;
  31. using System.IO;
  32. using Azure.Storage.Blobs.Models;
  33. using Azure.Storage.Sas;
  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("get-blobsize")]
  167. public async Task<ActionResult> GetBlobsSize(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("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. // request.TryGetProperty("cache", out JsonElement cache);
  191. var name =containerName.GetString();
  192. //if (cache.GetBoolean())
  193. //{
  194. long blobsize = 0;
  195. RedisValue value = default;
  196. value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", name);
  197. if (value != default && !value.IsNullOrEmpty)
  198. {
  199. JsonElement record = value.ToString().ToObject<JsonElement>();
  200. if (record.TryGetInt64(out blobsize))
  201. {
  202. }
  203. }
  204. else
  205. {
  206. var client = _azureStorage.GetBlobContainerClient(name);
  207. var size = await client.GetBlobsCatalogSize();
  208. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  209. foreach (var key in size.Item2.Keys)
  210. {
  211. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  212. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  213. }
  214. return Ok(new { size = size.Item1, catalog = size.Item2 ,teach });
  215. }
  216. Dictionary<string, double> catalog = new Dictionary<string, double>();
  217. SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{name}");
  218. if (Scores != null)
  219. {
  220. foreach (var score in Scores)
  221. {
  222. double val = score.Score;
  223. string key = score.Element.ToString();
  224. catalog.Add(key, val);
  225. }
  226. return Ok(new { size = blobsize, catalog = catalog, teach });
  227. }
  228. else {
  229. var client = _azureStorage.GetBlobContainerClient(name);
  230. var size = await client.GetBlobsCatalogSize();
  231. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  232. foreach (var key in size.Item2.Keys)
  233. {
  234. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  235. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  236. }
  237. return Ok(new { size = size.Item1, catalog = size.Item2, teach });
  238. }
  239. // }
  240. // else {
  241. //var client = _azureStorage.GetBlobContainerClient(name);
  242. //var size = await client.GetBlobsCatalogSize();
  243. //await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  244. //foreach (var key in size.Item2.Keys)
  245. //{
  246. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  247. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  248. //}
  249. //return Ok(new { size = size.Item1, catalog = size.Item2, teach });
  250. // }
  251. }
  252. catch (Exception ex){
  253. await _dingDing.SendBotMsg($"IES5,{_option.Location},blon/get-blobsize()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  254. }
  255. return BadRequest();
  256. }
  257. private static (string, string) BlobUrlString(string sasUrl)
  258. {
  259. sasUrl = sasUrl.Substring(8);
  260. string[] sasUrls = sasUrl.Split("/");
  261. string ContainerName;
  262. ContainerName = sasUrls[1].Clone().ToString();
  263. string item = sasUrls[0] + "/" + sasUrls[1] + "/";
  264. string blob = sasUrl.Replace(item, "");
  265. return (ContainerName, blob);
  266. }
  267. public static bool IsBlobName(string BlobName)
  268. {
  269. return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
  270. @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
  271. }
  272. /// <summary>
  273. ///
  274. /// </summary>
  275. /// <param name="request"></param>
  276. /// <returns></returns>
  277. [HttpPost("bloblog-list")]
  278. public async Task<ActionResult> BloblogList(JsonElement request)
  279. {
  280. List<Bloblog> bloblogs = new List<Bloblog>();
  281. try
  282. {
  283. request.TryGetProperty("name", out JsonElement name);
  284. request.TryGetProperty("type", out JsonElement type);
  285. request.TryGetProperty("scope", out JsonElement scope);
  286. request.TryGetProperty("periodId", out JsonElement periodId);
  287. var client = _azureCosmos.GetCosmosClient();
  288. if (scope.GetString().Equals("school"))
  289. {
  290. var queryslt = new StringBuilder($"SELECT value(c) FROM c join A1 in c.periodId WHERE c.type='{type}' and A1 in ('{periodId}') ");
  291. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  292. {
  293. bloblogs.Add(item);
  294. }
  295. }
  296. else if (scope.GetString().Equals("private"))
  297. {
  298. var queryslt = new StringBuilder($"SELECT value(c) FROM c WHERE c.type='{type}' ");
  299. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  300. {
  301. bloblogs.Add(item);
  302. }
  303. }
  304. return Ok(new { bloblogs = bloblogs });
  305. }
  306. catch (Exception ex)
  307. {
  308. return Ok(new { bloblogs = bloblogs });
  309. }
  310. }
  311. /// <summary>
  312. /// 重命名
  313. /// {"scope":"school","cntr":"hbcn","id":"bbf24ca7-487e-4196-8c99-b2c418a2d1b1","newName":"video/核能1.mp4"}
  314. /// </summary>
  315. /// <param name="json"></param>
  316. /// <returns></returns>
  317. [HttpPost("bloblog-rename")]
  318. public async Task<ActionResult> BloblogRename(JsonElement request)
  319. {
  320. var client = _azureCosmos.GetCosmosClient();
  321. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  322. if(!request.TryGetProperty("cntr", out JsonElement _cntr))return BadRequest();
  323. if (!request.TryGetProperty("id", out JsonElement _id))return BadRequest();
  324. if (!request.TryGetProperty("newName", out JsonElement _newName))return BadRequest();
  325. string tbname= $"{_scope}".Equals("school",StringComparison.OrdinalIgnoreCase) ? "School": "Teacher";
  326. try
  327. {
  328. Bloblog bloblog = await client.GetContainer("TEAMModelOS", tbname).ReadItemAsync<Bloblog>($"{_id}", new PartitionKey($"Bloblog-{_cntr}"));
  329. string oldUrl = bloblog.url;
  330. if (oldUrl.Equals($"{_newName}")) {
  331. return Ok();
  332. }
  333. if (oldUrl.StartsWith("res", StringComparison.OrdinalIgnoreCase) && oldUrl.EndsWith(".htex", StringComparison.OrdinalIgnoreCase)) {
  334. oldUrl = oldUrl.Replace(".htex", "", StringComparison.OrdinalIgnoreCase);
  335. }
  336. string newName = $"{_newName}";
  337. if (newName.StartsWith("res", StringComparison.OrdinalIgnoreCase) && newName.EndsWith(".htex", StringComparison.OrdinalIgnoreCase))
  338. {
  339. newName = newName.Replace(".htex", "", StringComparison.OrdinalIgnoreCase);
  340. }
  341. bloblog.name = $"{_newName}";
  342. bloblog.url = $"{_newName}";
  343. var bcc = _azureStorage.GetBlobContainerClient($"{_cntr}");
  344. string px = oldUrl;
  345. if (oldUrl.StartsWith("/"))
  346. {
  347. px = oldUrl.Substring(1);
  348. }
  349. List<BlobItem> blobItems = new List<BlobItem>();
  350. await foreach (var item in bcc.GetBlobsAsync(BlobTraits.None, BlobStates.None, px)) {
  351. blobItems.Add(item);
  352. }
  353. foreach (var item in blobItems)
  354. {
  355. if (item.Name.StartsWith("image") || item.Name.StartsWith("video"))
  356. {
  357. var thum = $"thum{ item.Name.Substring(5)}";
  358. var tnewName = $"thum{ newName.Substring(5)}";
  359. var tpx = $"thum{ px.Substring(5)}";
  360. string tname = thum.Replace(tpx, tnewName);
  361. if (item.Name.StartsWith("video")) {
  362. string fileexturl = thum.Substring(thum.LastIndexOf(".") > 0 ? thum.LastIndexOf(".") : 0);
  363. thum = thum.Replace(fileexturl, ".png");
  364. string fileextname = tname.Substring(tname.LastIndexOf(".") > 0 ? tname.LastIndexOf(".") : 0);
  365. tname = tname.Replace(fileextname, ".png");
  366. }
  367. var turl = _azureStorage.GetBlobSAS($"{_cntr}", thum, BlobSasPermissions.Read | BlobSasPermissions.List);
  368. bcc.GetBlobClient(tname).SyncCopyFromUri(new Uri(turl));
  369. await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { thum });
  370. }
  371. string targetName = item.Name.Replace(px, newName);
  372. var url = _azureStorage.GetBlobSAS($"{_cntr}", item.Name, BlobSasPermissions.Read | BlobSasPermissions.List);
  373. bcc.GetBlobClient(targetName).SyncCopyFromUri(new Uri(url));
  374. };
  375. await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { px });
  376. await client.GetContainer("TEAMModelOS", tbname).ReplaceItemAsync<Bloblog>(bloblog, $"{_id}", new PartitionKey($"Bloblog-{_cntr}"));
  377. string u = "";
  378. string[] uls = System.Web.HttpUtility.UrlDecode(px, Encoding.UTF8).Split("/");
  379. if (uls != null)
  380. {
  381. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  382. }
  383. if (!string.IsNullOrEmpty(u)) {
  384. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = u, name = $"{_cntr}" }.ToJsonString()); ;
  385. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  386. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  387. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  388. }
  389. return Ok();
  390. }
  391. catch (CosmosException ex)
  392. {
  393. return BadRequest();
  394. }
  395. catch (Exception ex )
  396. {
  397. return BadRequest();
  398. }
  399. }
  400. /*
  401. 新增 编辑接口
  402. {
  403. "periodId": "",
  404. "scope": "school",
  405. "name": "hbcn",
  406. "url": "video/xxx.png",
  407. "opt": "add",
  408. }
  409. */
  410. /*
  411. {
  412. "scope": "school",
  413. "name": "hbcn",
  414. "opt": "del",
  415. "id": "19ccce98-c524-4ea7-aabc-887d1391e551"
  416. }
  417. */
  418. /// <summary>
  419. ///
  420. /// </summary>
  421. /// <param name="request"></param>
  422. /// <returns></returns>
  423. [HttpPost("bloblog-upsert")]
  424. public async Task<ActionResult> BloblogOpt(JsonElement request) {
  425. try
  426. {
  427. request.TryGetProperty("periodId", out JsonElement periodId);
  428. request.TryGetProperty("subjectId", out JsonElement subjectId);
  429. request.TryGetProperty("gradeId", out JsonElement gradeId);
  430. request.TryGetProperty("scope", out JsonElement scope);
  431. request.TryGetProperty("name", out JsonElement name);
  432. request.TryGetProperty("url", out JsonElement jurls);
  433. request.TryGetProperty("id", out JsonElement ids);
  434. //获取文件的大小
  435. var client = _azureCosmos.GetCosmosClient();
  436. List<string> urls = jurls.ToObject<List<string>>();
  437. List<Bloblog> bloblog = new List<Bloblog>();
  438. foreach (var uri in urls) {
  439. var url = System.Web.HttpUtility.UrlDecode(uri, Encoding.UTF8);
  440. string[] uls = url.Split("/");
  441. var u = "";
  442. if (uls != null)
  443. {
  444. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  445. }
  446. var size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(url);
  447. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  448. //地址相同的,直接更新
  449. bool exsit = false;
  450. try
  451. {
  452. var queryslt = $"SELECT value(c) FROM c WHERE c.url='{url}'";
  453. if (scope.GetString().Equals("school"))
  454. {
  455. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Bloblog>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  456. {
  457. item.time = now;
  458. item.size = size != null && size.HasValue ? size.Value : 0;
  459. item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" };
  460. item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" };
  461. item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" };
  462. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
  463. bloblog.Add(item);
  464. exsit = true;
  465. }
  466. }
  467. else if (scope.GetString().Equals("private"))
  468. {
  469. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Bloblog>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  470. {
  471. item.time = now;
  472. item.size = size != null && size.HasValue ? size.Value : 0;
  473. item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" };
  474. item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" };
  475. item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" };
  476. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
  477. bloblog.Add(item);
  478. exsit = true;
  479. }
  480. }
  481. }
  482. catch (Exception ex)
  483. {
  484. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  485. }
  486. if (!exsit)
  487. {
  488. var blob = new Bloblog
  489. {
  490. id = Guid.NewGuid().ToString(),
  491. pk = "Bloblog",
  492. code = $"Bloblog-{name}",
  493. url = url,
  494. time = now,
  495. size = size != null && size.HasValue ? size.Value : 0,
  496. periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" },
  497. subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" },
  498. gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" },
  499. type = u
  500. };
  501. if (scope.GetString().Equals("school"))
  502. {
  503. await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
  504. }
  505. else if (scope.GetString().Equals("private"))
  506. {
  507. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
  508. }
  509. bloblog.Add(blob);
  510. }
  511. }
  512. return Ok(new { bloblog, status = 200 });
  513. }
  514. catch (Exception ex)
  515. {
  516. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}{ex.StackTrace}\n{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  517. }
  518. return Ok(new { status = 200 });
  519. }
  520. /// <summary>
  521. /// 删除prefix 不管是内容 模块还是其他试题试卷 评测 问卷投票等都在使用。
  522. ///
  523. /// {"cntr":"","prefix":"res/test"}
  524. /// </summary>
  525. /// <param name="request"></param>
  526. /// <returns></returns>
  527. [HttpPost("delete-prefix")]
  528. [AuthToken(Roles = "teacher,admin")]
  529. public async Task<IActionResult> DeletePrefix(JsonElement json)
  530. {
  531. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  532. string blobContainerName = null;
  533. string prefix = null;
  534. if (json.TryGetProperty("cntr", out JsonElement cntr) && cntr.ValueKind.Equals(JsonValueKind.String))
  535. {
  536. var cntrs = cntr.GetString();
  537. if (cntrs.Equals(id))
  538. {
  539. blobContainerName = id;
  540. }
  541. else if (cntrs.Equals(school))
  542. {
  543. blobContainerName = school;
  544. }
  545. else
  546. {
  547. return BadRequest("只能删除本人管理的文件夹");
  548. }
  549. }
  550. if (json.TryGetProperty("prefix", out JsonElement prefixjson) && prefixjson.ValueKind.Equals(JsonValueKind.String))
  551. {
  552. prefix = prefixjson.GetString();
  553. }
  554. if (prefix != null && blobContainerName != null)
  555. {
  556. var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, new List<string> { prefix });
  557. string u = "";
  558. string[] uls = System.Web.HttpUtility.UrlDecode($"{prefixjson}", Encoding.UTF8).Split("/");
  559. if (uls != null)
  560. {
  561. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  562. }
  563. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = u, name = $"{blobContainerName}" }.ToJsonString()); ;
  564. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  565. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  566. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  567. return Ok(new { status });
  568. }
  569. else
  570. {
  571. return BadRequest();
  572. }
  573. }
  574. public record ContBlob {
  575. public string path { get; set; }
  576. public string id { get; set; }
  577. }
  578. /// <summary>
  579. /// 删除多个Url,只会在内容模块使用该接口
  580. ///
  581. ///
  582. /// {"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"}]}
  583. /// </summary>
  584. /// <param name="request"></param>
  585. /// <returns></returns>
  586. [HttpPost("bloblog-delete")]
  587. [AuthToken(Roles = "teacher,admin")]
  588. public async Task<IActionResult> DeleteBlobs(JsonElement json)
  589. {
  590. ///BlobBaseClient copy
  591. try {
  592. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  593. string blobContainerName = null;
  594. if (!json.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  595. if (!json.TryGetProperty("blobs", out JsonElement _blobs)) return BadRequest();
  596. if (json.TryGetProperty("cntr", out JsonElement cntr))
  597. {
  598. var cntrs = cntr.ToString();
  599. if (cntrs.Equals(id))
  600. {
  601. blobContainerName = id;
  602. }
  603. else if (cntrs.Equals(school))
  604. {
  605. blobContainerName = school;
  606. }
  607. else
  608. {
  609. return BadRequest("只能删除本人管理的文件");
  610. }
  611. }
  612. bool flag = true;
  613. List<ContBlob> blobs = _blobs.ToObject<List<ContBlob>>();
  614. try
  615. {
  616. var client = _azureCosmos.GetCosmosClient();
  617. List<string> ids = blobs.Select(x => x.id).ToList();
  618. string containerId = "School";
  619. if (_scope.GetString().Equals("school"))
  620. {
  621. containerId = "School";
  622. }
  623. else if (_scope.GetString().Equals("private"))
  624. {
  625. containerId = "Teacher";
  626. }
  627. await client.GetContainer("TEAMModelOS", containerId).DeleteItemsAsync<Bloblog>(ids, $"Bloblog-{blobContainerName}");
  628. }
  629. catch (CosmosException ex )
  630. {
  631. //仅处理 cosmos不存在 但容器又存在的
  632. }
  633. if (flag)
  634. {
  635. var urls = blobs.Select(x => x.path).ToList();
  636. List<string> deleteUrl = new List<string>();
  637. urls.ForEach(x => {
  638. string delUrl = x;
  639. if (x.StartsWith("res") && x.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
  640. {
  641. delUrl = x.Substring(0, x.Length - 4);
  642. }
  643. //自动删除视频和图片的缩略图
  644. if (x.StartsWith("image")||x.StartsWith("video")) {
  645. var thum = $"thum{ x.Substring(5)}" ;
  646. if (x.StartsWith("video"))
  647. {
  648. string fileexturl = thum.Substring(thum.LastIndexOf(".") > 0 ? thum.LastIndexOf(".") : 0);
  649. thum = thum.Replace(fileexturl, ".png");
  650. }
  651. deleteUrl.Add(thum);
  652. }
  653. deleteUrl.Add(delUrl);
  654. });
  655. var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, deleteUrl);
  656. //释放的空间
  657. HashSet<string> root = new HashSet<string>();
  658. foreach (var x in deleteUrl)
  659. {
  660. string url = System.Web.HttpUtility.UrlDecode(x, Encoding.UTF8);
  661. string[] uls = url.Split("/");
  662. if (uls != null)
  663. {
  664. string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  665. root.Add(u);
  666. }
  667. }
  668. root.ToList().ForEach(async x => {
  669. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = x, name = $"{blobContainerName}" }.ToJsonString()); ;
  670. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  671. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  672. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  673. });
  674. return Ok(new { status });
  675. }
  676. else
  677. {
  678. return BadRequest("只能删除本人管理的文件");
  679. }
  680. } catch (Exception ex) {
  681. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/delete-blobs\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  682. return BadRequest();
  683. }
  684. }
  685. /// <summary>
  686. /// 列出blob的
  687. ///
  688. /// {"cntr":"","urls":["res/test/1.json","res/test/2.json"]}
  689. /// {"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"}]}
  690. /// </summary>
  691. /// <param name="request"></param>
  692. /// <returns></returns>
  693. [HttpPost("blob-list")]
  694. [AuthToken(Roles = "teacher,admin,student")]
  695. public async Task<IActionResult> BlobList(JsonElement json) {
  696. var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  697. List<string> paths = new List<string>();
  698. //文件的容器
  699. if (!json.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
  700. //业务存取类型 exam,vote,survey,item,paper,syllabus,records,content(doc,image,res,video,audio,other),thum,train,temp,jyzx
  701. if (!json.TryGetProperty("type", out JsonElement _type)) return BadRequest();
  702. //文件前缀prefix
  703. switch (true)
  704. {
  705. case bool when $"{_type}".Equals("exam", StringComparison.OrdinalIgnoreCase)
  706. || $"{_type}".Equals("vote", StringComparison.OrdinalIgnoreCase)
  707. || $"{_type}".Equals("survey", StringComparison.OrdinalIgnoreCase)
  708. || $"{_type}".Equals("item", StringComparison.OrdinalIgnoreCase)
  709. || $"{_type}".Equals("paper", StringComparison.OrdinalIgnoreCase):
  710. string type = $"{_type}".Substring(0, 1).ToUpper() + $"{_type}".Substring(1);
  711. //业务存取id
  712. if (!json.TryGetProperty("id", out JsonElement _aid)) return BadRequest();
  713. //业务存取分区键
  714. if (!json.TryGetProperty("code", out JsonElement _acode)) return BadRequest();
  715. //业务存取分区键
  716. if (!json.TryGetProperty("scope", out JsonElement _ascope)) return BadRequest();
  717. break;
  718. case bool when $"{_type}".Equals("doc", StringComparison.OrdinalIgnoreCase)
  719. || $"{_type}".Equals("image", StringComparison.OrdinalIgnoreCase)
  720. || $"{_type}".Equals("res", StringComparison.OrdinalIgnoreCase)
  721. || $"{_type}".Equals("video", StringComparison.OrdinalIgnoreCase)
  722. || $"{_type}".Equals("audio", StringComparison.OrdinalIgnoreCase)
  723. || $"{_type}".Equals("other", StringComparison.OrdinalIgnoreCase):
  724. //业务存取id
  725. if (!json.TryGetProperty("id", out JsonElement _bid)) return BadRequest();
  726. //业务存取分区键
  727. if (!json.TryGetProperty("code", out JsonElement _bcode)) return BadRequest();
  728. //业务存取分区键
  729. if (!json.TryGetProperty("scope", out JsonElement _bscope)) return BadRequest();
  730. break;
  731. case bool when $"{_type}".Equals("records", StringComparison.OrdinalIgnoreCase)
  732. || $"{_type}".Equals("syllabus", StringComparison.OrdinalIgnoreCase)
  733. || $"{_type}".Equals("thum", StringComparison.OrdinalIgnoreCase)
  734. || $"{_type}".Equals("temp", StringComparison.OrdinalIgnoreCase)
  735. || $"{_type}".Equals("jyzx", StringComparison.OrdinalIgnoreCase)
  736. || $"{_type}".Equals("train", StringComparison.OrdinalIgnoreCase):
  737. break;
  738. default:
  739. break;
  740. }
  741. var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { "aaa" });
  742. return Ok(new { paths ,status=1});
  743. }
  744. }
  745. }