|
@@ -33,6 +33,9 @@ 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
|
|
|
{
|
|
@@ -852,20 +855,98 @@ namespace TEAMModelOS.Controllers
|
|
|
[AuthToken(Roles = "teacher,admin")]
|
|
|
[HttpPost("delete-unlink")]
|
|
|
public async Task<IActionResult> DeleteUnlink(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;
|
|
|
- bool exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:ScanResult:{scope}:{containerName}");
|
|
|
- if (exists)
|
|
|
- {
|
|
|
- return Ok(new { status = 2, msg = "暂无清理项!" });
|
|
|
+ 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 = "请重新检查清理项!" });
|
|
|
|
|
|
- return Ok(new { status, msg = "暂无清理项!" });
|
|
|
}
|
|
|
[ProducesDefaultResponseType]
|
|
|
[Authorize(Roles = "IES")]
|
|
@@ -880,6 +961,9 @@ namespace TEAMModelOS.Controllers
|
|
|
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)
|
|
|
{
|
|
@@ -948,6 +1032,10 @@ namespace TEAMModelOS.Controllers
|
|
|
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;
|
|
|
}
|
|
@@ -1261,7 +1349,12 @@ namespace TEAMModelOS.Controllers
|
|
|
summary.Add(new { prefix = x.Key, count = count, size = size });
|
|
|
});
|
|
|
//为节省服务器开销, 限制只能一天清理一次
|
|
|
- _azureRedis.GetRedisClient(8).StringSet($"Blob:ScanResult:{scope}:{containerName}", unLinks.ToJsonString(), expiry: new TimeSpan(24, 0, 0));
|
|
|
+#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 });
|
|
|
}
|
|
|
}
|