From a2174b963edc4d4d21505007c46c5b8161acc2cc Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Mon, 29 Nov 2021 16:22:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(private-file):=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=88=9B=E5=BB=BA?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OssManagement/PrivateFileAppService.cs | 38 +++++++++++++++++-- .../Abp/OssManagement/GetOssObjectRequest.cs | 2 + .../Abp/OssManagement/GetOssObjectsRequest.cs | 1 + .../OssManagement/IOssContainerExtensions.cs | 8 +++- .../FileSystem/FileSystemOssContainer.cs | 8 +++- 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Application/LINGYUN/Abp/OssManagement/PrivateFileAppService.cs b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Application/LINGYUN/Abp/OssManagement/PrivateFileAppService.cs index 0ddeb607b..59b2afee4 100644 --- a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Application/LINGYUN/Abp/OssManagement/PrivateFileAppService.cs +++ b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Application/LINGYUN/Abp/OssManagement/PrivateFileAppService.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Authorization; +using LINGYUN.Abp.Features.LimitValidation; +using LINGYUN.Abp.OssManagement.Features; +using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Caching.Distributed; using System; using System.Collections.Generic; @@ -7,6 +9,7 @@ using System.Web; using Volo.Abp.Application.Dtos; using Volo.Abp.Caching; using Volo.Abp.Content; +using Volo.Abp.Features; using Volo.Abp.IO; using Volo.Abp.Users; @@ -37,15 +40,44 @@ namespace LINGYUN.Abp.OssManagement } [Authorize] + [RequiresFeature(AbpOssManagementFeatureNames.OssObject.DownloadFile)] + [RequiresLimitFeature( + AbpOssManagementFeatureNames.OssObject.DownloadLimit, + AbpOssManagementFeatureNames.OssObject.DownloadInterval, + LimitPolicy.Month)] public override async Task GetAsync(GetPublicFileInput input) { - return await base.GetAsync(input); + var ossObjectRequest = new GetOssObjectRequest( + GetCurrentBucket(), + // 需要处理特殊字符 + HttpUtility.UrlDecode(input.Name), + GetCurrentPath(HttpUtility.UrlDecode(input.Path)), + HttpUtility.UrlDecode(input.Process)) + { + MD5 = true, + // TODO: 用户访问自身目录可以实现无限制创建目录层级? + CreatePathIsNotExists = true, + }; + + var ossContainer = OssContainerFactory.Create(); + var ossObject = await ossContainer.GetObjectAsync(ossObjectRequest); + + return new RemoteStreamContent(ossObject.Content); } [Authorize] public override async Task> GetListAsync(GetFilesInput input) { - return await base.GetListAsync(input); + var ossContainer = OssContainerFactory.Create(); + var response = await ossContainer.GetObjectsAsync( + GetCurrentBucket(), + GetCurrentPath(HttpUtility.UrlDecode(input.Path)), + skipCount: 0, + maxResultCount: input.MaxResultCount, + createPathIsNotExists: true); // TODO: 用户访问自身目录可以实现无限制创建目录层级? + + return new ListResultDto( + ObjectMapper.Map, List>(response.Objects)); } [Authorize] diff --git a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectRequest.cs b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectRequest.cs index aa47c39c5..741c18951 100644 --- a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectRequest.cs +++ b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectRequest.cs @@ -14,6 +14,8 @@ namespace LINGYUN.Abp.OssManagement /// public string Process { get; } + public bool CreatePathIsNotExists { get; set; } = false; + public GetOssObjectRequest( [NotNull] string bucket, [NotNull] string @object, diff --git a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectsRequest.cs b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectsRequest.cs index 3e00c17d4..fc05f2457 100644 --- a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectsRequest.cs +++ b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/GetOssObjectsRequest.cs @@ -13,6 +13,7 @@ namespace LINGYUN.Abp.OssManagement public int Current { get; } public int? MaxKeys { get; } public bool MD5 { get; set; } + public bool CreatePathIsNotExists { get; set; } = false; public GetOssObjectsRequest( [NotNull] string bucketName, string prefix = null, diff --git a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/IOssContainerExtensions.cs b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/IOssContainerExtensions.cs index 81ff77c67..634f7382a 100644 --- a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/IOssContainerExtensions.cs +++ b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.Domain/LINGYUN/Abp/OssManagement/IOssContainerExtensions.cs @@ -59,12 +59,14 @@ namespace LINGYUN.Abp.OssManagement string bucket, string @object, string path = "", - bool md5 = false) + bool md5 = false, + bool createPathIsNotExists = false) { return await ossContainer.GetObjectAsync( new GetOssObjectRequest(bucket, @object, path) { MD5 = md5, + CreatePathIsNotExists = createPathIsNotExists }); } @@ -77,12 +79,14 @@ namespace LINGYUN.Abp.OssManagement string encodingType = null, bool md5 = false, int skipCount = 0, - int maxResultCount = 10) + int maxResultCount = 10, + bool createPathIsNotExists = false) { return await ossContainer.GetObjectsAsync( new GetOssObjectsRequest(name, prefix, marker, delimiter, encodingType, skipCount, maxResultCount) { MD5 = md5, + CreatePathIsNotExists = createPathIsNotExists, }); } } diff --git a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.FileSystem/LINGYUN/Abp/OssManagement/FileSystem/FileSystemOssContainer.cs b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.FileSystem/LINGYUN/Abp/OssManagement/FileSystem/FileSystemOssContainer.cs index 9f2d06390..244b5f760 100644 --- a/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.FileSystem/LINGYUN/Abp/OssManagement/FileSystem/FileSystemOssContainer.cs +++ b/aspnet-core/modules/oss-management/LINGYUN.Abp.OssManagement.FileSystem/LINGYUN/Abp/OssManagement/FileSystem/FileSystemOssContainer.cs @@ -264,11 +264,14 @@ namespace LINGYUN.Abp.OssManagement.FileSystem var filePath = CalculateFilePath(request.Bucket, objectName); if (!File.Exists(filePath)) { - if (!Directory.Exists(filePath)) + if (!Directory.Exists(filePath) && !request.CreatePathIsNotExists) { throw new BusinessException(code: OssManagementErrorCodes.ObjectNotFound); // throw new ContainerNotFoundException($"Can't not found object {objectName} in container {request.Bucket} with file system"); } + + DirectoryHelper.CreateIfNotExists(filePath); + var directoryInfo = new DirectoryInfo(filePath); var ossObject = new OssObject( directoryInfo.Name.EnsureEndsWith('/'), @@ -384,11 +387,12 @@ namespace LINGYUN.Abp.OssManagement.FileSystem { // 先定位检索的目录 var filePath = CalculateFilePath(request.BucketName, request.Prefix); - if (!Directory.Exists(filePath)) + if (!Directory.Exists(filePath) && !request.CreatePathIsNotExists) { throw new BusinessException(code: OssManagementErrorCodes.ContainerNotFound); // throw new ContainerNotFoundException($"Can't not found container {request.BucketName} in file system"); } + DirectoryHelper.CreateIfNotExists(filePath); // 目录也属于Oss对象,需要抽象的文件系统集合来存储 var fileSystemNames = Directory.GetFileSystemEntries(filePath); int maxFilesCount = fileSystemNames.Length;