Skip to content

Commit

Permalink
Merge pull request #120 from EasyAbp/get-by-path
Browse files Browse the repository at this point in the history
Add get file by path API
  • Loading branch information
gdlcf88 authored Jan 2, 2024
2 parents 88582f9 + 6e35916 commit 78008b6
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>5.1.0</Version>
<Version>5.2.0-preview.1</Version>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>EasyAbp Team</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public interface IFileAppService :
Task<PublicFileContainerConfiguration> GetConfigurationAsync(string fileContainerName, Guid? ownerUserId);

Task<FileLocationDto> GetLocationAsync(Guid id);

Task<FileInfoDto> GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ public virtual async Task<FileLocationDto> GetLocationAsync(Guid id)
};
}

public virtual async Task<FileInfoDto> GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId)
{
var file = await _fileManager.GetByPathAsync(path, fileContainerName, ownerUserId);

await AuthorizationService.CheckAsync(new FileGetInfoOperationInfoModel(file),
new OperationAuthorizationRequirement { Name = FileManagementPermissions.File.Default });

return await MapToGetOutputDtoAsync(file);
}

protected virtual string GenerateUniqueFileName([CanBeNull] string fileName)
{
return Guid.NewGuid().ToString("N") + Path.GetExtension(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public abstract Task<List<File>> CreateManyAsync(List<CreateFileModel> models,
public abstract Task<List<File>> CreateManyAsync(List<CreateFileWithStreamModel> models,
CancellationToken cancellationToken = default);

public abstract Task<File> GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId);

public virtual async Task<FileLocationModel> GetFileLocationAsync(File file,
CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
Expand Down Expand Up @@ -26,5 +27,7 @@ Task<List<File>> CreateManyAsync(List<CreateFileWithStreamModel> models,
Task<FileDownloadInfoModel> GetDownloadInfoAsync(File file);

Task<FileLocationModel> GetFileLocationAsync(File file, CancellationToken cancellationToken = default);

Task<File> GetByPathAsync([NotNull] string path, [NotNull] string fileContainerName, Guid? ownerUserId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public static class FileManagementConsts

public static int DirectoryMaxSubResourceCount { get; set; } = 99999; // todo

public static int DirectoryMaxDepth { get; set; } = 500; // todo

public static int FileContainerNameMaxLength { get; set; } = 64;

public static class File
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EasyAbp.FileManagement.Options;
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;

Expand Down Expand Up @@ -164,6 +166,41 @@ public override async Task<List<File>> CreateManyAsync(List<CreateFileWithStream
return await CreateManyAsync(targetModels, cancellationToken);
}

public override async Task<File> 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<IOptions<FileManagementOptions>>().Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,16 @@ public virtual async Task<PublicFileContainerConfiguration> GetConfigurationAsyn

[HttpGet]
[Route("location")]
public async Task<FileLocationDto> GetLocationAsync(Guid id)
public virtual Task<FileLocationDto> GetLocationAsync(Guid id)
{
return await _service.GetLocationAsync(id);
return _service.GetLocationAsync(id);
}

[HttpGet]
[Route("by-path")]
public virtual Task<FileInfoDto> GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId)
{
return _service.GetByPathAsync(path, fileContainerName, ownerUserId);
}
}
}
17 changes: 17 additions & 0 deletions test/EasyAbp.FileManagement.Domain.Tests/Files/FileDomainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,21 @@ public async Task Should_Get_File_Location()
fileLocation.FilePath.ShouldBe("dir/sub-dir/file.txt");
fileLocation.ParentPath.ShouldBe("dir/sub-dir");
}

[Fact]
public async Task Should_Get_File_By_Path()
{
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()));

(await FileManager.GetByPathAsync("dir", "test", null)).Id.ShouldBe(dir.Id);
(await FileManager.GetByPathAsync("dir/sub-dir", "test", null)).Id.ShouldBe(subDir.Id);
(await FileManager.GetByPathAsync("dir/sub-dir/file.txt", "test", null)).Id.ShouldBe(file.Id);
}
}

0 comments on commit 78008b6

Please sign in to comment.