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;
/// Capabilities of the shell, preferences of the interface, the icon sprite.
public static class SystemApi
{
/// Setting key of the theme: «chiaro», «scuro» or «sistema».
public const string ThemeKey = "interfaccia.tema";
public static readonly IReadOnlyList 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 , inlined once by the page.
var cached = new Lazy(() => 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()?.Current ?? []);
}
/// The sprite: each Lucide file's drawing inside a symbol; stroke and fill come from the CSS of svg.ic.
public static string BuildSprite(EmbeddedWwwroot files)
{
ArgumentNullException.ThrowIfNull(files);
var builder = new StringBuilder("").ToString();
}
}