From 675460a1e8a68163d56e042fe101d5c1e37458bc Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Wed, 17 Jan 2024 00:14:42 +0800 Subject: [PATCH] Refactor the get-by-path API --- common.props | 2 +- .../Pages/MyFiles/Index.cshtml | 11 ++++- .../Files/Dtos/GetFileByPathOutputDto.cs | 23 ++++++++++ .../FileManagement/Files/IFileAppService.cs | 3 +- .../FileManagement/Files/FileAppService.cs | 16 ++++--- .../FileManagement/Files/FileManagerBase.cs | 43 ++++++++++++++++++- .../FileManagement/Files/IFileManager.cs | 3 ++ .../FileManagement/Files/FileManager.cs | 39 +---------------- .../FileManagement/Files/FileController.cs | 3 +- 9 files changed, 93 insertions(+), 50 deletions(-) create mode 100644 src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/GetFileByPathOutputDto.cs diff --git a/common.props b/common.props index 266230f..2b12431 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ latest - 5.2.0-preview.3 + 5.2.0-preview.4 $(NoWarn);CS1591 true EasyAbp Team diff --git a/host/EasyAbp.FileManagement.Web.Unified/Pages/MyFiles/Index.cshtml b/host/EasyAbp.FileManagement.Web.Unified/Pages/MyFiles/Index.cshtml index f3aaaf8..947e20c 100644 --- a/host/EasyAbp.FileManagement.Web.Unified/Pages/MyFiles/Index.cshtml +++ b/host/EasyAbp.FileManagement.Web.Unified/Pages/MyFiles/Index.cshtml @@ -24,8 +24,15 @@ if (path is not null) { var fileAppService = HttpContext.RequestServices.GetRequiredService(); - var parent = await fileAppService.GetByPathAsync(path, fileContainerName, ownerUserId); - parentId = parent.Id; + var result = await fileAppService.GetByPathAsync(path, fileContainerName, ownerUserId); + + if (!result.Found) + { + Response.Redirect(Request.Path); + return; + } + + parentId = result.FileInfo!.Id; } // add a file manager widget diff --git a/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/GetFileByPathOutputDto.cs b/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/GetFileByPathOutputDto.cs new file mode 100644 index 0000000..b9db1e3 --- /dev/null +++ b/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/GetFileByPathOutputDto.cs @@ -0,0 +1,23 @@ +using System; +using JetBrains.Annotations; + +namespace EasyAbp.FileManagement.Files.Dtos; + +[Serializable] +public class GetFileByPathOutputDto +{ + public bool Found { get; set; } + + [CanBeNull] + public FileInfoDto FileInfo { get; set; } + + public GetFileByPathOutputDto() + { + } + + public GetFileByPathOutputDto(bool found, [CanBeNull] FileInfoDto fileInfo) + { + Found = found; + FileInfo = fileInfo; + } +} \ No newline at end of file diff --git a/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/IFileAppService.cs b/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/IFileAppService.cs index 34728e0..ecbf9eb 100644 --- a/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/IFileAppService.cs +++ b/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/IFileAppService.cs @@ -2,7 +2,6 @@ using System.Threading.Tasks; using EasyAbp.FileManagement.Containers; using EasyAbp.FileManagement.Files.Dtos; -using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Content; @@ -38,6 +37,6 @@ public interface IFileAppService : Task GetLocationAsync(Guid id); - Task GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId); + Task GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId); } } \ No newline at end of file diff --git a/src/EasyAbp.FileManagement.Application/EasyAbp/FileManagement/Files/FileAppService.cs b/src/EasyAbp.FileManagement.Application/EasyAbp/FileManagement/Files/FileAppService.cs index 3543f94..be3eb70 100644 --- a/src/EasyAbp.FileManagement.Application/EasyAbp/FileManagement/Files/FileAppService.cs +++ b/src/EasyAbp.FileManagement.Application/EasyAbp/FileManagement/Files/FileAppService.cs @@ -457,14 +457,20 @@ public virtual async Task GetLocationAsync(Guid id) }; } - public virtual async Task GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId) + public virtual async Task GetByPathAsync(string path, string fileContainerName, + Guid? ownerUserId) { - var file = await _fileManager.GetByPathAsync(path, fileContainerName, ownerUserId); + var file = await _fileManager.FindByPathAsync(path, fileContainerName, ownerUserId); - await AuthorizationService.CheckAsync(new FileGetInfoOperationInfoModel(file), - new OperationAuthorizationRequirement { Name = FileManagementPermissions.File.Default }); + var found = file is not null; - return await MapToGetOutputDtoAsync(file); + if (found) // todo: should throw auth exceptions even if file not found. + { + await AuthorizationService.CheckAsync(new FileGetInfoOperationInfoModel(file), + new OperationAuthorizationRequirement { Name = FileManagementPermissions.File.Default }); + } + + return new GetFileByPathOutputDto(found, found ? await MapToGetOutputDtoAsync(file) : null); } protected virtual string GenerateUniqueFileName([CanBeNull] string fileName) diff --git a/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/FileManagerBase.cs b/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/FileManagerBase.cs index 19f2509..57823b7 100644 --- a/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/FileManagerBase.cs +++ b/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/FileManagerBase.cs @@ -7,6 +7,7 @@ using EasyAbp.FileManagement.Options.Containers; using JetBrains.Annotations; using Volo.Abp; +using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Services; using Volo.Abp.EventBus.Distributed; using Volo.Abp.Uow; @@ -37,7 +38,47 @@ public abstract Task> CreateManyAsync(List models, public abstract Task> CreateManyAsync(List models, CancellationToken cancellationToken = default); - public abstract Task GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId); + public virtual async Task GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId) + { + var foundFile = await FindByPathAsync(path, fileContainerName, ownerUserId); + + if (foundFile is null) + { + throw new EntityNotFoundException(typeof(File), path); + } + + return foundFile; + } + + public virtual async Task FindByPathAsync(string path, string fileContainerName, Guid? ownerUserId) + { + Check.NotNullOrWhiteSpace(path, nameof(path)); + Check.NotNullOrWhiteSpace(fileContainerName, nameof(fileContainerName)); + + var splitFileName = path.Split(FileManagementConsts.DirectorySeparator); + + foreach (var fileName in splitFileName) + { + Check.Length(fileName, "fileName", FileManagementConsts.File.FileNameMaxLength, 1); + } + + File foundFile = null; + Guid? parentId = null; + + foreach (var fileName in splitFileName) + { + foundFile = await FileRepository.FindAsync(fileName, parentId, fileContainerName, ownerUserId); + + if (foundFile is null) + { + return null; + } + + parentId = foundFile.Id; + } + + return foundFile; + } public virtual async Task GetFileLocationAsync(File file, CancellationToken cancellationToken = default) diff --git a/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/IFileManager.cs b/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/IFileManager.cs index d40bcf9..23e95fd 100644 --- a/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/IFileManager.cs +++ b/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/IFileManager.cs @@ -29,5 +29,8 @@ Task> CreateManyAsync(List models, Task GetFileLocationAsync(File file, CancellationToken cancellationToken = default); Task GetByPathAsync([NotNull] string path, [NotNull] string fileContainerName, Guid? ownerUserId); + + [ItemCanBeNull] + Task FindByPathAsync([NotNull] string path, [NotNull] string fileContainerName, Guid? ownerUserId); } } \ No newline at end of file diff --git a/src/EasyAbp.FileManagement.Domain/EasyAbp/FileManagement/Files/FileManager.cs b/src/EasyAbp.FileManagement.Domain/EasyAbp/FileManagement/Files/FileManager.cs index b70236b..7486049 100644 --- a/src/EasyAbp.FileManagement.Domain/EasyAbp/FileManagement/Files/FileManager.cs +++ b/src/EasyAbp.FileManagement.Domain/EasyAbp/FileManagement/Files/FileManager.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -7,7 +6,6 @@ using EasyAbp.FileManagement.Options.Containers; using Microsoft.Extensions.Options; using Volo.Abp; -using Volo.Abp.Domain.Entities; using Volo.Abp.ObjectExtending; using Volo.Abp.Uow; @@ -166,41 +164,6 @@ public override async Task> CreateManyAsync(List GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId) - { - Check.NotNullOrWhiteSpace(path, nameof(path)); - Check.NotNullOrWhiteSpace(fileContainerName, nameof(fileContainerName)); - - var splitFileName = path.Split(FileManagementConsts.DirectorySeparator); - - foreach (var fileName in splitFileName) - { - Check.Length(fileName, "fileName", FileManagementConsts.File.FileNameMaxLength, 1); - } - - File foundFile = null; - Guid? parentId = null; - - foreach (var fileName in splitFileName) - { - foundFile = await FileRepository.FindAsync(fileName, parentId, fileContainerName, ownerUserId); - - if (foundFile is null) - { - throw new EntityNotFoundException(typeof(File)); - } - - parentId = foundFile.Id; - } - - if (foundFile is null) - { - throw new EntityNotFoundException(typeof(File)); - } - - return foundFile; - } - protected override IFileDownloadProvider GetFileDownloadProvider(File file) { var options = LazyServiceProvider.LazyGetRequiredService>().Value; diff --git a/src/EasyAbp.FileManagement.HttpApi/EasyAbp/FileManagement/Files/FileController.cs b/src/EasyAbp.FileManagement.HttpApi/EasyAbp/FileManagement/Files/FileController.cs index 0f7d1c0..a441776 100644 --- a/src/EasyAbp.FileManagement.HttpApi/EasyAbp/FileManagement/Files/FileController.cs +++ b/src/EasyAbp.FileManagement.HttpApi/EasyAbp/FileManagement/Files/FileController.cs @@ -218,7 +218,8 @@ public virtual Task GetLocationAsync(Guid id) [HttpGet] [Route("by-path")] - public virtual Task GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId) + public virtual Task GetByPathAsync(string path, string fileContainerName, + Guid? ownerUserId) { return _service.GetByPathAsync(path, fileContainerName, ownerUserId); }