Interfaccia, parte 2: Attività, Impostazioni, collegamento, Importa, doppioni, primo avvio ed errori di configurazione
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
90 files changed
+7825
-49
No files matched your search
@@ -0,0 +1,140 @@
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Import;
|
||||
using Febe.Core.Library;
|
||||
using Febe.Core.Storage;
|
||||
using Febe.Web.Library;
|
||||
|
||||
namespace Febe.Web.Import;
|
||||
|
||||
/// <summary>
|
||||
/// The import of the web host (spec §9, mockup 09). When the composition registers its own <see cref="ImportService"/>
|
||||
/// that one is used as it is; otherwise the hub builds one over the library of the web host (same thumbnails, journal
|
||||
/// and hash index), with the sources of the shell (Docker: <see cref="FebeWebOptions.ImportRoot"/>, read-only; desktop:
|
||||
/// any folder), and builds it again when the library settings change while no job runs.
|
||||
/// Library changes of a finished copy reach the open pages as <c>libreria</c> events.
|
||||
/// </summary>
|
||||
public sealed class ImportHub : IAsyncDisposable
|
||||
{
|
||||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||||
private readonly IFebeStorage? _storage;
|
||||
private readonly FebeLibrary? _library;
|
||||
private readonly IClock _clock;
|
||||
private readonly FebeWebOptions? _options;
|
||||
private readonly LibraryChanges _changes;
|
||||
private readonly bool _owned;
|
||||
private ImportService? _service;
|
||||
private bool _stale;
|
||||
|
||||
/// <summary>Uses a service built by the composition (never rebuilt nor disposed here).</summary>
|
||||
public ImportHub(ImportService service, IClock clock, LibraryChanges changes, string? sourceRoot = null)
|
||||
{
|
||||
_service = service ?? throw new ArgumentNullException(nameof(service));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
_changes = changes ?? throw new ArgumentNullException(nameof(changes));
|
||||
SourceRoot = sourceRoot ?? service.Sources.Root;
|
||||
_service.ProgressChanged += OnProgress;
|
||||
}
|
||||
|
||||
/// <summary>Builds its own service over the library of the web host.</summary>
|
||||
public ImportHub(IFebeStorage storage, FebeLibrary library, IClock clock, FebeWebOptions options, LibraryChanges changes)
|
||||
{
|
||||
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
|
||||
_library = library ?? throw new ArgumentNullException(nameof(library));
|
||||
_clock = clock ?? throw new ArgumentNullException(nameof(clock));
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
_changes = changes ?? throw new ArgumentNullException(nameof(changes));
|
||||
_owned = true;
|
||||
SourceRoot = options.Edition == FebeEdition.Docker ? options.ImportRoot : null;
|
||||
}
|
||||
|
||||
/// <summary>Docker: the root of the sources (the page lists its subfolders). Null: any folder (native picker).</summary>
|
||||
public string? SourceRoot { get; }
|
||||
|
||||
/// <summary>The service, built at the first use.</summary>
|
||||
public async Task<ImportService> GetAsync(CancellationToken ct = default)
|
||||
{
|
||||
if (_service is { } ready && !_stale)
|
||||
{
|
||||
return ready;
|
||||
}
|
||||
|
||||
await _gate.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
if (_service is not null && _stale && _owned && _service.ActiveRunId is null)
|
||||
{
|
||||
_service.ProgressChanged -= OnProgress;
|
||||
await _service.DisposeAsync().ConfigureAwait(false);
|
||||
_service = null;
|
||||
}
|
||||
|
||||
_stale = false;
|
||||
if (_service is null)
|
||||
{
|
||||
var service = await BuildAsync(ct).ConfigureAwait(false);
|
||||
service.ProgressChanged += OnProgress;
|
||||
await service.RecoverAsync(ct).ConfigureAwait(false);
|
||||
_service = service;
|
||||
}
|
||||
|
||||
return _service;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The library settings changed (folder, naming): the next use builds a new service when none is working.</summary>
|
||||
public void LibrarySettingsChanged()
|
||||
{
|
||||
if (_owned)
|
||||
{
|
||||
_stale = true;
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_service is { } service)
|
||||
{
|
||||
service.ProgressChanged -= OnProgress;
|
||||
if (_owned)
|
||||
{
|
||||
await service.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
_gate.Dispose();
|
||||
}
|
||||
|
||||
private async Task<ImportService> BuildAsync(CancellationToken ct)
|
||||
{
|
||||
var storage = _storage!;
|
||||
var library = _library!;
|
||||
var settings = await LibrarySettings.LoadAsync(storage.Settings, library.Layout.Root, ct).ConfigureAwait(false);
|
||||
return new ImportService(new ImportEnvironment
|
||||
{
|
||||
Storage = storage,
|
||||
Layout = library.Layout,
|
||||
Naming = settings.Naming,
|
||||
Journal = library.Journal,
|
||||
Clock = _clock,
|
||||
Thumbnails = library.Thumbnails,
|
||||
HashIndex = library.HashIndex,
|
||||
Frames = VideoFrameExtractor.TryCreate(),
|
||||
VideoProbe = FfprobeVideoMetadataProbe.TryCreate(),
|
||||
SetFileTimeToPublication = settings.SetFileTimeToPublication,
|
||||
Sources = SourceRoot is { } root ? ImportSourceOptions.Docker(root) : ImportSourceOptions.AnyFolder,
|
||||
DataFolder = library.Data.Root,
|
||||
});
|
||||
}
|
||||
|
||||
private void OnProgress(object? sender, ImportProgress progress)
|
||||
{
|
||||
if (progress.Phase == ImportPhase.Finished && progress.Copied > 0)
|
||||
{
|
||||
_changes.Publish(new LibraryChange(_clock.Now, null, "import"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user