ActivityHttpTrigger.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Azure.WebJobs;
  6. using Microsoft.Azure.WebJobs.Extensions.Http;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.Extensions.Logging;
  9. using TEAMModelOS.SDK.DI;
  10. using Azure.Cosmos;
  11. using System.Text.Json;
  12. using System.Collections.Generic;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  16. using TEAMModelOS.SDK.Models.Cosmos;
  17. using TEAMModelOS.SDK.Models.Cosmos.Common;
  18. using System.Linq;
  19. using TEAMModelOS.Services.Common;
  20. namespace TEAMModelFunction
  21. {
  22. public class ActivityHttpTrigger
  23. {
  24. private readonly AzureCosmosFactory _azureCosmos;
  25. private readonly DingDing _dingDing;
  26. private readonly AzureStorageFactory _azureStorage;
  27. private readonly AzureRedisFactory _azureRedis;
  28. public ActivityHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  29. , AzureRedisFactory azureRedis)
  30. {
  31. _azureCosmos = azureCosmos;
  32. _dingDing = dingDing;
  33. _azureStorage = azureStorage;
  34. _azureRedis = azureRedis;
  35. }
  36. /// <summary>
  37. /// 修复已存在的课程且未初始化学生课程列表的业务。
  38. /// </summary>
  39. /// <param name="req"></param>
  40. /// <param name="log"></param>
  41. /// <returns></returns>
  42. [FunctionName("fix-stu-course")]
  43. public async Task<IActionResult> StuCourse([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
  44. {
  45. log.LogInformation("fix-stu-course...");
  46. string originCode = await new StreamReader(req.Body).ReadToEndAsync();
  47. List<Course> courses = new List<Course>();
  48. var client = _azureCosmos.GetCosmosClient();
  49. var query = $"select * from c ";
  50. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Course>(queryText: query,
  51. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{originCode}") }))
  52. {
  53. courses.Add(item);
  54. }
  55. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Course>(queryText: query,
  56. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{originCode}") }))
  57. {
  58. courses.Add(item);
  59. }
  60. //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。
  61. foreach (var course in courses)
  62. {
  63. if (course.schedule.IsNotEmpty())
  64. {
  65. foreach (var sc in course.schedule)
  66. {
  67. if (!string.IsNullOrEmpty(sc.stulist))
  68. {
  69. (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string>() { sc.stulist }, course.school);
  70. foreach (var addStu in studentss)
  71. {
  72. var stuCourse = new StuCourse
  73. {
  74. id = course.id,
  75. scode = course.code,
  76. name = course.name,
  77. code = $"StuCourse-{course.school}-{addStu.id}",
  78. scope = course.scope,
  79. school = course.school,
  80. creatorId = course.creatorId,
  81. pk = "StuCourse"
  82. };
  83. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stuCourse, new PartitionKey(stuCourse.code));
  84. }
  85. foreach (var addTmd in tmdids)
  86. {
  87. var tmdCourse = new StuCourse
  88. {
  89. id = course.id,
  90. scode = course.code,
  91. name = course.name,
  92. code = $"StuCourse-{addTmd}",
  93. scope = course.scope,
  94. //school = courseChange.school,
  95. creatorId = course.creatorId,
  96. pk = "StuCourse"
  97. };
  98. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(tmdCourse, new PartitionKey(tmdCourse.code));
  99. }
  100. }
  101. }
  102. }
  103. }
  104. return new OkObjectResult(new { });
  105. }
  106. /// <summary>
  107. /// 设置评测未初始化学生列表的
  108. /// </summary>
  109. /// <param name="req"></param>
  110. /// <param name="log"></param>
  111. /// <returns></returns>
  112. [FunctionName("fix-exam-activity")]
  113. public async Task<IActionResult> ExamActivity([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,ILogger log)
  114. {
  115. log.LogInformation("fix-exam-activity...");
  116. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  117. List<string> datas = JsonSerializer.Deserialize<List<string>>(requestBody);
  118. var client = _azureCosmos.GetCosmosClient();
  119. var query = $"select * from c ";
  120. foreach (string data in datas) {
  121. List<ExamInfo> exams = new List<ExamInfo>();
  122. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
  123. queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{data}") }))
  124. {
  125. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  126. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  127. {
  128. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  129. {
  130. exams.Add(obj.ToObject<ExamInfo>());
  131. }
  132. }
  133. }
  134. log.LogInformation($"{exams.ToJsonString()}");
  135. foreach (var info in exams)
  136. {
  137. if (!info.classes.IsNotEmpty())
  138. {
  139. continue;
  140. }
  141. List<string> sub = new List<string>();
  142. foreach (ExamSubject subject in info.subjects)
  143. {
  144. sub.Add(subject.id);
  145. }
  146. (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, _dingDing, info.classes, info.school);
  147. List<StuActivity> stuActivities = new List<StuActivity>();
  148. List<StuActivity> tmdActivities = new List<StuActivity>();
  149. if (tmdids.IsNotEmpty())
  150. {
  151. tmdids.ForEach(x => {
  152. tmdActivities.Add(new StuActivity
  153. {
  154. pk = "Activity",
  155. id = info.id,
  156. code = $"Activity-{x}",
  157. type = "exam",
  158. name = info.name,
  159. startTime = info.startTime,
  160. endTime = info.endTime,
  161. scode = info.code,
  162. scope = info.scope,
  163. school = info.school,
  164. creatorId = info.creatorId,
  165. subjects = sub,
  166. blob = null,
  167. owner = info.owner
  168. });
  169. });
  170. }
  171. if (studentss.IsNotEmpty())
  172. {
  173. studentss.ForEach(x => {
  174. stuActivities.Add(new StuActivity
  175. {
  176. pk = "Activity",
  177. id = info.id,
  178. code = $"Activity-{info.school}-{x.id}",
  179. type = "exam",
  180. name = info.name,
  181. startTime = info.startTime,
  182. endTime = info.endTime,
  183. scode = info.code,
  184. scope = info.scope,
  185. school = info.school,
  186. creatorId = info.creatorId,
  187. subjects = sub,
  188. blob=null,
  189. owner = info.owner
  190. });
  191. });
  192. }
  193. await TriggerStuActivity.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities);
  194. }
  195. }
  196. return new OkObjectResult(new { });
  197. }
  198. /// <summary>
  199. /// 设置投票未初始化学生列表的业务
  200. /// </summary>
  201. /// <param name="req"></param>
  202. /// <param name="log"></param>
  203. /// <returns></returns>
  204. [FunctionName("fix-vote-activity")]
  205. public async Task<IActionResult> VoteActivity(
  206. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  207. ILogger log)
  208. {
  209. log.LogInformation("fix-vote-activity...");
  210. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  211. List<string> datas = JsonSerializer.Deserialize<List<string>>(requestBody);
  212. var client = _azureCosmos.GetCosmosClient();
  213. var query = $"select * from c ";
  214. foreach (string data in datas)
  215. {
  216. List<Vote> votes = new List<Vote>();
  217. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
  218. queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{data}") }))
  219. {
  220. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  221. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  222. {
  223. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  224. {
  225. votes.Add(obj.ToObject<Vote>());
  226. }
  227. }
  228. }
  229. log.LogInformation($"{votes.ToJsonString()}");
  230. foreach (var info in votes)
  231. {
  232. if (!info.classes.IsNotEmpty())
  233. {
  234. continue;
  235. }
  236. (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, _dingDing, info.classes, info.school);
  237. List<StuActivity> stuActivities = new List<StuActivity>();
  238. List<StuActivity> tmdActivities = new List<StuActivity>();
  239. if (tmdids.IsNotEmpty())
  240. {
  241. tmdids.ForEach(x => {
  242. tmdActivities.Add(new StuActivity
  243. {
  244. pk = "Activity",
  245. id = info.id,
  246. code = $"Activity-{x}",
  247. type = "vote",
  248. name = info.name,
  249. startTime = info.startTime,
  250. endTime = info.endTime,
  251. scode = info.code,
  252. scope = info.scope,
  253. school = info.school,
  254. creatorId = info.creatorId,
  255. subjects = new List<string>() { "" },
  256. blob = null,
  257. owner = info.owner
  258. });
  259. });
  260. }
  261. if (studentss.IsNotEmpty())
  262. {
  263. studentss.ForEach(x => {
  264. stuActivities.Add(new StuActivity
  265. {
  266. pk = "Activity",
  267. id = info.id,
  268. code = $"Activity-{info.school}-{x.id}",
  269. type = "vote",
  270. name = info.name,
  271. startTime = info.startTime,
  272. endTime = info.endTime,
  273. scode = info.code,
  274. scope = info.scope,
  275. school = info.school,
  276. creatorId = info.creatorId,
  277. subjects = new List<string>() { "" },
  278. blob = null,
  279. owner = info.owner
  280. });
  281. });
  282. }
  283. await TriggerStuActivity.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities);
  284. }
  285. }
  286. return new OkObjectResult(new { });
  287. }
  288. /// <summary>
  289. /// 设置问卷调查未初始化学生列表的业务
  290. /// </summary>
  291. /// <param name="req"></param>
  292. /// <param name="log"></param>
  293. /// <returns></returns>
  294. [FunctionName("fix-survey-activity")]
  295. public async Task<IActionResult> SurveyActivity(
  296. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  297. ILogger log)
  298. {
  299. log.LogInformation("fix-survey-activity...");
  300. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  301. List<string> datas = JsonSerializer.Deserialize<List<string>>(requestBody);
  302. var client = _azureCosmos.GetCosmosClient();
  303. var query = $"select * from c ";
  304. foreach (string data in datas)
  305. {
  306. List<Survey> surveys = new List<Survey>();
  307. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
  308. queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{data}") }))
  309. {
  310. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  311. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  312. {
  313. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  314. {
  315. surveys.Add(obj.ToObject<Survey>());
  316. }
  317. }
  318. }
  319. log.LogInformation($"{surveys.ToJsonString()}");
  320. foreach (var info in surveys)
  321. {
  322. if (!info.classes.IsNotEmpty())
  323. {
  324. continue;
  325. }
  326. (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, _dingDing, info.classes, info.school);
  327. List<StuActivity> stuActivities = new List<StuActivity>();
  328. List<StuActivity> tmdActivities = new List<StuActivity>();
  329. if (tmdids.IsNotEmpty())
  330. {
  331. tmdids.ForEach(x => {
  332. tmdActivities.Add(new StuActivity
  333. {
  334. pk = "Activity",
  335. id = info.id,
  336. code = $"Activity-{x}",
  337. type = "survey",
  338. name = info.name,
  339. startTime = info.startTime,
  340. endTime = info.endTime,
  341. scode = info.code,
  342. scope = info.scope,
  343. school = info.school,
  344. creatorId = info.creatorId,
  345. subjects = new List<string>() { "" },
  346. blob = info.blob,
  347. owner=info.owner
  348. });
  349. });
  350. }
  351. if (studentss.IsNotEmpty())
  352. {
  353. studentss.ForEach(x => {
  354. stuActivities.Add(new StuActivity
  355. {
  356. pk = "Activity",
  357. id = info.id,
  358. code = $"Activity-{info.school}-{x.id}",
  359. type = "survey",
  360. name = info.name,
  361. startTime = info.startTime,
  362. endTime = info.endTime,
  363. scode = info.code,
  364. scope = info.scope,
  365. school = info.school,
  366. creatorId = info.creatorId,
  367. subjects = new List<string>() { "" },
  368. blob=info.blob,
  369. owner = info.owner
  370. });
  371. });
  372. }
  373. await TriggerStuActivity.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities);
  374. }
  375. }
  376. return new OkObjectResult(new { });
  377. }
  378. /// <summary>
  379. //获取题目摘要信息
  380. /// </summary>
  381. /// <param name="request"></param>
  382. /// <returns></returns>
  383. [ProducesDefaultResponseType]
  384. //[AuthToken(Roles = "teacher")]
  385. [FunctionName("fix-itemcond")]
  386. public async Task<IActionResult> FixItemCond(
  387. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  388. ILogger log)
  389. {
  390. try {
  391. var client = _azureCosmos.GetCosmosClient();
  392. List<ItemInfo> items = new List<ItemInfo>();
  393. var queryslt = $"SELECT value(c) FROM c where c.pid = null ";
  394. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-hbcn") }))
  395. {
  396. items.Add(item);
  397. }
  398. List<ItemCond> itemConds = new List<ItemCond>();
  399. items.GroupBy(x => x.periodId).Select(y=>new {key= y.Key,list=y.ToList() }).ToList().ForEach(z => {
  400. ItemCond cond = new ItemCond() { id = z.key, code = $"ItemCond-hbcn", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
  401. z.list.ForEach(y => {
  402. ItemService.CountItemCond(y, null, cond);
  403. });
  404. itemConds.Add(cond);
  405. });
  406. itemConds.ForEach(async cond =>
  407. {
  408. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ItemCond>(cond, new PartitionKey(cond.code));
  409. });
  410. return new OkObjectResult(new { itemConds });
  411. } catch (Exception ex) { await _dingDing.SendBotMsg($"TEAMModelFunction,ActivityHttpTrigger,fix-itemcond()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組); }
  412. return new OkObjectResult(new { });
  413. }
  414. /// <summary>
  415. /// 设置问卷调查未初始化学生列表的业务
  416. /// </summary>
  417. /// <param name="req"></param>
  418. /// <param name="log"></param>
  419. /// <returns></returns>
  420. [FunctionName("refresh-stu-activity")]
  421. public async Task<IActionResult> RefreshStuActivity(
  422. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  423. ILogger log)
  424. {
  425. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  426. dynamic json = JsonSerializer.Deserialize<dynamic>(requestBody);
  427. string id = json.id;
  428. string code = json.code;
  429. if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(code)) {
  430. return new BadRequestResult();
  431. }
  432. var client = _azureCosmos.GetCosmosClient();
  433. // var query = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c join A1 in c.classes where c.pk='{type}' and A1 in('{stuListChange.listid}') ";
  434. MQActivity activity = null;
  435. try {
  436. var aactivity= await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(code));
  437. using var da = await JsonDocument.ParseAsync(aactivity.ContentStream);
  438. activity = da.ToObject<MQActivity>();
  439. } catch (Exception ex) {
  440. }
  441. if (activity != null)
  442. {
  443. (List<string> tmdids, List<Students> students) = await TriggerStuActivity.GetStuList(client, _dingDing, activity.classes, activity.school);
  444. if (tmdids.IsNotEmpty())
  445. {
  446. foreach (string tmdid in tmdids)
  447. {
  448. var stucourse = new StuActivity
  449. {
  450. id = activity.id,
  451. scode = activity.code,
  452. name = activity.name,
  453. code = $"Activity-{tmdid}",
  454. scope = activity.scope,
  455. school = activity.school,
  456. creatorId = activity.creatorId,
  457. pk = "Activity",
  458. type = activity.pk,
  459. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  460. startTime = activity.startTime,
  461. endTime = activity.endTime,
  462. blob = activity.blob,
  463. owner = activity.owner
  464. };
  465. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  466. }
  467. }
  468. if (students.IsNotEmpty())
  469. {
  470. foreach (Students student in students)
  471. {
  472. var stucourse = new StuActivity
  473. {
  474. id = activity.id,
  475. scode = activity.code,
  476. name = activity.name,
  477. code = $"Activity-{activity.school}-{student.id}",
  478. scope = activity.scope,
  479. school = activity.school,
  480. creatorId = activity.creatorId,
  481. pk = "Activity",
  482. type = activity.pk,
  483. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  484. startTime = activity.startTime,
  485. endTime = activity.endTime,
  486. blob = activity.blob,
  487. owner = activity.owner
  488. };
  489. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  490. }
  491. }
  492. return new OkObjectResult(new { code = 200 });
  493. }
  494. else {
  495. return new BadRequestResult();
  496. }
  497. }
  498. /// <summary>
  499. ///获取单个目录的大小,用于获取评测,试题,试卷,问卷,投票等 文件层级超过两层的文件。
  500. ///例如 /exam/uuid/xxx /item/uuid/xxx /paper/uuid/xxx /vote/uuid/xxx /suervy/uuid/xxx
  501. /// {"name":"hbcn","/item/uuid/xxx"}
  502. /// </summary>
  503. /// <param name="req"></param>
  504. /// <param name="log"></param>
  505. /// <returns></returns>
  506. [FunctionName("get-prefixsize")]
  507. public async Task<IActionResult> GetPrefixsize(
  508. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  509. ILogger log)
  510. {
  511. try {
  512. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  513. var data = System.Text.Json.JsonSerializer.Deserialize<JsonElement>(requestBody);
  514. if (data.TryGetProperty("name", out JsonElement name) && data.TryGetProperty("root", out JsonElement root))
  515. {
  516. var size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize($"{root}");
  517. return new OkObjectResult(new { size = size });
  518. }
  519. else
  520. {
  521. return new BadRequestResult();
  522. }
  523. } catch (Exception ex) {
  524. await _dingDing.SendBotMsg($"TEAMModelFunction,ActivityHttpTrigger,get-prefixsize()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  525. return new BadRequestResult();
  526. }
  527. }
  528. /// <summary>
  529. ///获取多个blob路径的文件大小
  530. /// {"name":"hbcn","blobs":["/paper/uuid/xxx.json","/paper/uuid/aaa.json"]}
  531. /// </summary>
  532. /// <param name="req"></param>
  533. /// <param name="log"></param>
  534. /// <returns></returns>
  535. [FunctionName("get-blobsize")]
  536. public async Task<IActionResult> GetBlobsize(
  537. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  538. ILogger log)
  539. {
  540. try {
  541. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  542. var data = System.Text.Json.JsonSerializer.Deserialize<JsonElement>(requestBody);
  543. if (data.TryGetProperty("name", out JsonElement name) && data.TryGetProperty("blobs", out JsonElement blob))
  544. {
  545. List<string> blobs = JsonSerializer.Deserialize<List<string>>(blob.ToJsonString());
  546. var size= await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(blobs);
  547. return new OkObjectResult(new { size = size });
  548. }
  549. else {
  550. return new BadRequestResult();
  551. }
  552. } catch (Exception ex)
  553. {
  554. await _dingDing.SendBotMsg($"TEAMModelFunction,ActivityHttpTrigger,get-blobsize()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  555. return new BadRequestResult();
  556. }
  557. }
  558. /// <summary>
  559. /// 修复容器的内容显示
  560. /// </summary>
  561. /// <param name="req"></param>
  562. /// <param name="log"></param>
  563. /// <returns></returns>
  564. [FunctionName("fix-blob-content")]
  565. public async Task<IActionResult> FixBlobContent(
  566. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  567. ILogger log)
  568. {
  569. try
  570. {
  571. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  572. var data = System.Text.Json.JsonSerializer.Deserialize<JsonElement>(requestBody);
  573. if (data.TryGetProperty("name", out JsonElement name) ) {
  574. var client = _azureCosmos.GetCosmosClient();
  575. List<string> prefixs = new List<string>() { "audio","doc","image","other","res","video", "thum" };
  576. var ContainerClient = _azureStorage.GetBlobContainerClient($"{name}");
  577. string scope = "private";
  578. if (data.TryGetProperty("scope", out JsonElement _scope)){
  579. scope = $"{_scope}";
  580. }
  581. var tb = "Teacher";
  582. if (scope != "private") {
  583. tb = "School";
  584. }
  585. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  586. // List<Task<ItemResponse<Bloblog>>> responses = new List<Task<ItemResponse<Bloblog>>>();
  587. foreach (var prefix in prefixs) {
  588. List<string> items = await ContainerClient.List(prefix);
  589. foreach (var item in items) {
  590. var urlsSize = await ContainerClient.GetBlobsSize(item);
  591. Bloblog bloblog = new Bloblog { id = Guid.NewGuid().ToString(),code= $"Bloblog-{name}", pk = "Bloblog", time = now,url= item, size = urlsSize != null && urlsSize.HasValue ? urlsSize.Value : 0, type = prefix};
  592. await client.GetContainer("TEAMModelOS", tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code)) ;
  593. }
  594. }
  595. // await Task.WhenAll(responses);
  596. }
  597. return new OkObjectResult(new { });
  598. }
  599. catch (Exception ex)
  600. {
  601. await _dingDing.SendBotMsg($"TEAMModelFunction,ActivityHttpTrigger,fix-blob-content()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  602. return new BadRequestResult();
  603. }
  604. }
  605. }
  606. }