BlobController.cs 42 KB

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