@page "/admin/system/customisation" @using System.Text.Json @using Microsoft.AspNetCore.Authorization @using MoonCore.Blazor.FlyonUi.DataTables @using MoonCore.Blazor.FlyonUi.Helpers @using MoonCore.Helpers @using MoonCore.Models @using Moonlight.Client.Models @using Moonlight.Client.Services @using Moonlight.Shared.Http.Requests.Admin.Sys.Theme @using Moonlight.Shared.Http.Responses.Admin @attribute [Authorize(Policy = "permissions:admin.system.theme")] @inject ThemeService ThemeService @inject AlertService AlertService @inject ToastService ToastService @inject DownloadService DownloadService @inject ILogger Logger Themes
@context.Name @if (context.IsEnabled) { }
@if (!string.IsNullOrEmpty(context.DonateUrl)) { Donate } @if (!string.IsNullOrEmpty(context.UpdateUrl)) { Update } Export
Images & Logos @code { private DataTable Table; private async Task> LoadItems(PaginationOptions options) => await ThemeService.Get(options.Page, options.PerPage); private async Task Import(InputFileChangeEventArgs eventArgs) { if(eventArgs.FileCount < 1) return; var files = eventArgs.GetMultipleFiles(); var maxFileSize = ByteConverter.FromMegaBytes(1).Bytes; foreach (var file in files) { try { if (!file.Name.EndsWith(".json")) { await ToastService.Error($"Unable to import {file.Name}", "Only .json files are supported"); continue; } if (file.Size > maxFileSize) { await ToastService.Error($"Unable to import {file.Name}", "Exceeded the maximum file limit of 1MB"); continue; } await using var stream = file.OpenReadStream(maxFileSize); var themeTransfer = await JsonSerializer.DeserializeAsync(stream); stream.Close(); if (themeTransfer == null) { await ToastService.Error($"Unable to import {file.Name}", "Failed to deserialize the content"); continue; } var theme = await ThemeService.Create(new CreateThemeRequest() { Name = themeTransfer.Name, Author = themeTransfer.Author, Content = themeTransfer.Content, DonateUrl = themeTransfer.DonateUrl, UpdateUrl = themeTransfer.UpdateUrl, Version = themeTransfer.Version }); await ToastService.Success("Successfully imported theme", theme.Name); await Table.Refresh(); } catch (Exception e) { Logger.LogError(e, "An unhandled error occured while importing file {file} as theme", file.Name); } } } private async Task Export(ThemeResponse theme) { var transfer = new ThemeTransferModel() { Name = theme.Name, Author = theme.Author, Content = theme.Content, DonateUrl = theme.DonateUrl, UpdateUrl = theme.UpdateUrl, Version = theme.Version }; var json = JsonSerializer.Serialize(transfer, new JsonSerializerOptions() { WriteIndented = true }); var fileName = $"{transfer.Name.Replace(" ", string.Empty).Trim()}.json"; await DownloadService.Download(fileName, json); } private async Task Delete(ThemeResponse response) { await AlertService.ConfirmDanger( "Theme deletion", $"Do you really want to delete the theme: {response.Name}", async () => { await ThemeService.Delete(response.Id); await ToastService.Success("Successfully deleted theme"); await Table.Refresh(); } ); } }