From 420ff46ceb504c5f95a069174129ead0c7dc1ded Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Mon, 17 Mar 2025 10:53:45 +0100 Subject: [PATCH] Improved upload progress tracking. Fixed path on frontend export --- .../Services/FrontendService.cs | 6 +- .../Implementations/SysFileSystemProvider.cs | 39 +++++---- Moonlight.Client/Moonlight.Client.csproj | 2 +- Moonlight.Client/Startup.cs | 3 + .../UI/Views/Admin/Sys/Files.razor | 13 ++- Moonlight.Client/wwwroot/index.html | 1 + Moonlight.Client/wwwroot/js/xmlHttpRequest.js | 80 +++++++++++++++++++ 7 files changed, 120 insertions(+), 24 deletions(-) create mode 100644 Moonlight.Client/wwwroot/js/xmlHttpRequest.js diff --git a/Moonlight.ApiServer/Services/FrontendService.cs b/Moonlight.ApiServer/Services/FrontendService.cs index e3a00647..8c2fc3be 100644 --- a/Moonlight.ApiServer/Services/FrontendService.cs +++ b/Moonlight.ApiServer/Services/FrontendService.cs @@ -91,7 +91,7 @@ public class FrontendService if (wasmMainFile is NotFoundFileInfo || string.IsNullOrEmpty(wasmMainFile.PhysicalPath)) throw new HttpApiException("Unable to find wasm location", 500); - var wasmPath = Path.GetDirectoryName(wasmMainFile.PhysicalPath)!; + var wasmPath = Path.GetDirectoryName(wasmMainFile.PhysicalPath)! + "/"; // Load and check the blazor framework files var blazorFile = WebHostEnvironment.WebRootFileProvider.GetFileInfo("_framework/blazor.webassembly.js"); @@ -99,7 +99,7 @@ public class FrontendService if (blazorFile is NotFoundFileInfo || string.IsNullOrEmpty(blazorFile.PhysicalPath)) throw new HttpApiException("Unable to find blazor location", 500); - var blazorPath = Path.GetDirectoryName(blazorFile.PhysicalPath)!; + var blazorPath = Path.GetDirectoryName(blazorFile.PhysicalPath)! + "/"; // Create zip var memoryStream = new MemoryStream(); @@ -124,7 +124,7 @@ public class FrontendService // Add plugin wwwroot files foreach (var pluginPath in PluginService.LoadedPlugins.Values) { - var wwwRootPluginPath = Path.Combine(pluginPath, "wwwroot"); + var wwwRootPluginPath = Path.Combine(pluginPath, "wwwroot/"); if (!Directory.Exists(wwwRootPluginPath)) continue; diff --git a/Moonlight.Client/Implementations/SysFileSystemProvider.cs b/Moonlight.Client/Implementations/SysFileSystemProvider.cs index 3f4c87f8..edd3efaa 100644 --- a/Moonlight.Client/Implementations/SysFileSystemProvider.cs +++ b/Moonlight.Client/Implementations/SysFileSystemProvider.cs @@ -2,6 +2,7 @@ using MoonCore.Blazor.Tailwind.Fm; using MoonCore.Blazor.Tailwind.Fm.Models; using MoonCore.Blazor.Tailwind.Services; +using MoonCore.Blazor.Tailwind.Xhr; using MoonCore.Helpers; using Moonlight.Shared.Http.Requests.Admin.Sys.Files; using Moonlight.Shared.Http.Responses.Admin.Sys; @@ -13,6 +14,7 @@ public class SysFileSystemProvider : IFileSystemProvider, ICompressFileSystemPro private readonly DownloadService DownloadService; private readonly HttpApiClient HttpApiClient; private readonly LocalStorageService LocalStorageService; + private readonly XmlHttpClient XmlHttpClient; private readonly string BaseApiUrl = "api/admin/system/files"; public CompressType[] CompressTypes { get; } = @@ -29,11 +31,12 @@ public class SysFileSystemProvider : IFileSystemProvider, ICompressFileSystemPro } ]; - public SysFileSystemProvider(HttpApiClient httpApiClient, DownloadService downloadService, LocalStorageService localStorageService) + public SysFileSystemProvider(HttpApiClient httpApiClient, DownloadService downloadService, LocalStorageService localStorageService, XmlHttpClient xmlHttpClient) { HttpApiClient = httpApiClient; DownloadService = downloadService; LocalStorageService = localStorageService; + XmlHttpClient = xmlHttpClient; } public async Task List(string path) @@ -88,26 +91,28 @@ public class SysFileSystemProvider : IFileSystemProvider, ICompressFileSystemPro public async Task Upload(Func updateProgress, string path, Stream stream) { - var cts = new CancellationTokenSource(); + var tcs = new TaskCompletionSource(); + var accessToken = await LocalStorageService.GetString("AccessToken"); + + await using var request = await XmlHttpClient.Create(); - Task.Run(async () => + request.OnUploadProgress += async ev => { - while (!cts.IsCancellationRequested) - { - await updateProgress.Invoke(stream.Position); - await Task.Delay(TimeSpan.FromMilliseconds(500), cts.Token); - } - }); + await updateProgress.Invoke(ev.Loaded); + }; - try + request.OnLoadend += _ => { - await Create(path, stream); - } - finally - { - // Ensure we aren't creating an endless loop ^^ - await cts.CancelAsync(); - } + tcs.SetResult(); + return Task.CompletedTask; + }; + + await request.Open("POST", $"{BaseApiUrl}/create?path={path}"); + await request.SetRequestHeader("Authorization", $"Bearer {accessToken}"); + + await request.SendFile(stream, "file", "file"); + + await tcs.Task; } public async Task Compress(CompressType type, string path, string[] itemsToCompress) diff --git a/Moonlight.Client/Moonlight.Client.csproj b/Moonlight.Client/Moonlight.Client.csproj index 6fe4dea4..deb398a2 100644 --- a/Moonlight.Client/Moonlight.Client.csproj +++ b/Moonlight.Client/Moonlight.Client.csproj @@ -27,7 +27,7 @@ - +