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>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-26 22:00:55 +01:00
1 parent 5209579631
commit 445dfd41a3
14 files changed
+419 -26

No files matched your search

@@ -1,6 +1,8 @@
using Chronos.Core.Archiving;
using Chronos.Core.Imaging;
using Chronos.Core.Import;
using Chronos.Core.Localization;
using Chronos.Core.Metadata;
using Chronos.Core.Tests.Archiving;
using Chronos.Testing;
using static Chronos.Core.Tests.Import.ImportTestKit;
@@ -163,6 +165,118 @@ public sealed class ImportPlannerTests(TestContext context)
Assert.SequenceEqual([5, 5, 10], split.Sessions.Select(s => s.Items.Count));
}
[Test]
public async Task AWholeFolderIsOneSequenceWhateverTheCameraBodiesAndPauses()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna eruption");
TestPhotos.WriteSeries(folder, Start, 10, Interval, prefix: "CAN_");
TestPhotos.WriteSeries(folder, Start.AddHours(3), 10, Interval, firstSeed: 20, prefix: "CAN_", firstNumber: 11);
TestPhotos.WriteSeries(folder, Start.AddSeconds(5), 3, Interval, firstSeed: 40, prefix: "NIK_", make: "NIKON CORPORATION", model: "NIKON Z 8");
var destination = context.CreateTempDirectory("archive");
Assert.Equal(3, (await PlanAsync(folder, destination)).Sessions.Count, "by sessions: two Canon sessions and the Nikon");
var plan = await PlanAsync(folder, destination, new ImportOptions { Grouping = ImportGrouping.WholeFolder, FolderTemplate = "{year}/{title}" });
Assert.Equal(ImportGrouping.WholeFolder, plan.Grouping);
var session = plan.Sessions.Single();
Assert.Equal(23, session.Items.Count);
Assert.Equal(2, session.Cameras);
Assert.Equal("Etna eruption", session.Title, "the folder's name is the default title");
Assert.Equal("Etna eruption", session.DefaultTitle);
Assert.Equal("2026/Etna eruption", session.Folder);
Assert.SequenceEqual(session.Items.OrderBy(i => i.CapturedAt).Select(i => i.SourcePath), session.Items.Select(i => i.SourcePath),
"both bodies in capture order");
Assert.Equal("2026-09-28_21-01-30_0023.JPG", session.Items[^1].FileName);
}
[Test]
public async Task AWholeFolderOfRawAndJpegIsTwoSequencesSoNoFrameIsDoubled()
{
var photos = new List<CardPhoto>();
for (var i = 0; i < 6; i++)
{
// An hour between shots: far beyond the session gap, ignored for a whole folder.
var at = Start.AddHours(i);
photos.Add(Photo($"IMG_{i:0000}.JPG", at, ImageFileFormat.Jpeg));
photos.Add(Photo($"IMG_{i:0000}.CR3", at, ImageFileFormat.Cr3));
}
var plan = await ImportPlanner.PlanAsync(context.CreateTempDirectory("raw+jpeg"), context.CreateTempDirectory("archive"), photos, [],
new ImportOptions { Grouping = ImportGrouping.WholeFolder }, new FakeLookup());
Assert.Equal(2, plan.Sessions.Count, "one sequence per format");
Assert.SequenceEqual([ImageFileFormat.Jpeg, ImageFileFormat.Cr3], plan.Sessions.Select(s => s.Format).OrderBy(f => f));
foreach (var session in plan.Sessions)
{
Assert.Equal(6, session.Items.Count);
Assert.Equal(1, session.Items.Select(i => Path.GetExtension(i.SourcePath)).Distinct().Count(), "one format per sequence");
}
Assert.Equal(12, plan.Items.Select(i => i.SourcePath).Distinct().Count(), "every photo is planned once");
Assert.NotEqual(plan.Sessions[0].SessionKey, plan.Sessions[1].SessionKey);
Assert.NotEqual(plan.Sessions[0].Folder, plan.Sessions[1].Folder);
}
[Test]
public async Task AWholeFolderTakesItsSubfoldersOnlyWhenAsked()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Construction");
TestPhotos.WriteSeries(folder, Start, 4, Interval);
TestPhotos.WriteSeries(Path.Combine(folder, "week 2"), Start.AddDays(7), 4, Interval, firstSeed: 50, firstNumber: 5);
TestPhotos.WriteSeries(Path.Combine(folder, "week 2", "monday"), Start.AddDays(8), 4, Interval, firstSeed: 70, firstNumber: 9);
var destination = context.CreateTempDirectory("archive");
var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder };
var all = (await PlanAsync(folder, destination, whole)).Sessions.Single();
Assert.Equal(12, all.Items.Count);
Assert.Equal("week 2/monday/IMG_0012.JPG", all.Items[^1].SourcePath);
var top = (await PlanAsync(folder, destination, whole with { IncludeSubfolders = false })).Sessions.Single();
Assert.Equal(4, top.Items.Count);
Assert.Equal(all.SessionKey, top.SessionKey, "the same folder's sequence, whatever is scanned in it");
}
[Test]
public async Task AWholeFolderKeepsItsKeySoAnInterruptedImportResumesIntoItsSequence()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Etna");
TestPhotos.WriteSeries(folder, Start, 10, Interval);
var destination = context.CreateTempDirectory("archive");
var whole = new ImportOptions { Grouping = ImportGrouping.WholeFolder };
var first = (await PlanAsync(folder, destination, whole)).Sessions.Single();
Assert.True(first.SessionKey.StartsWith("folder-", StringComparison.Ordinal), first.SessionKey);
Assert.Equal(first.SessionKey, ImportPlanner.FolderKey(folder + Path.DirectorySeparatorChar, ImageFileFormat.Jpeg),
"a trailing separator names the same folder");
Assert.NotEqual(first.SessionKey, ImportPlanner.FolderKey(folder, ImageFileFormat.Cr3));
Assert.NotEqual(first.SessionKey, ImportPlanner.FolderKey(Path.Combine(folder, "other"), ImageFileFormat.Jpeg));
// Photos added later, earlier ones too, keep the folder's key.
TestPhotos.WriteSeries(folder, Start.AddDays(-1), 2, Interval, firstSeed: 90, prefix: "OLD_");
Assert.Equal(first.SessionKey, (await PlanAsync(folder, destination, whole)).Sessions.Single().SessionKey);
var lookup = new FakeLookup();
lookup.SessionFolders[first.SessionKey] = "2026/Etna";
Assert.Equal("2026/Etna", (await PlanAsync(folder, destination, whole, lookup)).Sessions.Single().Folder, "the sequence of the interrupted import");
var typed = whole with { Titles = new Dictionary<string, string> { [first.SessionKey] = "Etna at night" } };
var titled = (await PlanAsync(folder, destination, typed)).Sessions.Single();
Assert.Equal("Etna at night", titled.Title);
Assert.Equal("Etna", titled.DefaultTitle);
}
[Test]
public async Task AWholeFolderSplitsOnlyWhereTheUserSplitsIt()
{
var folder = Path.Combine(context.CreateTempDirectory("source"), "Harbour");
TestPhotos.WriteSeries(folder, Start, 10, Interval);
var destination = context.CreateTempDirectory("archive");
var split = new ImportOptions { Grouping = ImportGrouping.WholeFolder, SplitBefore = new HashSet<string> { "IMG_0009.JPG" } };
var plan = await PlanAsync(folder, destination, split);
Assert.SequenceEqual([8, 2], plan.Sessions.Select(s => s.Items.Count), "a part the user split off stays, however short");
Assert.Equal(ImportPlanner.FolderKey(folder, ImageFileFormat.Jpeg), plan.Sessions[0].SessionKey);
Assert.NotEqual(plan.Sessions[0].SessionKey, plan.Sessions[1].SessionKey);
Assert.Equal(plan.Sessions[1].SessionKey, (await PlanAsync(folder, destination, split)).Sessions[1].SessionKey);
var merged = split with { SplitBefore = new HashSet<string>(), MergeAt = new HashSet<string> { "IMG_0009.JPG" } };
Assert.Equal(10, (await PlanAsync(folder, destination, merged)).Sessions.Single().Items.Count, "merging the part back undoes the split");
}
[Test]
public async Task APhotoWithoutExifIsPlannedAtItsFileTime()
{
@@ -177,4 +291,8 @@ public sealed class ImportPlannerTests(TestContext context)
// No camera identity: it forms its own body group rather than joining the Canon session.
Assert.SequenceEqual([8, 1], plan.Sessions.Select(s => s.Items.Count));
}
/// <summary>A photo as the scanner reads it, without a file behind it (the planner hashes only duplicate candidates).</summary>
private static CardPhoto Photo(string path, DateTime captured, ImageFileFormat format) => new(new CardFile(path, 1_000, captured),
new FrameMetadata { Format = format, Make = "Canon", Model = "Canon EOS R5", BodySerial = "0431", CapturedAt = captured });
}