Febe.Web: AddFebeWeb/UseFebe/FebeWebHost per le due vesti, interfaccia incorporata con IFileProvider proprio, indirizzi italiani, API JSON con contesto generato, eventi SSE, controlli di Host/Origin, gettone del desktop e password con blocco (§13.1), INativeShell e capacità per veste. Modalità --demo con 38 account e circa 18.000 elementi finti e scene del motore. Pagine Libreria, Visualizzatore, Account, Aggiungi account e Scheda account (contenuti, impostazioni con piano, cronologia); schermate in docs/schermate con le differenze dai mockup; prove delle API, della sicurezza, dell'esportazione CSV e dei dati dimostrativi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
158 lines
5.5 KiB
C#
158 lines
5.5 KiB
C#
using System.Runtime.CompilerServices;
|
|
using System.Threading.Channels;
|
|
using Febe.Core.Abstractions;
|
|
using Febe.Core.Collection.Downloads;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Library;
|
|
using Febe.Core.Storage;
|
|
|
|
namespace Febe.Web.Library;
|
|
|
|
/// <summary>
|
|
/// The library as the web layer uses it: Data folder, library folder, thumbnails, journal and deletion. The library
|
|
/// folder can change at run time (Impostazioni, Generali): <see cref="UseLibraryRoot"/> swaps the layout.
|
|
/// </summary>
|
|
public sealed class FebeLibrary
|
|
{
|
|
private readonly IFebeStorage _storage;
|
|
private readonly IClock _clock;
|
|
private readonly PerceptualHashIndex? _index;
|
|
private volatile LibraryLayout _layout;
|
|
|
|
public FebeLibrary(IFebeStorage storage, DataFolderLayout data, LibraryLayout layout, IClock clock, PerceptualHashIndex? index = null)
|
|
{
|
|
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
|
|
Data = data ?? throw new ArgumentNullException(nameof(data));
|
|
_layout = layout ?? throw new ArgumentNullException(nameof(layout));
|
|
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
|
_index = index;
|
|
Thumbnails = new ThumbnailStore(data.Thumbnails);
|
|
Journal = new AccountJournal(clock);
|
|
}
|
|
|
|
public DataFolderLayout Data { get; }
|
|
|
|
public LibraryLayout Layout => _layout;
|
|
|
|
public ThumbnailStore Thumbnails { get; }
|
|
|
|
public AccountJournal Journal { get; }
|
|
|
|
/// <summary>«Elimina dalla libreria», on Alberto's explicit choice only (rule 12).</summary>
|
|
public LibraryDeletion Deletion => new(_storage, _layout, Journal, _clock, Thumbnails, _index);
|
|
|
|
/// <summary>The library folder changed: new files go there; existing accounts keep their stored folders.</summary>
|
|
public void UseLibraryRoot(string root) => _layout = new LibraryLayout(root, _layout.MaxPathLength);
|
|
}
|
|
|
|
/// <summary>Where the file of a media record is on disk. The demo maps its shared sample files; the real one uses the layout.</summary>
|
|
public interface IMediaFileResolver
|
|
{
|
|
/// <summary>Full path of an existing file, or null when the file is not on disk.</summary>
|
|
string? Resolve(Account account, Media media);
|
|
}
|
|
|
|
/// <summary>Files under the account folders of the library (spec §4.7).</summary>
|
|
public sealed class LibraryFileResolver(FebeLibrary library) : IMediaFileResolver
|
|
{
|
|
public string? Resolve(Account account, Media media)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
ArgumentNullException.ThrowIfNull(media);
|
|
if (media.RelativePath is null || !EntryStateRules.IsPresent(media.State))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
var path = library.Layout.FullPath(account, media.RelativePath);
|
|
return File.Exists(path) ? path : null;
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Something changed in the library: the open pages reload what they show.</summary>
|
|
/// <param name="At">When.</param>
|
|
/// <param name="AccountId">The account concerned, when there is only one.</param>
|
|
/// <param name="Reason">Short machine reason: "stored", "deleted", "account", "import".</param>
|
|
public sealed record LibraryChange(DateTimeOffset At, long? AccountId, string Reason);
|
|
|
|
/// <summary>
|
|
/// Library notifications for the server-sent events (<c>/api/eventi</c>, event <c>libreria</c>). Publishers: the web
|
|
/// layer itself (deletions, account changes), the download queue (<see cref="Attach"/>) and the import (M7).
|
|
/// Slow subscribers lose the oldest notifications.
|
|
/// </summary>
|
|
public sealed class LibraryChanges
|
|
{
|
|
private readonly Lock _gate = new();
|
|
private readonly List<Channel<LibraryChange>> _subscribers = [];
|
|
|
|
public void Publish(LibraryChange change)
|
|
{
|
|
Channel<LibraryChange>[] subscribers;
|
|
lock (_gate)
|
|
{
|
|
subscribers = [.. _subscribers];
|
|
}
|
|
|
|
foreach (var subscriber in subscribers)
|
|
{
|
|
subscriber.Writer.TryWrite(change);
|
|
}
|
|
}
|
|
|
|
/// <summary>Publishes a change for every file the download queue stores.</summary>
|
|
public IDisposable Attach(DownloadQueue queue, IClock clock)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(queue);
|
|
ArgumentNullException.ThrowIfNull(clock);
|
|
void Handler(object? sender, DownloadProgress progress)
|
|
{
|
|
if (progress.Step == DownloadStep.Stored)
|
|
{
|
|
Publish(new LibraryChange(clock.Now, progress.AccountId, "stored"));
|
|
}
|
|
}
|
|
|
|
queue.Progress += Handler;
|
|
return new Detach(() => queue.Progress -= Handler);
|
|
}
|
|
|
|
public async IAsyncEnumerable<LibraryChange> SubscribeAsync([EnumeratorCancellation] CancellationToken ct = default)
|
|
{
|
|
var channel = Channel.CreateBounded<LibraryChange>(new BoundedChannelOptions(256) { FullMode = BoundedChannelFullMode.DropOldest, SingleReader = true });
|
|
lock (_gate)
|
|
{
|
|
_subscribers.Add(channel);
|
|
}
|
|
|
|
try
|
|
{
|
|
while (await channel.Reader.WaitToReadAsync(ct).ConfigureAwait(false))
|
|
{
|
|
while (channel.Reader.TryRead(out var change))
|
|
{
|
|
yield return change;
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_subscribers.Remove(channel);
|
|
}
|
|
}
|
|
}
|
|
|
|
private sealed class Detach(Action action) : IDisposable
|
|
{
|
|
public void Dispose() => action();
|
|
}
|
|
}
|