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

228 lines
12 KiB
C#

using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using Chronos.Core.Archiving;
using Chronos.Core.Imaging;
using Chronos.Core.Naming;
namespace Chronos.Core.Import;
/// <summary>An archive frame that may be the same photo.</summary>
public sealed record ArchiveMatch(Guid FrameId, string? Sha256);
/// <summary>What the planner needs to know about the destination (implemented over the archive index).</summary>
public interface IImportLookup
{
/// <summary>The file was imported from this card before (same path, size and time).</summary>
Task<bool> WasImportedAsync(CardFile file, CancellationToken cancellationToken);
/// <summary>Archive frames with this size and capture time; the planner compares hashes only for these.</summary>
Task<IReadOnlyList<ArchiveMatch>> FindCandidatesAsync(long size, DateTime capturedAt, CancellationToken cancellationToken);
/// <summary>Folder of the sequence an earlier import of this session went to, so a re-import continues it.</summary>
Task<string?> FindSessionFolderAsync(string sessionKey, CancellationToken cancellationToken);
/// <summary>The folder is taken by something else (another sequence), so the session needs another name.</summary>
bool IsFolderTaken(string folder);
}
/// <summary>Turns card photos into an <see cref="ImportPlan"/> (BRIEF §12.4, §13). Nothing is written.</summary>
public static class ImportPlanner
{
public static async Task<ImportPlan> PlanAsync(string sourceRoot, string destinationRoot, IReadOnlyList<CardPhoto> photos,
IReadOnlyList<ImportProblem> problems, ImportOptions options, IImportLookup lookup, CancellationToken cancellationToken = default)
{
var folderTemplate = PathTemplate.Parse(options.FolderTemplate);
var fileTemplate = PathTemplate.Parse(options.FileTemplate);
var wholeFolder = options.Grouping == ImportGrouping.WholeFolder;
var folderName = wholeFolder ? FolderName(sourceRoot) : null;
var drafts = new List<(string Key, List<CardPhoto> Photos)>();
// A whole folder groups by file format alone, never by camera body: RAW+JPEG of the same shots must stay two
// sequences, or each frame would appear twice in one.
Func<CardPhoto, string> groupKey = wholeFolder ? FormatKey : BodyKey;
foreach (var group in photos.GroupBy(groupKey, StringComparer.Ordinal))
{
var ordered = group.OrderBy(p => p.CaptureTime).ThenBy(p => p.File.RelativePath, StringComparer.Ordinal).ToList();
var times = ordered.ConvertAll(p => p.CaptureTime);
bool? UserBreak(int i) =>
options.SplitBefore.Contains(ordered[i].File.RelativePath) ? true : options.MergeAt.Contains(ordered[i].File.RelativePath) ? false : null;
// Only the user's splits break a whole folder: no pause starts a session and no group is too small to stand.
var ranges = wholeFolder
? SessionSplitter.Split(times, TimeSpan.MaxValue, 1, UserBreak)
: SessionSplitter.Split(times, options.SessionGap, options.MinimumSessionFrames, UserBreak);
for (var r = 0; r < ranges.Count; r++)
{
var members = ordered.GetRange(ranges[r].Start, ranges[r].Count);
var key = wholeFolder
? FolderKey(sourceRoot, members[0].Metadata.Format, r == 0 ? null : members[0].CaptureTime)
: SessionKey(group.Key, members[0].CaptureTime);
drafts.Add((key, members));
}
}
drafts.Sort((a, b) => a.Photos[0].CaptureTime.CompareTo(b.Photos[0].CaptureTime) is var c and not 0 ? c : string.CompareOrdinal(a.Key, b.Key));
var claimedFolders = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var sessions = new List<PlannedSession>(drafts.Count);
for (var s = 0; s < drafts.Count; s++)
{
var (key, members) = drafts[s];
var first = members[0];
var title = options.Titles.GetValueOrDefault(key) ?? folderName;
var context = Context(first, first.CaptureTime, s + 1, 1, title);
var folder = await lookup.FindSessionFolderAsync(key, cancellationToken).ConfigureAwait(false)
?? UniqueFolder(folderTemplate.ExpandFolder(context), claimedFolders, lookup);
claimedFolders.Add(folder);
var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var items = new List<PlannedItem>(members.Count);
for (var i = 0; i < members.Count; i++)
{
var photo = members[i];
var name = UniqueName(fileTemplate.ExpandFile(Context(photo, first.CaptureTime, s + 1, i + 1, title)), names);
items.Add(await PlanItemAsync(sourceRoot, photo, name, options, lookup, cancellationToken).ConfigureAwait(false));
}
sessions.Add(new PlannedSession
{
Number = s + 1,
SessionKey = key,
CameraMake = first.Metadata.Make,
CameraModel = first.Metadata.Model,
CameraSerial = first.Metadata.BodySerial,
Cameras = CountCameras(members),
Format = first.Metadata.Format,
Start = first.CaptureTime,
End = members[^1].CaptureTime,
Folder = folder,
Title = title,
DefaultTitle = folderName,
Items = items,
});
}
return new ImportPlan
{
Id = Guid.CreateVersion7(),
SourceRoot = Path.GetFullPath(sourceRoot),
DestinationRoot = Path.GetFullPath(destinationRoot),
Created = DateTimeOffset.UtcNow,
Grouping = options.Grouping,
Sessions = sessions,
Problems = problems,
AvailableBytes = FreeSpace(destinationRoot),
};
}
private static async Task<PlannedItem> PlanItemAsync(string sourceRoot, CardPhoto photo, string name, ImportOptions options, IImportLookup lookup,
CancellationToken cancellationToken)
{
var item = new PlannedItem
{
SourcePath = photo.File.RelativePath,
Size = photo.File.Size,
ModifiedUtc = photo.File.ModifiedUtc,
CapturedAt = photo.CaptureTime,
FileName = name,
};
if (await lookup.WasImportedAsync(photo.File, cancellationToken).ConfigureAwait(false))
return item with { Action = ImportAction.SkipImported };
var candidates = await lookup.FindCandidatesAsync(photo.File.Size, photo.CaptureTime, cancellationToken).ConfigureAwait(false);
if (candidates.Count == 0) return item;
var sha = await ContentHash.Sha256Async(Path.Combine(sourceRoot, photo.File.RelativePath), null, cancellationToken).ConfigureAwait(false);
var match = candidates.FirstOrDefault(c => sha.Equals(c.Sha256, StringComparison.OrdinalIgnoreCase));
if (match is null) return item with { Sha256 = sha };
return item with
{
Sha256 = sha,
ExistingFrameId = match.FrameId,
Action = options.Duplicates == DuplicatePolicy.Skip ? ImportAction.SkipInArchive : ImportAction.Copy,
};
}
private static NamingContext Context(CardPhoto photo, DateTime sessionStart, int session, int number, string? title) => new()
{
CapturedAt = photo.CaptureTime,
SessionStart = sessionStart,
Session = session,
Number = number,
NumberInSession = number,
CameraMake = photo.Metadata.Make,
CameraModel = photo.Metadata.Model,
Lens = photo.Metadata.LensModel,
Iso = photo.Metadata.Iso,
ExposureTime = photo.Metadata.ExposureTime,
FNumber = photo.Metadata.FNumber,
OriginalName = Path.GetFileNameWithoutExtension(photo.File.RelativePath),
Extension = Path.GetExtension(photo.File.RelativePath).TrimStart('.'),
Title = title,
};
/// <summary>Camera body and file format: RAW+JPEG shooting yields two sequences instead of doubled frames.</summary>
private static string BodyKey(CardPhoto p) => $"{CameraKey(p)}|{p.Metadata.Format}";
private static string CameraKey(CardPhoto p) => $"{p.Metadata.Make}|{p.Metadata.Model}|{p.Metadata.BodySerial}";
/// <summary>Camera bodies that took the photos; a photo without a camera in its metadata is no body of its own.</summary>
private static int CountCameras(List<CardPhoto> photos) =>
System.Math.Max(1, photos.Where(p => p.Metadata.Model is not null).Select(CameraKey).Distinct(StringComparer.Ordinal).Count());
/// <summary>File format alone (a whole folder): RAW+JPEG still yields two sequences instead of doubled frames.</summary>
private static string FormatKey(CardPhoto p) => p.Metadata.Format.ToString();
private static string SessionKey(string bodyKey, DateTime start) =>
"card-" + Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(bodyKey)))[..8] + "-"
+ start.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture);
/// <summary>
/// The key of a whole folder's sequence: the same folder of this PC and the same format always give the same key,
/// so an interrupted import resumes into its sequence and a later import of the folder adds to it. A part the user
/// split off also carries the time of its first photo.
/// </summary>
internal static string FolderKey(string sourceRoot, ImageFileFormat format, DateTime? splitAt = null)
{
var path = Path.TrimEndingDirectorySeparator(Path.GetFullPath(sourceRoot));
// Windows compares paths without case: D:\Trip and d:\trip are one folder.
if (OperatingSystem.IsWindows()) path = path.ToUpperInvariant();
// The same path on another PC is another folder: a server files every client's uploads by these keys.
var identity = $"{Environment.MachineName.ToUpperInvariant()}|{path}|{format}";
var key = "folder-" + Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(identity)))[..16];
return splitAt is { } start ? key + "-" + start.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture) : key;
}
/// <summary>The picked folder's own name, the default title of a whole folder; null for a drive root.</summary>
private static string? FolderName(string sourceRoot) =>
Path.GetFileName(Path.TrimEndingDirectorySeparator(Path.GetFullPath(sourceRoot))) is { Length: > 0 } name ? name : null;
private static string UniqueFolder(string folder, HashSet<string> claimed, IImportLookup lookup)
{
if (folder.Length == 0) folder = "Import";
var candidate = folder;
for (var n = 2; claimed.Contains(candidate) || lookup.IsFolderTaken(candidate); n++)
candidate = string.Create(CultureInfo.InvariantCulture, $"{folder} ({n})");
return candidate;
}
/// <summary>Adds _2, _3… before the extension when the template gives two photos the same name.</summary>
internal static string UniqueName(string name, HashSet<string> taken)
{
var candidate = name;
var stem = Path.GetFileNameWithoutExtension(name);
var extension = Path.GetExtension(name);
for (var n = 2; !taken.Add(candidate); n++) candidate = string.Create(CultureInfo.InvariantCulture, $"{stem}_{n}{extension}");
return candidate;
}
private static long? FreeSpace(string path)
{
try
{
var full = Path.GetFullPath(path);
while (!Directory.Exists(full) && Path.GetDirectoryName(full) is { } parent) full = parent;
// Windows wants a drive root; Unix accepts any directory and reports the volume it is mounted on.
return new DriveInfo(OperatingSystem.IsWindows() ? Path.GetPathRoot(full)! : full).AvailableFreeSpace;
}
catch (Exception ex) when (ex is ArgumentException or IOException or UnauthorizedAccessException)
{
return null;
}
}
}