From ee4c86990a114248a5b3a38d0ebd5df149e60604 Mon Sep 17 00:00:00 2001 From: Daniel Balk <67603460+Daniel-Balk@users.noreply.github.com> Date: Wed, 26 Apr 2023 16:41:05 +0200 Subject: [PATCH] image export + file download service --- Moonlight/App/Services/FileDownloadService.cs | 33 +++++++++++++++++++ Moonlight/Program.cs | 1 + .../Views/Admin/Servers/Images/Edit.razor | 15 +++++++++ Moonlight/wwwroot/assets/js/moonlight.js | 13 ++++++++ 4 files changed, 62 insertions(+) create mode 100644 Moonlight/App/Services/FileDownloadService.cs diff --git a/Moonlight/App/Services/FileDownloadService.cs b/Moonlight/App/Services/FileDownloadService.cs new file mode 100644 index 00000000..ad85c010 --- /dev/null +++ b/Moonlight/App/Services/FileDownloadService.cs @@ -0,0 +1,33 @@ +using System.Text; +using Microsoft.JSInterop; + +namespace Moonlight.App.Services; + +public class FileDownloadService +{ + private readonly IJSRuntime JSRuntime; + + public FileDownloadService(IJSRuntime jsRuntime) + { + JSRuntime = jsRuntime; + } + + public async Task DownloadStream(string fileName, Stream stream) + { + using var streamRef = new DotNetStreamReference(stream); + + await JSRuntime.InvokeVoidAsync("moonlight.downloads.downloadStream", fileName, streamRef); + } + + public async Task DownloadBytes(string fileName, byte[] bytes) + { + var ms = new MemoryStream(bytes); + + await DownloadStream(fileName, ms); + } + + public async Task DownloadString(string fileName, string content) + { + await DownloadBytes(fileName, Encoding.UTF8.GetBytes(content)); + } +} \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 8339cb49..33f4900d 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -105,6 +105,7 @@ namespace Moonlight builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Moonlight/Shared/Views/Admin/Servers/Images/Edit.razor b/Moonlight/Shared/Views/Admin/Servers/Images/Edit.razor index 0f2c76bf..cfb81c5c 100644 --- a/Moonlight/Shared/Views/Admin/Servers/Images/Edit.razor +++ b/Moonlight/Shared/Views/Admin/Servers/Images/Edit.razor @@ -10,6 +10,7 @@ @inject ImageRepository ImageRepository @inject SmartTranslateService SmartTranslateService @inject ToastService ToastService +@inject FileDownloadService FileDownloadService
@@ -230,6 +231,11 @@ Cancel + +