Files
Febe/src/Febe.Core/Export/ExportSupersession.cs
T
Alby96andClaude Opus 5.5 724c5bda6f 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>
2026-11-27 23:57:48 +01:00

66 lines
3.0 KiB
C#

using Febe.Core.Domain;
using Febe.Core.Library.Conversion;
using Febe.Core.Storage;
namespace Febe.Core.Export;
/// <summary>
/// «Superato» (spec §4.10): the library file of an account changed («Tieni il più grande», a slot filled again with
/// other bytes, a rebuild that found other bytes on disk), so its copies are of a file the library no longer has. The
/// copies stay where they are; they are only marked, and the new file is never copied by itself.
/// </summary>
public static class ExportSupersession
{
/// <summary>
/// Marks the copies of the account's old file as superseded by the new one. A no-op when the old SHA-256 is null or
/// equal to the new one, or when a present file of that account still has the old SHA-256. Writes are not
/// cancellable: the library already changed.
/// </summary>
public static async Task<int> FileChangedAsync(IFebeStorage storage, long accountId, string? oldSha256, string newSha256, DateTimeOffset at, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(storage);
ArgumentException.ThrowIfNullOrWhiteSpace(newSha256);
if (oldSha256 is not { Length: 64 } || newSha256.Length != 64 || string.Equals(oldSha256, newSha256, StringComparison.OrdinalIgnoreCase))
{
return 0;
}
var old = oldSha256.ToLowerInvariant();
await LibraryConversionsChangedAsync(storage, accountId, old, newSha256.ToLowerInvariant(), at).ConfigureAwait(false);
var users = await storage.Media.FindBySha256Async(old, ct).ConfigureAwait(false);
if (users.Any(u => u.Entry.AccountId == accountId && EntryStateRules.IsPresent(u.Media.State)))
{
return 0;
}
return await storage.Exports.SupersedeCopiesAsync(accountId, old, newSha256.ToLowerInvariant(), at, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// The conversions for the library of the account's file that now has <paramref name="newSha256"/> (tappa E3,
/// WP2.4): a converted file of the old bytes is «Superato» (it stays), the queue and the failures take the new bytes,
/// and the file is queued again in the library formats set now.
/// </summary>
private static async Task LibraryConversionsChangedAsync(IFebeStorage storage, long accountId, string oldSha256, string newSha256, DateTimeOffset at)
{
var none = CancellationToken.None;
var changed = await storage.Conversions.SupersedeAsync(accountId, oldSha256, newSha256, at, none).ConfigureAwait(false);
if (changed.Count == 0)
{
return;
}
var media = new List<Media>();
foreach (var id in changed)
{
if (await storage.Media.GetAsync(id, none).ConfigureAwait(false) is { } m)
{
media.Add(m);
}
}
await LibraryConversionQueue.EnqueueEntryAsync(storage, media, at, none).ConfigureAwait(false);
LibraryConversionQueue.Signal();
}
}