Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
116 lines
5.5 KiB
C#
116 lines
5.5 KiB
C#
using System.Text;
|
|
using Febe.Core.Abstractions;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Storage;
|
|
using Febe.Web.Connection;
|
|
using Febe.Web.Demo;
|
|
using Febe.Web.Hosting;
|
|
using Febe.Web.Native;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Febe.Web.Api;
|
|
|
|
/// <summary>Capabilities of the shell, preferences of the interface, the icon sprite.</summary>
|
|
public static class SystemApi
|
|
{
|
|
/// <summary>Setting key of the theme: «chiaro», «scuro» or «sistema».</summary>
|
|
public const string ThemeKey = "interfaccia.tema";
|
|
|
|
public static readonly IReadOnlyList<string> Themes = ["chiaro", "scuro", "sistema"];
|
|
|
|
public static void Map(IEndpointRouteBuilder endpoints, EmbeddedWwwroot files)
|
|
{
|
|
endpoints.MapGet("/api/capabilities", (HttpContext context, FebeWebOptions options, INativeShell shell, IBrowserWindow browser, IClock clock) =>
|
|
ApiHttp.Json(Capabilities(context, options, shell, browser, clock), FebeJsonContext.Default.CapabilitiesDto));
|
|
|
|
endpoints.MapGet("/api/preferences", async (IFebeStorage storage, CancellationToken ct) =>
|
|
{
|
|
var theme = await storage.Settings.GetAsync(ThemeKey, ct).ConfigureAwait(false);
|
|
return ApiHttp.Json(new PreferencesDto(Themes.Contains(theme) ? theme! : "sistema"), FebeJsonContext.Default.PreferencesDto);
|
|
});
|
|
|
|
endpoints.MapPut("/api/preferences", async (HttpContext context, IFebeStorage storage, CancellationToken ct) =>
|
|
{
|
|
var body = await ApiHttp.BodyAsync(context, FebeJsonContext.Default.PreferencesDto).ConfigureAwait(false);
|
|
if (!Themes.Contains(body.Theme))
|
|
{
|
|
throw new BadRequestException("Tema sconosciuto: si sceglie fra chiaro, scuro e come il sistema.");
|
|
}
|
|
|
|
await storage.Settings.SetAsync(ThemeKey, body.Theme, ct).ConfigureAwait(false);
|
|
return ApiHttp.Json(body, FebeJsonContext.Default.PreferencesDto);
|
|
});
|
|
|
|
// Every icon of wwwroot/icone as one sprite of <symbol id="i-name">, inlined once by the page.
|
|
var cached = new Lazy<string>(() => BuildSprite(files));
|
|
string Sprite() => files.FromDisk ? BuildSprite(files) : cached.Value;
|
|
endpoints.MapGet("/icone/tutte.svg", (HttpContext context) =>
|
|
{
|
|
context.Response.Headers.CacheControl = "no-cache";
|
|
return Results.Text(Sprite(), "image/svg+xml; charset=utf-8");
|
|
});
|
|
}
|
|
|
|
public static CapabilitiesDto Capabilities(HttpContext context, FebeWebOptions options, INativeShell shell, IBrowserWindow browser, IClock clock)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(context);
|
|
ArgumentNullException.ThrowIfNull(options);
|
|
ArgumentNullException.ThrowIfNull(shell);
|
|
ArgumentNullException.ThrowIfNull(browser);
|
|
ArgumentNullException.ThrowIfNull(clock);
|
|
var edition = options.Demo ? DemoRequest.Edition(context) ?? FebeEdition.Desktop : options.Edition;
|
|
var desktop = edition == FebeEdition.Desktop;
|
|
var demoDesktop = options.Demo && desktop;
|
|
return new CapabilitiesDto(
|
|
desktop ? "desktop" : "docker",
|
|
options.Demo,
|
|
options.Access switch { AccessMode.Password => "password", AccessMode.Token => "token", _ => "none" },
|
|
ShowInFolder: demoDesktop || (desktop && shell.CanShowInFolder),
|
|
PickFolder: demoDesktop || (desktop && shell.CanPickFolder),
|
|
OpenUrlNative: !options.Demo && shell.CanOpenUrl,
|
|
FoldersReadOnly: !desktop,
|
|
Autostart: demoDesktop || (desktop && shell.SupportsAutostart),
|
|
BrowserWindow: demoDesktop || (desktop && browser.IsSeparateWindow),
|
|
Notifications: demoDesktop || shell.CanNotify,
|
|
options.Version,
|
|
options.Demo ? DemoRequest.SceneName(DemoRequest.Scene(context)) : null,
|
|
options.Demo ? DemoRequest.SceneNames : [],
|
|
clock.Now.ToUnixTimeMilliseconds(),
|
|
ApiMapping.Day(LocalCalendar.Today(clock.Now, clock.LocalZone)),
|
|
context.RequestServices.GetService<IConfigurationProblems>()?.Current ?? []);
|
|
}
|
|
|
|
/// <summary>The sprite: each Lucide file's drawing inside a symbol; stroke and fill come from the CSS of <c>svg.ic</c>.</summary>
|
|
public static string BuildSprite(EmbeddedWwwroot files)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(files);
|
|
var builder = new StringBuilder("<svg xmlns=\"http://www.w3.org/2000/svg\" aria-hidden=\"true\" style=\"display:none\">");
|
|
foreach (var path in files.Files.Where(f => f.StartsWith("icone/", StringComparison.Ordinal) && f.EndsWith(".svg", StringComparison.Ordinal)).Order(StringComparer.Ordinal))
|
|
{
|
|
var text = files.ReadText(path);
|
|
if (text is null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var open = text.IndexOf("<svg", StringComparison.Ordinal);
|
|
var openEnd = open < 0 ? -1 : text.IndexOf('>', open);
|
|
var close = text.LastIndexOf("</svg>", StringComparison.Ordinal);
|
|
if (openEnd < 0 || close <= openEnd)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var name = path["icone/".Length..^".svg".Length];
|
|
builder.Append("<symbol id=\"i-").Append(name).Append("\" viewBox=\"0 0 24 24\">")
|
|
.Append(text[(openEnd + 1)..close].Trim())
|
|
.Append("</symbol>");
|
|
}
|
|
|
|
return builder.Append("</svg>").ToString();
|
|
}
|
|
}
|