fix(archive): Reorganize names as filing does and refuses templates that lose or hide photos

Reorganize with the archive's own templates planned renames and moves for
photos just filed, and some templates could strip extensions, hide sequences
from scans or stall a dry run. Owner review 2, phase 4a findings.

- Filing and Reorganize share one context: uploads and imports name every
  photo with the sequence's camera and lens (its first photo's, or the
  creator's hint), an upload's folder from its first frame in the new
  sequence, and a missing capture time from the sequence start instead of
  the current time. The planner expands folder and title templates from the
  first frame in the sidecar ({name}, {ext}, {iso}...), never today's date.
- Titles expand as text (ExpandText): "Paris: night" stays as typed.
- A sequence whose sidecar cannot be read (torn, another sequence's) keeps
  its folder, title and names, listed with why, and so does every folder
  around it; the dry run reads every sidecar, nested ones included.
- Kept never gives one folder to two sequences of a plan whatever the case,
  and a case-only move onto another folder is refused instead of taken as
  done.
- Shared template rules (TemplateRules) for the server's Reorganize and
  archive settings and the desktop's fields: a file template must keep the
  photo's extension ("{n}{ext}" refused) and tell a session's photos apart;
  a folder template may not write a level scans skip (a leading dot,
  originals, proxies, exports). Levels a title gives are escaped with "_".
- {ext} comes from the file, or from the original name when the file lost
  a photo's extension, so a Reorganize can put it back.
- Unique names cost linear time (UniqueNames) with the same names as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6c734777e01476881a3f34b3188571cc3f7150ab)
This commit is contained in:
Alby96 committed 2026-10-27 16:29:31 +01:00
1 parent 13c2ac321c
commit b253cac8d4
25 files changed
+777 -173

No files matched your search

