123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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.Context.Configuration;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
- using TEAMModelOS.SDK.DI;
- using System.Net.Http;
- using TEAMModelOS.SDK.Helper.Security.ShaHash;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelOS.Controllers.Core
- {
- [Route("api/[controller]")]
- [ApiController]
- public class BlobController : ControllerBase
- {
- private readonly AzureStorageFactory _azureStorage;
- private readonly SnowflakeId _snowflakeId;
- private readonly IHttpClientFactory _clientFactory;
- public BlobController(AzureStorageFactory azureStorage, SnowflakeId snowflakeId, IHttpClientFactory clientFactory)
- {
- _azureStorage = azureStorage;
- _snowflakeId = snowflakeId;
- _clientFactory = clientFactory;
- }
- /// <summary>
- /// 获取某个容器的只读权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("blobSasR")]
- public BaseResponse BlobSasR(BlobSas request)
- {
- ///返回金钥过期时间
- ResponseBuilder builder = new ResponseBuilder();
- // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
- // dict.Add(d.Key, d.Value);
- return builder.Data(_azureStorage.GetContainerSasUri(request, true)).build();
- }
- /// <summary>
- /// 某个文件的上传SAS rcw权限
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("blobSasRCW")]
- public BaseResponse BlobSasRCW(BlobSas request)
- {
- ///返回金钥过期时间
- ResponseBuilder builder = new ResponseBuilder();
- // Dictionary<string,object> dict= await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
- // Dictionary<string, object> dict = ;
- //dict.Add(d.Key, d.Value);
- return builder.Data(_azureStorage.GetContainerSasUri(request, false)).build();
- }
- /// <summary>
- /// 链接只读(读)
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("urlSasR")]
- public BaseResponse GetContainerSASRead(JsonElement azureBlobSASDto)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- azureBlobSASDto.TryGetProperty("params", 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 responseBuilder.Data(_azureStorage.GetBlobSasUriRead(ContainerName, BlobName)).build();
- }
- else
- {
- return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
- };
- }
-
-
- /// <summary>
- /// 获取文件内容
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("getText")]
- public async Task<BaseResponse> GetText(JsonElement request)
- {
- request.TryGetProperty("code", out JsonElement code);
- ResponseBuilder responseBuilder = new ResponseBuilder();
- 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 responseBuilder.Data(json.RootElement).build();
- }
- else
- {
- return responseBuilder.Error(ResponseCode.PARAMS_ERROR, "文件名错误").build();
- };
- }
- /// <summary>
- /// 测试单个文本内容的上传
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("deleteBlob")]
- public async Task<BaseResponse> DeleteBlob(JsonElement request)
- {
- ResponseBuilder responseBuilder = new ResponseBuilder();
- var client = _azureStorage.GetBlobBatchClient().DeleteBlobs(new Uri[] {new Uri("https://teammodelstorage.blob.core.chinacloudapi.cn/ydzt/%E6%96%B0%E5%BB%BA%E6%96%87%E4%BB%B6%E5%A4%B9%2FAnimation.xml") });
- return responseBuilder.Data(client[0].Status).build();
- }
- /// <summary>
- /// 测试单个文本内容的上传
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("get-blobsize")]
- public async Task<ActionResult> GetBlobsSize(JsonElement request)
- {
- request.TryGetProperty("containerName", out JsonElement containerName);
- var name =containerName.GetString();
- var client = _azureStorage.GetBlobContainerClient(name);
- var size = await client.GetBlobsSize();
- return Ok(new { size });
- }
- /// <summary>
- /// 测试单个文本内容的上传
- /// </summary>
- /// <param name="azureBlobSASDto"></param>
- /// <returns></returns>
- [HttpPost("testjson")]
- public async Task<BaseResponse> Uploadtest(JsonElement request)
- {
- JsonElement json= await RedisHelper.CacheShellAsync("test:" + "test", "bbbbbbbbbbbbbbbbbbbbb", 3600, () => { return test(request); });
- ResponseBuilder responseBuilder = new ResponseBuilder();
- return responseBuilder.Data(RedisHelper.HGet<JsonElement>("test:" + "test", "aaaaaaaaaaaaaaaaa")).build();
- }
- private Task<JsonElement> test(JsonElement request)
- {
- return Task.FromResult(request);
- }
- private static string ContainerUrlString(string sasUrl)
- {
- ///移除http://
- sasUrl = sasUrl.Substring(8);
- string[] sasUrls = sasUrl.Split("/");
- string ContainerName;
- ContainerName = sasUrls[1].Clone().ToString();
- return ContainerName;
- }
- 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}$)");
- }
- }
- }
|