mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Implements file system path existence check
This commit is contained in:
1 parent
dc47386726
commit
c29d829c91
16 files changed
+452
-125
No files matched your search
@@ -0,0 +1,10 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.Responses.FileManagement;
|
||||
using Mediator;
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Query for retrieving the existence of a file system path.
|
||||
/// </summary>
|
||||
/// <param name="Path">The path for which to check the existence.</param>
|
||||
public record CheckPathExistsQuery(string Path) : IRequest<PathExistsResponse>;
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Contracts.Responses.FileManagement;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
|
||||
using Mediator;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.FileManagement.Paths.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for the query to check the existence a file system path.
|
||||
/// </summary>
|
||||
public class CheckPathExistsQueryHandler : IRequestHandler<CheckPathExistsQuery, PathExistsResponse>
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly IPathService _pathService;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CheckPathExistsQueryHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="pathService">Injected service for managing file system paths.</param>
|
||||
public CheckPathExistsQueryHandler(IPathService pathService)
|
||||
{
|
||||
_pathService = pathService;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Checks the existence of the specified file system path.
|
||||
/// </summary>
|
||||
/// <param name="request">The query containing the request.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true"/> if the specified path exists, <see langword="false"/> otherwise.
|
||||
/// </returns>
|
||||
public ValueTask<PathExistsResponse> Handle(CheckPathExistsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return ValueTask.FromResult(new PathExistsResponse(_pathService.Exists(request.Path)));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentValidation;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Core.FileManagement.Paths.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the needed validation rules for <see cref="CheckPathExistsQuery"/>.
|
||||
/// </summary>
|
||||
public class CheckPathExistsQueryValidator : AbstractValidator<CheckPathExistsQuery>
|
||||
{
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CheckPathExistsQueryValidator"/> class.
|
||||
/// </summary>
|
||||
public CheckPathExistsQueryValidator()
|
||||
{
|
||||
RuleFor(x => x.Path).NotEmpty().WithMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Contracts.Responses.FileManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a response to the inquiry about the existence of a file system path.
|
||||
/// </summary>
|
||||
/// <param name="Exists">Whether a file system path exists or not.</param>
|
||||
[DebuggerDisplay("{Exists}")]
|
||||
public record PathExistsResponse(
|
||||
bool Exists
|
||||
);
|
||||
+7
@@ -27,6 +27,13 @@ public interface IPathService
|
||||
/// <returns><see langword="true"/> if <paramref name="path"/> is a valid path, <see langword="false"/> otherwise.</returns>
|
||||
bool IsValidPath(string path);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> exists.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to be checked.</param>
|
||||
/// <returns><see langword="true"/> if <paramref name="path"/> exists, <see langword="false"/> otherwise.</returns>
|
||||
bool Exists(string path);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to combine <paramref name="path"/> with <paramref name="name"/>.
|
||||
/// </summary>
|
||||
|
||||
+13
@@ -51,6 +51,19 @@ public class PathService : IPathService
|
||||
return _platformContext.PathStrategy.IsValidPath(newPathResult.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> exists.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to be checked.</param>
|
||||
/// <returns><see langword="true"/> if <paramref name="path"/> exists, <see langword="false"/> otherwise.</returns>
|
||||
public bool Exists(string path)
|
||||
{
|
||||
ErrorOr<FileSystemPathId> newPathResult = FileSystemPathId.Create(path);
|
||||
if (newPathResult.IsError)
|
||||
return false;
|
||||
return _platformContext.PathStrategy.Exists(newPathResult.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to combine <paramref name="path"/> with <paramref name="name"/>.
|
||||
/// </summary>
|
||||
|
||||
+7
@@ -23,6 +23,13 @@ public interface IPathStrategy
|
||||
/// <returns><see langword="true"/> if <paramref name="path"/> is a valid path, <see langword="false"/> otherwise.</returns>
|
||||
bool IsValidPath(FileSystemPathId path);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> exists.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to be checked.</param>
|
||||
/// <returns><see langword="true"/> if <paramref name="path"/> exists, <see langword="false"/> otherwise.</returns>
|
||||
bool Exists(FileSystemPathId path);
|
||||
|
||||
/// <summary>
|
||||
/// Tries to combine <paramref name="path"/> with <paramref name="name"/>.
|
||||
/// </summary>
|
||||
|
||||
+26
@@ -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.IO.Abstractions;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
#endregion
|
||||
@@ -15,6 +16,10 @@ namespace Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.S
|
||||
/// </summary>
|
||||
public class UnixPathStrategy : IUnixPathStrategy
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly IFileSystem _fileSystem;
|
||||
#endregion
|
||||
|
||||
#region ==================================================================== PROPERTIES =================================================================================
|
||||
public char PathSeparator
|
||||
{
|
||||
@@ -22,6 +27,17 @@ public class UnixPathStrategy : IUnixPathStrategy
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UnixPathStrategy"/> class.
|
||||
/// </summary>
|
||||
/// <param name="fileSystem">Injected service used to interact with the local filesystem.</param>
|
||||
public UnixPathStrategy(IFileSystem fileSystem)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> is a valid path.
|
||||
@@ -41,6 +57,16 @@ public class UnixPathStrategy : IUnixPathStrategy
|
||||
return Regex.IsMatch(path.Path, pathPattern);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> exists.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to be checked.</param>
|
||||
/// <returns><see langword="true"/> if <paramref name="path"/> exists, <see langword="false"/> otherwise.</returns>
|
||||
public bool Exists(FileSystemPathId path)
|
||||
{
|
||||
return _fileSystem.Path.Exists(path.Path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to combine <paramref name="path"/> with <paramref name="name"/>.
|
||||
/// </summary>
|
||||
|
||||
+26
@@ -6,6 +6,7 @@ using Lumina.Domain.Common.Errors;
|
||||
using System.Text.RegularExpressions;
|
||||
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
|
||||
using System.Linq;
|
||||
using System.IO.Abstractions;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Strategies.Path;
|
||||
@@ -15,6 +16,10 @@ namespace Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.S
|
||||
/// </summary>
|
||||
public class WindowsPathStrategy : IWindowsPathStrategy
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly IFileSystem _fileSystem;
|
||||
#endregion
|
||||
|
||||
#region ==================================================================== PROPERTIES =================================================================================
|
||||
public char PathSeparator
|
||||
{
|
||||
@@ -22,6 +27,17 @@ public class WindowsPathStrategy : IWindowsPathStrategy
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowsPathStrategy"/> class.
|
||||
/// </summary>
|
||||
/// <param name="fileSystem">Injected service used to interact with the local filesystem.</param>
|
||||
public WindowsPathStrategy(IFileSystem fileSystem)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> is a valid path.
|
||||
@@ -40,6 +56,16 @@ public class WindowsPathStrategy : IWindowsPathStrategy
|
||||
return Regex.IsMatch(path.Path, pathPattern);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if <paramref name="path"/> exists.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to be checked.</param>
|
||||
/// <returns><see langword="true"/> if <paramref name="path"/> exists, <see langword="false"/> otherwise.</returns>
|
||||
public bool Exists(FileSystemPathId path)
|
||||
{
|
||||
return _fileSystem.Path.Exists(path.Path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to combine <paramref name="path"/> with <paramref name="name"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -109,5 +109,17 @@ public class PathController : ApiController
|
||||
PathValidResponse result = await _mediator.Send(new ValidatePathQuery(path), cancellationToken).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether <paramref name="path"/> exists or not.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to be checked.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
[HttpGet("check-path-exists")]
|
||||
public async Task<IActionResult> CheckPathExists([FromQuery, ModelBinder(typeof(UrlStringBinder))] string path, CancellationToken cancellationToken)
|
||||
{
|
||||
PathExistsResponse result = await _mediator.Send(new CheckPathExistsQuery(path), cancellationToken).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.FileManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a response to the inquiry about the existence of a file system path.
|
||||
/// </summary>
|
||||
/// <param name="Exists">Whether a file system path exists or not.</param>
|
||||
[DebuggerDisplay("{Exists}")]
|
||||
public record PathExistsModel(
|
||||
bool Exists
|
||||
);
|
||||
@@ -1,20 +1,22 @@
|
||||
@inject IJSRuntime JSRuntime
|
||||
|
||||
<input type="@Type"
|
||||
@ref="inputElement"
|
||||
style="width: @($"{Width}{(UsesPercentForWidth ? "%" : "px")}"); height: @($"{Height}{(UsesPercentForHeight ? "%" : "px")}");"
|
||||
class="enlightenment-input @CssClass"
|
||||
name="@Name"
|
||||
autocomplete="@(AutoComplete ? "on" : "off")"
|
||||
value="@Value"
|
||||
@oninput="OnInputHandlerAsync"
|
||||
@onkeypress="OnKeyPressAsync"
|
||||
@onkeydown="OnKeyDownAsync"
|
||||
placeholder="@Placeholder"
|
||||
disabled="@IsDisabled"
|
||||
readonly="@IsReadOnly"
|
||||
data-has-error="@HasError" />
|
||||
data-has-error="@HasError"
|
||||
id="@Id" />
|
||||
|
||||
@code {
|
||||
[Parameter] public string? Id { get; set; }
|
||||
[Parameter] public string Type { get; set; } = "text";
|
||||
[Parameter] public bool UsesPercentForHeight { get; set; }
|
||||
[Parameter] public bool UsesPercentForWidth { get; set; } = true;
|
||||
@@ -32,6 +34,18 @@
|
||||
[Parameter] public bool IsReadOnly { get; set; }
|
||||
[Parameter] public bool HasError { get; set; }
|
||||
|
||||
private ElementReference inputElement;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the event risen after the component was rendered.
|
||||
/// </summary>
|
||||
/// <param name="firstRender">Indicates whether this is the first render or not.</param>
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
await JSRuntime.InvokeVoidAsync("windowKeyDownHandler", inputElement, DotNetObjectReference.Create(this));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the input change event.
|
||||
/// </summary>
|
||||
@@ -42,23 +56,22 @@
|
||||
await ValueChanged.InvokeAsync(Value).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the key press event.
|
||||
/// </summary>
|
||||
/// <param name="e">The event arguments.</param>
|
||||
private async Task OnKeyPressAsync(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Key == "Enter")
|
||||
await OnEnterPressed.InvokeAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the key down event.
|
||||
/// </summary>
|
||||
/// <param name="e">The event arguments.</param>
|
||||
private async Task OnKeyDownAsync(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Key == "Escape")
|
||||
await OnEscapePressed.InvokeAsync(Value);
|
||||
if (e.Key == "Enter" || e.Code == "NumpadEnter")
|
||||
await OnEnterPressed.InvokeAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the Escape key pressed.
|
||||
/// </summary>
|
||||
[JSInvokable]
|
||||
public void EscapePressed()
|
||||
{
|
||||
OnEscapePressed.InvokeAsync(Value);
|
||||
}
|
||||
}
|
||||
+208
-94
@@ -1,13 +1,20 @@
|
||||
@using Lumina.Presentation.Web.Common.Api
|
||||
@using Lumina.Presentation.Web.Common.Enums.FileSystem
|
||||
@using Lumina.Presentation.Web.Common.Enums.Notifications
|
||||
@using Lumina.Presentation.Web.Common.Exceptions
|
||||
@using Lumina.Presentation.Web.Common.Models.Common
|
||||
@using Lumina.Presentation.Web.Common.Models.FileManagement
|
||||
@using Lumina.Presentation.Web.Common.Models.UI
|
||||
@using Lumina.Presentation.Web.Common.Services
|
||||
@using Microsoft.AspNetCore.Components
|
||||
@using System.Text.Json
|
||||
@using System.Text
|
||||
|
||||
@implements IDisposable
|
||||
|
||||
@inject IJSRuntime JSRuntime
|
||||
@inject IApiHttpClient _apiHttpClient
|
||||
@inject INotificationService _notificationService
|
||||
|
||||
<div id="file-system-browser-dialog" class="container-background above-modal">
|
||||
<div id="file-system-browser-content">
|
||||
@@ -39,12 +46,12 @@
|
||||
@if (isNavigationPathEditMode)
|
||||
{
|
||||
<EnlightenmentInput CssClass="navigator-address-bar-input" Type="text" @bind-Value="@Path" Name="navigator-address-bar-input"
|
||||
AutoComplete="true" OnEnterPressed="ExitNavigationPathEditModeAsync" OnEscapePressed="HandleNavigatorInputEscapePressed" />
|
||||
AutoComplete="true" OnEnterPressed="ExitNavigationPathEditModeAsync" OnEscapePressed="HandleNavigatorInputEscapePressed" Id="navigator-address-bar-input" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<div id="navigator-address-bar">
|
||||
<ul id="navigator-path-segments">
|
||||
<ul id="navigator-path-segments" @onclick="EnterNavigationPathEditModeAsync">
|
||||
@if (pathSegments?.Length > 0)
|
||||
{
|
||||
for (int i = 0; i < pathSegments.Length; i++)
|
||||
@@ -54,11 +61,11 @@
|
||||
<li id="path-segment-@i">
|
||||
<div class="navigator-combobox inline-block">
|
||||
<div class="shine-effect" style="top: 1px;"></div>
|
||||
<input type="checkbox" class="navigator-toggle-checkbox" id="segment-toggle-@i"
|
||||
@onchange="@(e => OnNavigatorPathSegmentCheckChangeHandler(e, localIndex))" />
|
||||
<label class="navigator-toggle" for="segment-toggle-@i">
|
||||
<span class="navigator-selected-text">@pathSegment.Path</span>
|
||||
<span class="navigator-arrow"></span>
|
||||
<input type="checkbox" class="navigator-toggle-checkbox" id="segment-toggle-@i" checked="@(expandedPathSegmentIndex == localIndex)"
|
||||
@onchange="@(e => ToggleNavigatorPathSegment(e, localIndex))" @onclick:stopPropagation />
|
||||
<label class="navigator-toggle" for="segment-toggle-@i" @onclick:stopPropagation>
|
||||
<span class="navigator-selected-text" @onclick:stopPropagation>@pathSegment.Path</span>
|
||||
<span class="navigator-arrow" @onclick:stopPropagation></span>
|
||||
</label>
|
||||
</div>
|
||||
</li>
|
||||
@@ -91,7 +98,7 @@
|
||||
<div class="treeview">
|
||||
@foreach (FileSystemTreeNodeModel node in treeViewRootNodes)
|
||||
{
|
||||
<FileSystemTreeViewNode Node="node" OnExpand="ExpandNode" OnNodeSelected="NavigateToPathAsync" selectedNodePath="@selectedNodePath" />
|
||||
<FileSystemTreeViewNode Node="node" OnExpandToggle="ToggleTreeViewNode" OnNodeSelected="NavigateToPathAsync" selectedNodePath="@selectedNodePath" />
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
@@ -182,6 +189,7 @@
|
||||
private FileSystemViewMode viewMode = FileSystemViewMode.List; // TODO: take from config
|
||||
private FileSystemIconSize iconSize;
|
||||
private Dictionary<int, List<FileSystemTreeNodeModel>> pathSegmentsSubdirectoriesCache = [];
|
||||
private DotNetObjectReference<FileSystemBrowser>? reference;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the component.
|
||||
@@ -196,30 +204,60 @@
|
||||
await JSRuntime.InvokeVoidAsync("addHorizontalScrolling", "navigator-address-bar").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the event risen after the component was rendered.
|
||||
/// </summary>
|
||||
/// <param name="firstRender">Indicates whether this is the first render or not.</param>
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
// on first render, subscribe this component to the javascript function that handles window clicks
|
||||
if (firstRender)
|
||||
{
|
||||
reference = DotNetObjectReference.Create(this);
|
||||
await JSRuntime.InvokeVoidAsync("windowClickHandler", reference);
|
||||
}
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the window click event.
|
||||
/// </summary>
|
||||
/// <param name="e">The event arguments.</param>
|
||||
[JSInvokable]
|
||||
public void OnWindowClick(dynamic e)
|
||||
{
|
||||
// parse the event args and get the initiator of the event
|
||||
var jsonElement = (JsonElement)e;
|
||||
string? sender = jsonElement.GetProperty("sender").GetString();
|
||||
// ignore the event if it was initiated by a path segment (don't collapse an element that was just expanded)
|
||||
if (showPathSegmentDowpDown && sender is not null && !sender.Contains("segment-toggle-"))
|
||||
{
|
||||
// otherwise, collapse all path segments
|
||||
showPathSegmentDowpDown = false;
|
||||
expandedPathSegmentIndex = null;
|
||||
}
|
||||
// exit navigator path edit mode when clicking anywhere outside of it, or on elements that should not exit it
|
||||
if (sender is null || (sender is not null && !sender.Contains("navigator-path-segments") && !sender.Contains("navigator-edit-icon") && !sender.Contains("navigator-address-bar-input")))
|
||||
isNavigationPathEditMode = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Expands or collapses a node in the tree view.
|
||||
/// </summary>
|
||||
/// <param name="node">The node to expand or collapse.</param>
|
||||
private async Task ExpandNode(FileSystemTreeNodeModel node)
|
||||
private async Task ToggleTreeViewNode(FileSystemTreeNodeModel node)
|
||||
{
|
||||
if (!node.IsExpanded && !node.ChildrenLoaded)
|
||||
{
|
||||
node.Children = new List<FileSystemTreeNodeModel>();
|
||||
node.IsExpanded = true;
|
||||
StateHasChanged();
|
||||
try
|
||||
if (await CheckPathAsync(node.Path).ConfigureAwait(false))
|
||||
{
|
||||
await foreach (FileSystemTreeNodeModel? childNode in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"directories/get-tree-directories?path={Uri.EscapeDataString(node.Path)}").ConfigureAwait(false))
|
||||
try
|
||||
{
|
||||
if (childNode is not null)
|
||||
{
|
||||
node.Children.Add(childNode);
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
if (IncludeFiles)
|
||||
{
|
||||
await foreach (FileSystemTreeNodeModel? childNode in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"files/get-tree-files?path={Uri.EscapeDataString(node.Path)}").ConfigureAwait(false))
|
||||
await foreach (FileSystemTreeNodeModel? childNode in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"directories/get-tree-directories?path={Uri.EscapeDataString(node.Path)}").ConfigureAwait(false))
|
||||
{
|
||||
if (childNode is not null)
|
||||
{
|
||||
@@ -227,15 +265,26 @@
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
if (IncludeFiles)
|
||||
{
|
||||
await foreach (FileSystemTreeNodeModel? childNode in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"files/get-tree-files?path={Uri.EscapeDataString(node.Path)}").ConfigureAwait(false))
|
||||
{
|
||||
if (childNode is not null)
|
||||
{
|
||||
node.Children.Add(childNode);
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ApiException ex)
|
||||
{
|
||||
HandleApiException(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
node.ChildrenLoaded = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error expanding node: {ex.Message}"); // TODO: display to the user when notification system is implemented
|
||||
}
|
||||
finally
|
||||
{
|
||||
node.ChildrenLoaded = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -243,6 +292,66 @@
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the change event.
|
||||
/// </summary>
|
||||
/// <param name="e">The event arguments.</param>
|
||||
private async Task ToggleNavigatorPathSegment(ChangeEventArgs e, int toggledIndex)
|
||||
{
|
||||
// uncheck all other path segment checkboxes
|
||||
await JSRuntime.InvokeVoidAsync("uncheckAllExcept", $"segment-toggle-{toggledIndex}").ConfigureAwait(false);
|
||||
// check if the checkbox is checked
|
||||
bool isChecked = (bool)e.Value!;
|
||||
// if it's checked, get the subdirectories for the toggled path segment
|
||||
if (isChecked && pathSegments?.Length > 0)
|
||||
{
|
||||
// initialize the path with the first segment
|
||||
string concatenatedPath = pathSegments[0].Path;
|
||||
// concatenate only up to the toggled index
|
||||
for (int i = 1; i <= toggledIndex; i++)
|
||||
concatenatedPath = $"{(concatenatedPath != pathSeparator ? concatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}";
|
||||
string parentConcatenatedPath = concatenatedPath;
|
||||
try
|
||||
{
|
||||
if (concatenatedPath != pathSeparator) // on UNIX, dont get the parent, if we are at root path
|
||||
{
|
||||
List<PathSegmentModel> parentLocation = await _apiHttpClient.GetAsync<List<PathSegmentModel>>($"path/get-path-parent?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false);
|
||||
parentConcatenatedPath = parentLocation[0].Path;
|
||||
for (int i = 1; i < parentLocation.Count; i++)
|
||||
parentConcatenatedPath = $"{(parentConcatenatedPath != pathSeparator ? parentConcatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}";
|
||||
}
|
||||
}
|
||||
catch (ApiException ex)
|
||||
{
|
||||
HandleApiException(ex);
|
||||
}
|
||||
if (!concatenatedPath.EndsWith(pathSeparator))
|
||||
concatenatedPath += pathSeparator;
|
||||
// fetch directories for the expanded path segment
|
||||
List<FileSystemTreeNodeModel> directories = [
|
||||
new() { Path = concatenatedPath, Name = ".", }, // current directory
|
||||
new() { Path = parentConcatenatedPath, Name = ".." } // parent directory
|
||||
];
|
||||
// add the rest of the subdirectories of the expanded path node
|
||||
await foreach (FileSystemTreeNodeModel? directory in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"directories/get-tree-directories?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false))
|
||||
if (directory is not null)
|
||||
directories.Add(directory);
|
||||
// store the directories for this segment
|
||||
pathSegmentsSubdirectoriesCache[toggledIndex] = directories;
|
||||
expandedPathSegmentIndex = toggledIndex;
|
||||
showPathSegmentDowpDown = true;
|
||||
StateHasChanged();
|
||||
// invoke the javascript function that positions the dropdown underneath the toggled path segment
|
||||
await JSRuntime.InvokeVoidAsync("updateDropdownPosition", $"path-segment-{toggledIndex}").ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
expandedPathSegmentIndex = null;
|
||||
showPathSegmentDowpDown = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the selection of a node in the tree view.
|
||||
/// </summary>
|
||||
@@ -254,10 +363,11 @@
|
||||
{
|
||||
_path = path;
|
||||
await GetNavigatorPathAsync(path).ConfigureAwait(false);
|
||||
await GetFileSystemTreeViewAsync(path).ConfigureAwait(false);
|
||||
await GetFileSystemTreeAsync(path).ConfigureAwait(false);
|
||||
await GetFileSystemItemsAsync(path).ConfigureAwait(false);
|
||||
Console.WriteLine($"Selected node: {path}");
|
||||
}
|
||||
else
|
||||
Path = _path;
|
||||
await OnNodeSelected.InvokeAsync(path).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -279,13 +389,27 @@
|
||||
/// <returns><see langword="true"/> if the specified path is valid, <see langword="false"/> otherwise.</returns>
|
||||
private async Task<bool> CheckPathAsync(string path)
|
||||
{
|
||||
var isPathValidResponse = await _apiHttpClient.GetAsync<PathValidModel>($"path/validate?path={Uri.EscapeDataString(path)}").ConfigureAwait(false);
|
||||
if (!isPathValidResponse.IsValid)
|
||||
try
|
||||
{
|
||||
// TODO: display error message to user
|
||||
Console.WriteLine("specified path is not valid!");
|
||||
var isPathValidResponse = await _apiHttpClient.GetAsync<PathValidModel>($"path/validate?path={Uri.EscapeDataString(path)}").ConfigureAwait(false);
|
||||
if (!isPathValidResponse.IsValid)
|
||||
{
|
||||
_notificationService.Show("Specified path is not valid!", NotificationType.Error); // TODO: should ask error message from server when translation is implemented
|
||||
Path = _path;
|
||||
}
|
||||
PathExistsModel pathExistsResponse = await _apiHttpClient.GetAsync<PathExistsModel>($"path/check-path-exists?path={Uri.EscapeDataString(path)}").ConfigureAwait(false);
|
||||
if (!pathExistsResponse.Exists)
|
||||
{
|
||||
_notificationService.Show("Specified path does not exist!", NotificationType.Error); // TODO: should ask error message from server when translation is implemented
|
||||
return false;
|
||||
}
|
||||
return isPathValidResponse.IsValid;
|
||||
}
|
||||
catch (ApiException ex)
|
||||
{
|
||||
HandleApiException(ex);
|
||||
return false;
|
||||
}
|
||||
return isPathValidResponse.IsValid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -302,7 +426,7 @@
|
||||
/// Gets the file system items that are displayed in the file system tree view pane.
|
||||
/// </summary>
|
||||
/// <param name="path">The path for which to get the file system items.</param>
|
||||
private async Task GetFileSystemTreeViewAsync(string path)
|
||||
private async Task GetFileSystemTreeAsync(string path)
|
||||
{
|
||||
treeViewRootNodes = [];
|
||||
// on Windows, ask the list of drives
|
||||
@@ -375,67 +499,10 @@
|
||||
}
|
||||
catch (ApiException ex)
|
||||
{
|
||||
Console.Error.WriteLine($"Error expanding node: {ex.Message}"); // TODO: display to the user when notification system is implemented
|
||||
HandleApiException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the change event.
|
||||
/// </summary>
|
||||
/// <param name="e">The event arguments.</param>
|
||||
private async Task OnNavigatorPathSegmentCheckChangeHandler(ChangeEventArgs e, int toggledIndex)
|
||||
{
|
||||
// uncheck all other path segment checkboxes
|
||||
await JSRuntime.InvokeVoidAsync("uncheckAllExcept", $"segment-toggle-{toggledIndex}").ConfigureAwait(false);
|
||||
// check if the checkbox is checked
|
||||
bool isChecked = (bool)e.Value!;
|
||||
showPathSegmentDowpDown = isChecked;
|
||||
expandedPathSegmentIndex = null;
|
||||
// if it's checked, get the subdirectories for the toggled path segment
|
||||
if (isChecked && pathSegments?.Length > 0)
|
||||
{
|
||||
// initialize the path with the first segment
|
||||
string concatenatedPath = pathSegments[0].Path;
|
||||
// concatenate only up to the toggled index
|
||||
for (int i = 1; i <= toggledIndex; i++)
|
||||
concatenatedPath = $"{(concatenatedPath != pathSeparator ? concatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}";
|
||||
string parentConcatenatedPath = concatenatedPath;
|
||||
try
|
||||
{
|
||||
if (concatenatedPath != pathSeparator) // on UNIX, dont get the parent, if we are at root path
|
||||
{
|
||||
List<PathSegmentModel> parentLocation = await _apiHttpClient.GetAsync<List<PathSegmentModel>>($"path/get-path-parent?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false);
|
||||
parentConcatenatedPath = parentLocation[0].Path;
|
||||
for (int i = 1; i < parentLocation.Count; i++)
|
||||
parentConcatenatedPath = $"{(parentConcatenatedPath != pathSeparator ? parentConcatenatedPath : string.Empty)}{pathSeparator}{pathSegments[i].Path}";
|
||||
}
|
||||
}
|
||||
catch (ApiException ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
if (!concatenatedPath.EndsWith(pathSeparator))
|
||||
concatenatedPath += pathSeparator;
|
||||
// fetch directories for the expanded path segment
|
||||
List<FileSystemTreeNodeModel> directories = [
|
||||
new() { Path = concatenatedPath, Name = ".", }, // current directory
|
||||
new() { Path = parentConcatenatedPath, Name = ".." } // parent directory
|
||||
];
|
||||
// add the rest of the subdirectories of the expanded path node
|
||||
await foreach (FileSystemTreeNodeModel? directory in _apiHttpClient.GetAsyncEnumerable<FileSystemTreeNodeModel>($"directories/get-tree-directories?path={Uri.EscapeDataString(concatenatedPath)}").ConfigureAwait(false))
|
||||
if (directory is not null)
|
||||
directories.Add(directory);
|
||||
// store the directories for this segment
|
||||
pathSegmentsSubdirectoriesCache[toggledIndex] = directories;
|
||||
expandedPathSegmentIndex = toggledIndex;
|
||||
StateHasChanged();
|
||||
// invoke the javascript function that positions the dropdown underneath the toggled path segment
|
||||
await JSRuntime.InvokeVoidAsync("updateDropdownPosition", $"path-segment-{toggledIndex}").ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the current file system explorer view.
|
||||
/// </summary>
|
||||
@@ -503,4 +570,51 @@
|
||||
{
|
||||
await JSRuntime.InvokeVoidAsync("focusNavigatorInputElement");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles API exceptions, presenting standard problem details in a user friendly way.
|
||||
/// </summary>
|
||||
/// <param name="ex">The exception to handle</param>
|
||||
private void HandleApiException(ApiException ex)
|
||||
{
|
||||
StringBuilder errorMessage = new StringBuilder();
|
||||
if (ex.ProblemDetails is not null)
|
||||
{
|
||||
errorMessage.AppendLine($"HTTP {ex.ProblemDetails.Status} {ex.HttpStatusCode}<br>");
|
||||
if (!string.IsNullOrEmpty(ex.ProblemDetails.Detail))
|
||||
errorMessage.AppendLine($"<b>{ex.ProblemDetails.Detail}<b>");
|
||||
if (ex.ProblemDetails.Extensions is not null && ex.ProblemDetails.Extensions.TryGetValue("errors", out var errorsObj))
|
||||
{
|
||||
if (errorsObj is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
errorMessage.AppendLine("Validation Errors:");
|
||||
foreach (var error in jsonElement.EnumerateObject())
|
||||
{
|
||||
string fieldName = error.Name;
|
||||
if (error.Value.ValueKind == JsonValueKind.Array)
|
||||
foreach (var errorValue in error.Value.EnumerateArray())
|
||||
errorMessage.AppendLine($"- {fieldName}: {errorValue}<br>");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ex.ProblemDetails.Extensions is not null)
|
||||
foreach (var extension in ex.ProblemDetails.Extensions)
|
||||
if (extension.Key != "errors")
|
||||
errorMessage.AppendLine($"{extension.Key}: {extension.Value}<br>");
|
||||
}
|
||||
else
|
||||
{
|
||||
errorMessage.AppendLine($"HTTP {ex.HttpStatusCode}<br>");
|
||||
errorMessage.AppendLine($"{ex.Message}<br>");
|
||||
}
|
||||
_notificationService.Show(errorMessage.ToString(), NotificationType.Error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes this component.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
reference?.Dispose();
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -19,7 +19,7 @@
|
||||
<div class="child-nodes">
|
||||
@foreach (var childNode in Node.Children)
|
||||
{
|
||||
<FileSystemTreeViewNode Node="childNode" OnExpand="OnExpand" OnNodeSelected="OnNodeSelected" SelectedNodePath="@SelectedNodePath" />
|
||||
<FileSystemTreeViewNode Node="childNode" OnExpandToggle="OnExpandToggle" OnNodeSelected="OnNodeSelected" SelectedNodePath="@SelectedNodePath" />
|
||||
}
|
||||
</div>
|
||||
}
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
@code {
|
||||
[Parameter] public FileSystemTreeNodeModel Node { get; set; } = null!;
|
||||
[Parameter] public EventCallback<FileSystemTreeNodeModel> OnExpand { get; set; }
|
||||
[Parameter] public EventCallback<FileSystemTreeNodeModel> OnExpandToggle { get; set; }
|
||||
[Parameter] public EventCallback<string> OnNodeSelected { get; set; }
|
||||
[Parameter] public string? SelectedNodePath { get; set; }
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
/// </summary>
|
||||
private async Task ToggleExpand()
|
||||
{
|
||||
await OnExpand.InvokeAsync(Node).ConfigureAwait(false);
|
||||
await OnExpandToggle.InvokeAsync(Node).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -132,17 +132,21 @@ window.addEventListener('resize', function () {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Handles window click events.
|
||||
* @param {MouseEvent} event - The click event triggered on the window.
|
||||
*/
|
||||
window.addEventListener('click', function (event) {
|
||||
// check if the clicked element is not a checkbox or its label
|
||||
if (!event.target.closest('.navigator-toggle-checkbox, .navigator-toggle')) {
|
||||
windowWasClicked = true;
|
||||
closeNavigatorPathSegments();
|
||||
const dropdown = document.getElementById('navigatorDropdown');
|
||||
if (dropdown)
|
||||
dropdown.style.display = 'none';
|
||||
}
|
||||
});
|
||||
window.windowClickHandler = (dotnetHelper) => {
|
||||
window.addEventListener('click', (event) => {
|
||||
dotnetHelper.invokeMethodAsync('OnWindowClick', {
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
sender: event.target.getAttribute('id')
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
window.windowKeyDownHandler = (element, dotnetHelper) => {
|
||||
element.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault();
|
||||
dotnetHelper.invokeMethodAsync('EscapePressed');
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in new issue
Block a user