123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401 |
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
- using TEAMModelOS.SDK.DI;
- using System.Net.Http;
- using TEAMModelOS.SDK.Helper.Security.ShaHash;
- using TEAMModelOS.SDK.Extension;
- using System.IdentityModel.Tokens.Jwt;
- using Microsoft.AspNetCore.Authorization;
- using TEAMModelOS.Filter;
- using StackExchange.Redis;
- using Azure.Messaging.ServiceBus;
- using static TEAMModelOS.SDK.DI.AzureStorageBlobExtensions;
- using System.Linq;
- using Microsoft.AspNetCore.Http;
- using HTEXLib.COMM.Helpers;
- using TEAMModelOS.Models;
- using Microsoft.Extensions.Options;
- using TEAMModelOS.SDK.Models;
- using Azure.Cosmos;
- using Azure;
- using System.IO;
- using Azure.Storage.Blobs.Models;
- using Azure.Storage.Sas;
- using ContentTypeDict = TEAMModelOS.SDK.ContentTypeDict;
- using TEAMModelOS.SDK.Services;
- using DocumentFormat.OpenXml.Wordprocessing;
- using OpenXmlPowerTools;
- using Azure.Storage.Blobs;
- using Azure.Storage.Blobs.Specialized;
- using DocumentFormat.OpenXml.Drawing.Wordprocessing;
- namespace TEAMModelOS.Controllers
- {
- [Route("blob")]
- [ApiController]
- public class BlobController : ControllerBase
- {
- private readonly AzureStorageFactory _azureStorage;
- private readonly IHttpClientFactory _clientFactory;
- private readonly AzureRedisFactory _azureRedis;
- private readonly AzureServiceBusFactory _serviceBus;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly HttpTrigger _httpTrigger;
- public IConfiguration _configuration { get; set; }
- public BlobController(HttpTrigger httpTrigger, AzureStorageFactory azureStorage, AzureServiceBusFactory serviceBus, IHttpClientFactory clientFactory, AzureRedisFactory azureRedis, IConfiguration configuration,
- DingDing dingDing,
- IOptionsSnapshot<Option> option, AzureCosmosFactory azureCosmos)
- {
- _azureStorage = azureStorage;
- _clientFactory = clientFactory;
- _serviceBus = serviceBus;
- _azureRedis = azureRedis;
- _configuration = configuration;
- _dingDing = dingDing;
- _option = option?.Value;
- _azureCosmos = azureCosmos;
- _httpTrigger = httpTrigger;
- }
- /// <summary>
- /// 上传文件到指定的0-public
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("public-upload")]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin,business")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> PublicUpload([FromForm] IFormFile file,[FromForm]string path, [FromForm] int self=0 )
- {
- var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
- string fileExt = FileType.GetExtention(file.FileName).ToLower();
- if (string.IsNullOrWhiteSpace(path)) {
- path="school";
- }
- if (ContentTypeDict.dict.ContainsKey($".{fileExt}"))
- {
- string cnt = self==0 ? "0-public" : school;
- var url = await _azureStorage.GetBlobContainerClient(cnt).UploadFileByContainer(file.OpenReadStream(), path, $"{Guid.NewGuid()}.{fileExt}", false);
- return Ok(new { url });
- }
- else {
- return BadRequest();
- }
- }
- /// <summary>
- /// 上传文件到指定的0-public 课例报告
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("public-upload-lesson-report")]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- public async Task<IActionResult> PublicUploadLessonReport([FromForm] IFormFile file)
- {
- var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
- string fileExt = FileType.GetExtention(file.FileName).ToLower();
- if (ContentTypeDict.dict.ContainsKey($".{fileExt}"))
- {
- var url = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(file.OpenReadStream(), $"lesson-report/{school}", $"{file.FileName}", false);
- return Ok(new { url });
- }
- else
- {
- return BadRequest();
- }
- }
- /// <summary>
- /// 获取某个容器的只读权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("sas-r")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public IActionResult BlobSasR(BlobSas request)
- {
- ///返回金钥过期时间
- // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
- // dict.Add(d.Key, d.Value);
- return Ok(_azureStorage.GetContainerSasUri(request, true));
- } /// <summary>
- /// 获取某个容器的只读权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("sas-r-99")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public IActionResult BlobSasR99(JsonElement json )
- {
- ///返回金钥过期时间
- // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
- // dict.Add(d.Key, d.Value);
- var blob = _azureStorage.GetBlobContainerSAS99Year(json.GetProperty("containerName").GetString(), blobContainerSasPermissions: BlobContainerSasPermissions.Read|BlobContainerSasPermissions.List);
- return Ok(new { blob.uri, blob.sas});
- }
- /// <summary>
- /// 某个文件的上传SAS rcw权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("sas-rcwld")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public IActionResult BlobSasRCW(BlobSas request)
- {
- ///返回金钥过期时间
- // Dictionary<string,object> dict= await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
- // Dictionary<string, object> dict = ;
- //dict.Add(d.Key, d.Value);
- return Ok(_azureStorage.GetContainerSasUri(request, false));
- }
- /// <summary>
- /// 链接只读(读)
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("sas-url-r")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public IActionResult GetContainerSASRead(JsonElement azureBlobSASDto)
- {
- azureBlobSASDto.TryGetProperty("url", out JsonElement azureBlobSAS);
- //string azureBlobSAS = azureBlobSASDto;
- (string, string) a = BlobUrlString(azureBlobSAS.ToString());
- string ContainerName = a.Item1;
- string BlobName = a.Item2;
- bool flg = IsBlobName(BlobName);
- if (flg)
- {
- return Ok(_azureStorage.GetBlobSasUriRead(ContainerName, BlobName));
- }
- else
- {
- return Ok(new BlobAuth { url = $"{azureBlobSAS}", sas = "", name = $"{azureBlobSAS}" });
- };
- }
- /// <summary>
- /// 第三方企业上传logo到指定的0-public
- /// </summary>
- /// <param name="file"></param>
- /// <returns></returns>
- [HttpPost("biz-upload-public")]
- [RequestSizeLimit(102_400_000_00)] //最大10000m左右
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> UploadPublic([FromForm] IFormFile file)
- {
- var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
- string fileExt = FileType.GetExtention(file.FileName).ToLower();
- if (ContentTypeDict.dict.ContainsKey($".{fileExt}"))
- {
- var url = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(file.OpenReadStream(), "business", $"{Guid.NewGuid()}.{fileExt}", false);
- return Ok(new { url });
- }
- else
- {
- return BadRequest();
- }
- }
- /// <summary>
- /// 获取文件内容
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("get-text")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> GetText(JsonElement request)
- {
- request.TryGetProperty("code", out JsonElement code);
- string azureBlobSAS = System.Web.HttpUtility.UrlDecode(code.ToString(), Encoding.UTF8);
- (string, string) a = BlobUrlString(azureBlobSAS);
- string ContainerName = a.Item1;
- string BlobName = a.Item2;
- bool flg = IsBlobName(BlobName);
- if (flg)
- {
- //TODO 需驗證
- BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(ContainerName, BlobName);
- var response = await _clientFactory.CreateClient().GetAsync(new Uri(blobAuth.url + blobAuth.sas));
- response.EnsureSuccessStatusCode();
- using var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
- return Ok(json.RootElement);
- }
- else
- {
- return BadRequest("文件名错误");
- };
- }
- /// <summary>
- /// {"containerName":"hbcn","scope":"school"}
- /// 获取容器的 分类及总量
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("used-space")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public async Task<ActionResult> UsedSpace(JsonElement request)
- {
- request.TryGetProperty("scope", out JsonElement _scope);
- if (!request.TryGetProperty("containerName", out JsonElement containerName)) return BadRequest();
- (long usedSize, long teach, long total, long surplus, Dictionary<string, double?> catalog)space =await BlobService.GetSurplusSpace(containerName.ToString(), $"{_scope}",_option.Location, _azureCosmos, _azureRedis, _azureStorage,_dingDing,_httpTrigger);
- return Ok(new { size = space.usedSize, catalog = space.catalog, teach=space. teach , surplus = space.surplus, total=space.total });
- }
- private static (string, string) BlobUrlString(string sasUrl)
- {
- sasUrl = sasUrl.Substring(8);
- string[] sasUrls = sasUrl.Split("/");
- string ContainerName;
- ContainerName = sasUrls[1].Clone().ToString();
- string item = sasUrls[0] + "/" + sasUrls[1] + "/";
- string blob = sasUrl.Replace(item, "");
- return (ContainerName, blob);
- }
- public static bool IsBlobName(string BlobName)
- {
- return System.Text.RegularExpressions.Regex.IsMatch(BlobName,
- @"(?!((^(con)$)|^(con)\\..*|(^(prn)$)|^(prn)\\..*|(^(aux)$)|^(aux)\\..*|(^(nul)$)|^(nul)\\..*|(^(com)[1-9]$)|^(com)[1-9]\\..*|(^(lpt)[1-9]$)|^(lpt)[1-9]\\..*)|^\\s+|.*\\s$)(^[^\\\\\\:\\<\\>\\*\\?\\\\\\""\\\\|]{1,255}$)");
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("bloblog-list")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public async Task<ActionResult> BloblogList(JsonElement request)
- {
- List<Bloblog> bloblogs = new List<Bloblog>();
- try
- {
- request.TryGetProperty("name", out JsonElement name);
- request.TryGetProperty("type", out JsonElement type);
- request.TryGetProperty("scope", out JsonElement scope);
- request.TryGetProperty("periodId", out JsonElement periodId);
- request.TryGetProperty("ext", out JsonElement _ext);
- var client = _azureCosmos.GetCosmosClient();
- if (scope.GetString().Equals("school"))
- {
- var queryslt = new StringBuilder($"SELECT value(c) FROM c WHERE c.type='{type}' ");
- if (!string.IsNullOrEmpty($"{periodId}")) {
- queryslt = new StringBuilder($"SELECT value(c) FROM c join A1 in c.periodId WHERE c.type='{type}' and A1 in ('{periodId}') ");
- }
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
- {
- bloblogs.Add(item);
- }
- }
- else if (scope.GetString().Equals("private"))
- {
- var queryslt = new StringBuilder($"SELECT value(c) FROM c WHERE c.type='{type}' ");
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Bloblog>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
- {
- bloblogs.Add(item);
- }
- }
- bloblogs.ForEach(x => {
- string ext = x.type;
- if (x.url.LastIndexOf(".") > 0)
- {
- ext = x.url.Substring(x.url.LastIndexOf(".") > 0 ? x.url.LastIndexOf(".") : 0);
- ext = ext.Replace(".","").ToLower();
- }
- x.ext = ext;
- });
- if (!string.IsNullOrWhiteSpace($"{_ext}")) {
- bloblogs= bloblogs.Where(x => x.ext.Equals($"{_ext}", StringComparison.OrdinalIgnoreCase)).ToList();
- }
- return Ok(new { bloblogs = bloblogs });
- }
- catch (Exception ex)
- {
- return Ok(new { bloblogs = bloblogs });
- }
- }
-
- /// <summary>
- /// 重命名
- /// {"scope":"school","cntr":"hbcn","id":"bbf24ca7-487e-4196-8c99-b2c418a2d1b1","newName":"video/核能1.mp4"}
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- [HttpPost("bloblog-rename")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public async Task<ActionResult> BloblogRename(JsonElement request)
- {
- var client = _azureCosmos.GetCosmosClient();
- if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
- if(!request.TryGetProperty("cntr", out JsonElement _cntr))return BadRequest();
- if (!request.TryGetProperty("id", out JsonElement _id))return BadRequest();
- if (!request.TryGetProperty("newName", out JsonElement _newName))return BadRequest();
- string tbname= $"{_scope}".Equals("school",StringComparison.OrdinalIgnoreCase) ? "School": "Teacher";
- try
- {
- Bloblog bloblog = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<Bloblog>($"{_id}", new PartitionKey($"Bloblog-{_cntr}"));
- string oldUrl = bloblog.url;
- if (oldUrl.Equals($"{_newName}")) {
- return Ok();
- }
- if (oldUrl.StartsWith("res", StringComparison.OrdinalIgnoreCase) && oldUrl.EndsWith(".htex", StringComparison.OrdinalIgnoreCase)) {
- oldUrl = oldUrl.Replace(".htex", "", StringComparison.OrdinalIgnoreCase);
- }
- string newName = $"{_newName}";
- if (newName.StartsWith("res", StringComparison.OrdinalIgnoreCase) && newName.EndsWith(".htex", StringComparison.OrdinalIgnoreCase))
- {
- newName = newName.Replace(".htex", "", StringComparison.OrdinalIgnoreCase);
- }
- bloblog.name = $"{_newName}";
- bloblog.url = $"{_newName}";
- var bcc = _azureStorage.GetBlobContainerClient($"{_cntr}");
- string px = oldUrl;
- if (oldUrl.StartsWith("/"))
- {
- px = oldUrl.Substring(1);
- }
- List<BlobItem> blobItems = new List<BlobItem>();
- await foreach (var item in bcc.GetBlobsAsync(BlobTraits.None, BlobStates.None, px)) {
- blobItems.Add(item);
- }
- foreach (var item in blobItems)
- {
- if (item.Name.StartsWith("image") || item.Name.StartsWith("video"))
- {
- var thum = $"thum{ item.Name.Substring(5)}";
- var tnewName = $"thum{ newName.Substring(5)}";
- var tpx = $"thum{ px.Substring(5)}";
- string tname = thum.Replace(tpx, tnewName);
- if (item.Name.StartsWith("video")) {
- string fileexturl = thum.Substring(thum.LastIndexOf(".") > 0 ? thum.LastIndexOf(".") : 0);
- thum = thum.Replace(fileexturl, ".png");
- string fileextname = tname.Substring(tname.LastIndexOf(".") > 0 ? tname.LastIndexOf(".") : 0);
- tname = tname.Replace(fileextname, ".png");
- }
- var turl = _azureStorage.GetBlobSAS($"{_cntr}", thum, BlobSasPermissions.Read | BlobSasPermissions.List);
- try
- {
- bcc.GetBlobClient(tname).SyncCopyFromUri(new Uri(turl));
- await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { thum });
- }
- catch (Exception)
- {
- continue;
- }
- }
- string targetName = item.Name.Replace(px, newName);
- var url = _azureStorage.GetBlobSAS($"{_cntr}", item.Name, BlobSasPermissions.Read | BlobSasPermissions.List);
- bcc.GetBlobClient(targetName).SyncCopyFromUri(new Uri(url));
- };
- await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{_cntr}", new List<string> { px });
- await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<Bloblog>(bloblog, $"{_id}", new PartitionKey($"Bloblog-{_cntr}"));
- string u = "";
- string[] uls = System.Web.HttpUtility.UrlDecode(px, Encoding.UTF8).Split("/");
- if (uls != null)
- {
- u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
- }
- if (!string.IsNullOrEmpty(u)) {
-
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = u, name = $"{_cntr}" }, _serviceBus, _configuration, _azureRedis);
- }
- return Ok(new { status = true });
- }
- catch (CosmosException ex)
- {
- return BadRequest();
- }
- catch (Exception ex )
- {
- return BadRequest();
- }
- }
- /*
- 新增 编辑接口
- {
- "periodId": "",
- "scope": "school",
- "name": "hbcn",
- "url": "video/xxx.png",
- "opt": "add",
- }
- */
- /*
- {
- "scope": "school",
- "name": "hbcn",
- "opt": "del",
- "id": "19ccce98-c524-4ea7-aabc-887d1391e551"
- }
- */
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("bloblog-upsert")]
- [AuthToken(Roles = "teacher,admin,student")]
- [Authorize(Roles = "IES")]
- public async Task<ActionResult> BloblogOpt(JsonElement request) {
- try
- {
- request.TryGetProperty("periodId", out JsonElement periodId);
- request.TryGetProperty("subjectId", out JsonElement subjectId);
- request.TryGetProperty("gradeId", out JsonElement gradeId);
- request.TryGetProperty("scope", out JsonElement scope);
- request.TryGetProperty("name", out JsonElement name);
- request.TryGetProperty("url", out JsonElement jurls);
- //request.TryGetProperty("id", out JsonElement ids);
- //获取文件的大小
- var client = _azureCosmos.GetCosmosClient();
- List<string> urls = jurls.ToObject<List<string>>();
- HashSet<string> root = new HashSet<string>();
- List<Bloblog> bloblog = new List<Bloblog>();
- foreach (var uri in urls) {
- var url = System.Web.HttpUtility.UrlDecode(uri, Encoding.UTF8);
- string[] uls = url.Split("/");
- var u = "";
- if (uls != null)
- {
- u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
- root.Add(u);
- }
- long? size = 0;
- if (url.StartsWith("res/") && url.EndsWith("/index.json"))
- {
- var prefix= url.Substring(0, url.Length - 11);
- size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(prefix);
- }
- else {
- size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(url);
- }
-
- long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- //地址相同的,直接更新
- bool exsit = false;
-
- try
- {
- QueryDefinition queryDefinition = new QueryDefinition("SELECT value(c) FROM c WHERE c.url=@url");
- queryDefinition.WithParameter("@url", url);
- if (scope.GetString().Equals("school"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Bloblog>(queryDefinition: queryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
- {
- item.time = now;
- item.size = size != null && size.HasValue ? size.Value : 0;
- item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" };
- item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" };
- item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" };
- await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
- bloblog.Add(item);
- exsit = true;
- }
- }
- else if (scope.GetString().Equals("private"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Bloblog>(queryDefinition: queryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
- {
- item.time = now;
- item.size = size != null && size.HasValue ? size.Value : 0;
- item.periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" };
- item.subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" };
- item.gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" };
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Bloblog>(item, item.id, new Azure.Cosmos.PartitionKey(item.code));
- bloblog.Add(item);
- exsit = true;
- }
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}\n{ex.StackTrace}\n{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
- }
- if (!exsit)
- {
- string ext = u;
- if (url.LastIndexOf(".") > 0) {
- ext = url.Substring(url.LastIndexOf(".") > 0 ? url.LastIndexOf(".") : 0);
- ext= ext.ToLower();
- }
- var blob = new Bloblog
- {
- id = Guid.NewGuid().ToString(),
- pk = "Bloblog",
- code = $"Bloblog-{name}",
- url = url,
- time = now,
- size = size != null && size.HasValue ? size.Value : 0,
- periodId = periodId.ValueKind.Equals(JsonValueKind.Array) ? periodId.ToObject<List<string>>() : new List<string> { "" },
- subjectId = subjectId.ValueKind.Equals(JsonValueKind.Array) ? subjectId.ToObject<List<string>>() : new List<string> { "" },
- gradeId = gradeId.ValueKind.Equals(JsonValueKind.Array) ? gradeId.ToObject<List<string>>() : new List<string> { "" },
- type = u,
- ext= ext
- };
- if (scope.GetString().Equals("school"))
- {
- await client.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
- }
- else if (scope.GetString().Equals("private"))
- {
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(blob, new Azure.Cosmos.PartitionKey(blob.code));
- }
- bloblog.Add(blob);
- }
- }
- root.ToList().ForEach(async x => {
-
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = x, name = $"{name}" }, _serviceBus, _configuration, _azureRedis);
- });
- return Ok(new { bloblog, status = 200 });
-
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}\n{ex.StackTrace}\n{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
- }
- return Ok(new { status = 200 });
- }
- /// <summary>
- /// 删除prefix 不管是内容 模块还是其他试题试卷 评测 问卷投票等都在使用。
- ///
- /// {"cntr":"","prefix":"res/test"}
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("delete-prefix-student")]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,student")]
- public async Task<IActionResult> DeletePrefixStudent(JsonElement json)
- {
- var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
- string blobContainerName = null;
- string prefix = null;
- if (json.TryGetProperty("cntr", out JsonElement cntr) && cntr.ValueKind.Equals(JsonValueKind.String))
- {
- var cntrs = cntr.GetString();
- blobContainerName = $"{cntrs}";
- }
- if (json.TryGetProperty("prefix", out JsonElement prefixjson) && prefixjson.ValueKind.Equals(JsonValueKind.String))
- {
- prefix = prefixjson.GetString();
- }
- if (!prefix.Contains(id)) {
- return Ok(new { error = 400, msg = "学生端操作删除文件只能删除自己所在的文件夹或文件。" });
- }
- if (prefix != null && blobContainerName != null)
- {
- var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, new List<string> { prefix });
- string u = "";
- string[] uls = System.Web.HttpUtility.UrlDecode($"{prefixjson}", Encoding.UTF8).Split("/");
- if (uls != null)
- {
- u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
- }
-
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = u, name = $"{blobContainerName}" }, _serviceBus, _configuration, _azureRedis);
- return Ok(new { status });
- }
- else
- {
- return Ok(new { error=400,msg="参数错误!" });
- }
- }
- /// <summary>
- /// 删除prefix 不管是内容 模块还是其他试题试卷 评测 问卷投票等都在使用。
- ///
- /// {"cntr":"","prefix":"res/test"}
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("delete-prefix")]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- public async Task<IActionResult> DeletePrefix(JsonElement json)
- {
- var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
- string blobContainerName = null;
- string prefix = null;
- if (json.TryGetProperty("cntr", out JsonElement cntr) && cntr.ValueKind.Equals(JsonValueKind.String))
- {
- var keyToken = HttpContext.GetAuthTokenKey("area");
- var cntrs = cntr.GetString();
- if (cntrs.Equals(id))
- {
- blobContainerName = id;
- }
- else if (cntrs.Equals(school))
- {
- blobContainerName = school;
- }else if (cntrs.Equals("teammodelos"))
- {
- blobContainerName = "teammodelos";
- }
- else
- {
- if (!string.IsNullOrWhiteSpace(keyToken.area) && keyToken.area.Equals(cntrs))
- {
- blobContainerName =cntrs;
- }
- else {
- if (json.TryGetProperty("prefix", out JsonElement _prefixjson) && _prefixjson.ValueKind.Equals(JsonValueKind.String))
- {
- if (_prefixjson.GetString().Contains(id))
- {
- blobContainerName=cntrs;
- }
- else { return BadRequest("只能删除本人管理的文件夹"); }
- }
- else {
- return BadRequest("只能删除本人管理的文件夹");
- }
-
- }
- }
- }
- if (json.TryGetProperty("prefix", out JsonElement prefixjson) && prefixjson.ValueKind.Equals(JsonValueKind.String))
- {
- prefix = prefixjson.GetString();
- }
- if (prefix != null && blobContainerName != null)
- {
- var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, new List<string> { prefix });
- string u = "";
- string[] uls = System.Web.HttpUtility.UrlDecode($"{prefixjson}", Encoding.UTF8).Split("/");
- if (uls != null)
- {
- u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
- }
-
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = u, name = $"{blobContainerName}" }, _serviceBus, _configuration, _azureRedis);
- return Ok(new { status });
- }
- else
- {
- return BadRequest();
- }
- }
- public record ContBlob {
- public string path { get; set; }
- public string id { get; set; }
- }
- /// <summary>
- /// 删除多个Url,只会在内容模块使用该接口
- ///
- ///
- /// {"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"}]}
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("bloblog-delete")]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- public async Task<IActionResult> DeleteBlobs(JsonElement json)
- {
- ///BlobBaseClient copy
- try {
- var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
- string blobContainerName = null;
- if (!json.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
- if (!json.TryGetProperty("blobs", out JsonElement _blobs)) return BadRequest();
- if (json.TryGetProperty("cntr", out JsonElement cntr))
- {
- var cntrs = cntr.ToString();
- if (cntrs.Equals(id))
- {
- blobContainerName = id;
- }
- else if (cntrs.Equals(school))
- {
- blobContainerName = school;
- }
- else
- {
- return BadRequest("只能删除本人管理的文件");
- }
- }
- bool flag = true;
- List<ContBlob> blobs = _blobs.ToObject<List<ContBlob>>();
- try
- {
- var client = _azureCosmos.GetCosmosClient();
- List<string> ids = blobs.Where(b=>!string.IsNullOrWhiteSpace(b.id)).Select(x => x.id).ToList();
- string containerId = "School";
- if (_scope.GetString().Equals("school"))
- {
- containerId = "School";
- }
- else if (_scope.GetString().Equals("private"))
- {
- containerId = "Teacher";
- }
- await client.GetContainer(Constant.TEAMModelOS, containerId).DeleteItemsAsync<Bloblog>(ids, $"Bloblog-{blobContainerName}");
- }
- catch (CosmosException ex )
- {
- //仅处理 cosmos不存在 但容器又存在的
- }
- if (flag)
- {
- var urls = blobs.Select(x => x.path).ToList();
- List<string> deleteUrl = new List<string>();
- urls.ForEach(x => {
- string delUrl = x;
- if (x.StartsWith("res/") && x.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
- {
- delUrl = x.Substring(0, x.Length - 4);
- }
- if (x.StartsWith("res/") && x.EndsWith("/index.json", StringComparison.OrdinalIgnoreCase))
- {
- delUrl = x.Substring(0, x.Length -11);
- }
- //自动删除视频和图片的缩略图
- if (x.StartsWith("image")||x.StartsWith("video")) {
- var thum = $"thum{ x.Substring(5)}" ;
- if (x.StartsWith("video"))
- {
- string fileexturl = thum.Substring(thum.LastIndexOf(".") > 0 ? thum.LastIndexOf(".") : 0);
- thum = thum.Replace(fileexturl, ".png");
- }
- deleteUrl.Add(thum);
- }
- deleteUrl.Add(delUrl);
- });
- var status = await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, blobContainerName, deleteUrl);
- //释放的空间
- HashSet<string> root = new HashSet<string>();
- foreach (var x in deleteUrl)
- {
- string url = System.Web.HttpUtility.UrlDecode(x, Encoding.UTF8);
-
-
- string[] uls = url.Split("/");
- if (uls != null)
- {
- string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
- root.Add(u);
- }
- }
- root.ToList().ForEach(async x => {
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = x, name = $"{blobContainerName}" }, _serviceBus, _configuration, _azureRedis);
- });
- return Ok(new { status });
- }
- else
- {
- return BadRequest("只能删除本人管理的文件");
- }
- } catch (Exception ex) {
- await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/delete-blobs\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 列出blob的
- ///
- /// {"cntr":"","urls":["res/test/1.json","res/test/2.json"]}
- /// {"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"}]}
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("blob-list")]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin,student")]
- public async Task<IActionResult> BlobList(JsonElement json) {
- var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
- List<string> paths = new List<string>();
- //文件的容器
- if (!json.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
- //业务存取类型 exam,vote,survey,item,paper,syllabus,records,avatar,content(doc,image,res,video,audio,other),thum,train,temp,jyzx
- if (!json.TryGetProperty("type", out JsonElement _type)) return BadRequest();
- //文件前缀prefix
- switch (true)
- {
- case bool when $"{_type}".Equals("exam", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("vote", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("survey", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("item", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("paper", StringComparison.OrdinalIgnoreCase):
- string type = $"{_type}".Substring(0, 1).ToUpper() + $"{_type}".Substring(1);
- //业务存取id
- if (!json.TryGetProperty("id", out JsonElement _aid)) return BadRequest();
- //业务存取分区键
- if (!json.TryGetProperty("code", out JsonElement _acode)) return BadRequest();
- //业务存取分区键
- if (!json.TryGetProperty("scope", out JsonElement _ascope)) return BadRequest();
- break;
- case bool when $"{_type}".Equals("doc", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("image", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("res", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("video", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("audio", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("other", StringComparison.OrdinalIgnoreCase):
- //业务存取id
- if (!json.TryGetProperty("id", out JsonElement _bid)) return BadRequest();
- //业务存取分区键
- if (!json.TryGetProperty("code", out JsonElement _bcode)) return BadRequest();
- //业务存取分区键
- if (!json.TryGetProperty("scope", out JsonElement _bscope)) return BadRequest();
- break;
- case bool when $"{_type}".Equals("records", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("syllabus", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("thum", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("temp", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("jyzx", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("avatar", StringComparison.OrdinalIgnoreCase)
- || $"{_type}".Equals("train", StringComparison.OrdinalIgnoreCase):
- break;
- default:
- break;
- }
- return Ok(new { paths ,status=1});
- }
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("delete-unlink")]
- public async Task<IActionResult> DeleteUnlink(JsonElement json) {
- try {
- var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
- int status = 0;
- if (!json.TryGetProperty("scope", out JsonElement scope))
- {
- return Ok(new { status, msg = "参数错误" });
- }
- string containerName = scope.ToString().Equals("school", StringComparison.OrdinalIgnoreCase) ? school : userid;
- bool exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (exists)
- {
- var result = await _azureRedis.GetRedisClient(8).StringGetAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (result.HasValue)
- {
- List<UnLink> unLinksData = result.ToString().ToObject<List<UnLink>>();
- if (unLinksData.IsNotEmpty())
- {
- var uri = _azureStorage.GetBlobContainerClient(containerName).Uri;
- BlobBatchClient blobBatch = _azureStorage.GetBlobContainerClient(containerName).GetBlobBatchClient();
- foreach (var unLink in unLinksData)
- {
- var urls = unLink.blobs.Select(z => z.Key).Select(z => new Uri(Path.Combine(uri.ToString(), z)));
- int len = 100;
- if (urls.Count() > 0)
- {
- if (urls.Count() <= len)
- {
- try
- {
- await blobBatch.DeleteBlobsAsync(urls);
- }
- catch (Exception ex) { }
- }
- else
- {
- int pages = (urls.Count() + len) / len; //256是批量操作最大值,pages = (total + max -1) / max;
- for (int i = 0; i < pages; i++)
- {
- List<Uri> lists = urls.Skip((i) * len).Take(len).ToList();
- try
- {
- await blobBatch.DeleteBlobsAsync(lists);
- }
- catch (Exception ex) { }
- }
- }
- }
- }
- //为节省服务器开销, 限制只能一天清理一次
- #if DEBUG
- _azureRedis.GetRedisClient(8).StringSet($"Blob:ScanResult:{scope}:{containerName}", new List<UnLink>().ToJsonString(), expiry: new TimeSpan(0, 0, 30));
- #else
- _azureRedis.GetRedisClient(8).StringSet($"Blob:ScanResult:{scope}:{containerName}", new List<UnLink>().ToJsonString(), expiry: new TimeSpan(24, 0, 0));
- #endif
- HashSet<string> root = null;
- if (_option.Location.Contains("Test", StringComparison.OrdinalIgnoreCase) || _option.Location.Contains("Dep", StringComparison.OrdinalIgnoreCase))
- {
- root = unLinksData.Select(x => x.prefix).ToHashSet();
- }
- else
- {
- root = unLinksData.Where(z => z.size > 0).Select(x => x.prefix).ToHashSet();
- }
- if (root != null)
- {
- root.ToList().ForEach(async x => {
- await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = x, name = $"{containerName}" }, _serviceBus, _configuration, _azureRedis);
- });
- }
- return Ok(new { status = 3, msg = "清理成功!" });
- }
- else
- {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!" });
- }
- }
- else
- {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!" });
- }
- }
- else
- {
- return Ok(new { status = 4, msg = "请重新检查清理项!" });
- }
- } catch (Exception ex ) {
- await _dingDing.SendBotMsg($"{_option.Location},{DeleteUnlink}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- }
- return Ok(new { status = 4, msg = "请重新检查清理项!" });
- }
- [ProducesDefaultResponseType]
- [Authorize(Roles = "IES")]
- [AuthToken(Roles = "teacher,admin")]
- [HttpPost("scan-unlink")]
- public async Task<IActionResult> ScanUnlink(JsonElement json)
- {
- var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
- int status = 0;
- if ( !json.TryGetProperty("scope", out JsonElement scope))
- {
- return Ok(new { status, msg = "参数错误" });
- }
- string containerName = scope.ToString().Equals("school", StringComparison.OrdinalIgnoreCase) ? school : userid;
- if (string.IsNullOrWhiteSpace(containerName)) {
- return Ok(new { status, msg = "参数错误" });
- }
- bool exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (exists)
- {
- var result = await _azureRedis.GetRedisClient(8).StringGetAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (result.HasValue)
- {
- List<UnLink> unLinksData = result.ToString().ToObject<List<UnLink>>();
- if (unLinksData.IsNotEmpty())
- {
- var groupData = unLinksData.GroupBy(x => x.prefix).ToList();
- List<dynamic> summaryData = new List<dynamic>();
- long totalCountData = 0;
- long? totalSizeData = 0;
- groupData.ForEach(x =>
- {
- long count = x.ToList().SelectMany(z => z.blobs).Count();
- long? size = x.Select(y => y.size).Sum();
- totalSizeData += size;
- totalCountData += count;
- summaryData.Add(new { prefix = x.Key, count = count, size = size });
- });
- //为节省服务器开销, 限制只能一天清理一次
- return Ok(new { status = 1, totalCount = totalCountData, totalSize = totalSizeData, summary = summaryData, unLinks = unLinksData });
- }
- else {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!" });
- }
- }
- else {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!" });
- }
-
- }
- List<UnLink> unLinks = new List<UnLink>();
- //文件名(关联路径)导向的文件夹 内容(audio doc image video other thum 缩略图) 学生头像(avatar)
- string[] prefixFile = new string[] { "audio", "doc", "image", "video", "other", "thum", "avatar" };
- foreach (string prefix in prefixFile)
- {
- string tbname = scope.ToString().Equals("school") ? Constant.School : Constant.Teacher;
- switch (true)
- {
- //Bloblog
- case bool when prefix.Equals("doc", StringComparison.OrdinalIgnoreCase)
- || prefix.Equals("image", StringComparison.OrdinalIgnoreCase)
- || prefix.Equals("video", StringComparison.OrdinalIgnoreCase)
- || prefix.Equals("audio", StringComparison.OrdinalIgnoreCase)
- || prefix.Equals("other", StringComparison.OrdinalIgnoreCase):
- {
- // audio/最愛情歌80-89-這些日子以來 (張清芳+范怡文).mp3
- List<KeyValuePair<string, long?>> blobs = new List<KeyValuePair<string, long?>>();
- await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
- {
- blobs.Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- if (blobs.IsNotEmpty())
- {
- string code = $"Bloblog-{containerName}";
- string sql = $"select value c from c where c.url in ({string.Join(",", blobs.Select(z => $"'{z.Key}'"))})";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetList<Bloblog>(sql, code);
- if (result.list.IsNotEmpty())
- {
- var urls = result.list.Select(x => x.url).ToHashSet();
- var unlink = blobs.ExceptBy(urls, x => x.Key).ToList();
- long? size = unlink.Select(z => z.Value).Sum();
- unLinks.Add(new UnLink { prefix = prefix, blobs = unlink, size = size });
- }
- else {
- long? size = blobs.Select(z => z.Value).Sum();
- unLinks.Add(new UnLink { prefix = prefix, blobs = blobs, size = size });
- }
- }
- break;
- }
- case bool when prefix.Equals("avatar", StringComparison.OrdinalIgnoreCase) && scope.ToString().Equals("school"):
- {
- List<KeyValuePair<string, long?>> blobs = new List<KeyValuePair<string, long?>>();
- await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
- {
- blobs.Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- if (blobs.IsNotEmpty())
- {
- string code = $"Base-{containerName}";
- List<KeyValuePair<string, long?>> unlink = new List<KeyValuePair<string, long?>>();
- foreach (var blob in blobs)
- {
- string sql = $"select value c from c where contains(c.picture,'{blob.Key}')";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").GetList<Student>(sql, code, pageSize: 1);
- //找不到对应的数据存储
- if (!result.list.IsNotEmpty())
- {
- unlink.Add(blob);
- }
- }
- long? size = unlink.Select(z => z.Value).Sum();
- unLinks.Add(new UnLink { prefix = prefix, blobs = unlink, size = size });
- }
- break;
- }
- default: break;
- }
- }
- //文件夹(关联id)导向的文件夹 exam homework art records survey vote item
- string[] prefixDirId = new string[] { "exam", "homework", "art", "records", "survey", "vote", "item" };
- foreach (string prefix in prefixDirId)
- {
- string tbname = string.Empty;
- HashSet<string> ids = new HashSet<string>();
- Dictionary<string, List<KeyValuePair<string, long?>>> recordUrls = new Dictionary<string, List<KeyValuePair<string, long?>>>();
- await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
- {
- var path = blobItem.Name.Split("/");
- if (path.Length > 2)
- {
- string id = path[1];
- ids.Add(id);
- if (recordUrls.ContainsKey(id))
- {
- recordUrls[id].Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- else
- {
- recordUrls[id] = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) };
- }
- }
- if (path.Length == 2)
- {
- unLinks.Add(new UnLink { prefix = prefix, blobs = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) }, size = blobItem.Properties.ContentLength });
- }
- }
- string code = string.Empty;
- if (ids.Any() && ids.Count > 0)
- {
- string sql = $"select value c.id from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))}) ";
- switch (true)
- {
- case bool when prefix.Equals("exam", StringComparison.OrdinalIgnoreCase):
- {
- tbname = Constant.Common;
- code = $"Exam-{containerName}";
- break;
- }
- case bool when prefix.Equals("homework", StringComparison.OrdinalIgnoreCase):
- {
- tbname = Constant.Common;
- code = $"Homework-{containerName}";
- break;
- }
- case bool when prefix.Equals("art", StringComparison.OrdinalIgnoreCase):
- {
- tbname = Constant.Common;
- code = $"Art-{containerName}";
- break;
- }
- case bool when prefix.Equals("survey", StringComparison.OrdinalIgnoreCase):
- {
- tbname = Constant.Common;
- code = $"Survey-{containerName}";
- break;
- }
- case bool when prefix.Equals("vote", StringComparison.OrdinalIgnoreCase):
- {
- tbname = Constant.Common;
- code = $"Vote-{containerName}";
- break;
- }
- case bool when prefix.Equals("item", StringComparison.OrdinalIgnoreCase):
- {
- tbname = scope.ToString().Equals("school") ? Constant.School : Constant.Teacher;
- code = $"Item-{containerName}";
- break;
- }
- case bool when prefix.Equals("records", StringComparison.OrdinalIgnoreCase):
- {
- tbname = scope.ToString().Equals("school") ? Constant.School : Constant.Teacher;
- code = scope.ToString().Equals("school") ? $"LessonRecord-{containerName}" : "LessonRecord";
- sql =$"{sql} and c.status<>404 ";
- break;
- }
- }
- IEnumerable<string> unlink = null;
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetList<string>(sql, code);
- if (result != null && result.list.Any())
- {
- unlink = ids.Except(result.list);
- }
- else
- {
- unlink = ids;
- }
- var unlinkData = recordUrls.Where(x => unlink.Contains(x.Key));
- var unlinkPaths = unlinkData.SelectMany(z => z.Value).ToList();
- unLinks.Add(new UnLink { prefix = prefix, blobs = unlinkPaths, size = unlinkPaths.Select(z => z.Value).Sum() });
- }
- }
- //名字导向 试卷(paper)
- {
- string prefix = "paper";
- //List<KeyValuePair<string, long?>> blobs = new List<KeyValuePair<string, long?>>();
- HashSet<string> paperNames = new HashSet<string>();
- Dictionary<string, List<KeyValuePair<string, long?>>> recordUrls = new Dictionary<string, List<KeyValuePair<string, long?>>>();
- await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
- {
- var path = blobItem.Name.Split("/");
- if (path.Length > 2)
- {
- string paperName = path[1];
- paperNames.Add(paperName);
- if (recordUrls.ContainsKey(paperName))
- {
- recordUrls[paperName].Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- else
- {
- recordUrls[paperName] = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) };
- }
- }
- if (path.Length == 2)
- {
- unLinks.Add(new UnLink { prefix = prefix, blobs = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) }, size = blobItem.Properties.ContentLength });
- }
- }
- string tbname = scope.ToString().Equals("school") ? Constant.School : Constant.Teacher;
- string code = $"Paper-{containerName}";
- if (paperNames.Any() && paperNames.Count > 0)
- {
- IEnumerable<string> unlink = null;
- string sql = $"select value c.name from c where c.name in ({string.Join(",", paperNames.Select(x => $"'{x}'"))})";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetList<string>(sql, code);
- if (result != null && result.list.Any())
- {
- unlink = paperNames.Except(result.list);
- }
- else
- {
- unlink = paperNames;
- }
- var unlinkData = recordUrls.Where(x => unlink.Contains(x.Key));
- var unlinkPaths = unlinkData.SelectMany(z => z.Value).ToList();
- unLinks.Add(new UnLink { prefix = prefix, blobs = unlinkPaths, size = unlinkPaths.Select(z => z.Value).Sum() });
- }
- }
- //htex hte (res)
- {
- string prefix = "res";
- //List<KeyValuePair<string, long?>> blobs = new List<KeyValuePair<string, long?>>();
- HashSet<string> htexNames = new HashSet<string>();
- List<KeyValuePair<string, long?>> hteBlobs = new List<KeyValuePair<string, long?>>();
- Dictionary<string, List<KeyValuePair<string, long?>>> recordUrls = new Dictionary<string, List<KeyValuePair<string, long?>>>();
- await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
- {
- var path = blobItem.Name.Split("/");
- if (path.Length > 2)
- {
- string htexName = path[1];
- htexNames.Add(htexName);
- if (recordUrls.ContainsKey(htexName))
- {
- recordUrls[htexName].Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- else
- {
- recordUrls[htexName] = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) };
- }
- }
- if (path.Length == 2)
- {
- string blobName = "";
- if (blobItem.Name.EndsWith(".htex", StringComparison.OrdinalIgnoreCase))
- {
- blobName = blobItem.Name.Substring(0, blobItem.Name.Length - 5);
- }
- if (blobItem.Name.EndsWith(".hte", StringComparison.OrdinalIgnoreCase))
- {
- blobName = blobItem.Name.Substring(0, blobItem.Name.Length - 4);
- }
- if (!string.IsNullOrWhiteSpace(blobName))
- {
- hteBlobs.Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- //unLinks.Add(new UnLink { prefix = prefix, blobs = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) }, size = blobItem.Properties.ContentLength });
- }
- }
- string tbname = scope.ToString().Equals("school") ? Constant.School : Constant.Teacher;
- string code = $"Bloblog-{containerName}";
- if (htexNames.Any() && htexNames.Count > 0)
- {
- List<string> unlink = new List<string>();
- foreach (var htexName in htexNames)
- {
- string sql = $"select value c from c where contains(c.url,'{htexName}') ";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetList<Bloblog>(sql, code, pageSize: 1);
- //找不到对应的数据存储
- if (!result.list.IsNotEmpty())
- {
- unlink.Add(htexName);
- }
- }
- var unlinkData = recordUrls.Where(x => unlink.Contains(x.Key));
- var unlinkPaths = unlinkData.SelectMany(z => z.Value).ToList();
- unLinks.Add(new UnLink { prefix = prefix, blobs = unlinkPaths, size = unlinkPaths.Select(z => z.Value).Sum() });
- }
- if (hteBlobs.IsNotEmpty())
- {
- List<KeyValuePair<string, long?>> unlink = new List<KeyValuePair<string, long?>>();
- foreach (var hteName in hteBlobs)
- {
- string sql = $"select value c from c where contains(c.url,'{hteName}') ";
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetList<Bloblog>(sql, code, pageSize: 1);
- //找不到对应的数据存储
- if (!result.list.IsNotEmpty())
- {
- unlink.Add(hteName);
- }
- }
- long? size = unlink.Select(z => z.Value).Sum();
- unLinks.Add(new UnLink { prefix = prefix, blobs = unlink, size = size });
- }
- }
- // 课纲(syllabus) 节点id fc4b7ac5-24e7-4eba-b0f2-7eccd007b4cd
- {
- string prefix = "syllabus";
- //List<KeyValuePair<string, long?>> blobs = new List<KeyValuePair<string, long?>>();
- HashSet<string> rnodeIds = new HashSet<string>();
- Dictionary<string, List<KeyValuePair<string, long?>>> recordUrls = new Dictionary<string, List<KeyValuePair<string, long?>>>();
- await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
- {
- var path = blobItem.Name.Split("/");
- if (path.Length > 2)
- {
- string rnodeId = path[1];
- rnodeIds.Add(rnodeId);
- if (recordUrls.ContainsKey(rnodeId))
- {
- recordUrls[rnodeId].Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- else
- {
- recordUrls[rnodeId] = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) };
- }
- }
- if (path.Length == 2)
- {
- unLinks.Add(new UnLink { prefix = prefix, blobs = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) }, size = blobItem.Properties.ContentLength });
- }
- }
- if (rnodeIds.Any() && rnodeIds.Count > 0)
- {
- string code = $"Syllabus-{containerName}";
- string tbname = scope.ToString().Equals("school") ? Constant.School : Constant.Teacher; ;
- string sql = $"select distinct value b.id from c join b in c.children where c.pk='Syllabus' and b.id in ({string.Join(",", rnodeIds.Select(x => $"'{x}'"))})";
- IEnumerable<string> unlink = null;
- var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetList<string>(sql, code);
- if (result != null && result.list.Any())
- {
- unlink = rnodeIds.Except(result.list);
- }
- else
- {
- unlink = rnodeIds;
- }
- var unlinkData = recordUrls.Where(x => unlink.Contains(x.Key));
- var unlinkPaths = unlinkData.SelectMany(z => z.Value).ToList();
- unLinks.Add(new UnLink { prefix = prefix, blobs = unlinkPaths, size = unlinkPaths.Select(z => z.Value).Sum() });
- }
- }
- var group = unLinks.GroupBy(x => x.prefix).ToList();
- List<dynamic> summary = new List<dynamic>();
- long totalCount = 0;
- long? totalSize = 0;
- group.ForEach(x => {
- long count = x.ToList().SelectMany(z => z.blobs).Count();
- long? size = x.Select(y => y.size).Sum();
- totalSize += size;
- totalCount += count;
- summary.Add(new { prefix = x.Key, count = count, size = size });
- });
- //为节省服务器开销, 限制只能一天清理一次
- #if DEBUG
- _azureRedis.GetRedisClient(8).StringSet($"Blob:ScanResult:{scope}:{containerName}", unLinks.ToJsonString(), expiry: new TimeSpan(0, 0,30));
- #else
- _azureRedis.GetRedisClient(8).StringSet($"Blob:ScanResult:{scope}:{containerName}", unLinks.ToJsonString(), expiry: new TimeSpan(24, 0, 0));
- #endif
- return Ok(new { status = 1, totalCount, totalSize, summary, unLinks });
- }
- }
- }
|