Skip to content

Commit

Permalink
feat: can force create directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Sep 13, 2023
1 parent ac5e4a8 commit 434f64d
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 100 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ indent_size = 2

# Markdown Files
[*.{md,mdx}]
indent_size = 2
trim_trailing_whitespace = false

# Web Files
Expand Down Expand Up @@ -491,6 +492,7 @@ tab_width = 4
#
dotnet_diagnostic.CA1848.severity = silent
dotnet_diagnostic.CA1305.severity = silent
dotnet_diagnostic.CA2016.severity = warning

[{*.har,*.jsb2,*.jsb3,*.json,.babelrc,.eslintrc,.stylelintrc,bowerrc,jest.config}]
indent_style = space
Expand Down
4 changes: 2 additions & 2 deletions modules/file-management/src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project>
<PropertyGroup>
<Product>Passingwind.Abp.FileManagement</Product>
<Description>an abp module that provider file and file container management </Description>
<Description>an abp module that provider file and file container management.</Description>
<Version>0.1.0</Version>
<PackageVersion>0.1.8</PackageVersion>
<PackageVersion>0.1.9</PackageVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public override IEnumerable<ValidationResult> Validate(ValidationContext validat
yield return new ValidationResult("FileData should not be null", new[] { nameof(FileData) });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public class FileDirectoryCreateDto : ExtensibleObject
public string FileName { get; set; } = null!;

public Guid? ParentId { get; set; }

public bool Force { get; set; }
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace Passingwind.Abp.FileManagement.Files;
[Authorize]
public class FileShareAppService : FileManagementAppService, IFileShareAppService
{
private readonly IFileManager _fileManager;
private readonly IFileRepository _fileRepository;
private readonly IFileContainerRepository _fileContainerRepository;
private readonly IFileAccessTokenProvider _fileAccessTokenProvider;
private readonly FileManagementOptions _options;
protected IFileManager FileManager;
protected IFileRepository FileRepository;
protected IFileContainerRepository FileContainerRepository;
protected IFileAccessTokenProvider FileAccessTokenProvider;
protected FileManagementOptions FileManagementOptions;

public FileShareAppService(
IFileManager fileManager,
Expand All @@ -25,48 +25,48 @@ public FileShareAppService(
IFileAccessTokenProvider fileAccessTokenProvider,
IOptions<FileManagementOptions> options)
{
_fileManager = fileManager;
_fileRepository = fileRepository;
_fileContainerRepository = fileContainerRepository;
_fileAccessTokenProvider = fileAccessTokenProvider;
_options = options.Value;
FileManager = fileManager;
FileRepository = fileRepository;
FileContainerRepository = fileContainerRepository;
FileAccessTokenProvider = fileAccessTokenProvider;
FileManagementOptions = options.Value;
}

public virtual async Task<FileShareResultDto> CreateAsync(string containerName, Guid id, FileShareCreateRequestDto input)
{
var container = await _fileContainerRepository.GetByNameAsync(containerName);
var container = await FileContainerRepository.GetByNameAsync(containerName);

await CheckContainerPermissionAsync(container);

var entity = await _fileRepository.GetAsync(id);
var entity = await FileRepository.GetAsync(id);

await CheckFileIsInContainerAsync(container, entity);

TimeSpan? expiration = input.ExpirationSecond.HasValue ? TimeSpan.FromSeconds(input.ExpirationSecond.Value) : null;
var token = await _fileAccessTokenProvider.CreateAsync(container, entity, expiration);
var token = await FileAccessTokenProvider.CreateAsync(container, entity, expiration);

return new FileShareResultDto
{
FileName = entity.FileName,
Length = entity.Length,
MimeType = entity.MimeType,
ExpirationTime = expiration.HasValue ? Clock.Now.Add(expiration.Value) : null,
DownloadUrl = string.Format(_options.FileShareDownloadUrlFormat, token, container.Name),
DownloadUrl = string.Format(FileManagementOptions.FileShareDownloadUrlFormat, token, container.Name),
Token = token,
};
}

[AllowAnonymous]
public virtual async Task<FileShareResultDto> GetAsync(string token)
{
var validationResult = await _fileAccessTokenProvider.ValidAsync(token);
var validationResult = await FileAccessTokenProvider.ValidAsync(token);

if (!validationResult.IsValid || validationResult.File == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);

var file = validationResult.File;

var container = await _fileContainerRepository.FindAsync(file.ContainerId);
var container = await FileContainerRepository.FindAsync(file.ContainerId);

if (container == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);
Expand All @@ -77,27 +77,27 @@ public virtual async Task<FileShareResultDto> GetAsync(string token)
Length = file.Length,
MimeType = file.MimeType,
ExpirationTime = validationResult.ExpirationTime,
DownloadUrl = string.Format(_options.FileShareDownloadUrlFormat, token),
DownloadUrl = string.Format(FileManagementOptions.FileShareDownloadUrlFormat, token),
Token = token,
};
}

[AllowAnonymous]
public virtual async Task<IRemoteStreamContent?> GetBlobAsync(string token)
{
var validationResult = await _fileAccessTokenProvider.ValidAsync(token);
var validationResult = await FileAccessTokenProvider.ValidAsync(token);

if (!validationResult.IsValid || validationResult.File == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);

var file = validationResult.File;

var container = await _fileContainerRepository.FindAsync(file.ContainerId);
var container = await FileContainerRepository.FindAsync(file.ContainerId);

if (container == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);

var fileStream = await _fileManager.GetFileSteamAsync(container, file);
var fileStream = await FileManager.GetFileSteamAsync(container, file);

if (fileStream == null)
throw new BlobNotFoundException();
Expand All @@ -108,36 +108,36 @@ public virtual async Task<FileShareResultDto> GetAsync(string token)
[AllowAnonymous]
public virtual async Task<byte[]?> GetBytesAsync(string token)
{
var validationResult = await _fileAccessTokenProvider.ValidAsync(token);
var validationResult = await FileAccessTokenProvider.ValidAsync(token);

if (!validationResult.IsValid || validationResult.File == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);

var file = validationResult.File;

var container = await _fileContainerRepository.FindAsync(file.ContainerId);
var container = await FileContainerRepository.FindAsync(file.ContainerId);

if (container == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);

return await _fileManager.GetFileBytesAsync(container, file);
return await FileManager.GetFileBytesAsync(container, file);
}

[AllowAnonymous]
public virtual async Task<Stream?> GetStreamAsync(string token)
{
var validationResult = await _fileAccessTokenProvider.ValidAsync(token);
var validationResult = await FileAccessTokenProvider.ValidAsync(token);

if (!validationResult.IsValid || validationResult.File == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);

var file = validationResult.File;

var container = await _fileContainerRepository.FindAsync(file.ContainerId);
var container = await FileContainerRepository.FindAsync(file.ContainerId);

if (container == null)
throw new BusinessException(FileManagementErrorCodes.ShareFileNotExistsOrExpired);

return await _fileManager.GetFileSteamAsync(container, file);
return await FileManager.GetFileSteamAsync(container, file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public virtual async Task SaveBlobAsync(FileContainer container, File file, byte
await blobContainer.SaveAsync(file.BlobName, bytes, true, cancellationToken);
}

public virtual async Task<File> ChangeFileNameAsync(FileContainer container, File file, string newName, Guid? parentId, CancellationToken cancellationToken = default)
public virtual async Task<File> ChangeNameAsync(FileContainer container, File file, string newName, Guid? parentId, CancellationToken cancellationToken = default)
{
if (container == null)
throw new ArgumentNullException(nameof(container));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface IFileManager : IDomainService

Task<File> CreateDirectoryAsync(FileContainer container, string name, Guid? parentId, CancellationToken cancellationToken = default);

Task<File> ChangeFileNameAsync(FileContainer container, File file, string newName, Guid? parentId, CancellationToken cancellationToken = default);
Task<File> ChangeNameAsync(FileContainer container, File file, string newName, Guid? parentId, CancellationToken cancellationToken = default);

Task<byte[]> GetFileBytesAsync(FileContainer container, File file, CancellationToken cancellationToken = default);
Task<Stream?> GetFileSteamAsync(FileContainer container, File file, CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public interface IFileRepository : IRepository<File, Guid>
Task<List<File>> GetPagedListAsync(int skipCount, int maxResultCount, string? filter = null, Guid? containerId = null, Guid? parentId = null, bool? isDirectory = null, string? sorting = null, bool includeDetails = false, CancellationToken cancellationToken = default);

Task<bool> IsFileNameExistsAsync(Guid containerId, string fileName, Guid? parentId = null, CancellationToken cancellationToken = default);

Task<File?> FindByNameAsync(Guid containerId, string fileName, Guid? parentId = null, CancellationToken cancellationToken = default);

Task<File> GetByNameAsync(Guid containerId, string fileName, Guid? parentId = null, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Passingwind.Abp.FileManagement.Files;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

Expand All @@ -17,6 +18,26 @@ public FileRepository(IDbContextProvider<FileManagementDbContext> dbContextProvi
{
}

public async Task<File?> FindByNameAsync(Guid containerId, string fileName, Guid? parentId = null, CancellationToken cancellationToken = default)
{
var dbset = await GetDbSetAsync();

return await dbset
.WhereIf(parentId.HasValue, x => x.ParentId == parentId)
.FirstOrDefaultAsync(x => x.ContainerId == containerId && x.FileName == fileName, cancellationToken: cancellationToken);
}

public async Task<File> GetByNameAsync(Guid containerId, string fileName, Guid? parentId = null, CancellationToken cancellationToken = default)
{
var dbset = await GetDbSetAsync();

var entity = await dbset
.WhereIf(parentId.HasValue, x => x.ParentId == parentId)
.FirstOrDefaultAsync(x => x.ContainerId == containerId && x.FileName == fileName, cancellationToken: cancellationToken);

return entity ?? throw new EntityNotFoundException();
}

public virtual async Task<long> GetCountAsync(string? filter = null, Guid? containerId = null, Guid? parentId = null, bool? isDirectory = null, CancellationToken cancellationToken = default)
{
var dbset = await GetDbSetAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Passingwind.Abp.FileManagement.Files;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MongoDB;

Expand All @@ -18,6 +19,28 @@ public FileRepository(IMongoDbContextProvider<FileManagementMongoDbContext> dbCo
{
}

public async Task<File?> FindByNameAsync(Guid containerId, string fileName, Guid? parentId = null, CancellationToken cancellationToken = default)
{
var query = await GetMongoQueryableAsync();

return await query
.WhereIf(parentId.HasValue, x => x.ParentId == parentId)
.As<IMongoQueryable<File>>()
.FirstOrDefaultAsync(x => x.ContainerId == containerId && x.FileName == fileName, cancellationToken: cancellationToken);
}

public async Task<File> GetByNameAsync(Guid containerId, string fileName, Guid? parentId = null, CancellationToken cancellationToken = default)
{
var query = await GetMongoQueryableAsync();

var entity = await query
.WhereIf(parentId.HasValue, x => x.ParentId == parentId)
.As<IMongoQueryable<File>>()
.FirstOrDefaultAsync(x => x.ContainerId == containerId && x.FileName == fileName, cancellationToken: cancellationToken);

return entity ?? throw new EntityNotFoundException();
}

public async Task<long> GetCountAsync(string? filter = null, Guid? containerId = null, Guid? parentId = null, bool? isDirectory = null, CancellationToken cancellationToken = default)
{
var query = await GetMongoQueryableAsync();
Expand Down

0 comments on commit 434f64d

Please sign in to comment.