12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532 |
- 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.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 TEAMModelOS.Models;
- using Microsoft.Extensions.Options;
- using TEAMModelOS.SDK.Models;
- using Microsoft.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 Azure.Storage.Blobs;
- using Azure.Storage.Blobs.Specialized;
- using Microsoft.AspNetCore.Cors;
- using TEAMModelOS.Controllers.Third.LePei;
- using TEAMModelOS.SDK.Models.Table;
- using Microsoft.Azure.Cosmos.Table;
- using System.Text.Json.Nodes;
- 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;
- }
- public class OfficialVideo : TableEntity
- {
- ///// <summary>
- ///// OfficialVideo
- ///// </summary>
- //public string PartitionKey { get; set; }
- ///// <summary>
- ///// 视频id
- ///// </summary>
- //public string RowKey { get; set; }
- /// <summary>
- /// 名称
- /// </summary>
- public string? name { get; set; }
- /// <summary>
- /// 类型
- /// </summary>
- public string? type { get; set; }
- /// <summary>
- /// 地址
- /// </summary>
- public string? url { get; set; }
- }
- [EnableCors("AllowSpecificOrigin")]
- [HttpPost("video-list")]
- public async Task<IActionResult> VideoList(JsonElement json) {
- var table = _azureStorage.GetCloudTableClient().GetTableReference("ShortUrl");
- List<OfficialVideo> videos = new List<OfficialVideo>();
- if (json.TryGetProperty("rowKey", out JsonElement _rowKey) && !string.IsNullOrWhiteSpace($"{_rowKey}"))
- {
- videos= await table.FindListByDict<OfficialVideo>(new Dictionary<string, object>() { { Constant.PartitionKey, "OfficialVideo" }, { Constant.RowKey, _rowKey } });
- }
- else
- {
- videos= await table.FindListByDict<OfficialVideo>(new Dictionary<string, object>() { { Constant.PartitionKey, "OfficialVideo" } });
- }
- return Ok(new { videos });
- }
- /// <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").GetItemQueryIteratorSql<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").GetItemQueryIteratorSql<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.fullUri));
- 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.fullUri));
- };
- 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").GetItemQueryIteratorQuery<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 PartitionKey(item.code));
- bloblog.Add(item);
- exsit = true;
- }
- }
- else if (scope.GetString().Equals("private"))
- {
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIteratorQuery<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 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 PartitionKey(blob.code));
- }
- else if (scope.GetString().Equals("private"))
- {
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(blob, new 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) {
- long ttl = 0;
- 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)
- {
- ttl=0;
- var ttlVals = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (ttlVals.HasValue)
- {
- ttl =(long) ttlVals.Value.TotalMilliseconds;
- //ttl = DateTimeHelper.ToUnixTimestamp(ttlVals.Value);
- //ttl= ttl- DateTimeOffset.Now.ToUnixTimeMilliseconds();
- }
- 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) { }
- }
- }
- }
- }
- //为节省服务器开销, 限制只能一天清理一次
-
- _azureRedis.GetRedisClient(8).StringSet($"Blob:ScanResult:{scope}:{containerName}", new List<UnLink>().ToJsonString(), expiry: new TimeSpan(0, 3, 0));
-
- 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 = "清理成功!", ttl });
- }
- else
- {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!", ttl });
- }
- }
- else
- {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!", ttl });
- }
- }
- else
- {
- return Ok(new { status = 4, msg = "请重新检查清理项!", ttl });
- }
- } catch (Exception ex ) {
- await _dingDing.SendBotMsg($"{_option.Location},{DeleteUnlink}\n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- }
- return Ok(new { status = 4, msg = "请重新检查清理项!",ttl });
- }
- [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;
- long ttl = 0;
- if (string.IsNullOrWhiteSpace(containerName)) {
- return Ok(new { status, msg = "参数错误" });
- }
- bool exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (exists)
- {
- // var ttlVal= await _azureRedis.GetRedisClient(8).KeyExpireTimeAsync($"Blob:ScanResult:{scope}:{containerName}");
- var ttlVal = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (ttlVal.HasValue)
- {
- // var ttk = ttlVal.Value.TotalMilliseconds;
- ttl = (long )ttlVal.Value.TotalMilliseconds;
- // ttl= ttl- DateTimeOffset.Now.ToUnixTimeMilliseconds();
- }
- 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, ttl });
- }
- else {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!", ttl });
- }
- }
- else {
- return Ok(new { status = 2, msg = "最近清理过,暂无清理项!", ttl });
- }
-
- }
- 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 });
- });
- //为节省服务器开销, 限制只能一天清理一次
- _azureRedis.GetRedisClient(8).StringSet($"Blob:ScanResult:{scope}:{containerName}", unLinks.ToJsonString(), expiry: new TimeSpan(0, 3, 0));
-
- var ttlVals = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Blob:ScanResult:{scope}:{containerName}");
- if (ttlVals.HasValue)
- {
- ttl = (long)ttlVals.Value.TotalMilliseconds;
- // ttl= ttl- DateTimeOffset.Now.ToUnixTimeMilliseconds();
- }
- return Ok(new { status = 1, totalCount, totalSize, summary, unLinks, ttl });
- }
- [HttpPost("copy-blobs")]
- [AuthToken(Roles = "teacher,admin")]
- [Authorize(Roles = "IES")]
- public async Task<ActionResult> CopyBlobs(JsonNode request)
- {
- BlobServiceClient blobServiceClient = _azureStorage.GetBlobServiceClient();
- //原始容器
- var scnt = request["scnt"];
- //目标容器
- var tcnt = request["tcnt"];
- var blobs = request["blobs"];
- List<BlobCopyDto> copyDtos = new List<BlobCopyDto>();
- if (!string.IsNullOrWhiteSpace($"{scnt}") && !string.IsNullOrWhiteSpace($"{tcnt}") && blobs!=null)
- {
- // 确保目标容器存在
- await blobServiceClient.GetBlobContainerClient($"{tcnt}").CreateIfNotExistsAsync();
- copyDtos = JsonSerializer.Deserialize<List<BlobCopyDto>>(blobs);
- var pages =copyDtos.Page(200);
- foreach (var page in pages) {
- await Parallel.ForEachAsync(page, async (copyDto, token) =>
- {
- try
- {
- BlobClient sBlobClient = blobServiceClient.GetBlobContainerClient($"{scnt}").GetBlobClient(copyDto.s);
- BlobClient tBlobClient = blobServiceClient.GetBlobContainerClient($"{tcnt}").GetBlobClient(copyDto.t);
- await tBlobClient.StartCopyFromUriAsync(sBlobClient.Uri);
- // 检查复制是否成功
- if (await tBlobClient.ExistsAsync())
- {
- copyDto.c=1;
- }
- else
- {
- copyDto.c = 2;
- }
- }
- catch (Exception ex)
- {
- copyDto.c=2;
- }
- });
- }
- }
- return Ok(new {
- blobs = copyDtos,
- count_0 = copyDtos.Count(x => x.c == 0),
- count_1 = copyDtos.Count(x => x.c == 1),
- count_2 = copyDtos.Count(x => x.c == 2),
- count_d = copyDtos.Count(),
- });
- }
- }
- public class BlobCopyDto
- {
- /// <summary>
- /// 源文件
- /// </summary>
- public string s { get; set; }
- /// <summary>
- /// 目标文件
- /// </summary>
- public string t { get; set; }
- /// <summary>
- /// 复制状态 0未复制,1成功,2失败
- /// </summary>
- public int c{ get; set; }
- }
- }
|