Adds the server-side theme repository and reworks the web theme engine onto it

This commit is contained in:
BinaryAlley committed 2026-08-19 22:50:07 -07:00
1 parent 85928f7c3c
commit bf8faba24d
136 files changed
+6942 -1197

No files matched your search

@@ -192,6 +192,34 @@ public class ApiHttpClient : IApiHttpClient
return await SendRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Sends a POST request with a multipart form containing a single file to the specified <paramref name="endpoint"/> as an asynchronous operation and returns the result.
/// </summary>
/// <typeparam name="TResponse">The expected type of the response content.</typeparam>
/// <param name="endpoint">The API endpoint where the request is being sent.</param>
/// <param name="fileStream">The stream of the file to upload.</param>
/// <param name="fileName">The name of the file to upload.</param>
/// <param name="fieldName">The name of the form field carrying the file.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>The deserialized response containing the result of the POST request.</returns>
public async Task<TResponse> PostMultipartAsync<TResponse>(string endpoint, Stream fileStream, string fileName, string fieldName, CancellationToken cancellationToken = default)
{
// the file is streamed as a generic binary part, since the exact content type of the uploaded file is not known
using MultipartFormDataContent form = [];
using StreamContent fileContent = new(fileStream);
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
form.Add(fileContent, fieldName, fileName);
using HttpRequestMessage request = new(HttpMethod.Post, endpoint)
{
Content = form
};
AuthenticationHeaderValue? authenticationHeader = GetAuthenticationHeader();
if (authenticationHeader is not null)
request.Headers.Authorization = authenticationHeader;
return await SendRequestAsync<TResponse>(request, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Sends an HTTP request and deserializes the response into the specified type if the request is successful.
/// If the request fails, it attempts to deserialize the response content into a <see cref="ProblemDetailsDto"/> and throws an <see cref="ApiException"/>.
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DTO.FileSystemManagement;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -56,6 +57,18 @@ public interface IApiHttpClient
/// <returns>The deserialized response containing the result of the PUT request.</returns>
Task<TResponse> PutAsync<TResponse, TModel>(string endpoint, TModel data, CancellationToken cancellationToken = default);
/// <summary>
/// Sends a POST request with a multipart form containing a single file to the specified <paramref name="endpoint"/> as an asynchronous operation and returns the result.
/// </summary>
/// <typeparam name="TResponse">The expected type of the response content.</typeparam>
/// <param name="endpoint">The API endpoint where the request is being sent.</param>
/// <param name="fileStream">The stream of the file to upload.</param>
/// <param name="fileName">The name of the file to upload.</param>
/// <param name="fieldName">The name of the form field carrying the file.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>The deserialized response containing the result of the POST request.</returns>
Task<TResponse> PostMultipartAsync<TResponse>(string endpoint, Stream fileStream, string fileName, string fieldName, CancellationToken cancellationToken = default);
/// <summary>
/// Sends a POST request to the specified <paramref name="endpoint"/> as an asynchronous operation and returns the result.
/// </summary>
@@ -1,54 +0,0 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Configuration;
/// <summary>
/// Data transfer object for deserializing theme engine settings.
/// </summary>
[DebuggerDisplay("Section name: {SECTION_NAME}")]
public sealed class ThemeEngineOptionsDto
{
public const string SECTION_NAME = "ThemeEngine";
/// <summary>
/// Gets or sets the directory where installed theme packages are stored.
/// </summary>
public string StoragePath { get; set; } = "Themes";
/// <summary>
/// Gets or sets the file name of the persisted theme settings.
/// </summary>
public string SettingsPath { get; set; } = "theme-settings.json";
/// <summary>
/// Gets or sets the identifier of the theme selected when no valid current theme is persisted.
/// </summary>
public string DefaultThemeId { get; set; } = "lumina-default";
/// <summary>
/// Gets or sets the maximum allowed size of an uploaded theme archive, in bytes.
/// </summary>
public long MaxArchiveBytes { get; set; } = 8 * 1024 * 1024;
/// <summary>
/// Gets or sets the maximum allowed total size of an extracted theme archive, in bytes.
/// </summary>
public long MaxExpandedBytes { get; set; } = 24 * 1024 * 1024;
/// <summary>
/// Gets or sets the maximum allowed size of a single file within a theme archive, in bytes.
/// </summary>
public long MaxSingleFileBytes { get; set; } = 6 * 1024 * 1024;
/// <summary>
/// Gets or sets the maximum allowed number of entries in a theme archive.
/// </summary>
public int MaxEntries { get; set; } = 250;
/// <summary>
/// Gets or sets a value indicating whether theme templates may load script files.
/// </summary>
public bool AllowThemeScripts { get; set; }
}
@@ -1,18 +0,0 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Themes;
/// <summary>
/// Data transfer object for an installed theme.
/// </summary>
/// <param name="Manifest">The validated theme manifest.</param>
/// <param name="Info">The display metadata of the theme.</param>
/// <param name="RootPath">The absolute path of the installed theme directory.</param>
[DebuggerDisplay("Manifest name: {Manifest.Name}")]
public sealed record InstalledThemeDto(
ThemeManifestDto Manifest,
ThemeInfoDto Info,
string RootPath
);
@@ -1,17 +0,0 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Themes;
/// <summary>
/// Data transfer object for the settings persisted for the theme engine.
/// </summary>
[DebuggerDisplay("CurrentThemeId: {CurrentThemeId}")]
public sealed class PersistedThemeSettingsDto
{
/// <summary>
/// Gets or sets the unique identifier of the currently selected theme.
/// </summary>
public string CurrentThemeId { get; set; } = string.Empty;
}
@@ -1,24 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Enums.Themes;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Themes;
/// <summary>
/// Data transfer object for the metadata persisted alongside an installed theme.
/// </summary>
[DebuggerDisplay("Source: {Source}")]
public sealed class ThemeInstallationMetadataDto
{
/// <summary>
/// Gets or sets the source the theme was installed from.
/// </summary>
public ThemeInstallSource Source { get; set; }
/// <summary>
/// Gets or sets the UTC timestamp at which the theme was installed.
/// </summary>
public DateTimeOffset InstalledAtUtc { get; set; }
}
@@ -1,54 +0,0 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Themes;
/// <summary>
/// Data transfer object for the theme.json manifest of a theme package.
/// </summary>
[DebuggerDisplay("Id: {Id}, Name: {Name}")]
public sealed class ThemeManifestDto
{
/// <summary>
/// Gets or sets the supported schema version of the manifest.
/// </summary>
public int SchemaVersion { get; set; }
/// <summary>
/// Gets or sets the unique identifier of the theme, a lowercase kebab-case value.
/// </summary>
public string Id { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the display name of the theme.
/// </summary>
public string Name { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the description of the theme.
/// </summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the author of the theme.
/// </summary>
public string Author { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the version of the theme, using semantic version form.
/// </summary>
public string Version { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the preview image path relative to the theme package root, or <see langword="null"/> when the theme has no preview.
/// </summary>
public string? Preview { get; set; }
/// <summary>
/// Gets or sets the template mappings from template key to an HTML file path under templates/.
/// </summary>
public Dictionary<string, string> Templates { get; set; } = new(StringComparer.OrdinalIgnoreCase);
}
@@ -0,0 +1,34 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Enums.Themes;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Themes;
/// <summary>
/// Data transfer object for a theme returned by the remote API.
/// </summary>
/// <param name="Id">The unique identifier of the theme.</param>
/// <param name="ThemeId">The manifest id of the theme, used by clients to reference it.</param>
/// <param name="Name">The display name of the theme.</param>
/// <param name="Description">The description of the theme.</param>
/// <param name="Author">The author of the theme.</param>
/// <param name="Version">The version of the theme, using semantic version form.</param>
/// <param name="PreviewPath">The path of the theme preview image, relative to the theme pack root, or <see langword="null"/> when the theme has no preview.</param>
/// <param name="InstallSource">The source the theme was installed from.</param>
/// <param name="IsCurrent">Whether the theme is the currently active one.</param>
/// <param name="InstalledAtUtc">The UTC timestamp at which the theme was installed.</param>
[DebuggerDisplay("ThemeId: {ThemeId}, Name: {Name}")]
public sealed record ThemeResponseDto(
Guid Id,
string ThemeId,
string Name,
string Description,
string Author,
string Version,
string? PreviewPath,
ThemeInstallSource InstallSource,
bool? IsCurrent,
DateTime InstalledAtUtc
);
@@ -0,0 +1,19 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Themes;
/// <summary>
/// Data transfer object for the theme engine settings returned by the remote API.
/// </summary>
/// <param name="MaxArchiveBytes">The maximum allowed size of a theme archive, in bytes.</param>
/// <param name="AllowThemeScripts">Whether theme templates may contain script elements and theme assets may include script files.</param>
/// <param name="DefaultThemeId">The identifier of the theme selected when no valid current theme is available.</param>
[DebuggerDisplay("MaxArchiveBytes: {MaxArchiveBytes}")]
public sealed record ThemeSettingsResponseDto(
long MaxArchiveBytes,
bool AllowThemeScripts,
string DefaultThemeId
);
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.DTO.Themes;
/// <summary>
/// Data transfer object for the template of a theme returned by the remote API.
/// </summary>
/// <param name="Theme">The theme the template belongs to.</param>
/// <param name="Template">The sanitized content of the template.</param>
[DebuggerDisplay("ThemeId: {Theme.ThemeId}")]
public sealed record ThemeTemplateResponseDto(
ThemeResponseDto Theme,
string Template
);
@@ -36,11 +36,6 @@ internal static class PresentationWebLayerConfiguration
.ValidateFluently()
.ValidateOnStart();
serviceCollection.AddOptions<ThemeEngineOptionsDto>()
.Bind(configuration.GetRequiredSection(ThemeEngineOptionsDto.SECTION_NAME))
.ValidateFluently()
.ValidateOnStart();
return serviceCollection;
}
}
@@ -301,7 +301,6 @@ public static class PresentationWebLayerServices
services.AddHttpContextAccessor();
services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = 10 * 1024 * 1024);
services.AddSingleton<ThemeSettingsStore>();
services.AddSingleton<ThemeService>();
services.AddSingleton<ThemeTemplateEngine>();
services.AddSingleton<ThemePageRenderer>();
@@ -0,0 +1,14 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.Requests.Themes;
/// <summary>
/// Represents a request to delete a theme.
/// </summary>
/// <param name="ThemeId">The manifest id of the theme to delete.</param>
[DebuggerDisplay("ThemeId: {ThemeId}")]
public record DeleteThemeRequest(
string? ThemeId
);
@@ -0,0 +1,14 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.Requests.Themes;
/// <summary>
/// Represents a request to download the archive of a theme.
/// </summary>
/// <param name="ThemeId">The manifest id of the theme to download.</param>
[DebuggerDisplay("ThemeId: {ThemeId}")]
public record GetThemeArchiveRequest(
string? ThemeId
);
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.Requests.Themes;
/// <summary>
/// Represents a request to get an asset file of a theme.
/// </summary>
/// <param name="ThemeId">The manifest id of the theme.</param>
/// <param name="Path">The asset path relative to the theme pack root.</param>
[DebuggerDisplay("ThemeId: {ThemeId}, Path: {Path}")]
public record GetThemeAssetRequest(
string? ThemeId,
string? Path
);
@@ -0,0 +1,14 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics;
#endregion
namespace Lumina.Presentation.Web.Common.Requests.Themes;
/// <summary>
/// Represents a request to set the currently active theme.
/// </summary>
/// <param name="ThemeId">The manifest id of the theme to activate.</param>
[DebuggerDisplay("ThemeId: {ThemeId}")]
public record SetCurrentThemeRequest(
string? ThemeId
);
@@ -0,0 +1,23 @@
namespace Lumina.Presentation.Web.Common.Routes;
/// <summary>
/// Class for the collection of remote API routes called by this Web application.
/// </summary>
public static partial class ApiRoutes
{
/// <summary>
/// Routes for the themes endpoints of the remote API.
/// </summary>
public static class Themes
{
public const string GET_THEMES = "themes";
public const string INSTALL_THEME = "themes";
public const string GET_THEME_SETTINGS = "themes/settings";
public const string GET_CURRENT_THEME = "themes/current";
public const string SET_CURRENT_THEME = "themes/current";
public const string GET_THEME_TEMPLATE = "themes/{themeId}/templates/{pageKey}";
public const string GET_THEME_ASSET = "themes/{themeId}/assets/{*assetPath}";
public const string GET_THEME_ARCHIVE = "themes/{themeId}/archive";
public const string DELETE_THEME = "themes/{themeId}";
}
}
@@ -0,0 +1,20 @@
namespace Lumina.Presentation.Web.Common.Routes;
/// <summary>
/// Class for the collection of routes defined in the Web application.
/// </summary>
public static partial class WebRoutes
{
/// <summary>
/// Routes for the themes pages.
/// </summary>
public static class Themes
{
public const string GET_THEMES = "{culture}/admin/themes/api-get-themes";
public const string INSTALL_THEME = "{culture}/admin/themes/api-install-theme";
public const string SET_CURRENT_THEME = "{culture}/admin/themes/api-set-current-theme";
public const string DELETE_THEME = "{culture}/admin/themes/api-delete-theme/{themeId}";
public const string DOWNLOAD_THEME = "{culture}/admin/themes/api-download-theme/{themeId}";
public const string THEME_ASSETS = "theme-assets/{themeId}/{*path}";
}
}
@@ -1,48 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DTO.Configuration;
using Lumina.Presentation.Web.Common.Primitives;
using Lumina.Presentation.Web.Common.Utilities;
using Lumina.Presentation.Web.Common.Validation;
#endregion
namespace Lumina.Presentation.Web.Common.Validators;
/// <summary>
/// Validates the needed validation rules for the ThemeEngine application configuration settings section.
/// </summary>
public class ThemeEngineOptionsDtoValidator : AbstractValidator<ThemeEngineOptionsDto>
{
/// <summary>
/// Initializes a new instance of the <see cref="ThemeEngineOptionsDtoValidator"/> class.
/// </summary>
public ThemeEngineOptionsDtoValidator()
{
RuleFor(themeEngineOptions => themeEngineOptions.StoragePath)
.NotEmpty()
.WithError(Error.Validation(description: "Theme storage path cannot be empty!"));
RuleFor(themeEngineOptions => themeEngineOptions.SettingsPath)
.NotEmpty()
.WithError(Error.Validation(description: "Theme settings path cannot be empty!"));
RuleFor(themeEngineOptions => themeEngineOptions.DefaultThemeId)
.NotEmpty()
.WithError(Error.Validation(description: "Default theme Id cannot be empty!"));
RuleFor(themeEngineOptions => themeEngineOptions.MaxArchiveBytes)
.GreaterThan(0)
.WithError(Error.Validation(description: "Maximum archive size must be greater than 0!"));
RuleFor(themeEngineOptions => themeEngineOptions.MaxExpandedBytes)
.GreaterThan(0)
.WithError(Error.Validation(description: "Maximum expanded size must be greater than 0!"));
RuleFor(themeEngineOptions => themeEngineOptions.MaxSingleFileBytes)
.GreaterThan(0)
.WithError(Error.Validation(description: "Maximum single file size must be greater than 0!"));
RuleFor(themeEngineOptions => themeEngineOptions.MaxEntries)
.GreaterThan(0)
.WithError(Error.Validation(description: "Maximum entries count must be greater than 0!"));
}
}