From abeda7820d89f1b4dc148809b966dfb91e8e57c3 Mon Sep 17 00:00:00 2001 From: he <109787524@qq.com> Date: Mon, 17 Mar 2025 10:50:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.Application/Helper/OSSService.cs | 57 +++++++++++++++++++ .../Service/Doctor/AttachmentService.cs | 15 +++++ 2 files changed, 72 insertions(+) diff --git a/IRaCIS.Core.Application/Helper/OSSService.cs b/IRaCIS.Core.Application/Helper/OSSService.cs index 860f930d3..29dd6e263 100644 --- a/IRaCIS.Core.Application/Helper/OSSService.cs +++ b/IRaCIS.Core.Application/Helper/OSSService.cs @@ -1,5 +1,6 @@ using AlibabaCloud.SDK.Sts20150401; using Aliyun.OSS; +using Aliyun.OSS.Common; using Amazon; using Amazon.Runtime; using Amazon.S3; @@ -153,6 +154,8 @@ public interface IOSSService List GetRootFolderNames(); public ObjectStoreDTO GetObjectStoreTempToken(); + + public void MoveObject(string sourcePath, string destPath, bool overwrite = true); } @@ -533,6 +536,60 @@ public class OSSService : IOSSService } + + /// + /// 移动OSS文件到新路径 + /// + /// 原文件路径(格式:bucket/key) + /// 新文件路径(格式:bucket/key) + /// 是否覆盖已存在的目标文件(默认true) + public void MoveObject(string sourcePath, string destPath, bool overwrite = true) + { + + var aliConfig = ObjectStoreServiceOptions.AliyunOSS; + + var client = new OssClient(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? aliConfig.EndPoint : aliConfig.InternalEndpoint, AliyunOSSTempToken.AccessKeyId, AliyunOSSTempToken.AccessKeySecret, AliyunOSSTempToken.SecurityToken); + + + + var sourceBucket = aliConfig.BucketName; + var sourceKey = sourcePath; + + var destBucket = aliConfig.BucketName; + + var destKey = destPath; + + + try + { + // 检查目标是否存在(当不允许覆盖时) + if (!overwrite && client.DoesObjectExist(destBucket, destKey)) + { + throw new InvalidOperationException("File exist"); + } + + // 执行复制 + var copyRequest = new Aliyun.OSS.CopyObjectRequest( + sourceBucket, sourceKey, + destBucket, destKey); + + // 保持原文件元数据 + copyRequest.NewObjectMetadata = new ObjectMetadata + { + ContentType = client.GetObjectMetadata(sourceBucket, sourceKey).ContentType + }; + + var result = client.CopyObject(copyRequest); + + // 删除原文件(仅在复制成功后) + client.DeleteObject(sourceBucket, sourceKey); + } + catch (OssException ex) + { + throw new Exception($"[{ex.ErrorCode}] {ex.Message}", ex); + } + } + /// /// 获取所有根目录名称 /// diff --git a/IRaCIS.Core.Application/Service/Doctor/AttachmentService.cs b/IRaCIS.Core.Application/Service/Doctor/AttachmentService.cs index b8577426a..5e382518c 100644 --- a/IRaCIS.Core.Application/Service/Doctor/AttachmentService.cs +++ b/IRaCIS.Core.Application/Service/Doctor/AttachmentService.cs @@ -1,5 +1,6 @@ using IRaCIS.Application.Contracts; using IRaCIS.Application.Interfaces; +using IRaCIS.Core.Application.Helper; using IRaCIS.Core.Application.ViewModel; using IRaCIS.Core.Infrastructure.Extention; using Microsoft.AspNetCore.Mvc; @@ -14,6 +15,7 @@ namespace IRaCIS.Core.Application.Service public class AttachmentService(IRepository _attachmentrepository, IRepository _dictionaryRepository, IRepository _enrollRepository, + IOSSService _oSSService, IRepository _doctorrepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IAttachmentService { @@ -211,6 +213,19 @@ namespace IRaCIS.Core.Application.Service public async Task UpdateTrialAttachments(AttachmentDTO attachment) { + if (!attachment.Path.Contains(attachment.DoctorId.ToString())) + { + var attachmentData= await _attachmentrepository.FirstOrDefaultAsync(a => a.Id == attachment.Id); + + var fileName= attachmentData.Path.Split("/").Last(); + + + attachment.Path = $"/systemData/reviewe/{attachment.Type}/{attachment.DoctorId}/{fileName}`"; + + _oSSService.MoveObject(attachmentData.Path, attachment.Path); + + } + if (attachment.DoctorId != null) { await _attachmentrepository.DeleteFromQueryAsync(a => a.DoctorId == attachment.DoctorId && a.Type == attachment.Type);