Skip to content

Commit

Permalink
Merge pull request #119 from EasyAbp/refactor-file-location-apis
Browse files Browse the repository at this point in the history
Refactor file location APIs
  • Loading branch information
gdlcf88 committed Dec 31, 2023
2 parents bdf8bbd + 07c2348 commit 1a51438
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 31 deletions.
Expand Up @@ -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; }
}
Expand Up @@ -452,7 +452,6 @@ public virtual async Task<FileLocationDto> GetLocationAsync(Guid id)
return new FileLocationDto
{
Id = file.Id,
FileName = file.FileName,
Location = location
};
}
Expand Down
Expand Up @@ -37,7 +37,22 @@ public abstract class FileManagerBase : DomainService, IFileManager
public abstract Task<List<File>> CreateManyAsync(List<CreateFileWithStreamModel> models,
CancellationToken cancellationToken = default);

public abstract Task<string> GetFileLocationAsync(File file, CancellationToken cancellationToken = default);
public virtual async Task<FileLocationModel> 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);

Expand Down
Expand Up @@ -25,6 +25,6 @@ public interface IFileManager : IDomainService

Task<FileDownloadInfoModel> GetDownloadInfoAsync(File file);

Task<string> GetFileLocationAsync(File file, CancellationToken cancellationToken = default);
Task<FileLocationModel> GetFileLocationAsync(File file, CancellationToken cancellationToken = default);
}
}
@@ -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;
}
}
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -165,23 +164,6 @@ public class FileManager : FileManagerBase, IFileManager
return await CreateManyAsync(targetModels, cancellationToken);
}

public override async Task<string> 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<IOptions<FileManagementOptions>>().Value;
Expand Down
Expand Up @@ -28,7 +28,7 @@
<abp-card-header>
<abp-row>
<abp-column size-md="_6">
<abp-card-title>@L["Location"]: @Model.FullPath</abp-card-title>
<abp-card-title>@L["Location"]: @Model.Location.FilePath</abp-card-title>
</abp-column>
<abp-column size-md="_6" class="text-end">
@if (canCreateDirectory)
Expand Down
Expand Up @@ -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,
Expand Down
@@ -1,4 +1,5 @@
using System;
using EasyAbp.FileManagement.Files;
using JetBrains.Annotations;

namespace EasyAbp.FileManagement.Web.Pages.FileManagement.Components.FileManagerWidget;
Expand All @@ -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();
}
}
35 changes: 35 additions & 0 deletions test/EasyAbp.FileManagement.Domain.Tests/Files/FileDomainTests.cs
Expand Up @@ -157,4 +157,39 @@ public async Task Should_New_File_Have_LastModificationTime_Value()
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");
}
}

0 comments on commit 1a51438

Please sign in to comment.