E3: conversione della libreria in sottofondo con righe sempre vere

La libreria può ora tenere accanto a ogni originale una copia nel
formato scelto, preparata in sottofondo senza mai sostituire o
cancellare l'originale.

- LongWorkGate ha un permesso di sottofondo che il lavoro in primo
  piano (importazione, copie, ricostruzione) interrompe; il
  salvataggio in corso si aspetta al più CommitWait.
- La coda e il lavoratore: nuovi file accodati all'arrivo, quelli già
  in libreria solo su richiesta, pausa, nuovo tentativo, attesa
  durante una sessione del motore o con poco spazio, nomi liberi e
  adozione di un file identico già presente.
- Le righe di media_conversion restano vere in ogni via: eliminazione
  (Ricorda e Consenti il ritorno, con RemoveWithConversionsAsync per
  il vincolo RESTRICT), perdente del ripasso dei doppioni, file
  cambiato (superate e riaccodate), ricostruzione (conta e riconosce
  i convertiti, accoda solo Pending), importazione (una copia
  convertita o un fotogramma non rientra come file nuovo).
- FebeApp avvia il lavoratore con InBackground; la demo mai.
  _febe.jsonl non cambia.

La prova RecordsCannotBeDeletedFromUnderTheirCopies usa ora una riga
Converted: RemoveAsync toglie da sé le righe senza file, e il vincolo
si verifica sia in SQL sia attraverso il magazzino.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-27 23:57:48 +01:00
1 parent b746aff45d
commit 724c5bda6f
25 files changed
+2621 -38

No files matched your search

