CI / Build and test (push) Successful in 7m25s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
CI / PostgreSQL 16 integration (push) Successful in 5m20s
CI / PostgreSQL 17 integration (push) Successful in 5m22s
CI / Docker image (push) Successful in 50s
Release / Build, test and publish (push) Successful in 12m1s
Release / Server image (push) Successful in 1m26s
- Web panel Library: browse, view, delete sequences and photos, mass operations (analyse, proxies, thumbnails, verify, retag, delete, CSV). It never edits the video. - Photo trash with tombstones (migration 0008): crash-safe delete and restore, hash-checked restore with reasons, and emptying only by an explicit fingerprinted job. Sync and uploads keep deleted photos deleted; an explicit "Upload again" brings one back. - Upload from the web panel: in-house SHA-256 in a worker with a fail-closed self-test, capture time from EXIF, chunked, resumable; no folders. - Desktop trash dialog with photo entries and Empty. - Admin files are revalidated (no-cache + ETag). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
118 lines
5.8 KiB
C#
118 lines
5.8 KiB
C#
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. Until the sidecar is written
|
|
/// the entry's record has its <see cref="PendingRecord"/> name, which the trash never lists, so half an entry can be
|
|
/// neither restored nor emptied.
|
|
/// </summary>
|
|
/// <param name="Record">The entry's record, relative to the archive root (its name once the deletion is complete).</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)
|
|
{
|
|
/// <summary>The name a deletion's record has until its sidecar is written.</summary>
|
|
public static string PendingRecord(string record) => record + ".pending";
|
|
|
|
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, listed under its record's own name), and the entry's record and its emptied
|
|
/// folder go. A deletion whose sidecar was written is complete: its record takes its own name, and 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;
|
|
var archive = sequence.Archive;
|
|
if (journal is not null && manifest.Frames.Any(f => journal.Frames.Contains(f.Id)))
|
|
{
|
|
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);
|
|
}
|
|
var record = archive.FromRelative(journal.Record);
|
|
if (kept == 0)
|
|
{
|
|
if (File.Exists(record)) File.Delete(record);
|
|
if (File.Exists(PendingRecord(record))) File.Delete(PendingRecord(record));
|
|
RemoveEmpty(archive.FromRelative(journal.Folder));
|
|
}
|
|
else
|
|
{
|
|
// What stays in the trash is listed, restorable and erasable like any entry.
|
|
Publish(record);
|
|
}
|
|
Log.Warning("archive", $"An interrupted photo deletion in {sequence.RelativeFolder} was undone");
|
|
undone = true;
|
|
}
|
|
else if (journal is not null)
|
|
{
|
|
Publish(archive.FromRelative(journal.Record));
|
|
}
|
|
Delete(sequence);
|
|
return undone;
|
|
}
|
|
|
|
/// <summary>Gives a deletion's record its own name (<see cref="PendingRecord"/>), once its photos are out of the sidecar.</summary>
|
|
public static void Publish(string record)
|
|
{
|
|
var pending = PendingRecord(record);
|
|
if (File.Exists(pending) && !File.Exists(record)) File.Move(pending, record, overwrite: false);
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
}
|