using Titano.Core.Archiving; using Titano.Core.Diagnostics; using Titano.Core.Metadata; namespace Titano.Core.Import; /// A photo file on a card or source folder. /// Path below the source root, forward slashes (the card import history key). public sealed record CardFile(string RelativePath, long Size, DateTime ModifiedUtc); /// A card photo with its light metadata. public sealed record CardPhoto(CardFile File, FrameMetadata Metadata) { /// Capture time, or the file time when the photo records none. public DateTime CaptureTime => Metadata.CapturedAt ?? File.ModifiedUtc; } /// A file that could not be read while planning; it is listed, never a reason to stop. public sealed record ImportProblem(string RelativePath, string Message); /// Finds photos on a card (BRIEF §12.4, §13): the DCIM tree when present, otherwise the root. 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, }; /// /// Photos below : everything under DCIM (camera folders) when the card has one, /// otherwise the root's photos and, with , its subfolders'. Ordered by path. /// public static IReadOnlyList 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(); 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; } /// Reads light metadata of every file; unreadable files become . public static IReadOnlyList ReadMetadata(string root, IReadOnlyList files, List problems, IProgress? progress = null, CancellationToken cancellationToken = default) { var photos = new List(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; } }