浏览代码

updateBlobsSize还原

CrazyIter_Bin 3 年之前
父节点
当前提交
524d50af24

+ 6 - 0
TEAMModelOS.SDK/Extension/JsonExtensions.cs

@@ -29,6 +29,12 @@ namespace TEAMModelOS.SDK.Extension
 
         public static string ToJsonString(this Object obj, JsonSerializerOptions option = null)
         {
+            if (option == null) {
+                option = new System.Text.Json.JsonSerializerOptions
+                {
+                    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
+                };
+            }
             var json = JsonSerializer.Serialize(obj, option);
             return json;
         }

+ 105 - 56
TEAMModelOS/Controllers/Core/BlobController.cs

@@ -322,6 +322,61 @@ namespace TEAMModelOS.Controllers.Core
                 return Ok(new { bloblogs = bloblogs });
             }
         }
+        /// <summary>
+        /// 测试单个文本内容的上传
+        /// {"containerName":"hbcn","uploadSize":5000,"optUrls":[{"url":"video/37Z888piCvm9.mp4","size":0},{}]}
+        /// </summary>
+        /// <param name="azureBlobSASDto"></param>
+        /// <returns></returns>
+        [HttpPost("update-blobsize")]
+        public async Task<ActionResult> updateBlobsSize(JsonElement request)
+        {
+            request.TryGetProperty("containerName", out JsonElement containerName);
+            request.TryGetProperty("optUrls", out JsonElement optUrls);
+            var name = containerName.GetString();
+            var urls = optUrls.ToObject<List<OptUrl>>();
+            var client = _azureStorage.GetBlobContainerClient(name);
+            var disSize = urls.Select(x => x.size).Sum();
+            RedisValue value = default;
+            long blobSize = 0;
+            value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", name);
+            if (value != default && !value.IsNullOrEmpty)
+            {
+                JsonElement record = value.ToString().ToObject<JsonElement>();
+                if (record.TryGetInt64(out blobSize))
+                {
+                }
+            }
+            long? useSize = blobSize + disSize;
+            await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, useSize);
+            Dictionary<string, double?> catalog = new Dictionary<string, double?>();
+            HashSet<string> root = new HashSet<string>();
+            foreach (var x in urls)
+            {
+                string[] uls = System.Web.HttpUtility.UrlDecode(x.url, Encoding.UTF8).Split("/");
+                if (uls != null)
+                {
+                    string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
+                    root.Add(u);
+
+                    await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", u, x.size);
+                    catalog[u] = await _azureRedis.GetRedisClient(8).SortedSetScoreAsync($"Blob:Catalog:{name}", u);
+                    if (catalog[u] < 0)
+                    {
+                        await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", u);
+                        catalog[u] = 0;
+                    }
+                }
+            }
+            var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = root.ToArray(), name = name }.ToJsonString()); ;
+            messageBlob.ApplicationProperties.Add("name", "Blob");
+            var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
+            await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
+            return Ok(new { size = useSize, catalog });
+        }
+
+        //public string BloblogRename(JsonElement json) { 
+        //}
 
         /*
          新增 编辑接口
@@ -414,7 +469,7 @@ namespace TEAMModelOS.Controllers.Core
                         }
                         catch (Exception ex)
                         {
-                            await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/refresh-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
+                            await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
                         }
                         if (!exsit)
                         {
@@ -461,7 +516,7 @@ namespace TEAMModelOS.Controllers.Core
             }
             catch (Exception ex)
             {
-                await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/refresh-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
+                await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/bloblog-blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
             }
             return Ok(new { status = 200 });
         }
@@ -522,11 +577,16 @@ namespace TEAMModelOS.Controllers.Core
             }
         }
 
+        public record ContBlob { 
+            public string path { get; set; }
+            public string id { get; set; }
+        }
 
         /// <summary>
         /// 删除多个Url,只会在内容模块使用该接口
         /// 
         /// {"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>
@@ -534,64 +594,53 @@ namespace TEAMModelOS.Controllers.Core
         [AuthToken(Roles = "teacher,admin")]
         public async Task<IActionResult> DeleteBlobs(JsonElement json)
         {
-            var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
-            string blobContainerName = null;
-            List<string> uris = null;
-            if (!json.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
-            if (!json.TryGetProperty("ids", out JsonElement _ids)) return BadRequest();
-            if (json.TryGetProperty("cntr", out JsonElement cntr))
-            {
-                var cntrs = cntr.ToString();
-                if (cntrs.Equals(id))
+            ///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))
                 {
-                    blobContainerName = id;
-                }
-                else if (cntrs.Equals(school))
-                {
-                    blobContainerName = school;
+                    var cntrs = cntr.ToString();
+                    if (cntrs.Equals(id))
+                    {
+                        blobContainerName = id;
+                    }
+                    else if (cntrs.Equals(school))
+                    {
+                        blobContainerName = school;
+                    }
+                    else
+                    {
+                        return BadRequest("只能删除本人管理的文件");
+                    }
                 }
-                else
+                bool flag = true;
+                List<ContBlob> blobs = _blobs.ToObject<List<ContBlob>>();
+                try
                 {
-                    return BadRequest("只能删除本人管理的文件");
-                }
-            }
-            bool flag = true;
-            if (json.TryGetProperty("urls", out JsonElement urlsjson))
-            {
-                uris = urlsjson.ToObject<List<string>>();
-                uris.ForEach(x => {
-                    (string, string) a = BlobUrlString(x.ToString());
-                    string ContainerName = a.Item1;
-                    string BlobName = a.Item2;
-                    if (ContainerName != blobContainerName)
+                    var client = _azureCosmos.GetCosmosClient();
+                    List<string> ids = blobs.Select(x => x.id).ToList();
+                    string containerId = "School";
+                    if (_scope.GetString().Equals("school"))
                     {
-                        flag = false;
+                        containerId = "School";
                     }
-                });
-            }
-            try
-            {
-                var client = _azureCosmos.GetCosmosClient();
-                List<string> ids = _ids.ToObject<List<string>>();
-                if (_scope.GetString().Equals("school"))
-                {
-                    await client.GetContainer("TEAMModelOS", "School").DeleteItemsAsync<Bloblog>(ids, $"Bloblog-{blobContainerName}");
+                    else if (_scope.GetString().Equals("private"))
+                    {
+                        containerId = "Teacher";
+                    }
+                    await client.GetContainer("TEAMModelOS", containerId).DeleteItemsAsync<Bloblog>(ids, $"Bloblog-{blobContainerName}");
                 }
-                else if (_scope.GetString().Equals("private"))
+                catch
                 {
-                    await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemsAsync<Bloblog>(ids, $"Bloblog-{blobContainerName}");
+                    //仅处理 cosmos不存在 但容器又存在的
                 }
-            }
-            catch
-            {
-                //仅处理 cosmos不存在 但容器又存在的
-            }
-            if (flag)
-            {
-                if (blobContainerName != null && uris != null && uris.Count > 0)
+                if (flag)
                 {
-                    var urls = urlsjson.ToObject<List<string>>();
-                    var status = await _azureStorage.GetBlobServiceClient().DelectBlobs(blobContainerName, uris);
+                    var urls = blobs.Select(x => x.path).ToList();
+                    var status = await _azureStorage.GetBlobServiceClient().DelectBlobs(blobContainerName, urls);
                     //释放的空间
                     HashSet<string> root = new HashSet<string>();
                     foreach (var x in urls)
@@ -613,12 +662,12 @@ namespace TEAMModelOS.Controllers.Core
                 }
                 else
                 {
-                    return BadRequest();
+                    return BadRequest("只能删除本人管理的文件");
                 }
-            }
-            else
-            {
-                return BadRequest("只能删除本人管理的文件");
+            } catch (Exception ex) {
+
+                await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/delete-blobs\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
+                return BadRequest();
             }
         }
     }

+ 28 - 5
TEAMModelOS/Controllers/XTest/TestController.cs

@@ -20,6 +20,10 @@ using TEAMModelOS.SDK;
 using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Models.Cosmos.Common;
 using TEAMModelOS.Services.Common;
+using Microsoft.Extensions.Configuration;
+using TEAMModelOS.SDK.Models.Service;
+using TEAMModelOS.Models;
+using Microsoft.Extensions.Options;
 
 namespace TEAMModelOS.Controllers.XTest
 {
@@ -31,13 +35,18 @@ namespace TEAMModelOS.Controllers.XTest
         private readonly AzureRedisFactory _azureRedis;
         private readonly AzureCosmosFactory _azureCosmos;
         private readonly DingDing _dingDing;
-       
+        private readonly IConfiguration _configuration; 
+        private readonly Option _option;
+        private readonly NotificationService _notificationService;
 
-        public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing) {
+        public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing, IConfiguration configuration, IOptionsSnapshot<Option> option, NotificationService notificationService) {
             _azureCosmos = azureCosmos;
             _azureRedis = azureRedis;
             _azureStorage = azureStorage;
             _dingDing = dingDing;
+            _configuration = configuration;
+            _notificationService = notificationService;
+            _option = option?.Value;
         }
         /// <summary>
         /// 测试blob多线程写入同一个文件
@@ -46,9 +55,23 @@ namespace TEAMModelOS.Controllers.XTest
         [ProducesDefaultResponseType]
         [HttpPost("blobroot")]
         public async Task<IActionResult> MultipleBlob(JsonElement jsonMsg) {
-            var client = _azureCosmos.GetCosmosClient();
-        var a =  await  TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { "hbcn0601", "hbcn0602", "hbcn0603", "6e862f7a-3ef4-33e7-7ea0-5a7e3b5e2802", "8a607e3f-4c92-4cbe-b41d-c6c59e13bc92" }, "hbcn");
-            return Ok(new { a.students});
+            var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
+            var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
+            var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
+            var location = _option.Location;
+            var aa =  new { biz = "join_school", tmdid = "1595321354", tmdname = "醍摩豆管理员".ToString(), schoolcode = $"hbcn", schoolname = $"青城山学校", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() };
+            Notification notification = new Notification
+            {
+                hubName = "hita",
+                type = "msg",
+                from = $"ies5:hbcn",
+                to = new List<string> { "1595321354" },
+                label = $"join_school",
+                body = aa.ToJsonString(),
+                expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
+            };
+            var code = await _notificationService.SendNotification(clientID, clientSecret, location, url, notification);
+            return Ok(new { code });
         }
 
         private async Task<int> SendNotification( )

+ 3 - 3
TEAMModelOS/TEAMModelOS.csproj

@@ -43,9 +43,9 @@
     <SpaRoot>ClientApp\</SpaRoot>
     <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
     <UserSecretsId>078b5d89-7d90-4f6a-88fc-7d96025990a8</UserSecretsId>
-    <Version>5.2107.152</Version>
-    <AssemblyVersion>5.2107.152.1</AssemblyVersion>
-    <FileVersion>5.2107.152.1</FileVersion>
+    <Version>5.2107.161</Version>
+    <AssemblyVersion>5.2107.161.1</AssemblyVersion>
+    <FileVersion>5.2107.161.1</FileVersion>
     <Description>1.修复Word导入题目转码失败的html代码。
 2.修复Word导入题目选项不能识别全角的A-Z字符,转为半角。
 3.学生信息增加学段信息。</Description>