Files
Febe/src/Febe.Web/Demo/DemoHost.cs
T
Alby96andClaude Opus 5.5 ab0d717424 E3: API di formati e fotogrammi, conversione della libreria e demo
Porta le funzioni della tappa E3 fino all'interfaccia web, così che WP4
possa costruire le schermate su indirizzi veri.

- Impostazioni, cartelle delle copie e «Copia in…» hanno i formati
  (nomi original, jpg, png, webplossless, mp4); il riepilogo dice cosa si
  converte, cosa resta com'è e se c'è perdita. «Copia di nuovo» rifà la
  copia nella sua forma esatta.
- Fotogrammi: informazioni di un video, un fotogramma (anteprima o
  scaricamento), gli istanti di una modalità, «Scarica» di uno o di uno
  zip scritto mentre parte, con il permesso dei lavori lunghi oltre 20.
  Kestrel non vede mai scritture sincrone: il piccolo descrittore che lo
  zip di .NET 10 scrive in modo sincrono parte con la scrittura seguente.
- Conversione della libreria: stato, la domanda «Converti anche i N già
  in libreria?», riprova, pausa e ripresa; il visualizzatore mostra i
  file convertiti, e le eliminazioni li contano.
- Demo: un video H.264 scritto in casa (solo macroblocchi I_PCM) per
  ogni video, una copia in PNG, un fotogramma e due conversioni; la
  conversione non parte mai.

Due prove cambiano con il comportamento: i video della demo ora si
aprono, e un file non copiato può avere il segno «solo fotogramma».

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 01:02:37 +01:00

163 lines
7.0 KiB
C#

