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 + +