Files
Chronos/src/Titano.Core/Import/ImportPlan.cs
T
Claude 8cb9672520 feat(core): card import planning and verified copy engine
Card scanning (DCIM and flat), session splitting with test-shot merging and
user splits/merges, per-body import plans named by templates with archive
duplicate detection, hash-verified resumable copy with per-file errors, a
plain-folder destination and a verified card erase with dry run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 12:30:14 +00:00

119 lines
4.9 KiB
C#

using System.Text.Json.Serialization;
using Titano.Core.Imaging;
using Titano.Core.Naming;
namespace Titano.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>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>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, one sequence in the archive.</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; }
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; }
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; }
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;
}