feat(archive): photo delete, tombstones, trash kinds and emptying, library API
Owner review 2, R6: the web panel manages the library without editing it, so the engine and the server must delete single photos safely, keep them out of sync afterwards, and let an administrator restore or finally erase the trash. - Migration 0008: frame_removals (tombstones by sequence and SHA-256, reason, replacement); the sidecar gains "removed" (omitted when empty) and frames a reserved "source"; the indexer writes the tombstones on every sync, so a rebuild keeps them, and drops a hero frame that is gone. - FrameTrash: dry run (busy, frames changed, offline, would empty), delete under the sequence lease with a write-ahead journal that the next writer undoes after a crash, files to trash/photos/<seq>-<time>/originals with a record, sidecar without the frames plus tombstones, index sync clearing analyses, thumbnails and proxies, proxies renamed to follow their frames, follow-up jobs. Restore re-hashes every file, keeps mismatches in the trash, puts the same frame records back and drops their tombstones, or files the photos into a new sequence through the ingest when theirs is gone. - Project positions on vanished photos are translated by photo and wait as pending frames, so they come back with a restore. - Trash entries have a kind (sequence, photos, replaced); emptying is a dry run with a fingerprint and a new EmptyTrash job (18) that erases exactly the planned entries, refuses a stale plan and erases replaced originals only while their replacements still hash right. - Sync honours tombstones: nothing one side deleted is offered back, local files are never deleted, replaced photos count as their replacement; the server answers an upload of a tombstoned SHA-256 as already there. - Server: sequence thumbnail, photo delete, bulk delete/retag/jobs, trash list/restore/empty endpoints with their error messages; a verify of sequences now targets their archive (POST /jobs failed before). - Desktop trash dialog lists photo entries and empties the trash after a typed confirmation through the same job. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 35bb56690a1d69eae57153d036e1409201cd73f7)
This commit is contained in:
1 parent
4b9ba04fe4
commit
51782a3ada
63 files changed
+3303
-60
No files matched your search
@@ -14,6 +14,7 @@ namespace Chronos.Core.Archiving;
|
||||
[JsonSerializable(typeof(SequenceManifest))]
|
||||
[JsonSerializable(typeof(LeaseRecord))]
|
||||
[JsonSerializable(typeof(IngestJournal))]
|
||||
[JsonSerializable(typeof(FrameRemovalJournal))]
|
||||
[JsonSerializable(typeof(RenameJournal))]
|
||||
internal sealed partial class ArchiveJson : JsonSerializerContext;
|
||||
|
||||
|
||||
@@ -30,6 +30,9 @@ public static class ArchiveLayout
|
||||
/// <summary>Moves of an ingest batch that were started but not yet recorded in <see cref="SequenceManifestFile"/>.</summary>
|
||||
public const string IngestJournalFile = "ingest.pending.json";
|
||||
|
||||
/// <summary>Moves of a photo deletion into the trash that were started but not yet recorded in <see cref="SequenceManifestFile"/>.</summary>
|
||||
public const string RemovalJournalFile = "remove.pending.json";
|
||||
|
||||
/// <summary>Folder names inside a sequence folder that never hold a sequence of their own.</summary>
|
||||
public static bool IsReservedFolder(string name) =>
|
||||
name.Equals(MetaFolder, StringComparison.OrdinalIgnoreCase)
|
||||
@@ -74,11 +77,15 @@ public sealed class SequencePaths
|
||||
internal SequencePaths(ArchivePaths archive, string relativeFolder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(relativeFolder);
|
||||
Archive = archive;
|
||||
RelativeFolder = relativeFolder.Replace('\\', '/').Trim('/');
|
||||
Folder = archive.FromRelative(RelativeFolder);
|
||||
MetaDirectory = Path.Combine(Folder, ArchiveLayout.MetaFolder);
|
||||
}
|
||||
|
||||
/// <summary>The archive the sequence belongs to.</summary>
|
||||
public ArchivePaths Archive { get; }
|
||||
|
||||
/// <summary>Folder relative to the archive root, forward slashes; empty when the root itself holds the photos (adopted folder).</summary>
|
||||
public string RelativeFolder { get; }
|
||||
|
||||
@@ -92,6 +99,7 @@ public sealed class SequencePaths
|
||||
public string ProjectPath => Path.Combine(MetaDirectory, ArchiveLayout.ProjectFile);
|
||||
public string ThumbsPackPath => Path.Combine(MetaDirectory, ArchiveLayout.ThumbsPackFile);
|
||||
public string IngestJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.IngestJournalFile);
|
||||
public string RemovalJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.RemovalJournalFile);
|
||||
|
||||
public string Proxy(int index) => Path.Combine(Proxies, ArchiveLayout.ProxyFileName(index));
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.Text.Json;
|
||||
using Chronos.Core.Diagnostics;
|
||||
using Chronos.Core.IO;
|
||||
|
||||
namespace Chronos.Core.Archiving;
|
||||
|
||||
/// <summary>One file of a photo deletion: where it was in the sequence folder and where it went in the archive's trash.</summary>
|
||||
/// <param name="From">Path relative to the sequence folder (forward slashes).</param>
|
||||
/// <param name="To">Path relative to the archive root (forward slashes), inside the trash entry.</param>
|
||||
public sealed record FrameRemovalMove(string From, string To);
|
||||
|
||||
/// <summary>
|
||||
/// Write-ahead record of a photo deletion (<c>.chronos/remove.pending.json</c>, owner review 2, R6): the trash entry the
|
||||
/// photos move into, the frames leaving the sidecar and every file move. Written before the first move and removed once
|
||||
/// <c>sequence.json</c> no longer lists the frames, so a crash in between is undone by <see cref="Recover"/>: the files go
|
||||
/// back to their places, the entry's record and folder go, and the sequence is as it was.
|
||||
/// </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>
|
||||
public sealed record FrameRemovalJournal(string Record, string Folder, IReadOnlyList<Guid> Frames, IReadOnlyList<FrameRemovalMove> Moves)
|
||||
{
|
||||
public static void Write(SequencePaths sequence, FrameRemovalJournal journal)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(sequence);
|
||||
Directory.CreateDirectory(sequence.MetaDirectory);
|
||||
AtomicFile.WriteAllBytes(sequence.RemovalJournalPath, JsonSerializer.SerializeToUtf8Bytes(journal, ArchiveJson.Default.FrameRemovalJournal));
|
||||
}
|
||||
|
||||
public static void Delete(SequencePaths sequence)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(sequence);
|
||||
if (File.Exists(sequence.RemovalJournalPath)) File.Delete(sequence.RemovalJournalPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Undoes a deletion a crash interrupted before its sidecar was written: while <paramref name="manifest"/> still lists a
|
||||
/// frame of the journal, each file that reached the trash moves back (one whose place is taken again stays in the trash,
|
||||
/// logged, and so does the entry then), and the entry's record and its emptied folder go. A deletion whose sidecar was
|
||||
/// written is complete: only the journal goes. Returns whether a deletion was undone. The caller holds the lease.
|
||||
/// </summary>
|
||||
public static bool Recover(SequencePaths sequence, SequenceManifest manifest)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(sequence);
|
||||
ArgumentNullException.ThrowIfNull(manifest);
|
||||
if (!File.Exists(sequence.RemovalJournalPath)) return false;
|
||||
FrameRemovalJournal? journal;
|
||||
try
|
||||
{
|
||||
journal = JsonSerializer.Deserialize(AtomicFile.ReadAllBytes(sequence.RemovalJournalPath), ArchiveJson.Default.FrameRemovalJournal);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
journal = null; // torn while being written: nothing was moved yet
|
||||
}
|
||||
var undone = false;
|
||||
if (journal is not null && manifest.Frames.Any(f => journal.Frames.Contains(f.Id)))
|
||||
{
|
||||
var archive = sequence.Archive;
|
||||
var kept = 0;
|
||||
foreach (var move in journal.Moves)
|
||||
{
|
||||
var trashed = archive.FromRelative(move.To);
|
||||
var place = sequence.File(move.From);
|
||||
if (!File.Exists(trashed)) continue;
|
||||
if (File.Exists(place))
|
||||
{
|
||||
kept++;
|
||||
Log.Warning("archive", $"{sequence.ArchiveRelative(move.From)} is there again; its deleted copy stays in {move.To}");
|
||||
continue;
|
||||
}
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(place)!);
|
||||
File.Move(trashed, place, overwrite: false);
|
||||
}
|
||||
if (kept == 0)
|
||||
{
|
||||
var record = archive.FromRelative(journal.Record);
|
||||
if (File.Exists(record)) File.Delete(record);
|
||||
RemoveEmpty(archive.FromRelative(journal.Folder));
|
||||
}
|
||||
Log.Warning("archive", $"An interrupted photo deletion in {sequence.RelativeFolder} was undone");
|
||||
undone = true;
|
||||
}
|
||||
Delete(sequence);
|
||||
return undone;
|
||||
}
|
||||
|
||||
/// <summary>Removes <paramref name="folder"/> and its subfolders when they hold no file.</summary>
|
||||
public static void RemoveEmpty(string folder)
|
||||
{
|
||||
if (!Directory.Exists(folder)) return;
|
||||
foreach (var child in Directory.EnumerateDirectories(folder)) RemoveEmpty(child);
|
||||
if (!Directory.EnumerateFileSystemEntries(folder).Any()) Directory.Delete(folder);
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,13 @@ public sealed record SequenceManifest
|
||||
/// <summary>Session keys of automatic grouping (uploads, card imports) that resolve to this sequence.</summary>
|
||||
public IReadOnlyList<string> SessionKeys { get; init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Tombstones of the photos removed from the sequence (owner review 2, R6), by SHA-256; null when there are none, so a
|
||||
/// sidecar without removals is written as before. A build that does not know the field keeps it in its
|
||||
/// <see cref="Extra"/> and writes it back unchanged.
|
||||
/// </summary>
|
||||
public IReadOnlyList<RemovedPhoto>? Removed { get; init; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
@@ -127,6 +134,53 @@ public sealed record SidecarFrame
|
||||
/// <summary><see cref="FrameFlags"/> bits, stored as a number so bits of newer builds survive.</summary>
|
||||
public FrameFlags Flags { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The file this frame was converted from (reserved for the conversion of owner review 2, R7); null for an original.
|
||||
/// Read and written only: nothing acts on it yet.
|
||||
/// </summary>
|
||||
public FrameSource? Source { get; init; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The tombstone of a photo removed from a sequence (owner review 2, R6): sync never offers that content back to the
|
||||
/// sequence, and an upload of it is answered as already there. <see cref="Replacement"/> names the converted file that took
|
||||
/// its place (<see cref="RemovalReason.Replaced"/>), which sync then counts as the same photo.
|
||||
/// </summary>
|
||||
public sealed record RemovedPhoto
|
||||
{
|
||||
/// <summary>Lower-case hex SHA-256 of the removed photo.</summary>
|
||||
public required string Sha256 { get; init; }
|
||||
|
||||
/// <summary>Lower-case hex SHA-256 of the file that replaced it; null for a photo deleted outright.</summary>
|
||||
public string? Replacement { get; init; }
|
||||
|
||||
[JsonConverter(typeof(TolerantEnumConverter<RemovalReason>))]
|
||||
public RemovalReason Reason { get; init; }
|
||||
|
||||
/// <summary>When it was removed (UTC).</summary>
|
||||
public DateTimeOffset At { get; init; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>The original a converted frame was made from (reserved for owner review 2, R7): its content, format, file and size.</summary>
|
||||
public sealed record FrameSource
|
||||
{
|
||||
/// <summary>Lower-case hex SHA-256 of the source file.</summary>
|
||||
public string? Sha256 { get; init; }
|
||||
|
||||
[JsonConverter(typeof(TolerantEnumConverter<ImageFileFormat>))]
|
||||
public ImageFileFormat Format { get; init; }
|
||||
|
||||
/// <summary>The source's path relative to the sequence folder (forward slashes) when it was converted.</summary>
|
||||
public string? File { get; init; }
|
||||
|
||||
public long Size { get; init; }
|
||||
|
||||
[JsonExtensionData]
|
||||
public Dictionary<string, JsonElement>? Extra { get; set; }
|
||||
}
|
||||
Reference in new issue
Block a user