Files
Chronos/src/Chronos.Core/Archiving/ArchiveLayout.cs
T
Alby96 0e52929274 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)
2026-10-27 22:01:23 +01:00

125 lines
6.8 KiB
C#

using System.Globalization;
using Chronos.Core.IO;
namespace Chronos.Core.Archiving;
/// <summary>Names of the fixed files and folders of the archive layout (BRIEF §7).</summary>
public static class ArchiveLayout
{
/// <summary>Metadata folder at the archive root and inside every sequence folder.</summary>
public const string MetaFolder = ".chronos";
public const string ArchiveManifestFile = "archive.json";
public const string IndexSnapshotFile = "index-snapshot.bin";
public const string LocksFolder = "locks";
/// <summary>Verified copies waiting to be committed; on the archive volume so commits are renames.</summary>
public const string StagingFolder = "staging";
/// <summary>Resumable plans of mass operations (rename).</summary>
public const string JournalFolder = "journal";
public const string OriginalsFolder = "originals";
public const string ProxiesFolder = "proxies";
public const string ExportsFolder = "exports";
public const string SequenceManifestFile = "sequence.json";
public const string AnalysisFile = "analysis.json";
public const string ProjectFile = "project.json";
public const string ThumbsPackFile = "thumbs.pack";
/// <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>Moves of photos restored from the trash that were started but not yet settled in <see cref="SequenceManifestFile"/> and the trash record.</summary>
public const string RestoreJournalFile = "restore.pending.json";
/// <summary>File renames of a Reorganize that were started but not yet recorded in <see cref="SequenceManifestFile"/> and the index.</summary>
public const string FileRenameJournalFile = "rename-files.json";
/// <summary>Moves of a frame replacement (a conversion, or its undo) that were started but not yet settled in <see cref="SequenceManifestFile"/> and the trash.</summary>
public const string ReplaceJournalFile = "replace.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)
|| name.Equals(OriginalsFolder, StringComparison.OrdinalIgnoreCase)
|| name.Equals(ProxiesFolder, StringComparison.OrdinalIgnoreCase)
|| name.Equals(ExportsFolder, StringComparison.OrdinalIgnoreCase);
/// <summary><c>0001.jpg</c> for frame index 0: proxies are numbered from 1 like the frames of a render.</summary>
public static string ProxyFileName(int index) => (index + 1).ToString("0000", CultureInfo.InvariantCulture) + ".jpg";
}
/// <summary>Absolute paths of one archive root.</summary>
public sealed class ArchivePaths
{
public ArchivePaths(string root)
{
ArgumentException.ThrowIfNullOrWhiteSpace(root);
Root = LongPath.Normalize(Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)));
MetaDirectory = Path.Combine(Root, ArchiveLayout.MetaFolder);
}
public string Root { get; }
public string MetaDirectory { get; }
public string ManifestPath => Path.Combine(MetaDirectory, ArchiveLayout.ArchiveManifestFile);
public string SnapshotPath => Path.Combine(MetaDirectory, ArchiveLayout.IndexSnapshotFile);
public string LocksDirectory => Path.Combine(MetaDirectory, ArchiveLayout.LocksFolder);
public string StagingDirectory => Path.Combine(MetaDirectory, ArchiveLayout.StagingFolder);
public string JournalDirectory => Path.Combine(MetaDirectory, ArchiveLayout.JournalFolder);
/// <summary>Paths of the sequence stored in <paramref name="relativeFolder"/> (forward slashes).</summary>
public SequencePaths Sequence(string relativeFolder) => new(this, relativeFolder);
/// <summary>Archive-relative path with forward slashes, as stored in the index.</summary>
public string ToRelative(string path) => LongPath.ToPortableRelative(Root, path);
public string FromRelative(string relative) => LongPath.FromPortableRelative(Root, relative);
}
/// <summary>Absolute paths of one sequence folder.</summary>
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; }
public string Folder { get; }
public string MetaDirectory { get; }
public string Originals => Path.Combine(Folder, ArchiveLayout.OriginalsFolder);
public string Proxies => Path.Combine(Folder, ArchiveLayout.ProxiesFolder);
public string Exports => Path.Combine(Folder, ArchiveLayout.ExportsFolder);
public string ManifestPath => Path.Combine(MetaDirectory, ArchiveLayout.SequenceManifestFile);
public string AnalysisPath => Path.Combine(MetaDirectory, ArchiveLayout.AnalysisFile);
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 RestoreJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.RestoreJournalFile);
public string FileRenameJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.FileRenameJournalFile);
public string ReplaceJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.ReplaceJournalFile);
public string Proxy(int index) => Path.Combine(Proxies, ArchiveLayout.ProxyFileName(index));
/// <summary>Absolute path of a frame file given its sidecar path (relative to the sequence folder).</summary>
public string File(string sequenceRelative) => LongPath.FromPortableRelative(Folder, sequenceRelative);
/// <summary>Archive-relative path of a frame file (as stored in the frames table).</summary>
public string ArchiveRelative(string sequenceRelative) =>
RelativeFolder.Length == 0 ? sequenceRelative : RelativeFolder + "/" + sequenceRelative;
}