feat(archive): convert sequences to JPEG, TIFF-16 or linear DNG after import

Owner review 2, R7 asks for mass conversion after import on the web panel and the desktop,
with a choice at every run between Replace and New sequence. This is the engine and the
server API both UIs build on.

- Core/Conversion: ConvertOptions (JPEG 4:4:4 at quality 1-100, default 95; TIFF-16 sRGB
  Deflate with the predictor; linear DNG, the archival choice; converted names keep the
  stem and take the target's extension in the old one's case). FrameConverter decodes at
  the native tier only, encodes with the in-house writers, writes Exif from the sidecar
  (capture time to the tick, offset, exposure) and the original (camera, serial, lens,
  modes, GPS), hashes the bytes as made, writes atomically and verifies: read back, the
  metadata again, a decode by the server's decoders. ExifWriter now writes the exposure
  program, metering mode, flash and white balance. ConvertPlanner: convertible frames,
  skips with reasons, size estimates per pixel, the plan's space and fingerprint.
- FrameTrash.ReplaceAsync swaps the files of frames in place: same ids and indices, the old
  files and their XMP companions into one Replaced trash entry (records, replacement
  hashes, a copy of analysis.json), tombstones aliasing old content to new, proxies of the
  swapped frames dropped, under .chronos/replace.pending.json, which the next writer undoes
  or completes. Restoring a Replaced entry swaps back while every converted frame is
  unchanged (the converted files become a Replaced entry in turn), else the originals come
  back as a new sequence.
- MassConverter and JobKind.Convert (19): the dry run (busy, offline, unreadable, space
  against ArchiveServiceOptions.FreeSpace), stage A converting into
  staging/convert-<job>/ in parallel with each source re-hashed and each staged file
  appended to journal/convert-<job>.staged, stage B per sequence all or nothing under the
  lease at the planned frames version: Replace, or a new sequence through the ingest with
  the source's tags, rating, notes and project. Resumes keep verified staged files; a
  sequence changed, busy or with a failed photo is left as it was and fails the job at the
  end, the journal kept for a retry.
- POST /api/v1/archive/convert: dry run with the plan, its space line and fingerprint, or a
  202 convert job; formats the server cannot read at full resolution are skipped as
  convert_on_pc; validated, audited (archive_converted), sequenceChanged converted/created.
  The server measures the space volume for the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 809210d924cfc3f597ecdea26106fb3ff62d3762)
(cherry picked from commit e13d4c0bb1bd9441933b6f3e5b6220e865a7228c)
This commit is contained in:
Alby96 committed 2026-10-27 22:01:23 +01:00
1 parent d518e87733
commit 0e52929274
34 files changed
+3519 -32

No files matched your search

