Files
Chronos/src/Chronos.Core/Import/ImportPlan.cs
T
Alby96andClaude Opus 5.5 445dfd41a3 feat(import): import a whole folder as one sequence per format
Owner review 2, R9: Import › Folder… gains a "Group" choice for picked
folders: Sessions (as before) or Whole folder. A whole folder, with its
subfolders when Subfolders is on, becomes one sequence per file format;
camera bodies and pauses are ignored, while RAW+JPEG of the same shots
stays two sequences so no frame is ever doubled. Only the user's splits
break it, and Merge joins a split part back.

The planner implements it (ImportOptions.Grouping, Core, Linux-tested):
the session key is "folder-" plus a hash of this PC's name, the full
path and the format, so an interrupted or repeated import continues the
same sequence; the default title is the folder's name. The plan shows
one row per format with the format and the number of cameras. Cards and
drives always import by sessions; the choice is not part of a card
profile and a folder starts on Sessions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 22:00:55 +01:00

153 lines
6.2 KiB
C#

using System.Text.Json.Serialization;
using Chronos.Core.Imaging;
using Chronos.Core.Naming;
namespace Chronos.Core.Import;
/// <summary>What to do with a photo that is already in the archive (same SHA-256).</summary>
public enum DuplicatePolicy
{
/// <summary>Leave it on the card; the archive copy is identical.</summary>
Skip,
/// <summary>Import it again into this session's sequence (its name gets a suffix if taken).</summary>
ImportAgain,
}
/// <summary>How the planner turns the photos of a source into sequences (BRIEF §12.4, §13).</summary>
public enum ImportGrouping
{
/// <summary>One session per camera body and file format, split where the pause between two photos exceeds the gap.</summary>
Sessions,
/// <summary>
/// The whole source (a folder picked on this PC) is one sequence per file format: camera bodies and pauses are
/// ignored. RAW+JPEG of the same shots stays two sequences, so no frame is ever doubled.
/// </summary>
WholeFolder,
}
/// <summary>Card import settings (BRIEF §12.4, §13); the defaults are the brief's.</summary>
public sealed record ImportOptions
{
public string FolderTemplate { get; init; } = TemplateDefaults.Folder;
public string FileTemplate { get; init; } = TemplateDefaults.File;
public TimeSpan SessionGap { get; init; } = TimeSpan.FromMinutes(45);
public int MinimumSessionFrames { get; init; } = 8;
public bool IncludeSubfolders { get; init; } = true;
public bool VerifyCopies { get; init; } = true;
public DuplicatePolicy Duplicates { get; init; } = DuplicatePolicy.Skip;
/// <summary>Sessions, or the whole source as one sequence per format (the session gap and minimum then do not apply).</summary>
public ImportGrouping Grouping { get; init; } = ImportGrouping.Sessions;
/// <summary>Card paths where the user split a session (a new session starts at that photo).</summary>
public IReadOnlySet<string> SplitBefore { get; init; } = new HashSet<string>();
/// <summary>Card paths where the user merged a session into the previous one.</summary>
public IReadOnlySet<string> MergeAt { get; init; } = new HashSet<string>();
/// <summary>Titles given to sessions, by session key; used as the sequence title and the <c>{title}</c> token.</summary>
public IReadOnlyDictionary<string, string> Titles { get; init; } = new Dictionary<string, string>();
}
public enum ImportAction
{
Copy,
/// <summary>Imported from this card before (same path, size and time).</summary>
SkipImported,
/// <summary>Already in the archive (same size and capture time, then the same SHA-256).</summary>
SkipInArchive,
}
/// <summary>One photo of the plan and its destination name.</summary>
public sealed record PlannedItem
{
/// <summary>Path below the source root, forward slashes.</summary>
public required string SourcePath { get; init; }
public long Size { get; init; }
public DateTime ModifiedUtc { get; init; }
public DateTime CapturedAt { get; init; }
public required string FileName { get; init; }
public ImportAction Action { get; init; }
/// <summary>The archive frame this photo duplicates (<see cref="ImportAction.SkipInArchive"/>, or imported again).</summary>
public Guid? ExistingFrameId { get; init; }
/// <summary>SHA-256 computed while planning (duplicate check), if any.</summary>
public string? Sha256 { get; init; }
}
/// <summary>
/// A session of the plan: one camera body and file format (a whole folder: one file format), one sequence in the
/// archive. Its camera is the one of its first photo.
/// </summary>
public sealed record PlannedSession
{
/// <summary>1-based, in order of start time across the import.</summary>
public int Number { get; init; }
/// <summary>Stable key of the session; re-planning the same photos yields the same key, so an interrupted import
/// resumes into the same sequence.</summary>
public required string SessionKey { get; init; }
public string? CameraMake { get; init; }
public string? CameraModel { get; init; }
public string? CameraSerial { get; init; }
/// <summary>Camera bodies among its photos: 1 for a session, possibly more for a whole folder.</summary>
public int Cameras { get; init; } = 1;
public ImageFileFormat Format { get; init; }
public DateTime Start { get; init; }
public DateTime End { get; init; }
/// <summary>Destination folder relative to the destination root, forward slashes.</summary>
public required string Folder { get; init; }
public string? Title { get; init; }
/// <summary>
/// The title when the user types none: the source folder's name for a whole folder; null names the sequence
/// after its destination folder.
/// </summary>
public string? DefaultTitle { get; init; }
public IReadOnlyList<PlannedItem> Items { get; init; } = [];
}
/// <summary>
/// The exact result of an import before any byte is written (BRIEF §12.4 "Plan before touching disk"): sessions,
/// folders, file names, what is skipped and why, and the space it needs. Executing it copies exactly this.
/// </summary>
public sealed record ImportPlan
{
public required Guid Id { get; init; }
public required string SourceRoot { get; init; }
public required string DestinationRoot { get; init; }
public DateTimeOffset Created { get; init; }
/// <summary>How the photos were grouped into <see cref="Sessions"/>.</summary>
public ImportGrouping Grouping { get; init; }
public IReadOnlyList<PlannedSession> Sessions { get; init; } = [];
public IReadOnlyList<ImportProblem> Problems { get; init; } = [];
/// <summary>Free space at the destination when planned; null when the volume does not report it.</summary>
public long? AvailableBytes { get; init; }
[JsonIgnore]
public IEnumerable<PlannedItem> Items => Sessions.SelectMany(s => s.Items);
[JsonIgnore]
public int FileCount => Sessions.Sum(s => s.Items.Count);
[JsonIgnore]
public int CopyCount => Items.Count(i => i.Action == ImportAction.Copy);
[JsonIgnore]
public long CopyBytes => Items.Where(i => i.Action == ImportAction.Copy).Sum(i => i.Size);
[JsonIgnore]
public bool HasEnoughSpace => AvailableBytes is not { } free || free >= CopyBytes;
}