Files
Chronos/src/Titano.Core/Import/CardScanner.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

88 lines
3.9 KiB
C#

using Titano.Core.Archiving;
using Titano.Core.Diagnostics;
using Titano.Core.Metadata;
namespace Titano.Core.Import;
/// <summary>A photo file on a card or source folder.</summary>
/// <param name="RelativePath">Path below the source root, forward slashes (the card import history key).</param>
public sealed record CardFile(string RelativePath, long Size, DateTime ModifiedUtc);
/// <summary>A card photo with its light metadata.</summary>
public sealed record CardPhoto(CardFile File, FrameMetadata Metadata)
{
/// <summary>Capture time, or the file time when the photo records none.</summary>
public DateTime CaptureTime => Metadata.CapturedAt ?? File.ModifiedUtc;
}
/// <summary>A file that could not be read while planning; it is listed, never a reason to stop.</summary>
public sealed record ImportProblem(string RelativePath, string Message);
/// <summary>Finds photos on a card (BRIEF §12.4, §13): the <c>DCIM</c> tree when present, otherwise the root.</summary>
public static class CardScanner
{
private const int MaxDepth = 12;
private static readonly EnumerationOptions Listing = new()
{
IgnoreInaccessible = true,
AttributesToSkip = FileAttributes.ReparsePoint | FileAttributes.System | FileAttributes.Hidden,
RecurseSubdirectories = false,
MatchCasing = MatchCasing.CaseInsensitive,
};
/// <summary>
/// Photos below <paramref name="root"/>: everything under <c>DCIM</c> (camera folders) when the card has one,
/// otherwise the root's photos and, with <paramref name="includeSubfolders"/>, its subfolders'. Ordered by path.
/// </summary>
public static IReadOnlyList<CardFile> Find(string root, bool includeSubfolders, CancellationToken cancellationToken = default)
{
var dcim = Directory.EnumerateDirectories(root, "DCIM", Listing).FirstOrDefault();
var start = dcim ?? root;
var recurse = dcim is not null || includeSubfolders;
var files = new List<CardFile>();
var pending = new Stack<(string Path, int Depth)>();
pending.Push((start, 0));
while (pending.Count > 0)
{
cancellationToken.ThrowIfCancellationRequested();
var (directory, depth) = pending.Pop();
foreach (var file in new DirectoryInfo(directory).EnumerateFiles("*", Listing))
{
if (ArchiveScanner.IsPhoto(file.Name))
files.Add(new CardFile(Path.GetRelativePath(root, file.FullName).Replace('\\', '/'), file.Length, file.LastWriteTimeUtc));
}
if (!recurse || depth >= MaxDepth) continue;
foreach (var child in Directory.EnumerateDirectories(directory, "*", Listing))
{
if (!Path.GetFileName(child).StartsWith('.')) pending.Push((child, depth + 1));
}
}
files.Sort((a, b) => string.CompareOrdinal(a.RelativePath, b.RelativePath));
return files;
}
/// <summary>Reads light metadata of every file; unreadable files become <paramref name="problems"/>.</summary>
public static IReadOnlyList<CardPhoto> ReadMetadata(string root, IReadOnlyList<CardFile> files, List<ImportProblem> problems,
IProgress<int>? progress = null, CancellationToken cancellationToken = default)
{
var photos = new List<CardPhoto>(files.Count);
for (var i = 0; i < files.Count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
var file = files[i];
try
{
photos.Add(new CardPhoto(file, MetadataReader.Read(Path.Combine(root, file.RelativePath), cancellationToken)));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Log.Warning("import", $"Cannot read {file.RelativePath}", ex);
problems.Add(new ImportProblem(file.RelativePath, ex.Message));
}
progress?.Report(i + 1);
}
return photos;
}
}