@@ -0,0 +1,522 @@
using System.Text.Json;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.IO;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using SequenceFrames = Chronos.Core.Archiving.SequenceFrames;
namespace Chronos.Data.Archiving;
/// <summary>
/// One frame a replacement swaps (owner review 2, R7): the file that comes in, its companions, the name it is filed under
/// and the record the frame gets.
/// </summary>
/// <param name="Incoming">The file that comes in, relative to the archive root: a staged conversion, or an original in a
/// trash entry.</param>
/// <param name="Companions">Files that come in with it (the XMP companions of an original back from the trash).</param>
/// <param name="Name">The name it is filed under, from the frame as the sidecar has it under the lease, relative to the
/// sequence folder; made unique with <c>_2</c>, <c>_3</c>… when it, or one of its companions' names, is taken.</param>
/// <param name="Record">The frame's record after the swap, from the frame as the sidecar has it under the lease (its
/// <see cref="SidecarFrame.Sha256"/> names the incoming file): the replacement keeps the frame's id and index, files it
/// under its name, and keeps the flags a user or the capture set (<see cref="FrameTrash.ContentFlags"/> come from the
/// record).</param>
internal sealed record FrameSwap(Guid FrameId, string Incoming, IReadOnlyList<IncomingCompanion> Companions, Func<SidecarFrame, string> Name,
Func<SidecarFrame, SidecarFrame> Record);
/// <summary>A companion that comes in with its photo: where it is (relative to the archive root), and what its name adds to the photo's stem.</summary>
internal sealed record IncomingCompanion(string From, string Tail);
/// <summary>
/// The trash entry a replacement takes its incoming files from (the undo of a conversion): once the sidecar is written,
/// the photos of <see cref="Frames"/> leave its record, and its copy of <c>analysis.json</c> (<see cref="Analysis"/>) goes back
/// into the sequence.
/// </summary>
/// <param name="Record">The entry's record, relative to the archive root.</param>
/// <param name="Folder">The entry's folder, relative to the archive root.</param>
/// <param name="Lease">The entry's lease, which the replacement's caller holds.</param>
/// <param name="Analysis">The entry's copy of <c>analysis.json</c>, relative to the archive root; null when it has none.</param>
internal sealed record ConsumedEntry(string Record, string Folder, string Lease, IReadOnlyList<Guid> Frames, string? Analysis);
/// <summary>One file a replacement moves: its place in the sequence folder and the other end, relative to the archive root.</summary>
/// <param name="Place">Relative to the sequence folder (forward slashes).</param>
/// <param name="Other">Relative to the archive root: the trash entry an outgoing file goes to, or where an incoming file comes from.</param>
internal sealed record SwapMove(string Place, string Other);
/// <summary>What a replacement did: the trash entry of the files that went, the sequence as it is now, and its statuses before.</summary>
internal sealed record ReplaceResult(TrashEntry Entry, Sequence Sequence, SequenceStatus Had);
/// <summary>
/// Write-ahead record of a frame replacement (<c>.chronos/replace.pending.json</c>, owner review 2, R7): the sidecar's frames
/// version before it, the new trash entry (its record pending until the sidecar is written), every file that leaves the
/// sequence for the entry and every file that comes in, whether the entry holds a copy of <c>analysis.json</c>, and the
/// entry the incoming files come from (an undo). Written under the sequence's lease before the first move and removed once
/// the replacement is settled, so the next writer of the sequence (<see cref="SequenceWriter.ReadLocked"/>) undoes one a
/// crash interrupted before its sidecar was written, and completes one interrupted after.
/// </summary>
/// <param name="Record">The new entry's record, relative to the archive root (its name once the replacement is complete).</param>
/// <param name="Folder">The new entry's folder, relative to the archive root.</param>
/// <param name="Out">Files that leave the sequence for the new entry.</param>
/// <param name="In">Files that come into the sequence.</param>
internal sealed record FrameReplaceJournal(int FramesVersion, string Record, string Folder, IReadOnlyList<SwapMove> Out, IReadOnlyList<SwapMove> In,
bool Analysis, ConsumedEntry? Consumed)
{
public void Write(SequencePaths sequence)
{
System.IO.Directory.CreateDirectory(sequence.MetaDirectory);
AtomicFile.WriteAllBytes(sequence.ReplaceJournalPath, JsonSerializer.SerializeToUtf8Bytes(this, TrashJson.Default.FrameReplaceJournal));
}
/// <summary>The journal of a sequence; null when it is torn (written by a process that died while writing it: nothing moved yet).</summary>
public static FrameReplaceJournal? Read(SequencePaths sequence)
{
try
{
return JsonSerializer.Deserialize(AtomicFile.ReadAllBytes(sequence.ReplaceJournalPath), TrashJson.Default.FrameReplaceJournal);
}
catch (JsonException)
{
return null;
}
}
public static void Delete(SequencePaths sequence)
{
if (File.Exists(sequence.ReplaceJournalPath)) File.Delete(sequence.ReplaceJournalPath);
}
}
public sealed partial class FrameTrash
{
/// <summary>
/// Flags that follow a frame's file, not the frame: a replacement takes them from the incoming record and keeps the
/// others (excluded, intervalometer, black, white) as the frame has them.
/// </summary>
internal const FrameFlags ContentFlags = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Duplicate | FrameFlags.PreviewOnly | FrameFlags.Undecodable;
/// <summary>The name of the copy of a sequence's <c>analysis.json</c> a replaced entry keeps, inside its folder.</summary>
internal const string AnalysisCopy = "analysis.json";
/// <summary>
/// Replaces the files of frames of a sequence, keeping the frames (owner review 2, R7; BRIEF §7): under the sequence's
/// lease, with its frames version still <paramref name="framesVersion"/> (null: any) and <paramref name="unchanged"/>
/// (when given) still true, every swapped frame's original and its XMP companions move into one trash entry of kind
/// <see cref="TrashKind.Replaced"/> (its record holds the old frame records and each one's replacement SHA-256, and a copy
/// of <c>analysis.json</c>), and the incoming files move in under their names (made unique). The sidecar takes the
/// records with the same ids and indices (frames version + 1, the statuses the frames uncovered cleared) and a tombstone
/// per old content naming its replacement, so sync counts the old photo as the new one; content that comes back loses
/// its tombstone. Then the index follows, the proxies of the swapped frames are deleted (the proxy pass makes them
/// again) and the project's positions stay on their frames. A journal (<see cref="FrameReplaceJournal"/>) makes it all
/// or nothing: a failure before the sidecar is written puts every file back, and a crash is undone or completed by the
/// next writer of the sequence. Returns null, nothing touched, when the sequence is gone, its frames changed, a swapped
/// frame's original is not there whole or was never hashed, or <paramref name="unchanged"/> said no.
/// </summary>
/// <param name="replacedBy">Who asked (user name or client), kept in the trash record.</param>
/// <param name="consumed">The trash entry the incoming files come from (an undo), settled once the sidecar is written.</param>
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only; nothing moved.</exception>
/// <exception cref="LeaseUnavailableException">The sequence is busy, or holds a restore or a replacement not settled yet.</exception>
/// <exception cref="IOException">An incoming file is missing, or a file could not move; nothing changed.</exception>
internal async Task<ReplaceResult?> ReplaceAsync(Archive archive, Guid sequenceId, int? framesVersion, IReadOnlyList<FrameSwap> swaps, string? replacedBy,
ConsumedEntry? consumed, Func<SequenceManifest, SequencePaths, CancellationToken, Task<bool>>? unchanged, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(archive);
ArgumentNullException.ThrowIfNull(swaps);
if (swaps.Count == 0) throw new ArgumentException("No frame is swapped.", nameof(swaps));
var paths = new ArchivePaths(archive.RootPath);
ArchiveVolume.EnsureWritable(paths);
if (await _db.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) is not { } row || row.ArchiveId != archive.Id) return null;
var folder = paths.Sequence(row.Folder);
var had = row.Status;
TrashEntry entry;
SequenceManifest updated;
bool indexed;
await using (await _writer.LockAsync(paths, sequenceId, cancellationToken).ConfigureAwait(false))
{
var manifest = SequenceWriter.ReadLocked(folder, sequenceId, consumed?.Lease);
EnsureSettled(folder);
EnsureReplaced(folder);
if (framesVersion is { } expected && manifest.Versions.Frames != expected) return null;
var byId = manifest.Frames.ToDictionary(f => f.Id);
var swapped = new Dictionary<Guid, SidecarFrame>();
foreach (var swap in swaps)
{
if (!byId.TryGetValue(swap.FrameId, out var current) || current.Sha256 is null || swapped.ContainsKey(swap.FrameId)) return null;
var file = new FileInfo(folder.File(current.File));
if (!file.Exists || file.Length != current.Size) return null;
var next = swap.Record(current);
if (next.Sha256 is null) throw new ArgumentException($"The record that replaces {current.File} names no content.", nameof(swaps));
swapped[swap.FrameId] = next;
}
if (unchanged is not null && !await unchanged(manifest, folder, cancellationToken).ConfigureAwait(false)) return null;
foreach (var swap in swaps.Where(s => !File.Exists(paths.FromRelative(s.Incoming))))
throw new FileNotFoundException($"The file that replaces {byId[swap.FrameId].File} is missing.", swap.Incoming);
var inStep = await _indexer.InStepAsync(archive, folder, manifest, cancellationToken).ConfigureAwait(false);
var photosChanged = !inStep && await _indexer.PhotosChangedAsync(archive, folder, cancellationToken).ConfigureAwait(false);
var leaving = manifest.Frames.Where(f => swapped.ContainsKey(f.Id)).ToList();
var kept = manifest.Frames.Where(f => !swapped.ContainsKey(f.Id)).ToList();
var photos = (await TrashedAsync(folder, leaving, kept, cancellationToken).ConfigureAwait(false))
.Select(p => p with { Replacement = swapped[p.Frame.Id].Sha256!.ToLowerInvariant() }).ToList();
var (incoming, names) = Incoming(folder, manifest, swaps, byId);
var now = DateTimeOffset.UtcNow;
updated = SequenceFrames.WithFrames(manifest,
[.. manifest.Frames.Select(f => swapped.TryGetValue(f.Id, out var record) ? Swapped(f, record, names[f.Id]) : f)]) with
{
Status = manifest.Status & ~Uncovered,
Removed = WithAliases(manifest.Removed, leaving, swapped, kept, now),
};
var trash = Directory(paths);
System.IO.Directory.CreateDirectory(trash);
var name = EntryName(trash, sequenceId);
var entryFolder = Path.Combine(trash, name);
var recordPath = entryFolder + RecordExtension;
var analysis = new FileInfo(folder.AnalysisPath);
List<TrashedFile>? leftovers = analysis.Exists
? [new TrashedFile(ArchiveLayout.MetaFolder + "/" + ArchiveLayout.AnalysisFile, AnalysisCopy, analysis.Length, null)]
: null;
var record = new PhotoTrashRecord(TrashKind.Replaced, sequenceId, row.DisplayTitle, row.Folder, photos,
photos.Sum(p => p.Files.Sum(f => f.Size)) + (leftovers?.Sum(f => f.Size) ?? 0), now, replacedBy, leftovers);
var outgoing = photos.SelectMany(p => p.Files).Select(f => new SwapMove(f.From, paths.ToRelative(Path.Combine(entryFolder, f.Path)))).ToList();
var journal = new FrameReplaceJournal(manifest.Versions.Frames, paths.ToRelative(recordPath), paths.ToRelative(entryFolder), outgoing, incoming,
analysis.Exists, consumed);
cancellationToken.ThrowIfCancellationRequested();
// The journal first: a crash from here until the sidecar is written puts every file back (ReadLocked).
journal.Write(folder);
// Under a name the trash does not list until the sidecar is written: half an entry is never restored or emptied.
AtomicFile.WriteAllText(FrameRemovalJournal.PendingRecord(recordPath), JsonSerializer.Serialize(record, TrashJson.Default.PhotoTrashRecord));
System.IO.Directory.CreateDirectory(entryFolder);
var movedOut = new List<SwapMove>();
var movedIn = new List<SwapMove>();
try
{
if (analysis.Exists) File.Copy(analysis.FullName, Path.Combine(entryFolder, AnalysisCopy), overwrite: false);
foreach (var move in outgoing)
{
MoveFile(folder.File(move.Place), paths.FromRelative(move.Other));
movedOut.Add(move);
}
foreach (var move in incoming)
{
MoveFile(paths.FromRelative(move.Other), folder.File(move.Place));
movedIn.Add(move);
}
ManifestStore.WriteSequence(folder.ManifestPath, updated);
}
catch (Exception ex)
{
// A file is open elsewhere, or the sidecar cannot be written: what moved goes back, and nothing changed.
Log.Warning("archive", $"{row.DisplayTitle}: the frames could not be replaced; the files that moved go back", ex);
UndoReplace(folder, movedOut, movedIn, journal);
throw;
}
CompleteReplace(folder, journal, consumed?.Lease);
Log.Info("archive", $"{row.DisplayTitle}: {photos.Count} frame(s) replaced; the files they had are in the trash ({name}, {record.Bytes} bytes)");
// The files have moved: the index follows to the end, whatever the caller cancels meanwhile.
indexed = await IndexAsync(archive, folder, updated, photosChanged, row.DisplayTitle).ConfigureAwait(false);
DropProxies(folder, leaving);
entry = Entry(archive.Id, name, record);
}
var sequence = await RowAsync(row, updated, indexed).ConfigureAwait(false);
return new ReplaceResult(entry, sequence, had);
}
/// <summary>
/// Settles the replacement a crash interrupted (<see cref="FrameReplaceJournal"/>), if any; the caller holds the
/// sequence's lease, and <paramref name="heldLease"/> when it holds a trash entry's lease too. While
/// <paramref name="manifest"/> is at the journal's frames version the sidecar was not written: every file that moved
/// goes back (one whose place is taken again stays, logged, and keeps the journal) and the new entry goes. Otherwise the
/// replacement stands and is completed (<see cref="CompleteReplace"/>).
/// </summary>
internal static void RecoverReplace(SequencePaths sequence, SequenceManifest manifest, string? heldLease = null)
{
if (!File.Exists(sequence.ReplaceJournalPath)) return;
if (FrameReplaceJournal.Read(sequence) is not { } journal)
{
FrameReplaceJournal.Delete(sequence);
return;
}
if (manifest.Versions.Frames == journal.FramesVersion)
{
Log.Warning("archive", $"An interrupted frame replacement in {sequence.RelativeFolder} is undone");
UndoReplace(sequence, journal.Out, journal.In, journal, onlyPresent: true);
}
else
{
Log.Warning("archive", $"An interrupted frame replacement in {sequence.RelativeFolder} is completed");
CompleteReplace(sequence, journal, heldLease);
}
}
/// <summary>
/// Refuses to go on while a replacement of <paramref name="sequence"/> is not settled after
/// <see cref="SequenceWriter.ReadLocked"/> (the entry its files came from was busy): its journal is not written over.
/// </summary>
/// <exception cref="LeaseUnavailableException">The replacement is not settled; nothing was changed.</exception>
internal static void EnsureReplaced(SequencePaths sequence)
{
if (!File.Exists(sequence.ReplaceJournalPath)) return;
var lease = FrameReplaceJournal.Read(sequence)?.Consumed?.Lease ?? "replace";
throw new LeaseUnavailableException(lease, LeaseFile.Holder(sequence.Archive.LocksDirectory, lease));
}
/// <summary>
/// The incoming moves, each file under its name made free: every name the sequence's folders hold stays taken, those of
/// the files that leave included, so no incoming file takes the place of one that leaves (a crash between the two is
/// then undone without doubt), and so do the files of every frame and the names already given.
/// </summary>
private static (List<SwapMove> Moves, Dictionary<Guid, string> Names) Incoming(SequencePaths folder, SequenceManifest manifest, IReadOnlyList<FrameSwap> swaps,
Dictionary<Guid, SidecarFrame> frames)
{
var taken = new HashSet<string>(FileRenames.ListPresent(folder, manifest), StringComparer.OrdinalIgnoreCase);
foreach (var frame in manifest.Frames) taken.Add(frame.File);
var unique = new UniqueNames(taken);
var moves = new List<SwapMove>();
var names = new Dictionary<Guid, string>();
foreach (var swap in swaps)
{
var file = unique.Claim(swap.Name(frames[swap.FrameId]), c => swap.Companions.All(t => !taken.Contains(StemOf(c) + t.Tail)));
names[swap.FrameId] = file;
moves.Add(new SwapMove(file, swap.Incoming));
foreach (var companion in swap.Companions)
{
var to = StemOf(file) + companion.Tail;
taken.Add(to);
moves.Add(new SwapMove(to, companion.From));
}
}
return (moves, names);
}
/// <summary>A path without its extension (<c>originals/IMG_0001</c>): companions add their tail to it.</summary>
private static string StemOf(string file) => file[..^Path.GetExtension(file).Length];
/// <summary>The frame's record after a swap: <paramref name="record"/> at the frame's id and index, under its new name, with its own user flags.</summary>
private static SidecarFrame Swapped(SidecarFrame current, SidecarFrame record, string file) => record with
{
Id = current.Id,
I = current.I,
File = file,
Sha256 = record.Sha256!.ToLowerInvariant(),
Flags = (current.Flags & ~ContentFlags) | (record.Flags & ContentFlags),
};
/// <summary>
/// The tombstones after a replacement: those of before but the ones of content that comes back, and one per content
/// that leaves naming its replacement (<see cref="RemovalReason.Replaced"/>), unless a frame that stays holds it too.
/// </summary>
private static List<RemovedPhoto>? WithAliases(IReadOnlyList<RemovedPhoto>? before, List<SidecarFrame> leaving, Dictionary<Guid, SidecarFrame> swapped,
List<SidecarFrame> kept, DateTimeOffset at)
{
var incoming = swapped.Values.Select(s => s.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase);
var held = kept.Where(f => f.Sha256 is not null).Select(f => f.Sha256!).ToHashSet(StringComparer.OrdinalIgnoreCase);
var tombstones = new Dictionary<string, RemovedPhoto>(StringComparer.OrdinalIgnoreCase);
foreach (var tombstone in before ?? [])
{
if (!incoming.Contains(tombstone.Sha256)) tombstones[tombstone.Sha256] = tombstone;
}
foreach (var frame in leaving)
{
var old = frame.Sha256!.ToLowerInvariant();
var replacement = swapped[frame.Id].Sha256!.ToLowerInvariant();
if (old == replacement || held.Contains(old)) continue;
tombstones[old] = new RemovedPhoto { Sha256 = old, Replacement = replacement, Reason = RemovalReason.Replaced, At = at };
}
return tombstones.Count == 0 ? null : [.. tombstones.Values];
}
/// <summary>
/// Puts back what a replacement moved before its sidecar was written (incoming files back where they came from, the
/// files that left back into the sequence) and removes its entry and journal. With <paramref name="onlyPresent"/> (a
/// recovery) a file not at its new place, or whose old place is taken, is left where it is (logged); the journal and the
/// entry then stay, and the entry is listed under its record's own name. A failure (logged) keeps the journal, and the
/// next writer of the sequence tries again.
/// </summary>
private static void UndoReplace(SequencePaths folder, IReadOnlyList<SwapMove> movedOut, IReadOnlyList<SwapMove> movedIn, FrameReplaceJournal journal,
bool onlyPresent = false)
{
var archive = folder.Archive;
var recordPath = archive.FromRelative(journal.Record);
var entryFolder = archive.FromRelative(journal.Folder);
var stuck = 0;
try
{
foreach (var move in movedIn.Reverse())
{
if (!Back(folder.File(move.Place), archive.FromRelative(move.Other))) stuck++;
}
foreach (var move in movedOut.Reverse())
{
if (!Back(archive.FromRelative(move.Other), folder.File(move.Place))) stuck++;
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Error("archive", $"A frame replacement in {folder.RelativeFolder} could not be undone; the journal puts it back later", ex);
return;
}
if (stuck > 0)
{
// What could not go back is in the entry, listed, restorable and erasable like any entry.
FrameRemovalJournal.Publish(recordPath);
FrameReplaceJournal.Delete(folder);
return;
}
var copy = Path.Combine(entryFolder, AnalysisCopy);
if (journal.Analysis && File.Exists(copy)) File.Delete(copy);
if (File.Exists(FrameRemovalJournal.PendingRecord(recordPath))) File.Delete(FrameRemovalJournal.PendingRecord(recordPath));
FrameRemovalJournal.RemoveEmpty(entryFolder);
FrameReplaceJournal.Delete(folder);
bool Back(string from, string to)
{
if (!File.Exists(from)) return !onlyPresent || File.Exists(to) || Gone(from, to);
if (File.Exists(to))
{
Log.Warning("archive", $"{to} is there again; {from} stays where it is");
return false;
}
MoveFile(from, to);
return true;
}
// Neither here nor there: nothing to put back for this file.
static bool Gone(string from, string to)
{
Log.Warning("archive", $"{from} is not there to go back to {to}");
return true;
}
}
/// <summary>
/// Completes a replacement whose sidecar is written: its entry is listed under its record's own name, the entry its
/// incoming files came from (if any) is settled under its lease (the photos swapped leave its record, its copy of
/// <c>analysis.json</c> goes back into the sequence, and an entry left without anything goes), and the journal goes. An
/// entry busy now (or a failure, logged) keeps the journal for the next writer of the sequence.
/// </summary>
private static void CompleteReplace(SequencePaths folder, FrameReplaceJournal journal, string? heldLease)
{
var archive = folder.Archive;
try
{
FrameRemovalJournal.Publish(archive.FromRelative(journal.Record));
if (journal.Consumed is { } consumed && !Settle(folder, consumed, heldLease)) return;
FrameReplaceJournal.Delete(folder);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("archive", $"A frame replacement in {folder.RelativeFolder} is not settled yet; the next writer of the sequence settles it", ex);
}
}
/// <summary>Settles the entry a replacement took its files from (<see cref="CompleteReplace"/>); false while it is busy.</summary>
private static bool Settle(SequencePaths folder, ConsumedEntry consumed, string? heldLease)
{
var archive = folder.Archive;
LeaseFile? taken = null;
if (consumed.Lease != heldLease)
{
taken = LeaseFile.TryAcquire(archive.LocksDirectory, consumed.Lease, SettleOwner);
if (taken is null)
{
Log.Info("archive", $"A frame replacement in {folder.RelativeFolder} waits: the trash entry its files came from is busy");
return false;
}
}
using var held = taken;
var recordPath = archive.FromRelative(consumed.Record);
var entryFolder = archive.FromRelative(consumed.Folder);
// The copy of the analysis of the frames that came back takes the place of the analysis of the frames that went
// (the new entry keeps a copy of that one).
if (consumed.Analysis is { } copy && File.Exists(archive.FromRelative(copy)))
{
System.IO.Directory.CreateDirectory(folder.MetaDirectory);
File.Move(archive.FromRelative(copy), folder.AnalysisPath, overwrite: true);
}
if (ReadRecord(recordPath) is { } record)
{
var frames = consumed.Frames.ToHashSet();
var leftovers = record.Leftovers?.Where(f => File.Exists(Path.Combine(entryFolder, f.Path))).ToList();
Rewrite(recordPath, entryFolder, record with
{
Photos = [.. record.Photos.Where(p => !frames.Contains(p.Frame.Id))],
Leftovers = leftovers is { Count: > 0 } ? leftovers : null,
});
}
return true;
}
/// <summary>
/// Deletes the proxies of frames whose file was replaced: a proxy carries its frame's capture time, which a converted file
/// keeps, so the proxy pass would take the old one for current. A failure is logged; the pass then reuses what is left.
/// </summary>
private static void DropProxies(SequencePaths folder, IReadOnlyList<SidecarFrame> frames)
{
if (!System.IO.Directory.Exists(folder.Proxies)) return;
try
{
foreach (var frame in frames)
{
var proxy = folder.Proxy(frame.I);
if (File.Exists(proxy)) File.Delete(proxy);
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("archive", $"Proxies of the frames replaced in {folder.RelativeFolder} could not be deleted; the proxy pass may keep them", ex);
}
}
/// <summary>
/// Whether every photo of a replaced entry may swap back into its sequence: each one's frame is there with the content
/// that replaced it (its file re-hashed now) and still names the photo as its source.
/// </summary>
private static async Task<bool> SwappableAsync(PhotoTrashRecord record, SequenceManifest manifest, SequencePaths folder, CancellationToken cancellationToken)
{
var frames = manifest.Frames.ToDictionary(f => f.Id);
foreach (var photo in record.Photos)
{
if (photo.Replacement is not { } replacement || photo.Frame.Sha256 is not { } original || !frames.TryGetValue(photo.Frame.Id, out var frame)) return false;
if (!string.Equals(frame.Sha256, replacement, StringComparison.OrdinalIgnoreCase)
|| !string.Equals(frame.Source?.Sha256, original, StringComparison.OrdinalIgnoreCase)) return false;
var file = new FileInfo(folder.File(frame.File));
if (!file.Exists || file.Length != frame.Size
|| !string.Equals(await ContentHash.Sha256Async(file.FullName, null, cancellationToken).ConfigureAwait(false), replacement, StringComparison.OrdinalIgnoreCase))
return false;
}
return true;
}
/// <summary>
/// The undo of a conversion: the originals of a replaced entry take their frames' place again
/// (<see cref="ReplaceAsync"/>), and the files that replaced them go to the trash as a replaced entry in turn. Null,
/// nothing touched, when a frame changed since (<see cref="SwappableAsync"/>) or the sequence did.
/// </summary>
/// <param name="lease">The entry's lease, which the caller holds.</param>
private async Task<ReplaceResult?> SwapBackAsync(Archive archive, ArchivePaths paths, Sequence sequence, PhotoTrashRecord record, string recordPath,
string entryFolder, string lease, CancellationToken cancellationToken)
{
if (record.Photos.Count == 0 || record.Photos.Any(p => p.Files.Count == 0 || p.Frame.Sha256 is null || p.Replacement is null)) return null;
var swaps = record.Photos.Select(p => new FrameSwap(p.Frame.Id, paths.ToRelative(Path.Combine(entryFolder, p.Files[0].Path)),
[.. p.Files.Skip(1).Select(c => new IncomingCompanion(paths.ToRelative(Path.Combine(entryFolder, c.Path)), FrameCompanions.Tail(c.From, p.Files[0].From)))],
_ => p.Files[0].From, _ => p.Frame)).ToList();
var copy = Path.Combine(entryFolder, AnalysisCopy);
var analysis = record.Leftovers?.Any(f => f.Path == AnalysisCopy) == true && File.Exists(copy) ? paths.ToRelative(copy) : null;
var consumed = new ConsumedEntry(paths.ToRelative(recordPath), paths.ToRelative(entryFolder), lease, [.. record.Photos.Select(p => p.Frame.Id)], analysis);
return await ReplaceAsync(archive, sequence.Id, null, swaps, null, consumed, (manifest, folder, ct) => SwappableAsync(record, manifest, folder, ct),
cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// A replaced entry's record without its copy of <c>analysis.json</c> once no photo is left in it (all of them filed into
/// a new sequence, which is analysed again): the copy, derived from those photos, goes with the entry.
/// </summary>
private static PhotoTrashRecord WithoutAnalysisCopy(string entryFolder, PhotoTrashRecord record)
{
if (record.Kind != TrashKind.Replaced || record.Photos.Count > 0 || record.Leftovers?.Any(f => f.Path == AnalysisCopy) != true) return record;
var copy = Path.Combine(entryFolder, AnalysisCopy);
if (File.Exists(copy)) File.Delete(copy);
var leftovers = record.Leftovers.Where(f => f.Path != AnalysisCopy).ToList();
return record with { Leftovers = leftovers.Count > 0 ? leftovers : null };
}
}