黄贺彬 9 months ago
parent
commit
f97c6a6238
1 changed files with 140 additions and 0 deletions
  1. 140 0
      TEAMModelOS.Extension/HTEX.Complex/Controllers/MiniAppController.cs

+ 140 - 0
TEAMModelOS.Extension/HTEX.Complex/Controllers/MiniAppController.cs

@@ -0,0 +1,140 @@
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using System.Text.Json;
+using System.Web;
+using TEAMModelOS.SDK.DI;
+
+namespace HTEX.Complex.Controllers
+{
+    [ApiController]
+    [Route("miniapp")]
+    public class MiniAppController : ControllerBase
+    {
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly HttpClient _httpClient;
+        private readonly IConfiguration _configuration;
+        public MiniAppController(HttpClient httpClient, AzureStorageFactory azureStorage, IConfiguration configuration)
+        {
+            _httpClient = httpClient;
+            _azureStorage = azureStorage;
+            _configuration = configuration;
+            //   _httpContext=httpContext;
+        }
+        /// <summary>
+        ///  上传到blob
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost("from-miniapp-delete")]
+        [Authorize(Roles = "AClassONE")]
+        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
+        public async Task<IActionResult> FromMiniAPPDelete(JsonElement json)
+        {
+
+            List<string>? urls = json.GetProperty("urls").Deserialize<List<string>>();
+            string? cnt = json.GetProperty("cnt").GetString();
+            string? test = json.GetProperty("test").GetString();
+            if (urls!=null)
+            {
+                foreach (var url in urls)
+                {
+                    string dev = "Default";
+                    if (test.Equals("Test", StringComparison.OrdinalIgnoreCase))
+                    {
+                        dev="Test";
+                    }
+                    string uri = url.Split("?")[0];
+                    var blobinfo = BlobUrlString(uri);
+                    if (blobinfo.ContainerName.Equals(cnt))
+                    {
+                        var uld = HttpUtility.UrlDecode(blobinfo.blob);
+                        bool ds = await _azureStorage.GetBlobContainerClient(cnt, dev).DeleteBlobIfExistsAsync(uld);
+                    }
+                }
+            }
+            return Ok();
+        }
+        private static (string ContainerName, string blob) 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);
+        }
+        /// <summary>
+        ///  上传到blob
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost("from-miniapp-v2")]
+        [Authorize(Roles = "AClassONE")]
+        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
+        public async Task<IActionResult> FromMiniAPPV2()
+        {
+
+            try
+            {
+                List<dynamic> urls = new List<dynamic>();
+                var request = HttpContext.Request;
+                var files = request.Form.Files;
+                var cnt = request.Form["cnt"].ToString();
+                var test = request.Form["test"].ToString();
+                var path = request.Form["path"].ToString();
+                foreach (var file in files)
+                {
+
+                    string dev = "Default";
+                    if (test.Equals("Test", StringComparison.OrdinalIgnoreCase))
+                    {
+                        dev="Test";
+                    }
+                    if (path.EndsWith("/"))
+                    {
+                        path=path.Substring(0, path.Length -1);
+                    }
+                    (string name, string url) = await _azureStorage.GetBlobContainerClient(cnt, dev).UploadFileByContainerBName(file.OpenReadStream(), path, $"{file.FileName}", true);
+                    var auth = _azureStorage.GetBlobContainerClient(cnt, dev).GetBlobSasUriRead(_configuration, cnt, name, dev);
+                    var ext = file.FileName.Split(".");
+                    urls.Add(new { url = $"{url}?{auth.sas}", name = file.FileName, size = file.Length, extension = ext[ext.Length-1], type = file.ContentType.Split("/")[0], blob = name, cnt = cnt });
+                }
+                return Ok(new { code = 200, urls, });
+            }
+            catch (Exception vErr)
+            {
+                return Ok(new { code = 500, msg = vErr.Message });
+            }
+        }
+        /// <summary>
+        ///  上传到blob
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [HttpPost("from-miniapp")]
+        [Authorize(Roles = "AClassONE")]
+        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
+        public async Task<IActionResult> FromMiniAPP([FromForm] IFormFile[] files, [FromForm] string path, [FromForm] string cnt, [FromForm] string test)
+        {
+            List<string> urls = new List<string>();
+            foreach (var file in files)
+            {
+                string dev = "Default";
+                if (test.Equals("Test", StringComparison.OrdinalIgnoreCase))
+                {
+                    dev="Test";
+                }
+                if (path.EndsWith("/"))
+                {
+                    path=path.Substring(0, path.Length -1);
+                }
+                (string name, string url) = await _azureStorage.GetBlobContainerClient(cnt, dev).UploadFileByContainerBName(file.OpenReadStream(), path, $"{file.FileName}", true);
+                var auth = _azureStorage.GetBlobContainerClient(cnt, dev).GetBlobSasUriRead(_configuration, cnt, name, dev);
+                urls.Add($"{url}?{auth.sas}");
+            }
+            return Ok(new { urls });
+        }
+
+    }
+}