BlobController.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. namespace TEAMModelOS.Controllers.Core
  31. {
  32. [Route("blob")]
  33. [ApiController]
  34. public class BlobController : ControllerBase
  35. {
  36. private readonly AzureStorageFactory _azureStorage;
  37. private readonly IHttpClientFactory _clientFactory;
  38. private readonly AzureRedisFactory _azureRedis;
  39. private readonly AzureServiceBusFactory _serviceBus;
  40. private readonly DingDing _dingDing;
  41. private readonly Option _option;
  42. private readonly AzureCosmosFactory _azureCosmos;
  43. public IConfiguration _configuration { get; set; }
  44. public BlobController(AzureStorageFactory azureStorage, AzureServiceBusFactory serviceBus, IHttpClientFactory clientFactory, AzureRedisFactory azureRedis, IConfiguration configuration,
  45. DingDing dingDing,
  46. IOptionsSnapshot<Option> option, AzureCosmosFactory azureCosmos)
  47. {
  48. _azureStorage = azureStorage;
  49. _clientFactory = clientFactory;
  50. _serviceBus = serviceBus;
  51. _azureRedis = azureRedis;
  52. _configuration = configuration;
  53. _dingDing = dingDing;
  54. _option = option?.Value;
  55. _azureCosmos = azureCosmos;
  56. }
  57. /// <summary>
  58. /// 上传文件到指定的0-public
  59. /// </summary>
  60. /// <param name="request"></param>
  61. /// <returns></returns>
  62. [HttpPost("public-upload")]
  63. //[AuthToken(Roles = "teacher,admin")]
  64. [RequestSizeLimit(102_400_000_00)] //最大10000m左右
  65. public async Task<IActionResult> PublicUpload([FromForm] IFormFile file)
  66. {
  67. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  68. string fileExt = FileType.GetExtention(file.FileName).ToLower();
  69. if (ContentTypeDict.dict.ContainsKey($".{fileExt}"))
  70. {
  71. var url= await _azureStorage.UploadFileByContainer("0-public", file.OpenReadStream(), "school", $"{Guid.NewGuid()}.{fileExt}", false);
  72. return Ok(new { url });
  73. }
  74. else {
  75. return BadRequest();
  76. }
  77. }
  78. /// <summary>
  79. /// 获取某个容器的只读权限
  80. /// </summary>
  81. /// <param name="request"></param>
  82. /// <returns></returns>
  83. [HttpPost("sas-r")]
  84. public IActionResult BlobSasR(BlobSas request)
  85. {
  86. ///返回金钥过期时间
  87. // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
  88. // dict.Add(d.Key, d.Value);
  89. return Ok(_azureStorage.GetContainerSasUri(request, true));
  90. }
  91. /// <summary>
  92. /// 某个文件的上传SAS rcw权限
  93. /// </summary>
  94. /// <param name="request"></param>
  95. /// <returns></returns>
  96. [HttpPost("sas-rcwld")]
  97. public IActionResult BlobSasRCW(BlobSas request)
  98. {
  99. ///返回金钥过期时间
  100. // Dictionary<string,object> dict= await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
  101. // Dictionary<string, object> dict = ;
  102. //dict.Add(d.Key, d.Value);
  103. return Ok(_azureStorage.GetContainerSasUri(request, false));
  104. }
  105. /// <summary>
  106. /// 删除prefix
  107. ///
  108. /// {"cntr":"","prefix":"res/test"}
  109. /// </summary>
  110. /// <param name="request"></param>
  111. /// <returns></returns>
  112. [HttpPost("delete-prefix")]
  113. [AuthToken(Roles = "teacher,admin")]
  114. public async Task<IActionResult> DeletePrefix(JsonElement json)
  115. {
  116. var (id,_,_,school) = HttpContext.GetAuthTokenInfo();
  117. string blobContainerName = null;
  118. string prefix = null;
  119. if (json.TryGetProperty("cntr", out JsonElement cntr)&& cntr.ValueKind.Equals(JsonValueKind.String)) {
  120. var cntrs= cntr.GetString();
  121. if (cntrs.Equals(id))
  122. {
  123. blobContainerName = id;
  124. }
  125. else if(cntrs.Equals(school))
  126. {
  127. blobContainerName = school;
  128. }
  129. else
  130. {
  131. return BadRequest("只能删除本人管理的文件夹");
  132. }
  133. }
  134. if (json.TryGetProperty("prefix", out JsonElement prefixjson) && prefixjson.ValueKind.Equals(JsonValueKind.String))
  135. {
  136. prefix = prefixjson.GetString();
  137. }
  138. if (prefix != null && blobContainerName != null)
  139. {
  140. var status = await _azureStorage.GetBlobServiceClient().DelectBlobs(blobContainerName, prefix);
  141. string u ="";
  142. string[] uls = System.Web.HttpUtility.UrlDecode($"{prefixjson}", Encoding.UTF8).Split("/");
  143. if (uls != null)
  144. {
  145. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  146. }
  147. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root =u, name = $"{blobContainerName}" }.ToJsonString()); ;
  148. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  149. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  150. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  151. return Ok(new { status });
  152. }
  153. else {
  154. return BadRequest();
  155. }
  156. }
  157. /// <summary>
  158. /// 删除多个Url
  159. ///
  160. /// {"cntr":"","urls":["res/test/1.json","res/test/2.json"]}
  161. /// </summary>
  162. /// <param name="request"></param>
  163. /// <returns></returns>
  164. [HttpPost("delete-blobs")]
  165. [AuthToken(Roles = "teacher,admin")]
  166. public async Task<IActionResult> DeleteBlobs(JsonElement json)
  167. {
  168. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  169. string blobContainerName = null;
  170. List<Uri> uris = null;
  171. if (json.TryGetProperty("cntr", out JsonElement cntr))
  172. {
  173. var cntrs = cntr.ToString();
  174. if (cntrs.Equals(id))
  175. {
  176. blobContainerName = id;
  177. }
  178. else if (cntrs.Equals(school))
  179. {
  180. blobContainerName = school;
  181. }
  182. else {
  183. return BadRequest("只能删除本人管理的文件");
  184. }
  185. }
  186. bool flag = true;
  187. if (json.TryGetProperty("urls", out JsonElement urlsjson)) {
  188. uris= urlsjson.ToObject<List<Uri>>();
  189. uris.ForEach(x => {
  190. (string, string) a = BlobUrlString(x.ToString());
  191. string ContainerName = a.Item1;
  192. string BlobName = a.Item2;
  193. if (ContainerName!=blobContainerName) {
  194. flag = false;
  195. }
  196. });
  197. }
  198. if (flag)
  199. {
  200. if (blobContainerName != null && uris != null && uris.Count > 0)
  201. {
  202. var urls = urlsjson.ToObject<List<string>>();
  203. var status = await _azureStorage.GetBlobServiceClient().DelectBlobs(blobContainerName, uris);
  204. //释放的空间
  205. HashSet<string> root = new HashSet<string>();
  206. foreach (var x in urls)
  207. {
  208. string[] uls = System.Web.HttpUtility.UrlDecode(x, Encoding.UTF8).Split("/");
  209. if (uls != null)
  210. {
  211. string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  212. root.Add(u);
  213. }
  214. }
  215. root.ToList().ForEach(async x => {
  216. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = x, name = $"{blobContainerName}" }.ToJsonString()); ;
  217. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  218. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  219. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  220. });
  221. return Ok(new { status });
  222. }
  223. else
  224. {
  225. return BadRequest();
  226. }
  227. }
  228. else {
  229. return BadRequest("只能删除本人管理的文件");
  230. }
  231. }
  232. /// <summary>
  233. /// 链接只读(读)
  234. /// </summary>
  235. /// <param name="azureBlobSASDto"></param>
  236. /// <returns></returns>
  237. [HttpPost("sas-url-r")]
  238. public IActionResult GetContainerSASRead(JsonElement azureBlobSASDto)
  239. {
  240. azureBlobSASDto.TryGetProperty("url", out JsonElement azureBlobSAS);
  241. //string azureBlobSAS = azureBlobSASDto;
  242. (string, string) a = BlobUrlString(azureBlobSAS.ToString());
  243. string ContainerName = a.Item1;
  244. string BlobName = a.Item2;
  245. bool flg = IsBlobName(BlobName);
  246. if (flg)
  247. {
  248. return Ok(_azureStorage.GetBlobSasUriRead(ContainerName, BlobName));
  249. }
  250. else
  251. {
  252. return BadRequest("文件名错误");
  253. };
  254. }
  255. /// <summary>
  256. /// 获取文件内容
  257. /// </summary>
  258. /// <param name="azureBlobSASDto"></param>
  259. /// <returns></returns>
  260. [HttpPost("get-text")]
  261. public async Task<IActionResult> GetText(JsonElement request)
  262. {
  263. request.TryGetProperty("code", out JsonElement code);
  264. string azureBlobSAS = System.Web.HttpUtility.UrlDecode(code.ToString(), Encoding.UTF8);
  265. (string, string) a = BlobUrlString(azureBlobSAS);
  266. string ContainerName = a.Item1;
  267. string BlobName = a.Item2;
  268. bool flg = IsBlobName(BlobName);
  269. if (flg)
  270. {
  271. //TODO 需驗證
  272. BlobAuth blobAuth= _azureStorage.GetBlobSasUriRead(ContainerName, BlobName);
  273. var response= await _clientFactory.CreateClient().GetAsync(new Uri(blobAuth.url + blobAuth.sas));
  274. response.EnsureSuccessStatusCode();
  275. using var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
  276. return Ok(json.RootElement);
  277. }
  278. else
  279. {
  280. return BadRequest("文件名错误");
  281. };
  282. }
  283. /// <summary>
  284. /// 获取容器的 分类及总量
  285. /// </summary>
  286. /// <param name="azureBlobSASDto"></param>
  287. /// <returns></returns>
  288. [HttpPost("get-blobsize")]
  289. public async Task<ActionResult> GetBlobsSize(JsonElement request)
  290. {
  291. try
  292. {
  293. request.TryGetProperty("containerName", out JsonElement containerName);
  294. request.TryGetProperty("cache", out JsonElement cache);
  295. var name =containerName.GetString();
  296. if (cache.GetBoolean())
  297. {
  298. long blobsize = 0;
  299. RedisValue value = default;
  300. value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", name);
  301. if (value != default && !value.IsNullOrEmpty)
  302. {
  303. JsonElement record = value.ToString().ToObject<JsonElement>();
  304. if (record.TryGetInt64(out blobsize))
  305. {
  306. }
  307. }
  308. else
  309. {
  310. var client = _azureStorage.GetBlobContainerClient(name);
  311. var size = await client.GetBlobsCatalogSize();
  312. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  313. foreach (var key in size.Item2.Keys)
  314. {
  315. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  316. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  317. }
  318. return Ok(new { size = size.Item1, catalog = size.Item2 });
  319. }
  320. Dictionary<string, double> catalog = new Dictionary<string, double>();
  321. SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{name}");
  322. if (Scores != null)
  323. {
  324. foreach (var score in Scores)
  325. {
  326. double val = score.Score;
  327. string key = score.Element.ToString();
  328. catalog.Add(key, val);
  329. }
  330. return Ok(new { size = blobsize, catalog = catalog });
  331. }
  332. else {
  333. var client = _azureStorage.GetBlobContainerClient(name);
  334. var size = await client.GetBlobsCatalogSize();
  335. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  336. foreach (var key in size.Item2.Keys)
  337. {
  338. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  339. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  340. }
  341. return Ok(new { size = size.Item1, catalog = size.Item2 });
  342. }
  343. }
  344. else {
  345. var client = _azureStorage.GetBlobContainerClient(name);
  346. var size = await client.GetBlobsCatalogSize();
  347. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
  348. foreach (var key in size.Item2.Keys)
  349. {
  350. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  351. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  352. }
  353. return Ok(new { size = size.Item1, catalog = size.Item2 });
  354. }
  355. }
  356. catch (Exception ex){
  357. await _dingDing.SendBotMsg($"IES5,{_option.Location},blon/get-blobsize()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  358. }
  359. return BadRequest();
  360. }
  361. //TODO 不要了
  362. /// <summary>
  363. /// 测试单个文本内容的上传
  364. /// {"containerName":"hbcn","urls":["video/xxx.mp4","res/xxx.png"],"prefix":["res/xxx","res/aaa"]}
  365. /// </summary>
  366. /// <param name="azureBlobSASDto"></param>
  367. /// <returns></returns>
  368. [HttpPost("check-blobsize")]
  369. public async Task<ActionResult> checkBlobsSize(JsonElement request)
  370. {
  371. request.TryGetProperty("containerName", out JsonElement containerName);
  372. request.TryGetProperty("urls", out JsonElement optUrls);
  373. request.TryGetProperty("prefix", out JsonElement prefixjs);
  374. var name = containerName.GetString();
  375. var urls = optUrls.ValueKind.Equals(JsonValueKind.Array) ? optUrls.ToObject<List<string>>() : null ;
  376. var client = _azureStorage.GetBlobContainerClient(name);
  377. var urlsSize = await client.GetBlobsSize(urls);
  378. List<OptUrl> prefixSize = new List<OptUrl>();
  379. var prefixs = prefixjs.ValueKind.Equals(JsonValueKind.Array) ? prefixjs.ToObject<List<string>>() : new List<string>();
  380. foreach(var prefix in prefixs) {
  381. var size = await client.GetBlobsSize(System.Web.HttpUtility.UrlDecode(prefix, Encoding.UTF8));
  382. prefixSize.Add(new OptUrl { url = prefix, size = size.Value });
  383. }
  384. return Ok(new { urlsSize , prefixSize });
  385. }
  386. /*
  387. *
  388. {
  389. "containerName": "hbcn",
  390. "cache": true,
  391. "optUrls": [
  392. {
  393. "url": "video%2F37Z888piCvm9.mp4",
  394. "size": 1000
  395. }
  396. ]
  397. }
  398. */
  399. //TODO 不要了
  400. /// <summary>
  401. /// 测试单个文本内容的上传
  402. /// {"containerName":"hbcn","uploadSize":5000,"optUrls":[{"url":"video/37Z888piCvm9.mp4","size":0},{}]}
  403. /// </summary>
  404. /// <param name="azureBlobSASDto"></param>
  405. /// <returns></returns>
  406. [HttpPost("update-blobsize")]
  407. public async Task<ActionResult> updateBlobsSize(JsonElement request)
  408. {
  409. request.TryGetProperty("containerName", out JsonElement containerName);
  410. request.TryGetProperty("optUrls", out JsonElement optUrls);
  411. var name = containerName.GetString();
  412. var urls = optUrls.ToObject<List<OptUrl>>();
  413. var client = _azureStorage.GetBlobContainerClient(name);
  414. var disSize = urls.Select(x => x.size).Sum();
  415. RedisValue value = default;
  416. long blobSize = 0;
  417. value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", name);
  418. if (value != default && !value.IsNullOrEmpty)
  419. {
  420. JsonElement record = value.ToString().ToObject<JsonElement>();
  421. if (record.TryGetInt64(out blobSize))
  422. {
  423. }
  424. }
  425. long? useSize = blobSize + disSize;
  426. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, useSize);
  427. Dictionary<string, double?> catalog = new Dictionary<string, double?>();
  428. HashSet<string> root = new HashSet<string>();
  429. foreach (var x in urls) {
  430. string[] uls = System.Web.HttpUtility.UrlDecode(x.url, Encoding.UTF8).Split("/");
  431. if (uls != null)
  432. {
  433. string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  434. root.Add(u);
  435. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", u, x.size);
  436. catalog[u] = await _azureRedis.GetRedisClient(8).SortedSetScoreAsync($"Blob:Catalog:{name}", u);
  437. if (catalog[u] < 0)
  438. {
  439. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", u);
  440. catalog[u] = 0;
  441. }
  442. }
  443. }
  444. var messageBlob = new ServiceBusMessage(new {id=Guid.NewGuid().ToString(), progress = "update", root= root.ToArray(), name=name}.ToJsonString());;
  445. messageBlob.ApplicationProperties.Add("name", "Blob");
  446. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  447. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  448. return Ok(new { size=useSize, catalog });
  449. }
  450. /// <summary>
  451. /// {"name":"hbcn","urls":["root/xxxx/xxx.json","root/xxxx/xxx.json"]}
  452. /// </summary>
  453. /// <param name="azureBlobSASDto"></param>
  454. /// <returns></returns>
  455. [HttpPost("refresh-blobsize")]
  456. public async Task<ActionResult> RefreshBlob(JsonElement request) {
  457. try {
  458. request.TryGetProperty("name", out JsonElement name);
  459. request.TryGetProperty("urls", out JsonElement jurls);
  460. var urls = jurls.ToObject<List<string>>();
  461. HashSet<string> root = new HashSet<string>();
  462. foreach (var x in urls)
  463. {
  464. string[] uls = System.Web.HttpUtility.UrlDecode(x, Encoding.UTF8).Split("/");
  465. if (uls != null) {
  466. string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  467. root.Add(u);
  468. }
  469. }
  470. root.ToList().ForEach(async x => {
  471. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root =x, name = $"{name}" }.ToJsonString()); ;
  472. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  473. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  474. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  475. });
  476. return Ok(new { status = 200 });
  477. } catch (Exception ex) {
  478. await _dingDing.SendBotMsg($"IES5,{_option.Location},blon/refresh-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  479. }
  480. return Ok(new { status = 500 });
  481. }
  482. private static (string, string) BlobUrlString(string sasUrl)
  483. {
  484. sasUrl = sasUrl.Substring(8);
  485. string[] sasUrls = sasUrl.Split("/");
  486. string ContainerName;
  487. ContainerName = sasUrls[1].Clone().ToString();
  488. string item = sasUrls[0] + "/" + sasUrls[1] + "/";
  489. string blob = sasUrl.Replace(item, "");
  490. return (ContainerName, blob);
  491. }
  492. public static bool IsBlobName(string BlobName)
  493. {
  494. return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
  495. @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
  496. }
  497. /// <summary>
  498. ///
  499. /// </summary>
  500. /// <param name="request"></param>
  501. /// <returns></returns>
  502. [HttpPost("bloblog-list")]
  503. public async Task<ActionResult> BloblogList(JsonElement request) {
  504. List<Bloblog> bloblogs = new List<Bloblog>();
  505. try
  506. {
  507. request.TryGetProperty("name", out JsonElement name);
  508. request.TryGetProperty("type", out JsonElement type);
  509. request.TryGetProperty("scope", out JsonElement scope);
  510. request.TryGetProperty("periodId", out JsonElement periodId);
  511. var client = _azureCosmos.GetCosmosClient();
  512. if (scope.GetString().Equals("school"))
  513. {
  514. var queryslt = new StringBuilder($"SELECT value(c) FROM c join A1 in c.periodId WHERE c.type='{type}' and A1 in ('{periodId}') ");
  515. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  516. {
  517. bloblogs.Add(item);
  518. }
  519. }
  520. else if (scope.GetString().Equals("private"))
  521. {
  522. var queryslt = new StringBuilder($"SELECT value(c) FROM c WHERE c.type='{type}' ");
  523. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  524. {
  525. bloblogs.Add(item);
  526. }
  527. }
  528. return Ok(new { bloblogs = bloblogs });
  529. }
  530. catch (Exception ex) {
  531. return Ok(new { bloblogs = bloblogs });
  532. }
  533. }
  534. /*
  535. 新增 编辑接口
  536. {
  537. "periodId": "",
  538. "scope": "school",
  539. "name": "hbcn",
  540. "url": "video/xxx.png",
  541. "opt": "add",
  542. }
  543. */
  544. /*
  545. {
  546. "scope": "school",
  547. "name": "hbcn",
  548. "opt": "del",
  549. "id": "19ccce98-c524-4ea7-aabc-887d1391e551"
  550. }
  551. */
  552. /// <summary>
  553. ///
  554. /// </summary>
  555. /// <param name="request"></param>
  556. /// <returns></returns>
  557. [HttpPost("bloblog-opt")]
  558. public async Task<ActionResult> BloblogOpt(JsonElement request) {
  559. try
  560. {
  561. request.TryGetProperty("periodId", out JsonElement periodId);
  562. request.TryGetProperty("subjectId", out JsonElement subjectId);
  563. request.TryGetProperty("gradeId", out JsonElement gradeId);
  564. request.TryGetProperty("scope", out JsonElement scope);
  565. request.TryGetProperty("name", out JsonElement name);
  566. request.TryGetProperty("url", out JsonElement jurl);
  567. request.TryGetProperty("opt", out JsonElement opt);
  568. request.TryGetProperty("id", out JsonElement id);
  569. //获取文件的大小
  570. var client = _azureCosmos.GetCosmosClient();
  571. if (opt.GetString().Equals("add"))
  572. {
  573. var url = System.Web.HttpUtility.UrlDecode(jurl.GetString(), Encoding.UTF8);
  574. string[] uls = url.Split("/");
  575. var u = "";
  576. if (uls != null)
  577. {
  578. u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  579. }
  580. var size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(url);
  581. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  582. //地址相同的,直接更新
  583. bool exsit = false;
  584. try {
  585. var queryslt = $"SELECT value(c) FROM c WHERE c.url='{url}'";
  586. if (scope.GetString().Equals("school"))
  587. {
  588. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Bloblog>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  589. {
  590. item.time = now;
  591. item.size = size != null && size.HasValue ? size.Value : 0;
  592. item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) && periodId.ToObject<List<string>>().IsNotEmpty() ? periodId.ToObject<List<string>>() : new List<string> { "" };
  593. item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) && subjectId.ToObject<List<string>>().IsNotEmpty() ? subjectId.ToObject<List<string>>() : new List<string> { "" };
  594. item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) && gradeId.ToObject<List<string>>().IsNotEmpty() ? gradeId.ToObject<List<string>>() : new List<string> { "" };
  595. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
  596. exsit = true;
  597. }
  598. }
  599. else if (scope.GetString().Equals("private")) {
  600. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Bloblog>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  601. {
  602. item.time = now;
  603. item.size = size != null && size.HasValue ? size.Value : 0;
  604. item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) && periodId.ToObject<List<string>>().IsNotEmpty() ? periodId.ToObject<List<string>>() : new List<string> { "" };
  605. item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) && subjectId.ToObject<List<string>>().IsNotEmpty() ? subjectId.ToObject<List<string>>() : new List<string> { "" };
  606. item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) && gradeId.ToObject<List<string>>().IsNotEmpty() ? gradeId.ToObject<List<string>>() : new List<string> { "" };
  607. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
  608. exsit = true;
  609. }
  610. }
  611. } catch (Exception ex) {
  612. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/refresh-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  613. }
  614. if (!exsit) {
  615. var blob = new Bloblog
  616. {
  617. id = Guid.NewGuid().ToString(),
  618. pk = "Bloblog",
  619. code = $"Bloblog-{name}",
  620. url = url,
  621. time = now,
  622. size = size != null && size.HasValue ? size.Value : 0,
  623. periodId = periodId.ValueKind.Equals(JsonValueKind.Array) && periodId.ToObject<List<string>>().IsNotEmpty() ? periodId.ToObject<List<string>>() : new List<string> { "" } ,
  624. subjectId= subjectId.ValueKind.Equals(JsonValueKind.Array)&& subjectId.ToObject<List<string>>() .IsNotEmpty()? subjectId.ToObject<List<string>>() : new List<string> { ""},
  625. gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) && gradeId.ToObject<List<string>>().IsNotEmpty() ? gradeId.ToObject<List<string>>() : new List<string> { "" },
  626. type = u
  627. };
  628. if (scope.GetString().Equals("school"))
  629. {
  630. await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
  631. }
  632. else if (scope.GetString().Equals("private"))
  633. {
  634. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
  635. }
  636. }
  637. }
  638. else if (opt.GetString().Equals("del"))
  639. {
  640. if (scope.GetString().Equals("school"))
  641. {
  642. await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync($"{id}", new Azure.Cosmos.PartitionKey($"Bloblog-{name}"));
  643. }
  644. else if (scope.GetString().Equals("private"))
  645. {
  646. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync($"{id}", new Azure.Cosmos.PartitionKey($"Bloblog-{name}"));
  647. }
  648. }
  649. else { return BadRequest(); }
  650. return Ok(new { status = 200 });
  651. }
  652. catch (Exception ex)
  653. {
  654. await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/refresh-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  655. }
  656. return Ok(new { status = 200 });
  657. }
  658. }
  659. }