Adds Application layer unit tests for file system browser dialog

This commit is contained in:
BinaryAlley committed 2024-10-05 09:35:10 +03:00
1 parent 1f5322fa1d
commit 0e3e688eb7
79 files changed
+5484 -91

No files matched your search

@@ -1,4 +1,4 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Models.FileManagement;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate;
@@ -28,7 +28,7 @@ public class DirectoryMappingConfig : IRegister
.Map(dest => dest.ItemType, src => src.Type)
.Map(dest => dest.IsExpanded, () => false)
.Map(dest => dest.ChildrenLoaded, () => false)
.Map(dest => dest.Children, src => src.Items)
.Map(dest => dest.Children, src => src.Items.Adapt<List<FileSystemTreeNodeResponse>>())
.MapToConstructor(true);
config.NewConfig<Directory, DirectoryResponse>()
@@ -49,4 +49,4 @@ public class DirectoryMappingConfig : IRegister
));
}
#endregion
}
}
@@ -1,10 +1,11 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Enums.FileSystem;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Entities;
using Mapster;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Application.Common.Mapping.FileManagement;
@@ -38,7 +39,9 @@ public class FileSystemTreeMappingConfig : IRegister
.Map(dest => dest.Path, src => src.Id.Path)
.Map(dest => dest.Name, src => src.Name)
.Map(dest => dest.ItemType, src => FileSystemItemType.Root)
.Map(dest => dest.IsExpanded, () => false);
.Map(dest => dest.IsExpanded, () => false)
.Map(dest => dest.ChildrenLoaded, () => false)
.Map(dest => dest.Children, src => src.Items.Select(item => item.Adapt<FileSystemTreeNodeResponse>(config)).ToList());
config.NewConfig<FileSystemTreeNodeResponse, UnixRootItem>()
.MapWith(src => UnixRootItem.Create(
@@ -46,15 +49,19 @@ public class FileSystemTreeMappingConfig : IRegister
.Value);
config.NewConfig<FileSystemItem, FileSystemTreeNodeResponse>()
.MapWith(src => new FileSystemTreeNodeResponse
{
Path = src.Id.Path,
Name = src.Name,
ItemType = src.Type,
IsExpanded = false,
ChildrenLoaded = false,
Children = new List<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.ChildrenLoaded, _ => false)
.AfterMapping((src, dest) =>
{
if (src is Directory dir)
dest.Children = dir.Items.Select(item => item.Adapt<FileSystemTreeNodeResponse>(config)).ToList();
else
dest.Children = [];
});
}
#endregion
}
}
@@ -1,9 +1,10 @@
#region ========================================================================= USING =====================================================================================
#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;
@@ -19,6 +20,7 @@ public class GetDirectoriesQueryHandler : IRequestHandler<GetDirectoriesQuery, E
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IDirectoryService _directoryService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -26,9 +28,11 @@ public class GetDirectoriesQueryHandler : IRequestHandler<GetDirectoriesQuery, E
/// Initializes a new instance of the <see cref="GetDirectoriesQueryHandler"/> class.
/// </summary>
/// <param name="directoryService">Injected service for handling directories.</param>
public GetDirectoriesQueryHandler(IDirectoryService directoryService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetDirectoriesQueryHandler(IDirectoryService directoryService, IMapper mapper)
{
_directoryService = directoryService;
_mapper = mapper;
}
#endregion
@@ -44,7 +48,7 @@ public class GetDirectoriesQueryHandler : IRequestHandler<GetDirectoriesQuery, E
public ValueTask<ErrorOr<IEnumerable<DirectoryResponse>>> Handle(GetDirectoriesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<Directory>> result = _directoryService.GetSubdirectories(request.Path, request.IncludeHiddenElements);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<DirectoryResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<IEnumerable<DirectoryResponse>>(values)), errors => errors));
}
#endregion
}
}
@@ -1,9 +1,10 @@
#region ========================================================================= USING =====================================================================================
#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;
@@ -19,6 +20,7 @@ public class GetTreeDirectoriesQueryHandler : IRequestHandler<GetTreeDirectories
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IDirectoryService _directoryService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -26,9 +28,11 @@ public class GetTreeDirectoriesQueryHandler : IRequestHandler<GetTreeDirectories
/// Initializes a new instance of the <see cref="GetTreeDirectoriesQueryHandler"/> class.
/// </summary>
/// <param name="directoryService">Injected service for handling directories.</param>
public GetTreeDirectoriesQueryHandler(IDirectoryService directoryService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetTreeDirectoriesQueryHandler(IDirectoryService directoryService, IMapper mapper)
{
_directoryService = directoryService;
_mapper = mapper;
}
#endregion
@@ -44,7 +48,7 @@ public class GetTreeDirectoriesQueryHandler : IRequestHandler<GetTreeDirectories
public ValueTask<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>> Handle(GetTreeDirectoriesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<Directory>> result = _directoryService.GetSubdirectories(request.Path, request.IncludeHiddenElements);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileSystemTreeNodeResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<IEnumerable<FileSystemTreeNodeResponse>>(values)), errors => errors));
}
#endregion
}
}
@@ -1,9 +1,10 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Mapster;
using MapsterMapper;
using Mediator;
using System.Collections.Generic;
using System.Threading;
@@ -19,6 +20,7 @@ public class GetDrivesQueryHandler : IRequestHandler<GetDrivesQuery, ErrorOr<IEn
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IDriveService _driveService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -26,9 +28,11 @@ public class GetDrivesQueryHandler : IRequestHandler<GetDrivesQuery, ErrorOr<IEn
/// Initializes a new instance of the <see cref="GetDrivesQueryHandler"/> class.
/// </summary>
/// <param name="driveService">Injected service for handling file system drives.</param>
public GetDrivesQueryHandler(IDriveService driveService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetDrivesQueryHandler(IDriveService driveService, IMapper mapper)
{
_driveService = driveService;
_mapper = mapper;
}
#endregion
@@ -44,7 +48,7 @@ public class GetDrivesQueryHandler : IRequestHandler<GetDrivesQuery, ErrorOr<IEn
public ValueTask<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>> Handle(GetDrivesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<FileSystemItem>> result = _driveService.GetDrives();
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(values.Adapt<IEnumerable<FileSystemTreeNodeResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<IEnumerable<FileSystemTreeNodeResponse>>(values)), errors => errors));
}
#endregion
}
}
@@ -1,11 +1,11 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Responses.FileManagement;
using Mediator;
#endregion
namespace Lumina.Application.Core.FileManagement.FileSystem.Queries.GetFileSysem;
namespace Lumina.Application.Core.FileManagement.FileSystem.Queries.GetFileSystem;
/// <summary>
/// Query for retrieving the file system type.
/// </summary>
public record GetFileSystemQuery() : IRequest<FileSystemTypeResponse>;
public record GetFileSystemQuery() : IRequest<FileSystemTypeResponse>;
@@ -1,4 +1,4 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Strategies.Platform;
using Mediator;
@@ -6,7 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.FileManagement.FileSystem.Queries.GetFileSysem;
namespace Lumina.Application.Core.FileManagement.FileSystem.Queries.GetFileSystem;
/// <summary>
/// Handler for the query to get the file system type.
@@ -40,4 +40,4 @@ public class GetFileSystemQueryHandler : IRequestHandler<GetFileSystemQuery, Fil
return ValueTask.FromResult(new FileSystemTypeResponse(_platformContext.Platform));
}
#endregion
}
}
@@ -1,9 +1,10 @@
#region ========================================================================= USING =====================================================================================
#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;
@@ -19,6 +20,7 @@ public class GetFilesQueryHandler : IRequestHandler<GetFilesQuery, ErrorOr<IEnum
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFileService _fileService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -26,9 +28,11 @@ public class GetFilesQueryHandler : IRequestHandler<GetFilesQuery, ErrorOr<IEnum
/// Initializes a new instance of the <see cref="GetFilesQueryHandler"/> class.
/// </summary>
/// <param name="fileService">Injected service for handling files.</param>
public GetFilesQueryHandler(IFileService fileService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetFilesQueryHandler(IFileService fileService, IMapper mapper)
{
_fileService = fileService;
_mapper = mapper;
}
#endregion
@@ -44,7 +48,7 @@ public class GetFilesQueryHandler : IRequestHandler<GetFilesQuery, ErrorOr<IEnum
public ValueTask<ErrorOr<IEnumerable<FileResponse>>> Handle(GetFilesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<File>> result = _fileService.GetFiles(request.Path, request.IncludeHiddenElements);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<IEnumerable<FileResponse>>(values)), errors => errors));
}
#endregion
}
}
@@ -1,9 +1,10 @@
#region ========================================================================= USING =====================================================================================
#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;
@@ -19,6 +20,7 @@ public class GetTreeFilesQueryHandler : IRequestHandler<GetTreeFilesQuery, Error
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFileService _fileService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -26,9 +28,11 @@ public class GetTreeFilesQueryHandler : IRequestHandler<GetTreeFilesQuery, Error
/// Initializes a new instance of the <see cref="GetTreeFilesQueryHandler"/> class.
/// </summary>
/// <param name="fileService">Injected service for handling files.</param>
public GetTreeFilesQueryHandler(IFileService fileService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetTreeFilesQueryHandler(IFileService fileService, IMapper mapper)
{
_fileService = fileService;
_mapper = mapper;
}
#endregion
@@ -44,7 +48,7 @@ public class GetTreeFilesQueryHandler : IRequestHandler<GetTreeFilesQuery, Error
public ValueTask<ErrorOr<IEnumerable<FileSystemTreeNodeResponse>>> Handle(GetTreeFilesQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<File>> result = _fileService.GetFiles(request.Path, request.IncludeHiddenElements);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<FileSystemTreeNodeResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<IEnumerable<FileSystemTreeNodeResponse>>(values)), errors => errors));
}
#endregion
}
}
@@ -1,9 +1,9 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using Mapster;
using MapsterMapper;
using Mediator;
using System.Collections.Generic;
using System.Threading;
@@ -19,6 +19,7 @@ public class SplitPathCommandHandler : IRequestHandler<SplitPathCommand, ErrorOr
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IPathService _pathService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -26,9 +27,11 @@ public class SplitPathCommandHandler : IRequestHandler<SplitPathCommand, ErrorOr
/// Initializes a new instance of the <see cref="SplitPathCommandHandler"/> class.
/// </summary>
/// <param name="pathService">Injected service for handling file system paths.</param>
public SplitPathCommandHandler(IPathService pathService)
/// <param name="mapper">Injected service for mapping objects.</param>
public SplitPathCommandHandler(IPathService pathService, IMapper mapper)
{
_pathService = pathService;
_mapper = mapper;
}
#endregion
@@ -44,7 +47,7 @@ public class SplitPathCommandHandler : IRequestHandler<SplitPathCommand, ErrorOr
public ValueTask<ErrorOr<IEnumerable<PathSegmentResponse>>> Handle(SplitPathCommand request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<PathSegment>> result = _pathService.ParsePath(request.Path);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<PathSegmentResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<IEnumerable<PathSegmentResponse>>(values)), errors => errors));
}
#endregion
}
}
@@ -1,9 +1,9 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using Mapster;
using MapsterMapper;
using Mediator;
using System.Collections.Generic;
using System.Threading;
@@ -19,6 +19,7 @@ public class GetPathParentQueryHandler : IRequestHandler<GetPathParentQuery, Err
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IPathService _pathService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -26,9 +27,11 @@ public class GetPathParentQueryHandler : IRequestHandler<GetPathParentQuery, Err
/// Initializes a new instance of the <see cref="GetPathParentQueryHandler"/> class.
/// </summary>
/// <param name="pathService">Injected service for managing file system paths.</param>
public GetPathParentQueryHandler(IPathService pathService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetPathParentQueryHandler(IPathService pathService, IMapper mapper)
{
_pathService = pathService;
_mapper = mapper;
}
#endregion
@@ -44,7 +47,7 @@ public class GetPathParentQueryHandler : IRequestHandler<GetPathParentQuery, Err
public ValueTask<ErrorOr<IEnumerable<PathSegmentResponse>>> Handle(GetPathParentQuery request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<PathSegment>> result = _pathService.GoUpOneLevel(request.Path);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<IEnumerable<PathSegmentResponse>>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<IEnumerable<PathSegmentResponse>>(values)), errors => errors));
}
#endregion
}
}
@@ -1,9 +1,9 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using Mapster;
using MapsterMapper;
using Mediator;
using System.Threading;
using System.Threading.Tasks;
@@ -18,6 +18,7 @@ public class GetPathRootQueryHandler : IRequestHandler<GetPathRootQuery, ErrorOr
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IPathService _pathService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -25,9 +26,11 @@ public class GetPathRootQueryHandler : IRequestHandler<GetPathRootQuery, ErrorOr
/// Initializes a new instance of the <see cref="GetPathRootQueryHandler"/> class.
/// </summary>
/// <param name="pathService">Injected service for managing file system paths.</param>
public GetPathRootQueryHandler(IPathService pathService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetPathRootQueryHandler(IPathService pathService, IMapper mapper)
{
_pathService = pathService;
_mapper = mapper;
}
#endregion
@@ -41,7 +44,7 @@ public class GetPathRootQueryHandler : IRequestHandler<GetPathRootQuery, ErrorOr
public ValueTask<ErrorOr<PathSegmentResponse>> Handle(GetPathRootQuery request, CancellationToken cancellationToken)
{
ErrorOr<PathSegment> result = _pathService.GetPathRoot(request.Path);
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<PathSegmentResponse>()), errors => errors));
return ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<PathSegmentResponse>(values)), errors => errors));
}
#endregion
}
}
@@ -3,7 +3,7 @@ using ErrorOr;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using Mapster;
using MapsterMapper;
using Mediator;
using System.Threading;
using System.Threading.Tasks;
@@ -18,6 +18,7 @@ public class GetThumbnailQueryHandler : IRequestHandler<GetThumbnailQuery, Error
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IThumbnailService _thumbnailsService;
private readonly IMapper _mapper;
#endregion
#region ====================================================================== CTOR =====================================================================================
@@ -25,9 +26,11 @@ public class GetThumbnailQueryHandler : IRequestHandler<GetThumbnailQuery, Error
/// Initializes a new instance of the <see cref="GetThumbnailQueryHandler"/> class.
/// </summary>
/// <param name="thumbnailsService">Injected service for handling thumbnails.</param>
public GetThumbnailQueryHandler(IThumbnailService thumbnailsService)
/// <param name="mapper">Injected service for mapping objects.</param>
public GetThumbnailQueryHandler(IThumbnailService thumbnailsService, IMapper mapper)
{
_thumbnailsService = thumbnailsService;
_mapper = mapper;
}
#endregion
@@ -41,7 +44,7 @@ public class GetThumbnailQueryHandler : IRequestHandler<GetThumbnailQuery, Error
public async ValueTask<ErrorOr<ThumbnailResponse>> Handle(GetThumbnailQuery request, CancellationToken cancellationToken)
{
ErrorOr<Thumbnail> result = await _thumbnailsService.GetThumbnailAsync(request.Path, request.Quality, cancellationToken);
return await ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(result.Value.Adapt<ThumbnailResponse>()), errors => errors));
return await ValueTask.FromResult(result.Match(values => ErrorOrFactory.From(_mapper.Map<ThumbnailResponse>(values)), errors => errors));
}
#endregion
}
@@ -1,14 +0,0 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Contracts.Responses.FileManagement;
/// <summary>
/// Represents a response containing the result of splitting a file system path.
/// </summary>
/// <param name="PathSegments">The returned path segments.</param>
[DebuggerDisplay("{PathSegments}")]
public record SplitPathResponse(
string[] PathSegments
);
@@ -94,16 +94,19 @@ public class UnixPathStrategy : IUnixPathStrategy
return Errors.FileManagement.InvalidPath;
// get the path segments
List<string> splitSegments = [.. path.Path.Split(new[] { PathSeparator }, StringSplitOptions.RemoveEmptyEntries)];
IEnumerable<PathSegment> segments = splitSegments.Select((segment, index) =>
IEnumerable<ErrorOr<PathSegment>> segmentsResults = splitSegments.Select((segment, index) =>
{
bool isDirectory;
if (segment.Contains('.'))
isDirectory = index != splitSegments.Count - 1 || path.Path.EndsWith(PathSeparator); // check if it's the last segment or if the path ends with a '/'
else
isDirectory = true;
return new PathSegment(segment, isDirectory, isDrive: false);
}).Prepend(new PathSegment(PathSeparator.ToString(), isDirectory: false, isDrive: true)); // UNIX paths have '/' as "root drive"
return ErrorOrFactory.From(segments);
return PathSegment.Create(segment, isDirectory, isDrive: false);
}).Prepend(PathSegment.Create(PathSeparator.ToString(), isDirectory: false, isDrive: true)); // UNIX paths have '/' as "root drive"
foreach (ErrorOr<PathSegment> segment in segmentsResults)
if (segment.IsError)
return segment.Errors;
return ErrorOrFactory.From(segmentsResults.Select(segmentResult => segmentResult.Value));
}
/// <summary>
@@ -153,7 +156,7 @@ public class UnixPathStrategy : IUnixPathStrategy
// On Unix-like systems, the root is always "/"
if (path.Path.StartsWith(PathSeparator))
return new PathSegment(PathSeparator.ToString(), isDirectory: false, isDrive: true);
return PathSegment.Create(PathSeparator.ToString(), isDirectory: false, isDrive: true);
return Errors.FileManagement.InvalidPath;
}
#endregion
@@ -4,6 +4,7 @@ using Lumina.Domain.Common.Errors;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using System.Linq;
using System.Text.RegularExpressions;
@@ -91,11 +92,15 @@ public class WindowsPathStrategy : IWindowsPathStrategy
// Windows paths usually start with a drive letter and colon, e.g., "C:"
if (!path.Path.Contains(':') || !path.Path.Contains(PathSeparator) || !char.IsLetter(path.Path[0]) || path.Path[1] != ':' || path.Path[2] != PathSeparator)
return Errors.FileManagement.InvalidPath;
return ErrorOrFactory.From(GetPathSegments());
IEnumerable<PathSegment> GetPathSegments()
IEnumerable<ErrorOr<PathSegment>> getPathSegmentsResults = GetPathSegments();
foreach (ErrorOr<PathSegment> getPathSegmentsResult in getPathSegmentsResults)
if (getPathSegmentsResult.IsError)
return getPathSegmentsResult.Errors;
return ErrorOrFactory.From(getPathSegmentsResults.Select(getPathSegmentsResult => getPathSegmentsResult.Value));
IEnumerable<ErrorOr<PathSegment>> GetPathSegments()
{
// the drive segment
yield return new PathSegment(path.Path[..2], isDirectory: false, isDrive: true);
yield return PathSegment.Create(path.Path[..2], isDirectory: false, isDrive: true);
// extract the other segments
string[] segments = path.Path[3..].Split(new[] { PathSeparator }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < segments.Length; i++)
@@ -106,7 +111,7 @@ public class WindowsPathStrategy : IWindowsPathStrategy
isDirectory = i != segments.Length - 1 || path.Path.EndsWith(PathSeparator); // check if it's the last segment or if the next segment also contains a path delimiter
else
isDirectory = true;
yield return new PathSegment(segment, isDirectory, isDrive: false);
yield return PathSegment.Create(segment, isDirectory, isDrive: false);
}
}
}
@@ -164,7 +169,7 @@ public class WindowsPathStrategy : IWindowsPathStrategy
string root = path.Path[..2];
if (path.Path.Length > 2 && path.Path[2] == PathSeparator)
root += PathSeparator;
return new PathSegment(root, isDirectory: true, isDrive: true);
return PathSegment.Create(root, isDirectory: true, isDrive: true);
}
// check for UNC paths (e.g., "\\server\share")
if (path.Path.StartsWith('\\'))
@@ -177,7 +182,7 @@ public class WindowsPathStrategy : IWindowsPathStrategy
if (segments.Count >= 2)
{
string root = $@"\\{segments[0].Name}\{segments[1].Name}\";
return new PathSegment(root, isDirectory: true, isDrive: false);
return PathSegment.Create(root, isDirectory: true, isDrive: false);
}
}
return Errors.FileManagement.InvalidPath;
@@ -1,4 +1,6 @@
#region ========================================================================= USING =====================================================================================
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Models.Core;
using System.Collections.Generic;
using System.Diagnostics;
@@ -36,7 +38,7 @@ public class PathSegment : ValueObject
/// <param name="name">The name of the path segment.</param>
/// <param name="isDirectory">Value indicating if the current path segment is a file system directory or not.</param>
/// <param name="isDrive">Value indicating if the current path segment is a file system drive or not.</param>
public PathSegment(string name, bool isDirectory, bool isDrive)
private PathSegment(string name, bool isDirectory, bool isDrive)
{
Name = name;
IsDirectory = isDirectory;
@@ -45,6 +47,22 @@ public class PathSegment : ValueObject
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a new instance of the <see cref="PathSegment"/> class.
/// </summary>
/// <param name="name">The name of the path segment.</param>
/// <param name="isDirectory">Value indicating if the current path segment is a file system directory or not.</param>
/// <param name="isDrive">Value indicating if the current path segment is a file system drive or not.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully created <see cref="PathSegment"/> or an error message.
/// </returns>
public static ErrorOr<PathSegment> Create(string name, bool isDirectory, bool isDrive)
{
if (string.IsNullOrWhiteSpace(name))
return Errors.FileManagement.NameCannotBeEmpty;
return new PathSegment(name, isDirectory, isDrive);
}
/// <inheritdoc/>
public override IEnumerable<object> GetEqualityComponents()
{
@@ -53,4 +71,4 @@ public class PathSegment : ValueObject
yield return IsDrive;
}
#endregion
}
}
@@ -1,5 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileManagement.FileSystem.Queries.GetFileSysem;
using Lumina.Application.Core.FileManagement.FileSystem.Queries.GetFileSystem;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Presentation.Api.Core.Controllers.Common;
using Mediator;