+29
View File
@@ -7,6 +7,9 @@ namespace Febe.Core.Import;
/// <summary>What the comparison decided for one file.</summary>
internal sealed record Comparison(ImportOutcome Outcome, long? MatchedMediaId, DuplicatePair? Pair, string? Note);
/// <summary>A file of the source that is a converted copy of a library file; its original when known.</summary>
internal sealed record ConvertedCopyMatch(long? MediaId);
/// <summary>
/// The comparison with the library (spec §9.3), for one analysis session. In this order:
/// <list type="number">
@@ -39,6 +42,9 @@ internal sealed class ImportComparer
private readonly Dictionary<string, long> _sourceIdentity = new(StringComparer.Ordinal);
private readonly PerceptualHashIndex _sourceHashes = new();
// The SHA-256 of the converted copies and frames written in destinations (one read per run, tappa E3, Q37).
private IReadOnlySet<string> _copyShas = new HashSet<string>(StringComparer.Ordinal);
private ImportComparer(IFebeStorage storage, PerceptualHashIndex library, Func<DateTimeOffset> now)
{
_storage = storage;
@@ -69,6 +75,7 @@ internal sealed class ImportComparer
}
var comparer = new ImportComparer(storage, library, now);
comparer._copyShas = await storage.Exports.LoadConvertedCopyShasAsync(ct).ConfigureAwait(false);
var identifiers = await storage.Imports.LoadLibraryIdentifiersAsync(ct).ConfigureAwait(false);
foreach (var entry in identifiers.Entries)
{
@@ -141,6 +148,13 @@ internal sealed class ImportComparer
return new Comparison(ImportOutcome.AlreadyInLibrary, deleted.Media.Id, null, ImportText.NoteDeletedByAlberto);
}
// A converted file of the library, or a converted copy or a frame of a destination: already in the library as
// its original, never a probable duplicate of it (tappa E3, Q37).
if (await IsConvertedCopyAsync(_storage, _copyShas, file.Sha256, ct).ConfigureAwait(false) is { } converted)
{
return new Comparison(ImportOutcome.AlreadyInLibrary, converted.MediaId, null, ImportText.NoteConvertedCopy);
}
// 2. Same bytes earlier in the source.
if (_sourceSha.TryGetValue(file.Sha256, out var twin) && twin.Id != file.Id)
{
@@ -177,6 +191,21 @@ internal sealed class ImportComparer
return new Comparison(ImportOutcome.New, null, null, null);
}
/// <summary>
/// Whether <paramref name="sha256"/> is the SHA-256 of a converted file of the library (with its original's media
/// id) or of a converted copy or frame written in a destination (media id null); null when it is neither.
/// </summary>
internal static async Task<ConvertedCopyMatch?> IsConvertedCopyAsync(IFebeStorage storage, IReadOnlySet<string> copyShas, string sha256, CancellationToken ct)
{
var conversions = await storage.Conversions.FindBySha256Async(sha256, ct).ConfigureAwait(false);
if (conversions.Count > 0)
{
return new ConvertedCopyMatch(conversions[0].MediaId);
}
return copyShas.Contains(sha256) ? new ConvertedCopyMatch(null) : null;
}
private async Task<Comparison?> MatchIdentityAsync(ImportFile file, FileHints hints, long? target, CancellationToken ct)
{
var candidates = new List<(EntryIdentity Entry, long? MediaId)>();
+13 -1
View File
@@ -258,6 +258,14 @@ public sealed class ImportCopier(ImportEnvironment env)
return await SaveAsync(file with { State = ImportFileState.Skipped, MatchedMediaId = twin.Media.Id, LastError = ImportText.NoteAlreadyInLibrary }).ConfigureAwait(false);
}
// A converted copy of a library file written since the analysis (tappa E3, Q37): never imported as a new file.
context.CopyShas ??= await env.Storage.Exports.LoadConvertedCopyShasAsync(ct).ConfigureAwait(false);
if (file.EntryId is null && await ImportComparer.IsConvertedCopyAsync(env.Storage, context.CopyShas, file.Sha256!, ct).ConfigureAwait(false) is { } converted)
{
tally.Skipped++;
return await SaveAsync(file with { State = ImportFileState.Skipped, MatchedMediaId = converted.MediaId, LastError = ImportText.NoteConvertedCopy }).ConfigureAwait(false);
}
var account = await env.Storage.Accounts.GetAsync(file.TargetAccountId!.Value, ct).ConfigureAwait(false)
?? throw new InvalidOperationException("L'account di destinazione non esiste più: associa di nuovo la cartella.");
var hints = context.Recognizers.Recognize(new ImportFileContext(context.Layout, file.RelativePath));
@@ -790,7 +798,11 @@ public sealed class ImportCopier(ImportEnvironment env)
}
}
private sealed record CopyContext(ImportRun Run, SourceLayout Layout, ImportRecognizers Recognizers, LibraryIngest Ingest);
private sealed record CopyContext(ImportRun Run, SourceLayout Layout, ImportRecognizers Recognizers, LibraryIngest Ingest)
{
/// <summary>The SHA-256 of converted copies and frames in destinations, read once per run when first needed.</summary>
public IReadOnlySet<string>? CopyShas { get; set; }
}
private sealed class Tally
{
+3
View File
@@ -139,6 +139,9 @@ public static class ImportText
internal const string NoteLargerInSource = "Hai scelto di tenere il più grande: è un altro file della sorgente, che prende il posto di quello in libreria.";
internal const string NoteNotRecognized = "Né foto né video: ignorato.";
internal const string NoteAlreadyInLibrary = "Identico a un file già in libreria: saltato.";
/// <summary>A converted file of the library, a converted copy or a frame written in a destination (tappa E3, Q37).</summary>
internal const string NoteConvertedCopy = "È una copia convertita (o un fotogramma) di un file già in libreria.";
internal const string NoteReplaced = "Ha sostituito il file più piccolo della libreria, come hai scelto.";
internal const string NoteChanged = "Il file è cambiato dopo l'analisi: non è stato copiato. Per prenderlo avvia una nuova importazione della cartella.";
internal const string NoteNotVerified = "La copia in libreria non corrisponde all'originale: l'originale resta al suo posto. Controlla il disco della libreria.";