diff --git a/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/FileLocationDto.cs b/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/FileLocationDto.cs index dab5d84..745e84d 100644 --- a/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/FileLocationDto.cs +++ b/src/EasyAbp.FileManagement.Application.Contracts/EasyAbp/FileManagement/Files/Dtos/FileLocationDto.cs @@ -7,7 +7,5 @@ public class FileLocationDto { public Guid Id { get; set; } - public string FileName { get; set; } - - public string Location { get; set; } + public FileLocationModel Location { get; set; } } \ 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 8fde137..a86243d 100644 --- a/src/EasyAbp.FileManagement.Application/EasyAbp/FileManagement/Files/FileAppService.cs +++ b/src/EasyAbp.FileManagement.Application/EasyAbp/FileManagement/Files/FileAppService.cs @@ -452,7 +452,6 @@ public virtual async Task GetLocationAsync(Guid id) return new FileLocationDto { Id = file.Id, - FileName = file.FileName, Location = location }; } 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 d1ec7e0..fa6c0ed 100644 --- a/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/FileManagerBase.cs +++ b/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/FileManagerBase.cs @@ -37,7 +37,22 @@ public abstract Task> CreateManyAsync(List models, public abstract Task> CreateManyAsync(List models, CancellationToken cancellationToken = default); - public abstract Task GetFileLocationAsync(File file, CancellationToken cancellationToken = default); + public virtual async Task GetFileLocationAsync(File file, + CancellationToken cancellationToken = default) + { + var isDirectory = file.FileType == FileType.Directory; + + if (!file.ParentId.HasValue) + { + return new FileLocationModel(isDirectory, string.Empty, file.FileName); + } + + var parent = await FileRepository.GetAsync(file.ParentId.Value, true, cancellationToken); + + var parentLocation = await GetFileLocationAsync(parent, cancellationToken); + + return new FileLocationModel(isDirectory, parentLocation.FilePath, file.FileName); + } protected abstract IFileDownloadProvider GetFileDownloadProvider(File file); 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 480cc8c..ab87eaf 100644 --- a/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/IFileManager.cs +++ b/src/EasyAbp.FileManagement.Domain.Core/EasyAbp/FileManagement/Files/IFileManager.cs @@ -25,6 +25,6 @@ Task> CreateManyAsync(List models, Task GetDownloadInfoAsync(File file); - Task GetFileLocationAsync(File file, CancellationToken cancellationToken = default); + Task GetFileLocationAsync(File file, CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/src/EasyAbp.FileManagement.Domain.Shared/EasyAbp/FileManagement/Files/FileLocationModel.cs b/src/EasyAbp.FileManagement.Domain.Shared/EasyAbp/FileManagement/Files/FileLocationModel.cs new file mode 100644 index 0000000..b35b110 --- /dev/null +++ b/src/EasyAbp.FileManagement.Domain.Shared/EasyAbp/FileManagement/Files/FileLocationModel.cs @@ -0,0 +1,27 @@ +using System; +using JetBrains.Annotations; + +namespace EasyAbp.FileManagement.Files; + +public class FileLocationModel +{ + public bool IsDirectory { get; set; } + + [NotNull] + public string ParentPath { get; set; } + + [NotNull] + public string FileName { get; set; } + + [NotNull] + public string FilePath => ParentPath.IsNullOrEmpty() + ? FileName + : $"{ParentPath}{FileManagementConsts.DirectorySeparator}{FileName}"; + + public FileLocationModel(bool isDirectory, [NotNull] string parentPath, [NotNull] string fileName) + { + IsDirectory = isDirectory; + ParentPath = parentPath; + FileName = fileName; + } +} \ 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 11e7f67..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; @@ -165,23 +164,6 @@ public override async Task> CreateManyAsync(List GetFileLocationAsync(File file, - CancellationToken cancellationToken = default) - { - if (!file.ParentId.HasValue) - { - return file.FileName; - } - - var parent = await FileRepository.GetAsync(file.ParentId.Value, true, cancellationToken); - - var parentLocation = await GetFileLocationAsync(parent, cancellationToken); - - return parentLocation.IsNullOrEmpty() - ? file.FileName - : $"{parentLocation}{FileManagementConsts.DirectorySeparator}{file.FileName}"; - } - protected override IFileDownloadProvider GetFileDownloadProvider(File file) { var options = LazyServiceProvider.LazyGetRequiredService>().Value; diff --git a/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/Default.cshtml b/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/Default.cshtml index 48771cb..74f90c3 100644 --- a/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/Default.cshtml +++ b/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/Default.cshtml @@ -28,7 +28,7 @@ - @L["Location"]: @Model.FullPath + @L["Location"]: @Model.Location.FilePath @if (canCreateDirectory) diff --git a/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/DetailModal.cshtml.cs b/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/DetailModal.cshtml.cs index 4e1c34b..c046cc1 100644 --- a/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/DetailModal.cshtml.cs +++ b/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/DetailModal.cshtml.cs @@ -33,7 +33,7 @@ public virtual async Task OnGetAsync() MimeType = dto.MimeType, ByteSize = HumanFileSize(dto.ByteSize), Hash = dto.Hash, - Location = (await _service.GetLocationAsync(dto.Id)).Location, + Location = (await _service.GetLocationAsync(dto.Id)).Location.FilePath, Creator = dto.Creator?.UserName, Created = ToDateTimeString(dto.CreationTime), LastModifier = dto.LastModifier?.UserName, diff --git a/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/FileManagerViewModel.cs b/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/FileManagerViewModel.cs index 6b84d7f..266365a 100644 --- a/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/FileManagerViewModel.cs +++ b/src/EasyAbp.FileManagement.Web/Pages/FileManagement/Components/FileManagerWidget/FileManagerViewModel.cs @@ -1,4 +1,5 @@ using System; +using EasyAbp.FileManagement.Files; using JetBrains.Annotations; namespace EasyAbp.FileManagement.Web.Pages.FileManagement.Components.FileManagerWidget; @@ -14,19 +15,18 @@ public class FileManagerViewModel public Guid? GrandparentId { get; set; } - [CanBeNull] - public string FullPath { get; set; } + public FileLocationModel Location { get; set; } public FileManagerPolicyModel Policy { get; set; } public FileManagerViewModel([NotNull] string fileContainerName, Guid? ownerUserId, Guid? parentId, - Guid? grandparentId, [CanBeNull] string fullPath, [CanBeNull] FileManagerPolicyModel policy = null) + Guid? grandparentId, [CanBeNull] FileLocationModel location, [CanBeNull] FileManagerPolicyModel policy = null) { FileContainerName = fileContainerName; OwnerUserId = ownerUserId; ParentId = parentId; GrandparentId = grandparentId; - FullPath = fullPath; + Location = location; Policy = policy ?? new FileManagerPolicyModel(); } } \ No newline at end of file diff --git a/test/EasyAbp.FileManagement.Domain.Tests/Files/FileDomainTests.cs b/test/EasyAbp.FileManagement.Domain.Tests/Files/FileDomainTests.cs index af81c29..65a1d4a 100644 --- a/test/EasyAbp.FileManagement.Domain.Tests/Files/FileDomainTests.cs +++ b/test/EasyAbp.FileManagement.Domain.Tests/Files/FileDomainTests.cs @@ -157,4 +157,39 @@ await WithUnitOfWorkAsync(() => FileRepository.InsertAsync(new File(dirId, null, dir.LastModificationTime.ShouldNotBeNull(); dir.LastModificationTime.ShouldNotBe(DateTime.MinValue); } + + [Fact] + public async Task Should_Get_File_Location() + { + var dir = await FileManager.CreateAsync(new CreateFileModel("test", null, "dir", null, + FileType.Directory, null, null)); + + var subDir = await FileManager.CreateAsync(new CreateFileModel("test", null, "sub-dir", null, + FileType.Directory, dir, null)); + + var file = await FileManager.CreateAsync(new CreateFileModel("test", null, "file.txt", null, + FileType.RegularFile, subDir, "new-content"u8.ToArray())); + + var dirLocation = await FileManager.GetFileLocationAsync(dir); + var subDirLocation = await FileManager.GetFileLocationAsync(subDir); + var fileLocation = await FileManager.GetFileLocationAsync(file); + + dirLocation.ShouldNotBeNull(); + dirLocation.IsDirectory.ShouldBeTrue(); + dirLocation.FileName.ShouldBe("dir"); + dirLocation.FilePath.ShouldBe("dir"); + dirLocation.ParentPath.ShouldBe(""); + + subDirLocation.ShouldNotBeNull(); + subDirLocation.IsDirectory.ShouldBeTrue(); + subDirLocation.FileName.ShouldBe("sub-dir"); + subDirLocation.FilePath.ShouldBe("dir/sub-dir"); + subDirLocation.ParentPath.ShouldBe("dir"); + + fileLocation.ShouldNotBeNull(); + fileLocation.IsDirectory.ShouldBeFalse(); + fileLocation.FileName.ShouldBe("file.txt"); + fileLocation.FilePath.ShouldBe("dir/sub-dir/file.txt"); + fileLocation.ParentPath.ShouldBe("dir/sub-dir"); + } } \ No newline at end of file