From 87e3d953d3353a3cb486f5dcca59317fc690b190 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 13 Mar 2024 13:24:14 +0800 Subject: [PATCH] =?UTF-8?q?oss=E7=A7=81=E6=9C=89=E8=AF=BB=E9=A2=84?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NewtonsoftJson/NewtonsoftJsonSetup.cs | 9 ++- .../NewtonsoftJson/ObjectStorePathConvert.cs | 65 ++++++++++++++++ IRaCIS.Core.Application/Helper/OSSService.cs | 74 +++++++++++++++++++ .../IRaCIS.Core.Application.xml | 5 ++ .../Service/ImageAndDoc/StudyService.cs | 2 +- 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/ObjectStorePathConvert.cs diff --git a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs index 96383d8c4..130d8b122 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/NewtonsoftJsonSetup.cs @@ -1,4 +1,6 @@  +using IRaCIS.Core.API._ServiceExtensions.NewtonsoftJson; +using IRaCIS.Core.Application.Helper; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Converters; @@ -12,6 +14,8 @@ namespace IRaCIS.Core.API { services.AddHttpContextAccessor(); services.AddScoped(); + services.AddScoped(); + services.AddScoped(); builder.AddNewtonsoftJson(options => { @@ -29,7 +33,10 @@ namespace IRaCIS.Core.API //options.SerializerSettings.Converters.Add(new JSONCustomDateConverter()) ; - //options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService()); + options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService()); + + //options.SerializerSettings.Converters.Add(services.BuildServiceProvider().GetService()); + //IsoDateTimeConverter //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; diff --git a/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/ObjectStorePathConvert.cs b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/ObjectStorePathConvert.cs new file mode 100644 index 000000000..b9df7af84 --- /dev/null +++ b/IRaCIS.Core.API/_ServiceExtensions/NewtonsoftJson/ObjectStorePathConvert.cs @@ -0,0 +1,65 @@ +using IRaCIS.Core.Application.Helper; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; +using System; +using System.Linq; +using System.Text.RegularExpressions; + +namespace IRaCIS.Core.API._ServiceExtensions.NewtonsoftJson +{ + public class CustomStringConverter : JsonConverter + { + //private readonly IHttpContextAccessor _httpContextAccessor; + private readonly IOSSService _oSSService; + + // 构造函数 + public CustomStringConverter(/*IHttpContextAccessor httpContextAccessor*/ IOSSService oSSService) + { + //_httpContextAccessor = httpContextAccessor; + _oSSService = oSSService; + } + + public override string ReadJson(JsonReader reader, Type objectType, string existingValue, bool hasExistingValue, JsonSerializer serializer) + { + if (reader.TokenType == JsonToken.String) + { + return (string)reader.Value; + } + + return null; + } + + public override void WriteJson(JsonWriter writer, string value, JsonSerializer serializer) + { + if (value != null) + { + // 在这里对字符串进行处理,例如转换大小写、去除空格等 + // 这里只是一个示例,您可以根据实际需求进行更改 + + // 获取当前正在序列化的属性名 + string propertyName = writer.Path.Split('.').Last(); + + Regex guidRegex = new Regex(@"[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}"); + + if (propertyName.IndexOf("Path") > -1 && value.IndexOf('.')>-1 && guidRegex.IsMatch(value)) + { + var tt= _oSSService.GetSignedUrl(value); + writer.WriteValue(tt); + } + else + { + // 将处理后的字符串写入到 JsonWriter 中 + writer.WriteValue(value); + } + + } + else + { + writer.WriteNull(); + } + } + + } +} diff --git a/IRaCIS.Core.Application/Helper/OSSService.cs b/IRaCIS.Core.Application/Helper/OSSService.cs index 26cbd2b3c..5ddf2c30a 100644 --- a/IRaCIS.Core.Application/Helper/OSSService.cs +++ b/IRaCIS.Core.Application/Helper/OSSService.cs @@ -103,6 +103,8 @@ namespace IRaCIS.Core.Application.Helper public ObjectStoreServiceOptions ObjectStoreServiceOptions { get; set; } + public Task GetSignedUrl(string ossRelativePath); + } @@ -337,9 +339,81 @@ namespace IRaCIS.Core.Application.Helper } + public async Task GetSignedUrl(string ossRelativePath) + { + ossRelativePath = ossRelativePath.TrimStart('/'); + try + { + if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS") + { + var aliConfig = ObjectStoreServiceOptions.AliyunOSS; + var _ossClient = new OssClient(aliConfig.endPoint, aliConfig.accessKeyId, aliConfig.accessKeySecret); + + // 生成签名URL。 + var req = new GeneratePresignedUriRequest(aliConfig.bucketName, ossRelativePath, SignHttpMethod.Get) + { + // 设置签名URL过期时间,默认值为3600秒。 + Expiration = DateTime.Now.AddHours(1), + }; + var uri = _ossClient.GeneratePresignedUri(req); + + return uri.PathAndQuery; + + } + else if (ObjectStoreServiceOptions.ObjectStoreUse == "MinIO") + { + var minIOConfig = ObjectStoreServiceOptions.MinIO; + + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}:{minIOConfig.port}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + //var reqParams = new Dictionary(StringComparer.Ordinal) + // { + // { "response-content-type", "application/json" } + // }; + + var args = new PresignedGetObjectArgs() + .WithBucket(minIOConfig.bucketName) + .WithObject(ossRelativePath) + .WithExpiry(3600) + /*.WithHeaders(reqParams)*/; + + var presignedUrl = await minioClient.PresignedGetObjectAsync(args); + + Uri uri = new Uri(presignedUrl); + + string relativePath = uri.PathAndQuery; + + + return relativePath; + + } + else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS") + { + var minIOConfig = ObjectStoreServiceOptions.AWS; + + var minioClient = new MinioClient().WithEndpoint($"{minIOConfig.endPoint}") + .WithCredentials(minIOConfig.accessKey, minIOConfig.secretKey).WithSSL(minIOConfig.useSSL) + .Build(); + + return string.Empty; + } + else + { + throw new BusinessValidationFailedException("未定义的存储介质类型"); + } + } + catch (Exception ex) + { + + throw new BusinessValidationFailedException("oss授权url失败!" + ex.Message); + } + + } } diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index bd3ee9667..3955af7f8 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -5856,6 +5856,11 @@ 问题标识 + + + 分组ID + + 系统标准Id diff --git a/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs b/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs index eb978edc1..4119aa9d2 100644 --- a/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs +++ b/IRaCIS.Core.Application/Service/ImageAndDoc/StudyService.cs @@ -772,7 +772,7 @@ namespace IRaCIS.Core.Application.Service.ImageAndDoc var result = new VerifyStudyUploadResult(); - if (_provider.Exists($"StudyUid_{trialId}_{studyInstanceUid}")) + if (_provider.Exists($"StudyUid_{trialId}_{studyInstanceUid}") && _provider.Get($"StudyUid_{trialId}_{studyInstanceUid}").Value!=_userInfo.Id) { result.AllowUpload = false;