@@ -113,4 +113,46 @@ public sealed class FrameNamingTests(TestContext context)
Assert.SequenceEqual(session.Items.Select(i => i.FileName),
manifest.Frames.Select(f => PathTemplate.Parse(fileTemplate).ExpandFile(FrameNaming.Context(f, manifest, session.Title))));
}
[Test]
public async Task ALensChangedDuringASessionNamesItsPhotosAsTheirSequenceDoes()
{
var card = context.CreateTempDirectory("card");
var folder = Path.Combine(card, "DCIM", "100CANON");
for (var i = 0; i < 4; i++)
TestPhotos.Write(Path.Combine(folder, $"IMG_{i + 1:0000}.JPG"), Start.AddSeconds(10 * i), i + 1, lens: i < 2 ? "RF15-35mm" : "RF24-70mm");
const string fileTemplate = "{lens}_{n:0000}";
var plan = await PlanAsync(card, context.CreateTempDirectory("archive"), new ImportOptions { FileTemplate = fileTemplate });
var session = plan.Sessions.Single();
// The sequence carries the lens of its first photo, which names every photo of it.
Assert.SequenceEqual(["RF15-35mm_0001.JPG", "RF15-35mm_0002.JPG", "RF15-35mm_0003.JPG", "RF15-35mm_0004.JPG"], session.Items.Select(i => i.FileName));
var frames = session.Items.Select(item => SequenceFrames.FromMetadata(Guid.NewGuid(), "originals/" + item.FileName, Path.GetFileName(item.SourcePath),
item.Size, new string('c', 64), MetadataReader.Read(Path.Combine(card, item.SourcePath)), item.CapturedAt)).ToList();
var manifest = SequenceFrames.Insert(new SequenceManifest
{
Id = Guid.NewGuid(),
Camera = new SidecarCamera { Make = "Canon", Model = "Canon EOS R5" },
Lens = "RF15-35mm",
}, frames);
Assert.Empty(FileRenames.Plan(manifest, session.Title, PathTemplate.Parse(fileTemplate), [.. manifest.Frames.Select(f => f.File)]).Moves);
}
[Test]
public void AnUploadIsNamedWithItsSequencesCameraAsReorganizeNamesIt()
{
// The creator's hint stands for the camera the photos do not name; a second body's photo takes the sequence's camera.
var start = new DateTime(2026, 9, 28, 18, 2, 11);
var frames = new[] { Metadata(start), Metadata(start.AddSeconds(5)) with { Make = "GoPro", Model = "HERO12 Black", LensModel = null } }
.Select((m, i) => SequenceFrames.FromMetadata(Guid.NewGuid(), $"IMG_{i}.JPG", $"IMG_{i}.JPG", 100, new string('a', 64), m)).ToList();
var manifest = SequenceFrames.Insert(new SequenceManifest
{
Id = Guid.NewGuid(),
Camera = new SidecarCamera { Model = "Hinted body" },
Lens = "RF15-35mm F2.8 L IS USM",
}, frames);
var template = PathTemplate.Parse("{camera}_{lens}_{n:00}");
Assert.SequenceEqual(["Hinted body_RF15-35mm F2.8 L IS USM_01.JPG", "Hinted body_RF15-35mm F2.8 L IS USM_02.JPG"],
manifest.Frames.Select(f => template.ExpandFile(FrameNaming.Context(f, manifest, null))));
}
}
@@ -58,6 +58,57 @@ public sealed class PathTemplateTests
public void ReservedWindowsNamesAreEscaped() =>
Assert.Equal("_CON.CR2", PathTemplate.Parse("{title}").ExpandFile(Frame with { Title = "CON" }));
[Theory]
[Case(".NET Conf", "2026/_.NET Conf")]
[Case("Exports", "2026/_Exports")]
[Case("originals", "2026/_originals")]
[Case("PROXIES", "2026/_PROXIES")]
[Case(".chronos", "2026/_.chronos")]
[Case("Harbour", "2026/Harbour")]
public void FolderLevelsScansNeverEnterAreEscaped(string title, string expected)
{
var template = PathTemplate.Parse("{year}/{title}");
Assert.Equal(expected, template.ExpandFolder(Frame with { Title = title }));
Assert.Equal(title != "Harbour", template.WritesHiddenFolder(Frame with { Title = title }));
Assert.Equal(title != "Harbour", PathTemplate.IsHiddenFolder(title));
}
[Theory]
[Case("{n}", true)]
[Case("{name}.{ext}", true)]
[Case("{n} .{ext}", true)]
[Case("{n}.CR3", true)]
[Case("{n}{ext}", false)]
[Case("{n}_{ext}", false)]
[Case("{n}. {ext}", false)]
[Case("{n}.{ext}{ext}", false)]
public void AFileTemplateKeepsTheExtensionOnlyWithItsDot(string template, bool keeps)
{
var parsed = PathTemplate.Parse(template);
Assert.Equal(keeps, parsed.KeepsExtension, template);
Assert.Equal(keeps, parsed.ExpandFile(Frame).EndsWith(".CR2", StringComparison.Ordinal), parsed.ExpandFile(Frame));
}
[Theory]
[Case("{title} 2026", "Paris: night", "Paris: night 2026")]
[Case("{title} 2026", "24/7 roof", "24/7 roof 2026")]
[Case("{title}", "Aux. trip", "Aux. trip")]
[Case("{title}", "What?", "What?")]
[Case("{title}", "Sunset...", "Sunset...")]
[Case(" {title}\t", "Dawn", "Dawn")]
public void TitlesAreTextNotFileNames(string template, string title, string expected) =>
Assert.Equal(expected, PathTemplate.Parse(template).ExpandText(Frame with { Title = title }));
[Test]
public void TextKeepsToTheLongestTitle()
{
var text = PathTemplate.Parse("{title}{title}").ExpandText(Frame with { Title = new string('a', 150) });
Assert.Equal(PathTemplate.MaxTextLength, text.Length);
// A character outside the basic plane is never cut in two.
var emoji = PathTemplate.Parse("{title}").ExpandText(Frame with { Title = new string('a', 199) + "\U0001F305" });
Assert.Equal(new string('a', 199), emoji);
}
[Test]
public void ErrorsCarryPositionAndToken()
{