using Febe.Core.Abstractions;
using Febe.Core.Engine;
using Febe.Core.Export;
using Febe.Core.Import;
using Febe.Core.Library;
using Febe.Core.Storage;
using Febe.Data;
using Febe.Web.Connection;
using Febe.Web.Engine;
using Febe.Web.Library;
using Febe.Web.Native;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace Febe.Web.Demo;
/// <summary>Options of <c>--demo</c>.</summary>
/// <param name="Port">Port on 127.0.0.1 (8480 by default; 0 picks a free one).</param>
/// <param name="Zone">Alberto's time zone for the demo (the system zone by default).</param>
/// <param name="Root">Parent of the temporary folder (the system temporary folder by default).</param>
/// <param name="InterfaceFolder">Development: serve the interface from this folder (see <see cref="FebeWebOptions.InterfaceFolder"/>).</param>
public sealed record DemoOptions(int Port = FebeListen.DefaultPort, TimeZoneInfo? Zone = null, string? Root = null, string? InterfaceFolder = null);
/// <summary>
/// Demo mode (spec §10.6): starts without browser and network, listening only on 127.0.0.1 and without password, with
/// a temporary Data folder and a temporary library seeded with coherent demo data. It cannot write to the real
/// library: every path lives under a new temporary folder, removed when the demo ends.
/// </summary>
public sealed class DemoHost : IAsyncDisposable
{
private readonly FebeDatabase _database;
private readonly ImportService _import;
private DemoHost(string root, FebeDatabase database, ImportService import, WebApplication app, DemoWorld world, Uri address)
{
Root = root;
_database = database;
_import = import;
App = app;
World = world;
Address = address;
}
/// <summary>The temporary folder of this demo (Data and library inside).</summary>
public string Root { get; }
public WebApplication App { get; }
public DemoWorld World { get; }
/// <summary>Where the interface answers, e.g. http://127.0.0.1:8480/.</summary>
public Uri Address { get; }
public IFebeStorage Storage => _database;
public static async Task<DemoHost> StartAsync(DemoOptions options, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(options);
var root = Path.Combine(options.Root ?? Path.GetTempPath(), "febe-dimostrazione-" + Guid.NewGuid().ToString("N")[..10]);
Directory.CreateDirectory(root);
FebeDatabase? database = null;
ImportService? import = null;
try
{
var clock = new DemoClock(options.Zone ?? TimeZoneInfo.Local, DateTimeOffset.UtcNow);
var data = new DataFolderLayout(Path.Combine(root, "dati"));
var layout = new LibraryLayout(Path.Combine(root, "libreria"));
Directory.CreateDirectory(data.Root);
Directory.CreateDirectory(layout.Root);
database = await FebeDatabase.OpenAsync(data.Root, ct).ConfigureAwait(false);
await database.Settings.SetAsync(SettingKeys.LibraryRoot, layout.Root, ct).ConfigureAwait(false);
var world = await DemoSeeder.SeedAsync(database, data, layout, clock, ct).ConfigureAwait(false);
import = await DemoImport.PrepareAsync(root, database, world, layout, data, clock, ct).ConfigureAwait(false);
await DemoExport.SeedAsync(root, database, world, layout, data, clock, ct).ConfigureAwait(false);
var exportRoot = DemoExport.Root(root);
var demoImport = import;
var library = new FebeLibrary(database, data, layout, clock);
var web = new FebeWebOptions
{
Edition = FebeEdition.Desktop,
Access = AccessMode.None,
AllowedHosts = FebeWebOptions.LoopbackHosts(options.Port),
Demo = true,
InterfaceFolder = options.InterfaceFolder,
};
var db = database;
var app = FebeWebHost.Build(web, FebeListen.Loopback(options.Port), services =>
{
services.AddSingleton<IFebeStorage>(db);
services.AddSingleton(library);
services.AddSingleton<IClock>(clock);
services.AddSingleton(world);
services.AddSingleton<DemoEngine>();
services.AddSingleton<IEngineMonitor>(sp => sp.GetRequiredService<DemoEngine>());
services.AddSingleton<IEngineControl>(sp => sp.GetRequiredService<DemoEngine>());
services.AddSingleton<Febe.Core.Collection.Search.IProfileSearch>(sp => new DemoProfileSearch(db, clock, sp.GetRequiredService<IHttpContextAccessor>()));
services.AddSingleton(sp => new DemoShell(sp.GetRequiredService<IHttpContextAccessor>(), root));
services.AddSingleton<INativeShell>(sp => sp.GetRequiredService<DemoShell>());
services.AddSingleton<IBrowserWindow>(sp => sp.GetRequiredService<DemoShell>());
services.AddSingleton<IMediaFileResolver, DemoFileResolver>();
services.AddSingleton<IConversionFileResolver, DemoConversionFileResolver>();
services.AddSingleton<IConfigurationProblems, DemoConfigurationProblems>();
services.AddSingleton<IConnectionService, DemoConnectionService>();
services.AddSingleton<IBrowserPreview, DemoBrowserPreview>();
services.AddSingleton<IIncidentHistory, DemoIncidentHistory>();
services.AddSingleton<IDiagnosticSource, DemoDiagnosticSource>();
services.AddSingleton<IDataFolderChange, DemoDataFolderChange>();
services.AddSingleton(demoImport);
services.AddSingleton(exportRoot);
services.AddSingleton<IExportDisk, DemoExportDisk>();
services.AddSingleton<IEngineSessionState>(NoEngineSession.Instance);
});
await app.StartAsync(ct).ConfigureAwait(false);
return new DemoHost(root, database, import, app, world, FebeWebHost.BoundAddress(app));
}
catch
{
if (import is not null)
{
await import.DisposeAsync().ConfigureAwait(false);
}
if (database is not null)
{
await database.DisposeAsync().ConfigureAwait(false);
}
TryDelete(root);
throw;
}
}
public async ValueTask DisposeAsync()
{
try
{
await App.StopAsync().ConfigureAwait(false);
}
finally
{
await App.DisposeAsync().ConfigureAwait(false);
await _import.DisposeAsync().ConfigureAwait(false);
await _database.DisposeAsync().ConfigureAwait(false);
TryDelete(Root);
}
}
private static void TryDelete(string root)
{
try
{
Directory.Delete(root, recursive: true);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
}