CrazyIter_Bin 4 лет назад
Родитель
Сommit
ddf5b056c2

+ 16 - 7
TEAMModelOS.SDK/DI/AzureStorage/AzureStorageBlobExtensions.cs

@@ -69,30 +69,39 @@ namespace TEAMModelOS.SDK.DI
                 return (size, dict);
             }
         }
+
+        public class OptUrl
+        {
+            public string url { get; set; }
+            public long size { get; set; }
+        }
         /// <summary>
         /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
         /// </summary>      
         /// <param name="urls">多个文件的连接/param>
         /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
-        public static async Task<long?> GetBlobsSize(this BlobContainerClient client, List<string>urls)
+        public static async Task<List<OptUrl>> GetBlobsSize(this BlobContainerClient client, List<string> urls)
         {
-            long? size = 0;
+            List<OptUrl> optUrls = new List<OptUrl>();
             try
             {
+               
                 foreach (var url in urls) {
-                 
-                    var eurl = System.Web.HttpUtility.UrlDecode(url.ToString(), Encoding.UTF8);
+                    OptUrl optUrl = new OptUrl { url = url, size =0};
+                    var eurl = System.Web.HttpUtility.UrlDecode(url, Encoding.UTF8);
                     var blob = client.GetBlobClient(eurl);
                     if (blob.Exists()) {
                         var props = await blob.GetPropertiesAsync();
-                        size += props.Value.ContentLength;
+                        var size= props.Value.ContentLength;
+                        optUrl.size = size;
                     }
+                    optUrls.Add(optUrl);
                 }
-                return size;
+                return optUrls;
             }
             catch
             {
-                return size;
+                return optUrls;
             }
         }
         /// <summary>        

+ 28 - 12
TEAMModelOS/Controllers/Core/BlobController.cs

@@ -18,6 +18,8 @@ using Microsoft.AspNetCore.Authorization;
 using TEAMModelOS.Filter;
 using StackExchange.Redis;
 using Azure.Messaging.ServiceBus;
+using static TEAMModelOS.SDK.DI.AzureStorageBlobExtensions;
+using System.Linq;
 
 namespace TEAMModelOS.Controllers.Core
 {
@@ -235,7 +237,6 @@ namespace TEAMModelOS.Controllers.Core
         [HttpPost("get-blobsize")]
         public async Task<ActionResult> GetBlobsSize(JsonElement request)
         {
-            
             request.TryGetProperty("containerName", out JsonElement containerName);
             request.TryGetProperty("cache", out JsonElement cache);
             var name =containerName.GetString();
@@ -259,24 +260,43 @@ namespace TEAMModelOS.Controllers.Core
                     {
                         foreach (var score in Scores) {
                             double val = score.Score;
-                           // string key= score.Element
+                            string key = score.Element.ToString();
+                            catalog.Add(key, val);
                         }   
                     }
-                    return Ok(new { size = blobsize });
+                    return Ok(new { size = blobsize, catalog= catalog });
                 }
             } catch { }
             var client = _azureStorage.GetBlobContainerClient(name);
             var size = await client.GetBlobsCatalogSize();
-
             await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
             foreach (var key in size.Item2.Keys) {
                 await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue?size.Item2[key].Value:0);
             }
             return Ok(new { size = size.Item1, catalog = size.Item2 });
         }
+
+        /// <summary>
+        /// 测试单个文本内容的上传
+        /// {"containerName":"hbcn","urls":["video/xxx.mp4","res/xxx.png"]}
+        /// </summary>
+        /// <param name="azureBlobSASDto"></param>
+        /// <returns></returns>
+        [HttpPost("check-blobsize")]
+        public async Task<ActionResult> checkBlobsSize(JsonElement request)
+        {
+            request.TryGetProperty("containerName", out JsonElement containerName);
+            request.TryGetProperty("urls", out JsonElement optUrls);
+            var name = containerName.GetString();
+            var urls = optUrls.ToObject<List<string>>();
+            var client = _azureStorage.GetBlobContainerClient(name);
+            var urlsSize = await client.GetBlobsSize(urls);
+            return Ok(new {  urlsSize });
+        }
+
         /// <summary>
         /// 测试单个文本内容的上传
-        /// {"containerName":"hbcn","uploadSize":5000,"optUrls":[]}
+        /// {"containerName":"hbcn","uploadSize":5000,"optUrls":[{"url":"video/37Z888piCvm9.mp4","size":0},{}]}
         /// </summary>
         /// <param name="azureBlobSASDto"></param>
         /// <returns></returns>
@@ -285,13 +305,10 @@ namespace TEAMModelOS.Controllers.Core
         {
             request.TryGetProperty("containerName", out JsonElement containerName);
             request.TryGetProperty("optUrls", out JsonElement optUrls);
-            request.TryGetProperty("uploadSize", out JsonElement uploadSize);
             var name = containerName.GetString();
-            uploadSize.TryGetInt64(out long size);
-            var urls = optUrls.ToObject<List<string>>();
+            var urls = optUrls.ToObject<List<OptUrl>>();
             var client = _azureStorage.GetBlobContainerClient(name);
-            var urlsSize = await client.GetBlobsSize(urls);
-            long? disSize= size - urlsSize;
+            var disSize = urls.Select(x => x.size).Sum();
             RedisValue value = default;
             long? useSize = 0;
             long blobSize = 0;
@@ -299,9 +316,8 @@ namespace TEAMModelOS.Controllers.Core
             if (value != default && !value.IsNullOrEmpty)
             {
                 JsonElement record = value.ToString().ToObject<JsonElement>();
-                if (record.TryGetInt64(out   blobSize))
+                if (record.TryGetInt64(out blobSize))
                 {
-                    useSize = blobSize;
                 }
             }
             useSize = useSize + disSize;