修改文件路径
continuous-integration/drone/push Build is passing Details

Test_IRC_Net8
he 2025-03-17 10:50:21 +08:00
parent 2dac67618c
commit abeda7820d
2 changed files with 72 additions and 0 deletions

View File

@ -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<string> GetRootFolderNames();
public ObjectStoreDTO GetObjectStoreTempToken();
public void MoveObject(string sourcePath, string destPath, bool overwrite = true);
}
@ -533,6 +536,60 @@ public class OSSService : IOSSService
}
/// <summary>
/// 移动OSS文件到新路径
/// </summary>
/// <param name="sourcePath">原文件路径格式bucket/key</param>
/// <param name="destPath">新文件路径格式bucket/key</param>
/// <param name="overwrite">是否覆盖已存在的目标文件默认true</param>
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);
}
}
/// <summary>
/// 获取所有根目录名称
/// </summary>

View File

@ -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<Attachment> _attachmentrepository,
IRepository<Dictionary> _dictionaryRepository,
IRepository<Enroll> _enrollRepository,
IOSSService _oSSService,
IRepository<Doctor> _doctorrepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IAttachmentService
{
@ -211,6 +213,19 @@ namespace IRaCIS.Core.Application.Service
public async Task<IResponseOutput> 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);