Implements directory browser dialog, part 3: file system explorer

This commit is contained in:
BinaryAlley committed 2024-09-19 17:40:33 +03:00
1 parent a4f9199d2c
commit 493f34c883
76 files changed
+2377 -437

No files matched your search

@@ -1,7 +1,11 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Models.FileManagement;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
using Mapster;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Common.Mapping.FileManagement;
@@ -19,13 +23,30 @@ public class DirectoryMappingConfig : IRegister
public void Register(TypeAdapterConfig config)
{
config.NewConfig<Directory, FileSystemTreeNodeResponse>()
.Map(dest => dest.Name, src => src.Name)
.Map(dest => dest.Path, src => src.Id.Path)
.Map(dest => dest.ItemType, src => src.Type)
.Map(dest => dest.IsExpanded, () => false)
.Map(dest => dest.ChildrenLoaded, () => false)
.Map(dest => dest.Children, src => src.Items)
.MapToConstructor(true);
.Map(dest => dest.Name, src => src.Name)
.Map(dest => dest.Path, src => src.Id.Path)
.Map(dest => dest.ItemType, src => src.Type)
.Map(dest => dest.IsExpanded, () => false)
.Map(dest => dest.ChildrenLoaded, () => false)
.Map(dest => dest.Children, src => src.Items)
.MapToConstructor(true);
config.NewConfig<Directory, DirectoryResponse>()
.MapWith(src => new DirectoryResponse(
src.Id.Path,
src.Name,
src.DateCreated.Value,
src.DateModified.Value,
src.Items.Adapt<List<FileSystemItemModel>>()
));
config.NewConfig<FileSystemItem, FileSystemItemModel>()
.MapWith(src => new FileSystemItemModel(
src.Id.Path,
src.Name,
DateTime.Now,
DateTime.Now
));
}
#endregion
}
@@ -25,6 +25,15 @@ public class FileMappingConfig : IRegister
.Map(dest => dest.IsExpanded, () => false)
.Map(dest => dest.ChildrenLoaded, () => false)
.MapToConstructor(true);
config.NewConfig<File, FileResponse>()
.MapWith(src => new FileResponse(
src.Id.Path,
src.Name,
src.DateCreated.HasValue ? src.DateCreated.Value : default,
src.DateModified.HasValue ? src.DateModified.Value : default,
src.Size
));
}
#endregion
}
@@ -11,4 +11,4 @@ namespace Lumina.Application.Core.FileManagement.Directories.Queries;
/// Query for retrieving the list of directories at a path.
/// </summary>
/// <param name="Path">The path for which to retrieve the list of directories.</param>
public record GetDirectoriesQuery(string Path) : IRequest<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>;
public record GetDirectoriesQuery(string Path) : IRequest<ErrorOr<IEnumerable<DirectoryResponse>>>;
@@ -16,7 +16,7 @@ namespace Lumina.Application.Core.FileManagement.Directories.Queries;
/// <summary>
/// Handler for the query to get all directories.
/// </summary>
public class GetDirectoriesQueryHandler : IRequestHandler<GetDirectoriesQuery, ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>
public class GetDirectoriesQueryHandler : IRequestHandler<GetDirectoriesQuery, ErrorOr<IEnumerable<DirectoryResponse>>>
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IDirectoryService _directoryService;
@@ -40,12 +40,12 @@ public class GetDirectoriesQueryHandler : IRequestHandler<GetDirectoriesQuery, E
/// <param name="request">The query containing the requested path.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="FileSystemTreeNodeResponse"/>, or an error message.
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="DirectoryResponse"/>, or an error message.
/// </returns>
public ValueTask<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>> Handle(GetDirectoriesQuery request, CancellationToken cancellationToken)
public ValueTask<ErrorOr<IEnumerable<DirectoryResponse>>> Handle(GetDirectoriesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<Directory>> result = _directoryService.GetSubdirectories(request.Path);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileSystemTreeNodeResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<DirectoryResponse>>()), errors => errors));
}
#endregion
}
@@ -11,4 +11,5 @@ namespace Lumina.Application.Core.FileManagement.Directories.Queries;
/// Query for retrieving the tree of directories of a path.
/// </summary>
/// <param name="Path">The path for which to retrieve the tree of directories.</param>
public record GetDirectoryTreeQuery(string Path) : IRequest<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>;
/// <param name="IncludeFiles">Whether to include files in the directory tree or not.</param>
public record GetDirectoryTreeQuery(string Path, bool IncludeFiles) : IRequest<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>;
@@ -73,7 +73,7 @@ public class GetDirectoryTreeQueryHandler : IRequestHandler<GetDirectoryTreeQuer
return buildDirectoryTreeResult.Errors;
// add subdirectories of the last directory node
ErrorOr<Success> loadChildrenResult = LoadChildren(currentNode);
ErrorOr<Success> loadChildrenResult = LoadChildren(currentNode, request.IncludeFiles);
if (loadChildrenResult.IsError)
return loadChildrenResult.Errors;
return await ValueTask.FromResult(result);
@@ -135,8 +135,9 @@ public class GetDirectoryTreeQueryHandler : IRequestHandler<GetDirectoryTreeQuer
/// Loads the child directories and files of the specified directory node.
/// </summary>
/// <param name="node">The directory node to load children for.</param>
/// <param name="includeFiles">Whether to include files in the directories or not.</param>
/// <returns>A result indicating a successful operation, or an error message.</returns>
private ErrorOr<Success> LoadChildren(FileSystemTreeNodeResponse node)
private ErrorOr<Success> LoadChildren(FileSystemTreeNodeResponse node, bool includeFiles)
{
// get subdirectories under the current node's path
ErrorOr<IEnumerable<Directory>> subDirectoriesResult = _directoryService.GetSubdirectories(node.Path);
@@ -155,22 +156,25 @@ public class GetDirectoryTreeQueryHandler : IRequestHandler<GetDirectoryTreeQuer
};
node.Children.Add(subDirNode);
}
// get files under the current node's path
ErrorOr<IEnumerable<File>> filesResult = _fileService.GetFiles(node.Path);
if (filesResult.IsError)
return filesResult.Errors;
// add each file as a child node of the current directory
foreach (File file in filesResult.Value)
if (includeFiles)
{
FileSystemTreeNodeResponse fileNode = new FileSystemTreeNodeResponse
// get files under the current node's path
ErrorOr<IEnumerable<File>> filesResult = _fileService.GetFiles(node.Path);
if (filesResult.IsError)
return filesResult.Errors;
// add each file as a child node of the current directory
foreach (File file in filesResult.Value)
{
Name = file.Name,
Path = file.Id.Path,
ItemType = FileSystemItemType.File,
IsExpanded = false,
ChildrenLoaded = true
};
node.Children.Add(fileNode);
FileSystemTreeNodeResponse fileNode = new FileSystemTreeNodeResponse
{
Name = file.Name,
Path = file.Id.Path,
ItemType = FileSystemItemType.File,
IsExpanded = false,
ChildrenLoaded = true
};
node.Children.Add(fileNode);
}
}
node.ChildrenLoaded = true;
return Result.Success;
@@ -0,0 +1,14 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Mediator;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.FileManagement.Directories.Queries;
/// <summary>
/// Query for retrieving the list of directories at a path.
/// </summary>
/// <param name="Path">The path for which to retrieve the list of directories.</param>
public record GetTreeDirectoriesQuery(string Path) : IRequest<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>;
@@ -0,0 +1,51 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Mapster;
using MapsterMapper;
using Mediator;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.FileManagement.Directories.Queries;
/// <summary>
/// Handler for the query to get all directories.
/// </summary>
public class GetTreeDirectoriesQueryHandler : IRequestHandler<GetTreeDirectoriesQuery, ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IDirectoryService _directoryService;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetTreeDirectoriesQueryHandler"/> class.
/// </summary>
/// <param name="directoryService">Injected service for handling directories.</param>
public GetTreeDirectoriesQueryHandler(IDirectoryService directoryService)
{
_directoryService = directoryService;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Gets the list of directories at the specified path.
/// </summary>
/// <param name="request">The query containing the requested path.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="FileSystemTreeNodeResponse"/>, or an error message.
/// </returns>
public ValueTask<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>> Handle(GetTreeDirectoriesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<Directory>> result = _directoryService.GetSubdirectories(request.Path);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileSystemTreeNodeResponse>>()), errors => errors));
}
#endregion
}
@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Domain.Common.Errors;
#endregion
namespace Lumina.Application.Core.FileManagement.Directories.Queries;
/// <summary>
/// Validates the needed validation rules for <see cref="GetTreeDirectoriesQuery"/>.
/// </summary>
public class GetTreeDirectoriesQueryValidator : AbstractValidator<GetTreeDirectoriesQuery>
{
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetTreeDirectoriesQueryValidator"/> class.
/// </summary>
public GetTreeDirectoriesQueryValidator()
{
RuleFor(x => x.Path).NotEmpty().WithMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
#endregion
}
@@ -11,4 +11,4 @@ namespace Lumina.Application.Core.FileManagement.Files.Queries;
/// Query for retrieving the list of files at a path.
/// </summary>
/// <param name="Path">The path for which to retrieve the list of files.</param>
public record GetFilesQuery(string Path) : IRequest<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>;
public record GetFilesQuery(string Path) : IRequest<ErrorOr<IEnumerable<FileResponse>>>;
@@ -15,7 +15,7 @@ namespace Lumina.Application.Core.FileManagement.Files.Queries;
/// <summary>
/// Handler for the query to get all files.
/// </summary>
public class GetFilesQueryHandler : IRequestHandler<GetFilesQuery, ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>
public class GetFilesQueryHandler : IRequestHandler<GetFilesQuery, ErrorOr<IEnumerable<FileResponse>>>
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFileService _fileService;
@@ -39,12 +39,12 @@ public class GetFilesQueryHandler : IRequestHandler<GetFilesQuery, ErrorOr<IEnum
/// <param name="request">The query containing the requested path.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="FileSystemTreeNodeResponse"/>, or an error message.
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="FileResponse"/>, or an error message.
/// </returns>
public ValueTask<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>> Handle(GetFilesQuery request, CancellationToken cancellationToken)
public ValueTask<ErrorOr<IEnumerable<FileResponse>>> Handle(GetFilesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<File>> result = _fileService.GetFiles(request.Path);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileSystemTreeNodeResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileResponse>>()), errors => errors));
}
#endregion
}
@@ -0,0 +1,14 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Mediator;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.FileManagement.Files.Queries;
/// <summary>
/// Query for retrieving the list of files at a path.
/// </summary>
/// <param name="Path">The path for which to retrieve the list of files.</param>
public record GetTreeFilesQuery(string Path) : IRequest<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>;
@@ -0,0 +1,50 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Mapster;
using Mediator;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.FileManagement.Files.Queries;
/// <summary>
/// Handler for the query to get all files.
/// </summary>
public class GetTreeFilesQueryHandler : IRequestHandler<GetTreeFilesQuery, ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>>
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFileService _fileService;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetTreeFilesQueryHandler"/> class.
/// </summary>
/// <param name="fileService">Injected service for handling files.</param>
public GetTreeFilesQueryHandler(IFileService fileService)
{
_fileService = fileService;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Gets the list of files at the specified path.
/// </summary>
/// <param name="request">The query containing the requested path.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="FileSystemTreeNodeResponse"/>, or an error message.
/// </returns>
public ValueTask<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>> Handle(GetTreeFilesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<File>> result = _fileService.GetFiles(request.Path);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileSystemTreeNodeResponse>>()), errors => errors));
}
#endregion
}
@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Domain.Common.Errors;
#endregion
namespace Lumina.Application.Core.FileManagement.Files.Queries;
/// <summary>
/// Validates the needed validation rules for <see cref="GetTreeFilesQuery"/>.
/// </summary>
public class GetTreeFilesQueryValidator : AbstractValidator<GetTreeFilesQuery>
{
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetTreeFilesQueryValidator"/> class.
/// </summary>
public GetTreeFilesQueryValidator()
{
RuleFor(x => x.Path).NotEmpty().WithMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
#endregion
}