using System.Globalization;
using Chronos.Core.IO;
namespace Chronos.Core.Archiving;
/// Names of the fixed files and folders of the archive layout (BRIEF ยง7).
public static class ArchiveLayout
{
/// Metadata folder at the archive root and inside every sequence folder.
public const string MetaFolder = ".chronos";
public const string ArchiveManifestFile = "archive.json";
public const string IndexSnapshotFile = "index-snapshot.bin";
public const string LocksFolder = "locks";
/// Verified copies waiting to be committed; on the archive volume so commits are renames.
public const string StagingFolder = "staging";
/// Resumable plans of mass operations (rename).
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";
/// Moves of an ingest batch that were started but not yet recorded in .
public const string IngestJournalFile = "ingest.pending.json";
/// Moves of a photo deletion into the trash that were started but not yet recorded in .
public const string RemovalJournalFile = "remove.pending.json";
/// Moves of photos restored from the trash that were started but not yet settled in and the trash record.
public const string RestoreJournalFile = "restore.pending.json";
/// File renames of a Reorganize that were started but not yet recorded in and the index.
public const string FileRenameJournalFile = "rename-files.json";
/// Folder names inside a sequence folder that never hold a sequence of their own.
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);
/// 0001.jpg for frame index 0: proxies are numbered from 1 like the frames of a render.
public static string ProxyFileName(int index) => (index + 1).ToString("0000", CultureInfo.InvariantCulture) + ".jpg";
}
/// Absolute paths of one archive root.
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);
/// Paths of the sequence stored in (forward slashes).
public SequencePaths Sequence(string relativeFolder) => new(this, relativeFolder);
/// Archive-relative path with forward slashes, as stored in the index.
public string ToRelative(string path) => LongPath.ToPortableRelative(Root, path);
public string FromRelative(string relative) => LongPath.FromPortableRelative(Root, relative);
}
/// Absolute paths of one sequence folder.
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);
}
/// The archive the sequence belongs to.
public ArchivePaths Archive { get; }
/// Folder relative to the archive root, forward slashes; empty when the root itself holds the photos (adopted folder).
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 Proxy(int index) => Path.Combine(Proxies, ArchiveLayout.ProxyFileName(index));
/// Absolute path of a frame file given its sidecar path (relative to the sequence folder).
public string File(string sequenceRelative) => LongPath.FromPortableRelative(Folder, sequenceRelative);
/// Archive-relative path of a frame file (as stored in the frames table).
public string ArchiveRelative(string sequenceRelative) =>
RelativeFolder.Length == 0 ? sequenceRelative : RelativeFolder + "/" + sequenceRelative;
}