Skip to content

Commit

Permalink
Merge pull request #123 from EasyAbp/refactor-get-by-path-api
Browse files Browse the repository at this point in the history
Refactor the get-by-path API
  • Loading branch information
gdlcf88 authored Jan 16, 2024
2 parents 6a01813 + 675460a commit 0b682fb
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 50 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.2.0-preview.3</Version>
<Version>5.2.0-preview.4</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 @@ -24,8 +24,15 @@
if (path is not null)
{
var fileAppService = HttpContext.RequestServices.GetRequiredService<IFileAppService>();
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,6 +37,6 @@ public interface IFileAppService :

Task<FileLocationDto> GetLocationAsync(Guid id);

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

public virtual async Task<FileInfoDto> GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId)
public virtual async Task<GetFileByPathOutputDto> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +38,47 @@ 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<File> 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<File> 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<FileLocationModel> GetFileLocationAsync(File file,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ Task<List<File>> CreateManyAsync(List<CreateFileWithStreamModel> models,
Task<FileLocationModel> GetFileLocationAsync(File file, CancellationToken cancellationToken = default);

Task<File> GetByPathAsync([NotNull] string path, [NotNull] string fileContainerName, Guid? ownerUserId);

[ItemCanBeNull]
Task<File> FindByPathAsync([NotNull] string path, [NotNull] string fileContainerName, Guid? ownerUserId);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
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 @@ -166,41 +164,6 @@ 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 @@ -218,7 +218,8 @@ public virtual Task<FileLocationDto> GetLocationAsync(Guid id)

[HttpGet]
[Route("by-path")]
public virtual Task<FileInfoDto> GetByPathAsync(string path, string fileContainerName, Guid? ownerUserId)
public virtual Task<GetFileByPathOutputDto> GetByPathAsync(string path, string fileContainerName,
Guid? ownerUserId)
{
return _service.GetByPathAsync(path, fileContainerName, ownerUserId);
}
Expand Down

0 comments on commit 0b682fb

Please sign in to comment.