Importazione e dati: «Tieni il più grande» per gruppi, righe di _febe.jsonl mai perse, ricostruzione robusta (import-1, import-2, library-1, journal-1, rebuild-1, rebuild-2, rebuild-3, backup-1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-14 00:53:10 +00:00
1 parent 78c92eb2a4
commit 628fe5db2b
13 files changed
+1145 -95

No files matched your search

+56 -10
View File
@@ -67,6 +67,7 @@ public sealed class ImportCopier(ImportEnvironment env)
var files = await LoadFilesAsync(runId, ct).ConfigureAwait(false);
var planner = await ImportPlanner.LoadAsync(env.Storage, runId, files, ct).ConfigureAwait(false);
var ingest = new LibraryIngest(env.Storage, env.Layout, env.Naming, env.Journal, env.Clock, env.Thumbnails, hashIndex: null, env.SetFileTimeToPublication);
await ingest.AppendPendingJournalAsync(ct).ConfigureAwait(false); // lines a busy _febe.jsonl refused earlier
var context = new CopyContext(run, layout, recognizers, ingest);
var total = files.Count;
var bytesTotal = files.Values.Sum(f => f.Bytes);
@@ -99,6 +100,7 @@ public sealed class ImportCopier(ImportEnvironment env)
progress?.Report(new ImportProgress(runId, ImportPhase.Copying, done, total, bytesDone, bytesTotal, file.RelativePath, new Dictionary<ImportOutcome, int>(), copied, env.Clock.Now));
}
await ingest.AppendPendingJournalAsync(CancellationToken.None).ConfigureAwait(false);
run = run with { State = ImportRunState.Completed, CompletedAt = env.Clock.Now, LastError = null };
await env.Storage.Imports.UpdateRunAsync(run, CancellationToken.None).ConfigureAwait(false);
await env.Storage.Events.AppendAsync(
@@ -226,15 +228,22 @@ public sealed class ImportCopier(ImportEnvironment env)
catch (ContentChangedException)
{
tally.Failed++;
return await SaveAsync(file with { State = ImportFileState.Failed, LastError = ImportText.NoteChanged }).ConfigureAwait(false);
return await SaveAsync(await CurrentAsync(file).ConfigureAwait(false) with { State = ImportFileState.Failed, LastError = ImportText.NoteChanged }).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
tally.Failed++;
return await SaveAsync(file with { State = ImportFileState.Failed, LastError = ImportAnalyzer.ErrorText(ex) }).ConfigureAwait(false);
return await SaveAsync(await CurrentAsync(file).ConfigureAwait(false) with { State = ImportFileState.Failed, LastError = ImportAnalyzer.ErrorText(ex) }).ConfigureAwait(false);
}
}
/// <summary>
/// The file's record as saved during the attempt (the entry it was given, so a resumed copy reuses it instead of
/// taking the file for one already in the library).
/// </summary>
private async Task<ImportFile> CurrentAsync(ImportFile file) =>
await env.Storage.Imports.GetFileAsync(file.Id, CancellationToken.None).ConfigureAwait(false) ?? file;
/// <summary>Copies one file into its destination account as an imported entry (or into an entry it completes).</summary>
private async Task<ImportFile> ImportAsync(CopyContext context, ImportFile file, Tally tally, CancellationToken ct)
{
@@ -267,7 +276,7 @@ public sealed class ImportCopier(ImportEnvironment env)
if (EntryStateRules.IsPresent(media.State) && string.Equals(media.Sha256, file.Sha256, StringComparison.Ordinal) && media.RelativePath is { } already)
{
// Stored before an interruption: only the journal line and the state are missing.
await context.Ingest.AppendJournalAsync(entry.Id, CancellationToken.None).ConfigureAwait(false);
await context.Ingest.TryAppendJournalAsync(entry.Id, CancellationToken.None).ConfigureAwait(false);
return await FinishAsync(context, file, account, entry.Id, already, null, tally).ConfigureAwait(false);
}
@@ -329,7 +338,7 @@ public sealed class ImportCopier(ImportEnvironment env)
await env.Storage.Highlights.AddItemAsync(collection.Id, entry.Id, none).ConfigureAwait(false);
}
await context.Ingest.AppendJournalAsync(entry.Id, CancellationToken.None).ConfigureAwait(false);
await context.Ingest.TryAppendJournalAsync(entry.Id, CancellationToken.None).ConfigureAwait(false);
return await FinishAsync(context, file, account, entry.Id, result.File.RelativePath, null, tally).ConfigureAwait(false);
}
@@ -444,6 +453,14 @@ public sealed class ImportCopier(ImportEnvironment env)
return await FinishAsync(context, file, (await AccountOfEntryAsync(media.EntryId, ct).ConfigureAwait(false)).Account, media.EntryId, media.RelativePath, ImportText.NoteReplaced, tally).ConfigureAwait(false);
}
// Alberto chose «keep the larger»: the file in the library now is what counts, not the one seen when the copy
// was planned. A smaller or equal file never takes the place of a larger one.
if (!ImportPlanner.IsLarger(file.Width, file.Height, file.Bytes, media.Width, media.Height, media.Bytes))
{
tally.Skipped++;
return await SaveAsync(file with { State = ImportFileState.Skipped, MatchedMediaId = media.Id, LastError = ImportText.NoteLibraryLarger }).ConfigureAwait(false);
}
var (account, entry) = await AccountOfEntryAsync(media.EntryId, ct).ConfigureAwait(false);
var folder = env.Layout.AccountFolder(account);
var source = LibraryLayout.FullPath(context.Layout.Root, file.RelativePath);
@@ -473,9 +490,19 @@ public sealed class ImportCopier(ImportEnvironment env)
ct).ConfigureAwait(false))!;
}
// From here the replacement goes to the end: a half-done replacement is worse than a late one.
// From here the replacement goes to the end: a half-done replacement is worse than a late one. The new file is
// either registered or removed again: never left in the library with nothing pointing to it.
var none = CancellationToken.None;
var onDisk = await env.FileSystem.ComputeSha256Async(saved.FullPath, none).ConfigureAwait(false);
string? onDisk = null;
try
{
onDisk = await env.FileSystem.ComputeSha256Async(saved.FullPath, none).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// Not readable: not verified.
}
if (!string.Equals(onDisk, file.Sha256, StringComparison.Ordinal))
{
TryDelete(saved.FullPath); // the new file was never registered: nothing in the library points to it
@@ -488,14 +515,19 @@ public sealed class ImportCopier(ImportEnvironment env)
var thumbnail = false;
if (file.MediaKind == MediaKind.Photo)
{
hash = PerceptualHash.ComputeFromFile(saved.FullPath);
await using (var stream = File.OpenRead(saved.FullPath))
try
{
hash = PerceptualHash.ComputeFromFile(saved.FullPath);
await using var stream = File.OpenRead(saved.FullPath);
if (ImageDecoding.ReadSize(stream) is { } size)
{
(width, height) = size;
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// Verified a moment ago: recorded without perceptual hash, with the size measured by the analysis.
}
thumbnail = TryThumbnail(saved.Sha256, () => File.OpenRead(saved.FullPath));
}
@@ -513,7 +545,21 @@ public sealed class ImportCopier(ImportEnvironment env)
media.State,
env.Clock.Now,
none).ConfigureAwait(false);
await context.Ingest.AppendJournalAsync(entry.Id, none).ConfigureAwait(false);
await context.Ingest.TryAppendJournalAsync(entry.Id, none).ConfigureAwait(false);
if (entry.InstagramId is null && string.IsNullOrWhiteSpace(entry.Code) && !string.Equals(oldRelative, saved.RelativePath, StringComparison.Ordinal))
{
// An entry without identifier or code is known in the journal by its file: the line of the old file is
// closed («delete», not remembered), so a rebuild does not bring back an entry whose file is gone.
try
{
await env.Journal.AppendDeletedAsync(folder, account, entry, [oldRelative], remember: false, none).ConfigureAwait(false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// Without this line a rebuild reports the old file as missing and does not restore it: nothing is lost.
}
}
if (hash is { } h)
{
env.HashIndex?.Add(media.Id, account.Id, h);
@@ -538,7 +584,7 @@ public sealed class ImportCopier(ImportEnvironment env)
{
thumbnails.Delete(oldSha);
}
catch (IOException)
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// A thumbnail left behind costs a few kilobytes.
}