From b253cac8d43d14c41be1e81976188ff146a782ef Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 27 Oct 2026 16:05:26 +0100 Subject: [PATCH] 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 (cherry picked from commit 6c734777e01476881a3f34b3188571cc3f7150ab) --- docs/ARCHITECTURE.md | 4 +- i18n/en/Archive.resx | 2 +- i18n/en/Errors.resx | 2 + i18n/it/Archive.it.resx | 2 +- i18n/it/Errors.it.resx | 2 + src/Chronos.App/Archive/ReorganizePlans.cs | 2 +- src/Chronos.App/Settings/NamingPreview.cs | 38 +--- src/Chronos.Core/Archiving/FileRenames.cs | 13 +- src/Chronos.Core/Archiving/MassRename.cs | 168 ++++++++++----- src/Chronos.Core/Import/ImportPlanner.cs | 10 +- src/Chronos.Core/Naming/FrameNaming.cs | 90 ++++++-- src/Chronos.Core/Naming/PathTemplate.cs | 62 +++++- src/Chronos.Core/Naming/TemplateRules.cs | 65 ++++++ src/Chronos.Data/Archiving/ArchiveIngest.cs | 35 ++-- src/Chronos.Data/Archiving/MassRenamer.cs | 31 +-- src/Chronos.Server/Api/ArchiveEndpoints.cs | 8 +- .../Settings/SettingsLogicTests.cs | 14 ++ .../Archiving/ReorganizeTests.cs | 193 ++++++++++++++++++ .../Archiving/TestPhotos.cs | 8 +- .../Naming/FrameNamingTests.cs | 42 ++++ .../Naming/PathTemplateTests.cs | 51 +++++ .../Archiving/ArchiveFixture.cs | 8 +- .../Archiving/ReorganizeTests.cs | 76 ++++++- tests/Chronos.Server.Tests/Api/AdminTests.cs | 12 ++ .../Api/ReorganizeTests.cs | 12 ++ 25 files changed, 777 insertions(+), 173 deletions(-) create mode 100644 src/Chronos.Core/Naming/TemplateRules.cs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 12cf3a52..2a0c0352 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -42,7 +42,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | `Video` (+ `Mp4`, `Avi`, `Bitstream`, `Yuv`, `Managed`) | `IVideoEncoderProvider`, MP4 muxer and inspector, AVI-MJPEG, NV12/P010, managed encoders (MJPEG-AVI, GIF, APNG, image sequences) | | `Archiving` | Archive layout and paths, sidecars (`sequence.json`, `project.json`), scanner, index snapshot, leases, thumbnail packs, verify, adopt, rename plans (`RenamePlanner`: folder, title and file templates, `RenameTemplates`, the plan's fingerprint; `FileRenames`: a sequence's new file names; `FileRenameJournal`: two-phase renames through temporary names, `.chronos/rename-files.json`, a recovery safe to run again from wherever it stopped, a new name taken meanwhile sending that photo and its companions back to their old names or a free `_2`; `FrameCompanions`: the XMP files that follow a photo, one rule for the trash and Reorganize, matched whatever their case and each folder listed once by `FrameCompanions.Index`) | | `Import` | Card scan, session detection, import plan, hash-verified copy, card eraser (re-hash before delete) | -| `Naming` | Folder/file templates and tokens; `FrameNaming` (the naming context and unique file name of a frame, shared by import, upload and Reorganize) | +| `Naming` | Folder/file templates and tokens (folder levels scans skip escaped with `_`; `ExpandText` for titles); `FrameNaming` (the naming context and unique file name of a frame, shared by import, upload and Reorganize: the sequence's camera and lens, the first frame for folders; `UniqueNames`: `_2`, `_3`… in linear time); `TemplateRules` (what every place a template is typed or sent refuses: a folder scans skip, a lost extension, names that repeat) | | `Model` | Domain entities and enums (§5) shared by Data, Server and clients | | `Packaging` | TPK1 installer payload writer/reader, staged install transaction | | `Diagnostics` | `Log` (never throws: a line its sink cannot write is dropped), `FileLogSink` with `LogFileOptions` (level, daily or hourly files, numbered parts past a size, pruning by age and by the total size, never the file being written), synthetic sequences, `SecretScrubber`, `DiagnosticsArchive` (scrubbed diagnostics zip) | @@ -58,7 +58,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | `Jobs` | `JobEngine` (one job at a time; reviews the priority queue and makes a lower running job yield, requeued with its checkpoint; no history purge, the housekeeping schedule's clean-up does it), `JobPriorityQueue` (a level per job kind, the claim order, settings round trip), `IJobHandler`, `IJobHandlerProvider`, `JobContext` (progress, checkpoints, follow-up jobs that never interrupt their parent), `JobThroughput`/`JobEta` (rate and bounded time left) | | `Scheduling` | `Scheduler` (one per host: seeds the host's system schedules, claims due runs by compare-and-set, catch-up, clock changes, create/edit/reset/delete/run now as API v1 DTOs), `ScheduleRecurrence` (the in-house `every:`/`daily:`/`weekly:`/`monthly:` form, wall-clock times across DST), `ScheduleActions` (action names, parameters, availability, the archive jobs a run queues), `IScheduleHost` and `SystemSchedule` (what the server and the desktop supply), `ScheduleOutcome`, `ScheduleMapping` (model ↔ DTO) | | `Housekeeping` | `BackupJobHandler`, `CleanupJobHandler`, `ICleanupStep` with the shared steps (`CleanupSteps`: job history, audit log, trash report that never deletes); hosts add their own steps through `ArchiveServiceOptions.CleanupSteps` | -| `Archiving` | `ArchiveService` (create/adopt/open, archive settings written to `archive.json` then the index), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages` (thumbnail and proxy passes, several photos at once through `OrderedWork`, which hands results back in frame order), `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer` (mass rename and Reorganize: the dry run reads each sidecar for a file template; each step checked under its leases before anything of it moves (where its sequences are, what is inside and at its new folder, a sidecar of a newer format, the title it was planned from, its file renames by digest at the planned frames version), then the folder, every index row, then the files under the plan's journal in `journal/` (a sequence whose files are found changed or in use there keeps their names, and the step completes); the journal keeps the tally of the whole plan, and a plan its job held (paused, cancelled or failed) is left to that job, only its half-done file renames completed at an archive open; the file renames of a step: sidecar, index (`FrameRepository.RenameFilesAsync`) and the sequence's journal), `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails; lists sequence and photo entries; `SequenceTrash.Empty`: the emptying's dry run, fingerprint and erase, each entry checked against its fingerprint again under its lease), `FrameTrash` (photos of a sequence into `trash/photos` with a dry run, a journal that undoes an interrupted deletion whose entry stays unlisted until its sidecar is written, tombstones (a photo never hashed is hashed as it goes; one whose original is missing goes without a file or tombstone; a frame that stays and may be a twin is hashed, its hash kept), positions parked and placed back, proxies and thumbnails moved to follow their frames; `FrameTrash.Restore`: restore re-hashes each file and says why each kept photo stayed (`KeptReason`), under a journal of its own into the sequence that another writer settles only under the entry's lease, into a new sequence through the ingest when theirs is gone, each original staged inside its own entry and the record rewritten after each photo filed), job handlers | +| `Archiving` | `ArchiveService` (create/adopt/open, archive settings written to `archive.json` then the index), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages` (thumbnail and proxy passes, several photos at once through `OrderedWork`, which hands results back in frame order), `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer` (mass rename and Reorganize: the dry run reads each sidecar for a file template; each step checked under its leases before anything of it moves (where its sequences are, what is inside and at its new folder, a sidecar of a newer format, the title it was planned from, its file renames by digest at the planned frames version), then the folder, every index row, then the files under the plan's journal in `journal/` (a sequence whose files are found changed or in use there keeps their names, and the step completes); the journal keeps the tally of the whole plan, and a plan its job held (paused, cancelled or failed) is left to that job, only its half-done file renames completed at an archive open; the file renames of a step: sidecar, a sequence whose sidecar cannot be read staying as it is with every folder around it, index (`FrameRepository.RenameFilesAsync`) and the sequence's journal), `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails; lists sequence and photo entries; `SequenceTrash.Empty`: the emptying's dry run, fingerprint and erase, each entry checked against its fingerprint again under its lease), `FrameTrash` (photos of a sequence into `trash/photos` with a dry run, a journal that undoes an interrupted deletion whose entry stays unlisted until its sidecar is written, tombstones (a photo never hashed is hashed as it goes; one whose original is missing goes without a file or tombstone; a frame that stays and may be a twin is hashed, its hash kept), positions parked and placed back, proxies and thumbnails moved to follow their frames; `FrameTrash.Restore`: restore re-hashes each file and says why each kept photo stayed (`KeptReason`), under a journal of its own into the sequence that another writer settles only under the entry's lease, into a new sequence through the ingest when theirs is gone, each original staged inside its own entry and the record rewritten after each photo filed), job handlers | | `Processing` | `ProcessingService`, `SequenceAnalyser`, `DeepReports` (a saved deep report while it still describes the frames), `SequenceRender`, `ProjectStore`, `ProjectBundles` (a `.chronos` file for a sequence, and the sequence a file names), `RenderNodeClient`, job handlers | | `Sync` | `SyncService` (sync table of one server: compare, link by content, settle conflicts, enqueue transfers), `SyncRules` (status, settings merge and differences, no I/O), `SyncTombstones` (photos either side removed are not offered back, a replaced photo counts as its replacement), `SyncPositions` (the settings' frame positions translated by photo to the frames of the copy they go to), `SyncLinkStore` (`sync_links`, `sync_state`), `SyncTransfers` and `SyncJobHandler` (resumable, hash-verified uploads and downloads as jobs; downloads commit through the archive ingest), `SyncAgent` (background comparison, server events, backoff, offline proxies), `ProxyCache` and `RemoteSequence` (server sequences opened with proxies), `ISyncRemote` (the server calls; `ChronosSyncRemote` over `ChronosClient`) | diff --git a/i18n/en/Archive.resx b/i18n/en/Archive.resx index 6a661956..1f192fa6 100644 --- a/i18n/en/Archive.resx +++ b/i18n/en/Archive.resx @@ -223,7 +223,7 @@ Photos kept in place outside originals keep their names.long: why photos keep their names in a Reorganize dry run Missing photos keep their names.long: why photos keep their names in a Reorganize dry run Photos never verified keep their names; verify the sequence first.long: why photos keep their names in a Reorganize dry run - The sequence file cannot be read; its photos keep their names.long: why photos keep their names in a Reorganize dry run + The sequence file cannot be read; the sequence stays as it is, and so does any folder it is in.long: why a sequence keeps its folder, title and photo names in a Reorganize dry run Follows its folder {0}: {1} to {2}long: what a screen reader says for one line of the Reorganize dry run; {0} Folder, Title or File, {1} before, {2} after File diff --git a/i18n/en/Errors.resx b/i18n/en/Errors.resx index 13ec9fad..50a1c81e 100644 --- a/i18n/en/Errors.resx +++ b/i18n/en/Errors.resx @@ -38,8 +38,10 @@ The template is empty. Empty token at position {1}: write a name between the braces. File names cannot contain a slash; put folders in the folder template. + The photos would lose their extension: write a dot before {ext}, or leave {ext} out. The format after the colon does not suit its token. Names would repeat: add the frame number, the time or the original name. + Scans skip folders that start with a dot or are named originals, proxies or exports. The brace opened at position {1} is never closed. Unknown token “{0}” at position {1}. The closing brace at position {1} has no opening one; double it for a literal brace. diff --git a/i18n/it/Archive.it.resx b/i18n/it/Archive.it.resx index bb9bae0d..846a8639 100644 --- a/i18n/it/Archive.it.resx +++ b/i18n/it/Archive.it.resx @@ -217,7 +217,7 @@ Le foto tenute sul posto fuori da originals mantengono il loro nome. Le foto mancanti mantengono il loro nome. Le foto mai verificate mantengono il loro nome; verifica prima la sequenza. - Il file della sequenza non è leggibile; le sue foto mantengono il loro nome. + Il file della sequenza non è leggibile; la sequenza resta com'è, e così ogni cartella che la contiene. Segue la sua cartella {0}: da {1} a {2} File diff --git a/i18n/it/Errors.it.resx b/i18n/it/Errors.it.resx index 4d36a221..8230fc73 100644 --- a/i18n/it/Errors.it.resx +++ b/i18n/it/Errors.it.resx @@ -38,8 +38,10 @@ Il modello è vuoto. Segnaposto vuoto alla posizione {1}: scrivi un nome tra le parentesi graffe. I nomi dei file non possono contenere la barra; metti le cartelle nel modello delle cartelle. + Le foto perderebbero l'estensione: scrivi un punto prima di {ext}, oppure togli {ext}. Il formato dopo i due punti non è adatto al suo segnaposto. I nomi si ripeterebbero: aggiungi il numero del fotogramma, l'ora o il nome originale. + Le scansioni saltano le cartelle che iniziano con un punto o si chiamano originals, proxies o exports. La parentesi graffa aperta alla posizione {1} non viene mai chiusa. Segnaposto sconosciuto «{0}» alla posizione {1}. La parentesi graffa chiusa alla posizione {1} non ha un'apertura; raddoppiala per una graffa letterale. diff --git a/src/Chronos.App/Archive/ReorganizePlans.cs b/src/Chronos.App/Archive/ReorganizePlans.cs index ed2b387d..b756fc2f 100644 --- a/src/Chronos.App/Archive/ReorganizePlans.cs +++ b/src/Chronos.App/Archive/ReorganizePlans.cs @@ -61,7 +61,7 @@ public static class ReorganizePlans if (parsed.Errors is [var error, ..]) return Loc.L("Errors.Template." + error.Code, error.Token ?? string.Empty, error.Position + 1); try { - foreach (var frame in NamingPreview.SampleFrames) parsed.ExpandFile(frame); + foreach (var frame in NamingPreview.SampleFrames) parsed.ExpandText(frame); } catch (FormatException) { diff --git a/src/Chronos.App/Settings/NamingPreview.cs b/src/Chronos.App/Settings/NamingPreview.cs index 98cd0f0d..4b1d7819 100644 --- a/src/Chronos.App/Settings/NamingPreview.cs +++ b/src/Chronos.App/Settings/NamingPreview.cs @@ -15,7 +15,10 @@ public static class NamingPreview /// public static IReadOnlyList SampleFrames => BuildSample(); - /// The first problem of , localized; null when it is valid. + /// + /// The first problem of , localized; null when it is valid. Beyond parsing and expanding, + /// the rules every place a template is used shares (), as the server checks them. + /// public static string? Validate(string? template, bool isFolder) { var text = template ?? string.Empty; @@ -33,12 +36,12 @@ public static class NamingPreview var name = isFolder ? parsed.ExpandFolder(frame) : parsed.ExpandFile(frame); if (name.Length == 0) return Loc.L("Errors.Template.Empty", string.Empty, 1); } + if ((isFolder ? TemplateRules.FolderRule(parsed) : TemplateRules.FileRule(parsed)) is { } rule) return Loc.L("Errors.Template." + rule); } catch (FormatException) { return Loc.L("Errors.Template.Format"); } - if (!isFolder && !NumbersFrames(text)) return Loc.L("Errors.Template.NotUnique"); return null; } @@ -53,34 +56,5 @@ public static class NamingPreview private static string Combine(string folder, string file) => folder.Length == 0 ? file : folder + "/" + file; - /// File names must tell frames apart: a frame number, the time of day or the original name. - private static bool NumbersFrames(string template) - { - var parsed = PathTemplate.Parse(template); - var names = SampleFrames.Select(parsed.ExpandFile).ToList(); - return names.Distinct(StringComparer.OrdinalIgnoreCase).Count() == names.Count; - } - - private static List BuildSample() - { - var start = new DateTime(2026, 9, 28, 18, 2, 11); - var title = Loc.L("Settings.Naming.SampleTitle"); - return Enumerable.Range(0, 3).Select(i => new NamingContext - { - CapturedAt = start.AddSeconds(2 * i), - SessionStart = start, - Session = 1, - Number = i + 1, - NumberInSession = i + 1, - CameraMake = "Canon", - CameraModel = "Canon EOS R5", - Lens = "RF15-35mm F2.8 L IS USM", - Iso = 100, - ExposureTime = 1.0 / 250, - FNumber = 8, - OriginalName = "IMG_" + (4211 + i).ToString("0000", System.Globalization.CultureInfo.InvariantCulture), - Extension = "CR2", - Title = title, - }).ToList(); - } + private static IReadOnlyList BuildSample() => TemplateRules.Sample(Loc.L("Settings.Naming.SampleTitle")); } diff --git a/src/Chronos.Core/Archiving/FileRenames.cs b/src/Chronos.Core/Archiving/FileRenames.cs index fa9f0fea..b3241d0f 100644 --- a/src/Chronos.Core/Archiving/FileRenames.cs +++ b/src/Chronos.Core/Archiving/FileRenames.cs @@ -49,9 +49,10 @@ public sealed record FileRenamePlan(IReadOnlyList Moves, int Frames, /// /// Plans the new file names of a sequence's frames from a file template (Reorganize, owner review 2, R7; BRIEF §7.1, §12.5). -/// Frames are named in index order exactly as filing names them (); a name already given, or held -/// by a file that stays (a frame left alone, a file no frame lists), gets _2, _3… as filing does, so the same -/// frames, template and folder always give the same names. Only photos under originals/ whose SHA-256 is known and +/// Frames are named in index order with the context an upload in capture order is filed with (, +/// which says where filing gave other names); a name already given, or held by a file that stays (a frame left alone, a file +/// no frame lists), gets _2, _3… as filing does, so the same frames, template and folder always give the same +/// names. Only photos under originals/ whose SHA-256 is known and /// whose file is there are renamed, each within its folder; the XMP companions of a photo follow it /// (; one named by a stem two frames share follows the first). The sidecar's frame order never /// changes. @@ -101,14 +102,14 @@ public static class FileRenames var moves = new List(); var samples = new List(); var renamed = 0; + var names = new UniqueNames(taken); foreach (var frame in eligible) { var own = companions[frame.Id]; var tails = own.ConvertAll(c => FrameCompanions.Tail(c, frame.File)); var directory = frame.File[..(frame.File.LastIndexOf('/') + 1)]; - var file = FrameNaming.FileName(template, FrameNaming.Context(frame, manifest, title), - candidate => !taken.Contains(candidate) && tails.TrueForAll(t => !taken.Contains(Stem(candidate) + t)), directory); - taken.Add(file); + var file = names.Claim(directory + template.ExpandFile(FrameNaming.Context(frame, manifest, title)), + candidate => tails.TrueForAll(t => !taken.Contains(Stem(candidate) + t))); foreach (var tail in tails) taken.Add(Stem(file) + tail); if (string.Equals(file, frame.File, StringComparison.Ordinal)) continue; var move = new FileRename(frame.Id, frame.File, file); diff --git a/src/Chronos.Core/Archiving/MassRename.cs b/src/Chronos.Core/Archiving/MassRename.cs index 2464337c..ced7b6e2 100644 --- a/src/Chronos.Core/Archiving/MassRename.cs +++ b/src/Chronos.Core/Archiving/MassRename.cs @@ -8,11 +8,23 @@ using Chronos.Core.Naming; namespace Chronos.Core.Archiving; -/// What the planner needs to know about a sequence to rename it. +/// +/// What the planner needs to know about a sequence to rename it. Without its sidecar () the templates see +/// the index row's capture time, camera and lens, and a sequence without a capture time keeps its folder and title. +/// public sealed record RenameSubject(Guid SequenceId, string Folder, string? Title, DateTime? FirstCapture, string? CameraMake, string? CameraModel, string? Lens) { - /// The sidecar and the files beside its frames, which a file template renames; null when they cannot be read. + /// + /// The sidecar, whose first frame the folder and title templates see as filing saw it, and the files beside its frames, + /// which a file template renames (empty without one); null when it was not read, or could not be. + /// public SequenceFiles? Files { get; init; } + + /// + /// The sidecar could not be read, or belongs to another sequence: the sequence stays as it is, and so does every folder + /// it is inside, which could not take it along (). + /// + public bool Unreadable { get; init; } } /// A sequence's sidecar as read for a plan, and every file in the folders of its frames (). @@ -57,37 +69,26 @@ public sealed record RenameStep(Guid SequenceId, string FromFolder, string ToFol /// public sealed record RenameTemplates(string? Folder = null, string? File = null, string? Title = null) { - private static readonly NamingContext Sample = new() - { - CapturedAt = new DateTime(2026, 9, 28, 18, 2, 11), - SessionStart = new DateTime(2026, 9, 28, 18, 2, 11), - CameraMake = "Canon", - CameraModel = "Canon EOS R5", - Lens = "RF15-35mm F2.8 L IS USM", - Iso = 100, - ExposureTime = 1.0 / 250, - FNumber = 8, - OriginalName = "IMG_4211", - Extension = "CR2", - Title = "Sunset", - }; + private static readonly NamingContext Sample = TemplateRules.Sample()[0]; public bool IsEmpty => Folder is null && File is null && Title is null; /// /// The first template that cannot be used, by its API name (folderTemplate, fileTemplate, - /// titleTemplate): blank, not valid as the archive settings' templates (), or - /// with a format a sample cannot be written with ({date:%}); null when every one given can. + /// titleTemplate): blank, not valid (), with a format a sample cannot be written + /// with ({date:%}), expanding to nothing, or, for a folder or file template, breaking a rule of the archive's + /// templates (: a folder scans never enter; a slash, a lost extension or names that would + /// repeat); null when every one given can. The archive settings' templates are checked alike. /// public string? Problem() { - if (Unusable(Folder, t => t.ExpandFolder(Sample))) return "folderTemplate"; - if (Unusable(File, t => t.ExpandFile(Sample))) return "fileTemplate"; - if (Unusable(Title, t => t.ExpandFile(Sample))) return "titleTemplate"; + if (Unusable(Folder, t => t.ExpandFolder(Sample), TemplateRules.FolderRule)) return "folderTemplate"; + if (Unusable(File, t => t.ExpandFile(Sample), TemplateRules.FileRule)) return "fileTemplate"; + if (Unusable(Title, t => t.ExpandText(Sample))) return "titleTemplate"; return null; } - private static bool Unusable(string? template, Func expand) + private static bool Unusable(string? template, Func expand, Func? rule = null) { if (template is null) return false; if (string.IsNullOrWhiteSpace(template)) return true; @@ -95,7 +96,7 @@ public sealed record RenameTemplates(string? Folder = null, string? File = null, if (!parsed.IsValid) return true; try { - return expand(parsed).Length == 0; + return expand(parsed).Length == 0 || rule?.Invoke(parsed) is not null; } catch (FormatException) { @@ -198,15 +199,17 @@ public static class RenamePlanner { /// /// Expands (where {title} is the current title) and then - /// (where {title} is the new title) for every subject. A target folder - /// that is any current sequence folder, exists on disk or was given to an earlier subject gets " (2)", " (3)"…. - /// With every subject's frames get their names from it as filing names them - /// (, with the new title), counted and sampled in its step, and the frames that keep - /// their names are listed there with the reason; a subject whose sidecar could not be read - /// ( null) keeps them all. Null templates leave that part unchanged; subjects without a - /// change produce no step, unless frames of theirs keep their names. A folder that moves takes the sequences inside - /// it along: each gets a step after every move, so the index follows its files; a - /// selected sequence inside a moving folder that moves elsewhere leaves first. + /// (where {title} is the new title) for every subject, from its first frame as + /// filing saw it (); a title is text, not a file + /// name (). A target folder that is any current sequence folder, exists on disk or + /// was given to an earlier subject (whatever its case) gets " (2)", " (3)"…. With every + /// subject's frames get their names from it as filing names them (, with the new title), + /// counted and sampled in its step, and the frames that keep their names are listed there with the reason. A subject + /// whose sidecar could not be read (, or no for a + /// file template) stays as it is, listed with the reason, and so does the folder of every subject it is inside. Null + /// templates leave that part unchanged; subjects without a change produce no step, unless frames of theirs keep their + /// names. A folder that moves takes the sequences inside it along: each gets a step after + /// every move, so the index follows its files; a selected sequence inside a moving folder that moves elsewhere leaves first. /// /// Every sequence folder of the archive (renamed or not). /// The archive's other sequences inside a subject's folder, with their titles. @@ -219,29 +222,41 @@ public static class RenamePlanner var file = fileTemplate is null ? null : PathTemplate.Parse(fileTemplate); var taken = occupied.ToList(); var claimed = new HashSet(taken, StringComparer.OrdinalIgnoreCase); + // The folders given in this plan so far: Kept never gives one twice (two folders that differ only in case). + var given = new HashSet(StringComparer.OrdinalIgnoreCase); + bool Unreadable(RenameSubject s) => s.Unreadable || (file is not null && s.Files is null); + // A folder that holds a sequence whose sidecar cannot be read cannot take it along. + var stuck = subjects.Where(Unreadable).Concat((nested ?? []).Where(n => n.Unreadable)).ToList(); + var blockers = new HashSet(); var own = new List(subjects.Count); foreach (var s in subjects) { - var context = new NamingContext + if (Unreadable(s)) { - CapturedAt = s.FirstCapture ?? DateTime.Today, - SessionStart = s.FirstCapture ?? DateTime.Today, - CameraMake = s.CameraMake, - CameraModel = s.CameraModel, - Lens = s.Lens, - Title = s.Title, - }; - var newTitle = title is null ? s.Title : Blank(title.ExpandFile(context)) ?? s.Title; + own.Add(new RenameStep(s.SequenceId, s.Folder, s.Folder, s.Title, s.Title) { Blocked = [new RenameBlocked(RenameBlock.Unreadable, 0)] }); + continue; + } + var context = Context(s); + var newTitle = title is null || context is null ? s.Title : Blank(title.ExpandText(context)) ?? s.Title; var newFolder = s.Folder; // The archive root itself (a folder adopted in place) cannot move; only its title changes. - if (folder is not null && s.Folder.Length > 0 && Blank(folder.ExpandFolder(context with { Title = newTitle })) is { } expanded + if (folder is not null && context is not null && s.Folder.Length > 0 && Blank(folder.ExpandFolder(context with { Title = newTitle })) is { } expanded && !expanded.Equals(s.Folder, StringComparison.Ordinal)) { - newFolder = Kept(s.Folder, expanded, taken) ?? Unique(expanded, claimed, existsOnDisk); - claimed.Add(newFolder); + var inside = stuck.Where(u => IsInside(s.Folder, u.Folder)).Select(u => u.SequenceId).ToList(); + if (inside.Count > 0) + { + blockers.UnionWith(inside); + } + else + { + newFolder = Kept(s.Folder, expanded, taken, given) ?? Unique(expanded, claimed, existsOnDisk); + claimed.Add(newFolder); + given.Add(newFolder); + } } var step = new RenameStep(s.SequenceId, s.Folder, newFolder, s.Title, newTitle); - own.Add(file is null ? step : WithFiles(step, s.Files, file, newTitle)); + own.Add(file is null ? step : WithFiles(step, s.Files!, file, newTitle)); } var moving = own.Where(s => s.MovesFolder).ToList(); @@ -252,7 +267,12 @@ public static class RenamePlanner if (host is not null) throw new NestedRenameException(step.ToFolder, host); } var steps = new List<(RenameStep Step, int Order)>(); - foreach (var step in own.Concat((nested ?? []).Select(n => new RenameStep(n.SequenceId, n.Folder, n.Folder, n.Title, n.Title)))) + // An unreadable sequence that kept a folder around it in place is listed with why, as an unreadable subject is. + var inner = (nested ?? []).Select(n => new RenameStep(n.SequenceId, n.Folder, n.Folder, n.Title, n.Title) + { + Blocked = n.Unreadable && blockers.Contains(n.SequenceId) ? [new RenameBlocked(RenameBlock.Unreadable, 0)] : [], + }); + foreach (var step in own.Concat(inner)) { // The nearest folder around it that moves carries it; a sequence that moves elsewhere leaves before its folder does. var carrier = step.MovesFolder ? null : moving.Where(m => IsInside(m.FromFolder, step.FromFolder)).MaxBy(m => m.FromFolder.Length); @@ -267,10 +287,43 @@ public static class RenamePlanner return new RenamePlan(Guid.CreateVersion7(), archiveId, [.. steps.OrderBy(s => s.Order).Select(s => s.Step)]) { FileTemplate = fileTemplate, Subjects = [.. subjects.Select(s => s.SequenceId)] }; } - /// The step with the file renames of its sequence (), or all its frames kept when its sidecar could not be read. - private static RenameStep WithFiles(RenameStep step, SequenceFiles? files, PathTemplate template, string? title) + /// + /// What the folder and title templates of a subject see: its first frame in its sidecar, as an upload filed it and as + /// Reorganize names its files (a sidecar without frames: when the sequence was made); without the sidecar, the index + /// row's capture time, camera and lens. Null when no time is known: never today's date, which would move the folder + /// again on another day. + /// + private static NamingContext? Context(RenameSubject s) + { + if (s.Files?.Manifest is { } manifest) + { + if (manifest.Frames.Count > 0) return FrameNaming.Context(manifest.Frames[0], manifest, s.Title); + var start = FrameNaming.SessionStart(manifest); + return new NamingContext + { + CapturedAt = start, + SessionStart = start, + CameraMake = manifest.Camera?.Make, + CameraModel = manifest.Camera?.Model, + Lens = manifest.Lens, + Title = s.Title, + }; + } + if (s.FirstCapture is not { } first) return null; + return new NamingContext + { + CapturedAt = first, + SessionStart = first, + CameraMake = s.CameraMake, + CameraModel = s.CameraModel, + Lens = s.Lens, + Title = s.Title, + }; + } + + /// The step with the file renames of its sequence (). + private static RenameStep WithFiles(RenameStep step, SequenceFiles files, PathTemplate template, string? title) { - if (files is null) return step with { Blocked = [new RenameBlocked(RenameBlock.Unreadable, 0)] }; var plan = FileRenames.Plan(files.Manifest, title, template, files.Present); return step with { @@ -293,7 +346,8 @@ public static class RenamePlanner /// Moves the folder of a step; idempotent so a resumed plan can repeat it: an already moved folder is accepted, /// a case-only change goes through a temporary name (case-insensitive file systems). /// - /// The target is occupied by something else, or neither folder exists. + /// The target is occupied by something else (for a case-only change, a folder of its own + /// on a case-sensitive file system), or neither folder exists. public static void MoveFolder(ArchivePaths archive, RenameStep step) { if (!step.MovesFolder) return; @@ -302,7 +356,8 @@ public static class RenamePlanner var caseOnly = step.FromFolder.Equals(step.ToFolder, StringComparison.OrdinalIgnoreCase); if (Directory.Exists(to) && (!caseOnly || HasExactName(to))) { - if (Directory.Exists(from) && !caseOnly) throw new IOException($"Rename target {step.ToFolder} already exists."); + // Both names there: the target is another folder (for a case-only change, on a case-sensitive file system). + if (Directory.Exists(from) && (!caseOnly || HasExactName(from))) throw new IOException($"Rename target {step.ToFolder} already exists."); return; } if (!Directory.Exists(from)) throw new DirectoryNotFoundException($"Sequence folder {step.FromFolder} is missing."); @@ -328,11 +383,12 @@ public static class RenamePlanner /// /// The folder a sequence keeps (in the template's case) when it already is , or the /// " (n)" of it that filing or an earlier Reorganize gave it beside another sequence of that name; null when it is - /// neither, or when another sequence holds that name exactly (a case-sensitive file system). Without it a sequence at - /// 2026-09-28 (2) would be planned to (3), its own folder being taken, and back to (2) by the - /// next Reorganize. + /// neither, when another sequence holds that name exactly (a case-sensitive file system), or when this plan already gave + /// it to another sequence (, whatever its case: trip (2) and TRIP (2) both keep + /// Trip (2) otherwise). Without it a sequence at 2026-09-28 (2) would be planned to (3), its own + /// folder being taken, and back to (2) by the next Reorganize. /// - private static string? Kept(string current, string expanded, IReadOnlyList occupied) + private static string? Kept(string current, string expanded, IReadOnlyList occupied, IReadOnlySet given) { string? same = null; if (current.Equals(expanded, StringComparison.OrdinalIgnoreCase)) same = expanded; @@ -341,7 +397,7 @@ public static class RenamePlanner out var n) && n >= 2) same = string.Create(CultureInfo.InvariantCulture, $"{expanded} ({n})"); // "(02)" is not a number Unique gives: that folder moves to a name of its own. - if (same is null || !same.Equals(current, StringComparison.OrdinalIgnoreCase)) return null; + if (same is null || !same.Equals(current, StringComparison.OrdinalIgnoreCase) || given.Contains(same)) return null; return same.Equals(current, StringComparison.Ordinal) || !occupied.Any(o => o.Equals(same, StringComparison.Ordinal)) ? same : null; } diff --git a/src/Chronos.Core/Import/ImportPlanner.cs b/src/Chronos.Core/Import/ImportPlanner.cs index 2ca4cbd8..c396725d 100644 --- a/src/Chronos.Core/Import/ImportPlanner.cs +++ b/src/Chronos.Core/Import/ImportPlanner.cs @@ -73,12 +73,15 @@ public static class ImportPlanner ?? UniqueFolder(folderTemplate.ExpandFolder(context), claimedFolders, lookup); claimedFolders.Add(folder); - var names = new HashSet(StringComparer.OrdinalIgnoreCase); + var names = new UniqueNames(new HashSet(StringComparer.OrdinalIgnoreCase)); var items = new List(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); + // With the camera and lens of the session's first photo, which its sequence carries: Reorganize names them alike. + var metadata = photo.Metadata with { Make = first.Metadata.Make, Model = first.Metadata.Model, LensModel = first.Metadata.LensModel }; + var name = names.Claim(fileTemplate.ExpandFile(FrameNaming.Context(metadata, photo.File.RelativePath, photo.CaptureTime, first.CaptureTime, + i + 1, title, s + 1))); items.Add(await PlanItemAsync(sourceRoot, photo, name, options, lookup, cancellationToken).ConfigureAwait(false)); } sessions.Add(new PlannedSession @@ -185,9 +188,6 @@ public static class ImportPlanner return candidate; } - /// Adds _2, _3… before the extension when the template gives two photos the same name. - internal static string UniqueName(string name, HashSet taken) => FrameNaming.Unique(name, taken.Add); - private static long? FreeSpace(string path) { try diff --git a/src/Chronos.Core/Naming/FrameNaming.cs b/src/Chronos.Core/Naming/FrameNaming.cs index f7b920d0..be7c9609 100644 --- a/src/Chronos.Core/Naming/FrameNaming.cs +++ b/src/Chronos.Core/Naming/FrameNaming.cs @@ -1,14 +1,20 @@ using System.Globalization; using Chronos.Core.Archiving; +using Chronos.Core.Imaging; using Chronos.Core.Metadata; namespace Chronos.Core.Naming; /// -/// The values a file template sees for one photo and the unique name it gives (BRIEF §7.1), shared by everything that -/// names frames: a card or folder import (), an upload filed by the archive ingest, and -/// Reorganize, which renames the frames of a sequence already in the archive (owner review 2, R7). The same photo in the -/// same place therefore gets the same name whichever of them names it. +/// The values a template sees for one photo and the unique name it gives (BRIEF §7.1), shared by everything that names +/// frames: a card or folder import (), an upload filed by the archive ingest, and +/// Reorganize, which renames the frames of a sequence already in the archive (owner review 2, R7). Every photo of a sequence +/// is named with the sequence's camera and lens (its first photo's, or the hint its creator gave), since the sidecar keeps +/// no camera or lens per frame. An upload in capture order, and an import session filed whole into a sequence of its own, +/// therefore get the names Reorganize gives them with the same templates. Reorganize gives other names, which its dry run +/// lists, where filing could not know the sequence as it is now: {session} of a card's second session on (an import +/// numbers its sessions; a sequence is one), {n} after photos skipped in the middle of a session (duplicates of +/// photos elsewhere) or filed later out of capture order, and a folder another photo now comes first in. /// public static class FrameNaming { @@ -44,11 +50,11 @@ public static class FrameNaming } /// - /// The context of a frame already in a sequence (Reorganize): its sidecar record with the sequence's camera and lens, - /// numbered by its index ({n} = index + 1), the sequence as one session starting at its first frame. {name} - /// is the name the photo had on the card, {ext} the extension of the file as it is now. These are the values an - /// upload of the same photos in capture order was named with; a frame without a capture time takes the session start, - /// so a dry run and its application always agree. + /// The context of a frame in its sequence (an upload as it is filed, Reorganize, and the folder of either from the first + /// frame): its sidecar record with the sequence's camera and lens, numbered by its index ({n} = index + 1), the + /// sequence as one session ({session} = 1) starting at its first frame. {name} is the name the photo had on + /// the card, {ext} the extension of the file as it is now, or of that name when the file has lost the extension of + /// a photo. A frame without a capture time takes the session start, so a dry run and its application always agree. /// public static NamingContext Context(SidecarFrame frame, SequenceManifest sequence, string? title) { @@ -56,6 +62,9 @@ public static class FrameNaming ArgumentNullException.ThrowIfNull(sequence); var start = SessionStart(sequence); var file = Path.GetFileName(frame.File); + var extension = Path.GetExtension(file); + if (!ImageFormatInfo.IsSupportedExtension(file) && frame.Original is { } original && ImageFormatInfo.IsSupportedExtension(original)) + extension = Path.GetExtension(original); return new NamingContext { CapturedAt = frame.CapturedAt ?? start, @@ -70,7 +79,7 @@ public static class FrameNaming ExposureTime = frame.Exposure, FNumber = frame.FNumber, OriginalName = Path.GetFileNameWithoutExtension(frame.Original ?? file), - Extension = Path.GetExtension(file).TrimStart('.'), + Extension = extension.TrimStart('.'), Title = title, }; } @@ -96,6 +105,19 @@ public static class FrameNaming return file; } + /// + /// (empty or ending with '/') and the name gives, made unique + /// in the set of as + /// does, then added to it. + /// + public static string FileName(PathTemplate template, NamingContext context, UniqueNames names, string directory = "") + { + ArgumentNullException.ThrowIfNull(template); + ArgumentNullException.ThrowIfNull(names); + ArgumentNullException.ThrowIfNull(directory); + return names.Claim(directory + template.ExpandFile(context)); + } + /// /// and the name gives, or the first of stem_2.ext, /// stem_3.ext… that accepts. @@ -113,13 +135,49 @@ public static class FrameNaming { ArgumentNullException.ThrowIfNull(file); ArgumentNullException.ThrowIfNull(isFree); - var slash = file.LastIndexOf('/'); - var directory = file[..(slash + 1)]; - var name = file[(slash + 1)..]; - var stem = Path.GetFileNameWithoutExtension(name); - var extension = Path.GetExtension(name); var candidate = file; - for (var n = 2; !isFree(candidate); n++) candidate = string.Create(CultureInfo.InvariantCulture, $"{directory}{stem}_{n}{extension}"); + for (var n = 2; !isFree(candidate); n++) candidate = Numbered(file, n); return candidate; } + + /// itself for = 1, else stem_n.ext in its directory. + internal static string Numbered(string file, int n) + { + if (n == 1) return file; + var slash = file.LastIndexOf('/'); + var name = file[(slash + 1)..]; + return string.Create(CultureInfo.InvariantCulture, $"{file[..(slash + 1)]}{Path.GetFileNameWithoutExtension(name)}_{n}{Path.GetExtension(name)}"); + } +} + +/// +/// Unique file names in a set that only grows (BRIEF §7.1): exactly the names gives against +/// it, without trying again for each photo the numbers earlier photos already took. A template that gives a whole session +/// one name ({title}) then costs about one check per photo instead of one per photo before it. +/// +/// The names given or held so far; every name claimed is added to it, and none may leave it while in use. +public sealed class UniqueNames(ISet taken) +{ + // Per name asked: the first number whose name the set did not hold when last tried. The set only grows, so every number + // below it stays taken. Ordinal whatever the set compares: only that exact name's numbers are known. + private readonly Dictionary _first = new(StringComparer.Ordinal); + + /// + /// , or the first of stem_2.ext, stem_3.ext… that the set does not hold and + /// (when given) accepts; added to the set. + /// + public string Claim(string file, Func? alsoFree = null) + { + ArgumentNullException.ThrowIfNull(file); + var n = _first.GetValueOrDefault(file, 1); + while (taken.Contains(FrameNaming.Numbered(file, n))) n++; + _first[file] = n; + for (var m = n; ; m++) + { + var candidate = FrameNaming.Numbered(file, m); + if (taken.Contains(candidate) || alsoFree?.Invoke(candidate) == false) continue; + taken.Add(candidate); + return candidate; + } + } } diff --git a/src/Chronos.Core/Naming/PathTemplate.cs b/src/Chronos.Core/Naming/PathTemplate.cs index 94b51beb..fb55a0a1 100644 --- a/src/Chronos.Core/Naming/PathTemplate.cs +++ b/src/Chronos.Core/Naming/PathTemplate.cs @@ -1,5 +1,6 @@ using System.Globalization; using System.Text; +using Chronos.Core.Archiving; namespace Chronos.Core.Naming; @@ -20,6 +21,9 @@ public sealed class PathTemplate "iso", "shutter", "aperture", "n", "nsession", "name", "ext", "title", ]; + /// The longest text gives: the longest title a sequence takes. + public const int MaxTextLength = 200; + private static readonly HashSet TokenSet = new(Tokens, StringComparer.OrdinalIgnoreCase); private static readonly char[] Illegal = ['<', '>', ':', '"', '|', '?', '*', '\\', '/', '\0']; private static readonly HashSet ReservedNames = new(StringComparer.OrdinalIgnoreCase) @@ -103,16 +107,29 @@ public sealed class PathTemplate return new PathTemplate(template, parts, errors); } - /// Expands to a relative folder path (forward slashes; each segment sanitized). - public string ExpandFolder(NamingContext context) + /// + /// Expands to a relative folder path (forward slashes; each segment sanitized). A level archive scans never enter + /// (), which a token value such as a title can give, gets a leading _, so every + /// sequence filed or moved by a template stays where a scan finds it. + /// + public string ExpandFolder(NamingContext context) => string.Join('/', Levels(context).Select(s => IsHiddenFolder(s) ? "_" + s : s)); + + /// Whether a level of the folder this template gives for is one scans never enter, before it is escaped. + public bool WritesHiddenFolder(NamingContext context) => Levels(context).Any(IsHiddenFolder); + + /// + /// A folder name archive scans never enter (): one that starts with a dot, or a folder + /// the layout keeps for itself (.chronos, originals, proxies, exports), whatever its case. + /// + public static bool IsHiddenFolder(string name) { - var raw = Expand(context, allowSeparators: true); - var segments = raw.Split('/', StringSplitOptions.RemoveEmptyEntries) - .Select(SanitizeSegment) - .Where(s => s.Length > 0); - return string.Join('/', segments); + ArgumentNullException.ThrowIfNull(name); + return name.StartsWith('.') || ArchiveLayout.IsReservedFolder(name); } + private IEnumerable Levels(NamingContext context) => + Expand(context, allowSeparators: true).Split('/', StringSplitOptions.RemoveEmptyEntries).Select(SanitizeSegment).Where(s => s.Length > 0); + /// Expands to a file name; the original extension is appended unless the template already ends with {ext}. public string ExpandFile(NamingContext context) { @@ -123,7 +140,34 @@ public sealed class PathTemplate return name; } - private string Expand(NamingContext c, bool allowSeparators) + /// + /// Whether every file name the template gives ends with the photo's own extension: the template does not end with + /// {ext} (the extension is appended), or ends with .{ext}. {n}{ext} would drop the dot, and with it + /// the extension every reader of the photo goes by. + /// + public bool KeepsExtension => + _parts is not [.., { IsToken: true, Text: "ext" }] || _parts is [.., { IsToken: false, Text: var before }, { IsToken: true, Text: "ext" }] && before.EndsWith('.'); + + /// + /// Expands to plain text, such as a sequence title: the literals and the token values as they are, since a title may hold + /// characters a file name cannot (Paris: night), control characters as spaces, trimmed, no extension; at most + /// characters. + /// + public string ExpandText(NamingContext context) + { + var text = new StringBuilder(Expand(context, allowSeparators: false, raw: true)); + for (var i = 0; i < text.Length; i++) + { + if (char.IsControl(text[i])) text[i] = ' '; + } + var s = text.ToString().Trim(); + if (s.Length <= MaxTextLength) return s; + var cut = char.IsHighSurrogate(s[MaxTextLength - 1]) ? MaxTextLength - 1 : MaxTextLength; + return s[..cut].TrimEnd(); + } + + /// Token values as they are: text, not a path. + private string Expand(NamingContext c, bool allowSeparators, bool raw = false) { var inv = CultureInfo.InvariantCulture; var sb = new StringBuilder(); @@ -158,7 +202,7 @@ public sealed class PathTemplate _ => string.Empty, }; // Token values never introduce folder levels. - sb.Append(value.Replace('/', '-').Replace('\\', '-')); + sb.Append(raw ? value : value.Replace('/', '-').Replace('\\', '-')); } return sb.ToString(); } diff --git a/src/Chronos.Core/Naming/TemplateRules.cs b/src/Chronos.Core/Naming/TemplateRules.cs new file mode 100644 index 00000000..19fb9707 --- /dev/null +++ b/src/Chronos.Core/Naming/TemplateRules.cs @@ -0,0 +1,65 @@ +using System.Globalization; + +namespace Chronos.Core.Naming; + +/// +/// What a folder or file template must give beyond parsing (BRIEF §7.1), the same wherever one is typed or sent: Settings › +/// Archive, an import, a Reorganize on the desktop or the server, the server's archive settings. Checked on a sample session +/// of three photos, as the previews show it. Each rule is named by the key that explains it, Errors.Template.<rule>. +/// +public static class TemplateRules +{ + /// + /// A sample session: three photos two seconds apart from a Canon EOS R5, each with its own original name, titled + /// (made on each call). + /// + public static IReadOnlyList Sample(string? title = "Sunset") + { + var start = new DateTime(2026, 9, 28, 18, 2, 11); + return [.. Enumerable.Range(0, 3).Select(i => new NamingContext + { + CapturedAt = start.AddSeconds(2 * i), + SessionStart = start, + Session = 1, + Number = i + 1, + NumberInSession = i + 1, + CameraMake = "Canon", + CameraModel = "Canon EOS R5", + Lens = "RF15-35mm F2.8 L IS USM", + Iso = 100, + ExposureTime = 1.0 / 250, + FNumber = 8, + OriginalName = "IMG_" + (4211 + i).ToString("0000", CultureInfo.InvariantCulture), + Extension = "CR2", + Title = title, + })]; + } + + /// + /// The rule a folder template breaks, for a template that parses and expands: ReservedFolder when a level it writes + /// is one archive scans never enter (), so no index rebuilt from the disk would + /// find the sequences filed there; null when it keeps them all. + /// + /// A token's format cannot be written. + public static string? FolderRule(PathTemplate template) + { + ArgumentNullException.ThrowIfNull(template); + return Sample().Any(template.WritesHiddenFolder) ? "ReservedFolder" : null; + } + + /// + /// The rule a file template breaks, for a template that parses and expands: FileSeparator (a slash: folders belong + /// in the folder template), Extension (a name would not end with the photo's extension, + /// ), NotUnique (the photos of a session would get the same name: no frame + /// number, time or original name tells them apart); null when it keeps them all. + /// + /// A token's format cannot be written. + public static string? FileRule(PathTemplate template) + { + ArgumentNullException.ThrowIfNull(template); + if (template.Source.Contains('/', StringComparison.Ordinal)) return "FileSeparator"; + if (!template.KeepsExtension) return "Extension"; + var names = Sample().Select(template.ExpandFile).ToList(); + return names.Distinct(StringComparer.OrdinalIgnoreCase).Count() == names.Count ? null : "NotUnique"; + } +} diff --git a/src/Chronos.Data/Archiving/ArchiveIngest.cs b/src/Chronos.Data/Archiving/ArchiveIngest.cs index 3ae9ffcd..e57753d4 100644 --- a/src/Chronos.Data/Archiving/ArchiveIngest.cs +++ b/src/Chronos.Data/Archiving/ArchiveIngest.cs @@ -217,12 +217,17 @@ public sealed class ArchiveIngest : IArchiveIngest } } - /// A new folder for : the plan's folder or the archive's folder template, made unique. + /// + /// A new folder for : the plan's folder or the archive's folder template, made unique. The + /// template sees the first frame in its new sequence, as Reorganize sees a sequence's first frame (). + /// private async Task ClaimNewFolderAsync(Archive archive, ArchivePaths paths, string? preferredFolder, StagedFrame first, SequenceManifest manifest, CancellationToken cancellationToken) { - var captured = CaptureTime(first); - var wanted = preferredFolder ?? PathTemplate.Parse(archive.FolderTemplate).ExpandFolder(Naming(first, captured, captured, 1, manifest.Title)); + var frame = SequenceFrames.FromMetadata(Guid.Empty, first.OriginalFileName, first.OriginalFileName, first.Size, first.Sha256, first.Metadata, + first.ClientCapturedAt); + var wanted = preferredFolder + ?? PathTemplate.Parse(archive.FolderTemplate).ExpandFolder(FrameNaming.Context(frame, manifest with { Frames = [frame] }, manifest.Title)); if (wanted.Length == 0) wanted = "Sequence"; var taken = (await _db.Sequences.ListLocationsAsync(archive.Id, cancellationToken).ConfigureAwait(false)) .Select(l => l.Folder).ToHashSet(StringComparer.OrdinalIgnoreCase); @@ -411,7 +416,11 @@ public sealed class ArchiveIngest : IArchiveIngest return left.Count == 0 ? null : left; } - /// Final file names, in capture order: the plan's name or the template with {n} = index + 1, made unique. + /// + /// Final file names, in capture order: the plan's name, or the template with each frame in the sequence it joins + /// (: {n} = index + 1, the sequence's + /// camera and lens), the context Reorganize names it with; made unique. + /// private static Dictionary Name(Archive archive, SequencePaths sequence, SequenceManifest manifest, List<(Pending Pending, SidecarFrame Frame)> accepted) { @@ -428,16 +437,14 @@ public sealed class ArchiveIngest : IArchiveIngest foreach (var file in Directory.EnumerateFiles(onDisk)) taken.Add(directory + Path.GetFileName(file)); } var template = PathTemplate.Parse(archive.FileTemplate); - var start = merged.Frames.Count > 0 ? merged.Frames[0].CapturedAt : null; + var names = new UniqueNames(taken); var named = new Dictionary(); foreach (var frame in merged.Frames) { if (!incoming.TryGetValue(frame.Id, out var entry)) continue; - var staged = entry.Pending.Frame; - var captured = frame.CapturedAt ?? DateTime.Now; - var file = staged.PlannedName is { } planned - ? FrameNaming.Unique(directory + planned, taken.Add) - : FrameNaming.FileName(template, Naming(staged, captured, start ?? captured, frame.I + 1, manifest.Title), taken, directory); + var file = entry.Pending.Frame.PlannedName is { } planned + ? names.Claim(directory + planned) + : FrameNaming.FileName(template, FrameNaming.Context(frame, merged, manifest.Title), names, directory); named[frame.Id] = entry.Frame with { File = file }; } return named; @@ -533,14 +540,6 @@ public sealed class ArchiveIngest : IArchiveIngest private static IngestResult Result(Guid sequenceId, SequencePaths sequence, SidecarFrame frame, bool duplicate) => new(sequenceId, frame.Id, frame.I, sequence.ArchiveRelative(frame.File), duplicate); - private static DateTime CaptureTime(StagedFrame frame) => - frame.Metadata.TimestampSource is TimestampSource.FileSystem or TimestampSource.Unknown && frame.ClientCapturedAt is { } client - ? client - : frame.Metadata.CapturedAt ?? frame.ClientCapturedAt ?? DateTime.Now; - - private static NamingContext Naming(StagedFrame frame, DateTime captured, DateTime sessionStart, int number, string? title) => - FrameNaming.Context(frame.Metadata, frame.OriginalFileName, captured, sessionStart, number, title); - private sealed class CommitQueue { public SemaphoreSlim Gate { get; } = new(1, 1); diff --git a/src/Chronos.Data/Archiving/MassRenamer.cs b/src/Chronos.Data/Archiving/MassRenamer.cs index 7d512d8f..287ef6e4 100644 --- a/src/Chronos.Data/Archiving/MassRenamer.cs +++ b/src/Chronos.Data/Archiving/MassRenamer.cs @@ -80,10 +80,12 @@ public sealed class MassRenamer PlanAsync(archive, sequenceIds, new RenameTemplates(folderTemplate, null, titleTemplate), cancellationToken); /// - /// The dry run of a Reorganize: the plan of over . With a file - /// template each sequence's sidecar is read (without its lease) with the files beside its frames, so the plan names - /// exactly the files the apply will rename while nothing changes. A sequence created without photos, which has no - /// folder yet, is left out. + /// The dry run of a Reorganize: the plan of over . Each + /// sequence's sidecar is read (without its lease), and so is that of every sequence a moving folder would take along: + /// the folder and title templates see the first frame as filing did, and a sequence whose sidecar cannot be read stays + /// as it is, with every folder around it. With a file template the files beside its frames are listed too, so the plan + /// names exactly the files the apply will rename while nothing changes. A sequence created without photos, which has + /// no folder yet, is left out. /// /// A sequence is not in the archive. /// A new folder would lie inside a sequence's folder. @@ -100,7 +102,7 @@ public sealed class MassRenamer if (await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } s || s.ArchiveId != archive.Id) throw new ArgumentException($"Sequence {id} is not in archive {archive.Id}.", nameof(sequenceIds)); if (ArchiveIngest.AwaitsFolder(s, paths)) continue; - subjects.Add(Subject(s) with { Files = templates.File is null ? null : ReadFiles(paths, s) }); + subjects.Add(templates.IsEmpty ? Subject(s) : Read(paths, s, listFiles: templates.File is not null)); } var nested = new List(); if (templates.Folder is not null) @@ -108,7 +110,7 @@ public sealed class MassRenamer var selected = sequenceIds.ToHashSet(); foreach (var location in locations.Where(l => !selected.Contains(l.Id) && subjects.Any(s => RenamePlanner.IsInside(s.Folder, l.Folder)))) { - if (await _db.Sequences.GetAsync(location.Id, cancellationToken).ConfigureAwait(false) is { } inner) nested.Add(Subject(inner)); + if (await _db.Sequences.GetAsync(location.Id, cancellationToken).ConfigureAwait(false) is { } inner) nested.Add(Read(paths, inner, listFiles: false)); } } return RenamePlanner.Plan(archive.Id, subjects, templates.Folder, templates.Title, locations.Select(l => l.Folder), @@ -118,27 +120,28 @@ public sealed class MassRenamer private static RenameSubject Subject(Sequence s) => new(s.Id, s.Folder, s.Title, s.FirstCapture, s.CameraMake, s.CameraModel, s.Lens); /// - /// The sidecar of a sequence and the files beside its frames; null (logged) when it cannot be read, is another - /// sequence's, or is of a newer format this build never rewrites (its files cannot get new names here). + /// A sequence with its sidecar and, when , the files beside its frames; marked + /// (logged) when the sidecar cannot be read, is another sequence's, or is of a + /// newer format this build never rewrites. /// - private static SequenceFiles? ReadFiles(ArchivePaths paths, Sequence sequence) + private static RenameSubject Read(ArchivePaths paths, Sequence sequence, bool listFiles) { var folder = paths.Sequence(sequence.Folder); try { var manifest = ManifestStore.ReadSequence(folder.ManifestPath); if (manifest.Id != sequence.Id) - Log.Warning("archive", $"{folder.ManifestPath} belongs to sequence {manifest.Id}, not {sequence.Id}; its files keep their names"); + Log.Warning("archive", $"{folder.ManifestPath} belongs to sequence {manifest.Id}, not {sequence.Id}; the sequence stays as it is"); else if (manifest.SchemaVersion > SequenceManifest.CurrentSchemaVersion) - Log.Warning("archive", $"{folder.ManifestPath} was written by a newer Chronos (format {manifest.SchemaVersion}); its files keep their names"); + Log.Warning("archive", $"{folder.ManifestPath} was written by a newer Chronos (format {manifest.SchemaVersion}); the sequence stays as it is"); else - return new SequenceFiles(manifest, FileRenames.ListPresent(folder, manifest)); + return Subject(sequence) with { Files = new SequenceFiles(manifest, listFiles ? FileRenames.ListPresent(folder, manifest) : []) }; } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException) { - Log.Warning("archive", $"The sidecar of {sequence.DisplayTitle} cannot be read; its files keep their names", ex); + Log.Warning("archive", $"The sidecar of {sequence.DisplayTitle} cannot be read; the sequence stays as it is", ex); } - return null; + return Subject(sequence) with { Unreadable = true }; } /// Applies a plan; each completed step is journaled before the next starts. diff --git a/src/Chronos.Server/Api/ArchiveEndpoints.cs b/src/Chronos.Server/Api/ArchiveEndpoints.cs index 323c7865..dc26a8de 100644 --- a/src/Chronos.Server/Api/ArchiveEndpoints.cs +++ b/src/Chronos.Server/Api/ArchiveEndpoints.cs @@ -81,10 +81,10 @@ public static class ArchiveEndpoints { var name = request.Name?.Trim(); if (string.IsNullOrEmpty(name) || name.Length > ArchiveSettings.MaxNameLength) return ApiErrors.BadRequest(context, "bad_request", "name"); - foreach (var (field, template) in new[] { ("folderTemplate", request.FolderTemplate), ("fileTemplate", request.FileTemplate) }) - { - if (string.IsNullOrWhiteSpace(template) || !PathTemplate.Parse(template).IsValid) return ApiErrors.BadRequest(context, "invalid_template", field); - } + if (string.IsNullOrWhiteSpace(request.FolderTemplate)) return ApiErrors.BadRequest(context, "invalid_template", "folderTemplate"); + if (string.IsNullOrWhiteSpace(request.FileTemplate)) return ApiErrors.BadRequest(context, "invalid_template", "fileTemplate"); + // The rules Reorganize checks: a folder scans never enter, a lost extension or names that repeat are refused alike. + if (new RenameTemplates(request.FolderTemplate, request.FileTemplate).Problem() is { } field) return ApiErrors.BadRequest(context, "invalid_template", field); if (!ProxyPolicy.IsValidLongEdge(request.ProxyLongEdge)) return ApiErrors.BadRequest(context, "bad_request", "proxyLongEdge"); var engine = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner }); diff --git a/tests/Chronos.App.Tests/Settings/SettingsLogicTests.cs b/tests/Chronos.App.Tests/Settings/SettingsLogicTests.cs index 97223f09..3d48219f 100644 --- a/tests/Chronos.App.Tests/Settings/SettingsLogicTests.cs +++ b/tests/Chronos.App.Tests/Settings/SettingsLogicTests.cs @@ -37,6 +37,8 @@ public sealed class SettingsLogicTests [Case("{date}", "Errors.Template.NotUnique", "", 0)] [Case("{year}/{n}", "Errors.Template.FileSeparator", "", 0)] [Case("{date:%}_{n}", "Errors.Template.Format", "", 0)] + [Case("{n}{ext}", "Errors.Template.Extension", "", 0)] + [Case("{title}_{ext}", "Errors.Template.Extension", "", 0)] public void InvalidFileTemplatesExplainTheProblem(string template, string key, string token, int position) { var expected = position > 0 ? Loc.L(key, token, position) : Loc.L(key); @@ -56,6 +58,18 @@ public sealed class SettingsLogicTests { Assert.Null(NamingPreview.Validate("{year}/{month}/{camera}", isFolder: true)); Assert.Equal("2026/09/Canon EOS R5/IMG_4211.CR2", NamingPreview.FirstNames("{year}/{month}/{camera}", "{name}")[0]); + Assert.Null(NamingPreview.Validate("{name}.{ext}", isFolder: false)); + } + + [Theory] + [Case("Originals/{year}")] + [Case("{year}/exports")] + [Case(".archive/{date}")] + [Case("{year}/.chronos")] + public void FolderTemplatesScansWouldSkipAreRefused(string template) + { + Assert.Equal(Loc.L("Errors.Template.ReservedFolder"), NamingPreview.Validate(template, isFolder: true), template); + Assert.Empty(NamingPreview.FirstNames(template, TemplateDefaults.File)); } [Test] diff --git a/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs b/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs index ca710206..5dfc4920 100644 --- a/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs +++ b/tests/Chronos.Core.Tests/Archiving/ReorganizeTests.cs @@ -178,6 +178,7 @@ public sealed class ReorganizeTests(TestContext context) public void TemplatesAreCheckedAsTheArchiveSettingsAre() { Assert.Null(new RenameTemplates("{year}/{title}", "{date}_{n}", "{title} 2026").Problem()); + Assert.Null(new RenameTemplates(File: "{name}.{ext}", Title: "Paris: {title}?").Problem()); Assert.Equal("folderTemplate", new RenameTemplates(Folder: " ").Problem()); Assert.Equal("fileTemplate", new RenameTemplates(File: "{nope}").Problem()); Assert.Equal("fileTemplate", new RenameTemplates(File: "{date:%}").Problem()); @@ -185,6 +186,198 @@ public sealed class ReorganizeTests(TestContext context) Assert.True(new RenameTemplates().IsEmpty); } + [Theory] + [Case("{n}{ext}", "fileTemplate", "Extension")] + [Case("{date}_{n}_{ext}", "fileTemplate", "Extension")] + [Case("{title}", "fileTemplate", "NotUnique")] + [Case("{date}_{hm}", "fileTemplate", "NotUnique")] + [Case("{year}/{n}", "fileTemplate", "FileSeparator")] + [Case("Originals/{date:yyyy}/{title}", "folderTemplate", "ReservedFolder")] + [Case("{year}/exports", "folderTemplate", "ReservedFolder")] + [Case(".archive/{date}", "folderTemplate", "ReservedFolder")] + [Case("{date:.yyyy}", "folderTemplate", "ReservedFolder")] + public void TemplatesThatWouldLoseOrHidePhotosAreRefused(string template, string field, string rule) + { + var folder = field == "folderTemplate"; + var templates = folder ? new RenameTemplates(Folder: template) : new RenameTemplates(File: template); + Assert.Equal(field, templates.Problem()); + Assert.Equal(rule, folder ? TemplateRules.FolderRule(PathTemplate.Parse(template)) : TemplateRules.FileRule(PathTemplate.Parse(template))); + } + + /// A sidecar of , each photo's first capture time (none when null), titled . + private static RenameSubject Filed(string folder, string? title, DateTime? first, params string[] files) + { + var manifest = Sequence(files) with { Title = title, Created = new DateTimeOffset(2026, 10, 1, 9, 30, 0, TimeSpan.Zero) }; + manifest = manifest with { Frames = [.. manifest.Frames.Select(f => f with { CapturedAt = first?.AddSeconds(10 * f.I) })] }; + return new RenameSubject(manifest.Id, folder, title, first, "Canon", "Canon EOS R5", null) + { + Files = new SequenceFiles(manifest, [.. manifest.Frames.Select(f => f.File)]), + }; + } + + [Theory] + [Case("{year}/{date} {ext}", "2026/2026-09-28 CR2", "2026/2026-09-28 JPG")] + [Case("{date} {name}", "2026-09-28 IMG_0001", "2026-09-28 IMG_0001 (2)")] + [Case("{year}/{date} {camera} {iso}", "2026/2026-09-28 Canon EOS R5", "2026/2026-09-28 Canon EOS R5 (2)")] + public void FoldersFiledFromTheFirstPhotoStayWhereFilingPutThem(string template, string raw, string jpeg) + { + // RAW and JPEG of one day, filed by the template from their first photo ({ext}, {name}… are that photo's). + RenameSubject[] subjects = + [ + Filed(raw, null, Start, "originals/2026-09-28_0001.CR2", "originals/2026-09-28_0002.CR2"), + Filed(jpeg, null, Start, "originals/2026-09-28_0001.JPG", "originals/2026-09-28_0002.JPG"), + ]; + var folders = subjects.Select(s => s.Folder).ToHashSet(StringComparer.OrdinalIgnoreCase); + Assert.Empty(RenamePlanner.Plan(Guid.NewGuid(), subjects, template, null, folders, folders.Contains).Steps); + // The context filing saw: the folder each first frame gives. + var context = FrameNaming.Context(subjects[0].Files!.Manifest.Frames[0], subjects[0].Files!.Manifest, null); + Assert.Equal(raw.Replace(" (2)", string.Empty, StringComparison.Ordinal), PathTemplate.Parse(template).ExpandFolder(context)); + } + + [Test] + public void ASequenceWithoutACaptureTimeNeverTakesTodaysDate() + { + // No time in the sidecar: the day the sequence was made, as its files are named; no sidecar and no time: no move. + var timeless = Filed("inbox/a", null, null, "originals/a.JPG"); + var plan = RenamePlanner.Plan(Guid.NewGuid(), [timeless], "{date}", null, ["inbox/a"], _ => false); + Assert.Equal("2026-10-01", plan.Steps.Single().ToFolder); + var row = new RenameSubject(Guid.NewGuid(), "inbox/b", "B", null, null, null, null); + Assert.Empty(RenamePlanner.Plan(Guid.NewGuid(), [row], "{date}", "{title} {date}", ["inbox/b"], _ => false).Steps); + } + + [Test] + public void ASequenceWhoseSidecarCannotBeReadStaysAsItIsAndSoDoesTheFolderAroundIt() + { + var a = Filed("a", "A", Start, "originals/a.JPG"); + // Unreadable, titled as D: had it been planned, D would get " (2)". + var unreadable = new RenameSubject(Guid.NewGuid(), "u", "D", Start, null, null, null) { Unreadable = true }; + var carrier = Filed("c", "C", Start, "originals/c.JPG"); + var inner = new RenameSubject(Guid.NewGuid(), "c/n", "N", Start, null, null, null) { Unreadable = true }; + var d = Filed("d", "D", Start, "originals/d.JPG"); + var plan = RenamePlanner.Plan(Guid.NewGuid(), [a, unreadable, carrier, d], "moved/{title}", "{title} 2026", ["a", "u", "c", "c/n", "d"], _ => false, + [inner]); + var steps = plan.Steps.ToDictionary(s => s.SequenceId); + Assert.Equal(("moved/A 2026", "A 2026"), (steps[a.SequenceId].ToFolder, steps[a.SequenceId].ToTitle)); + Assert.Equal(("moved/D 2026", "D 2026"), (steps[d.SequenceId].ToFolder, steps[d.SequenceId].ToTitle)); + foreach (var kept in new[] { unreadable, inner }) + { + var step = steps[kept.SequenceId]; + Assert.Equal((kept.Folder, kept.Title), (step.ToFolder, step.ToTitle)); + Assert.False(step.Changes); + Assert.Equal(RenameBlock.Unreadable, step.Blocked.Single().Reason); + } + // Its own sidecar reads: the title changes, the folder cannot take the sequence inside along. + Assert.Equal(("c", "C 2026"), (steps[carrier.SequenceId].ToFolder, steps[carrier.SequenceId].ToTitle)); + Assert.False(steps[inner.SequenceId].Carried); + + // With a file template, a sequence without its sidecar's files stays too. + var noFiles = new RenameSubject(Guid.NewGuid(), "x", "X", Start, null, null, null); + var files = RenamePlanner.Plan(Guid.NewGuid(), [noFiles], "moved/{title}", null, ["x"], _ => false, fileTemplate: "{n}"); + Assert.Equal(("x", false), (files.Steps.Single().ToFolder, files.Steps.Single().Changes)); + } + + [Test] + public void AFolderATitleWouldHideFromScansIsEscapedAndFound() + { + var archive = new ArchivePaths(context.CreateTempDirectory("archive")); + var originals = archive.FromRelative("inbox/a/originals"); + Directory.CreateDirectory(originals); + File.WriteAllBytes(Path.Combine(originals, "IMG_0001.JPG"), TestPhotos.Jpeg(Start, 1)); + var subject = new RenameSubject(Guid.NewGuid(), "inbox/a", "Exports", Start, null, null, null); + var step = RenamePlanner.Plan(Guid.NewGuid(), [subject], "{year}/{title}", null, ["inbox/a"], _ => false).Steps.Single(); + Assert.Equal("2026/_Exports", step.ToFolder); + RenamePlanner.MoveFolder(archive, step); + Assert.SequenceEqual(["2026/_Exports"], ArchiveScanner.FindSequenceFolders(archive).Select(f => f.RelativeFolder)); + } + + [Test] + public void TitlesKeepWhatAFileNameCannotHold() + { + var paris = new RenameSubject(Guid.NewGuid(), "s/paris", "Paris: night", Start, null, null, null); + var plan = RenamePlanner.Plan(Guid.NewGuid(), [paris], "{title}", "{title} 2026", ["s/paris"], _ => false); + // The title is text; the folder it names is a file name. + Assert.Equal(("Paris: night 2026", "Paris_ night 2026"), (plan.Steps.Single().ToTitle, plan.Steps.Single().ToFolder)); + Assert.Empty(RenamePlanner.Plan(Guid.NewGuid(), [paris with { Folder = "Paris_ night" }], "{title}", "{title}", ["Paris_ night"], _ => false).Steps); + } + + [Test] + public void TwoFoldersThatDifferOnlyInCaseAreNeverGivenOneFolder() + { + // A case-sensitive server: "trip (2)" and "TRIP (2)" are two folders, both of which "Trip (2)" would keep. + RenameSubject At(string folder) => new(Guid.NewGuid(), folder, "Trip", Start, null, null, null); + RenameSubject[] subjects = [At("trip (2)"), At("TRIP (2)")]; + var onDisk = new HashSet(["trip (2)", "TRIP (2)"], StringComparer.Ordinal); + var plan = RenamePlanner.Plan(Guid.NewGuid(), subjects, "{title}", null, onDisk, onDisk.Contains); + var targets = plan.Steps.Select(s => s.ToFolder).ToList(); + Assert.SequenceEqual(["Trip (2)", "Trip"], targets); + + // Where they went, the same template changes nothing. + RenameSubject[] after = [.. subjects.Zip(targets, (s, t) => s with { Folder = t })]; + var moved = new HashSet(targets, StringComparer.Ordinal); + Assert.Empty(RenamePlanner.Plan(Guid.NewGuid(), after, "{title}", null, moved, moved.Contains).Steps); + } + + [Test] + public void ACaseOnlyMoveOntoAnotherFolderIsRefused() + { + var archive = new ArchivePaths(context.CreateTempDirectory("archive")); + Directory.CreateDirectory(archive.FromRelative("trip (2)")); + File.WriteAllText(Path.Combine(archive.FromRelative("trip (2)"), "a.txt"), "a"); + var step = new RenameStep(Guid.NewGuid(), "trip (2)", "Trip (2)", "Trip", "Trip"); + if (Directory.Exists(archive.FromRelative("TRIP (2)"))) + { + // Case-insensitive: the folder takes the new case, and the step repeated finds it done. + RenamePlanner.MoveFolder(archive, step); + RenamePlanner.MoveFolder(archive, step); + Assert.SequenceEqual(["Trip (2)"], Directory.EnumerateDirectories(archive.Root).Select(Path.GetFileName)); + Assert.Equal("a", File.ReadAllText(Path.Combine(archive.FromRelative("Trip (2)"), "a.txt"))); + } + else + { + // Case-sensitive: another folder holds the new name exactly; neither moves. + Directory.CreateDirectory(archive.FromRelative("Trip (2)")); + Assert.Throws(() => RenamePlanner.MoveFolder(archive, step)); + Assert.Equal("a", File.ReadAllText(Path.Combine(archive.FromRelative("trip (2)"), "a.txt"))); + Assert.Empty(Directory.EnumerateFileSystemEntries(archive.FromRelative("Trip (2)"))); + } + } + + [Test] + public void ExtensionsAPhotoLostComeBackFromItsName() + { + var manifest = Sequence("originals/0001CR3", "originals/0002.CR3"); + manifest = manifest with { Frames = [.. manifest.Frames.Select(f => f with { Original = $"IMG_{f.I + 1:0000}.CR3" })] }; + Assert.Equal("CR3", FrameNaming.Context(manifest.Frames[0], manifest, null).Extension); + Assert.SequenceEqual(["originals/0001CR3>originals/0001.CR3"], Plan(manifest, "{n:0000}").Moves.Select(m => $"{m.From}>{m.To}")); + // A file that has an extension of a photo keeps that one, whatever the card's name said. + Assert.Equal("CR3", FrameNaming.Context(manifest.Frames[1] with { Original = "IMG_0002.JPG" }, manifest, null).Extension); + } + + [Test] + public void ManyPhotosOfOneNameAreNamedInLinearTimeAsTheSearchFromTwoNamesThem() + { + // Against the search from _2 it replaces, with names taken by stray files and companions in the way. + var taken = new HashSet(["a/x.JPG", "a/x_3.JPG", "a/x_4.xmp"], StringComparer.OrdinalIgnoreCase); + var reference = new HashSet(taken, StringComparer.OrdinalIgnoreCase); + var names = new UniqueNames(taken); + for (var i = 0; i < 12; i++) + { + // Every third photo has an XMP companion: "x_4" is free for the others, not for it. + Func companion = i % 3 == 1 ? c => !reference.Contains(Path.ChangeExtension(c, ".xmp")) : _ => true; + var expected = FrameNaming.Unique("a/x.JPG", c => !reference.Contains(c) && companion(c)); + reference.Add(expected); + Assert.Equal(expected, names.Claim("a/x.JPG", companion), $"photo {i}"); + } + + var frames = Enumerable.Range(0, 20_000).Select(i => $"originals/{i:00000}.JPG").ToArray(); + var manifest = Sequence(frames); + var clock = System.Diagnostics.Stopwatch.StartNew(); + var plan = Plan(manifest, "{title}", title: "Sunset"); + Assert.True(clock.Elapsed.TotalSeconds < TestTiming.Budget(5), $"{clock.Elapsed.TotalSeconds:0.0} s"); + Assert.SequenceEqual(["originals/Sunset.JPG", "originals/Sunset_2.JPG", "originals/Sunset_3.JPG"], plan.Samples.Select(s => s.To)); + Assert.Equal("originals/Sunset_20000.JPG", plan.Moves[^1].To); + } + private SequencePaths Folder(out ArchivePaths archive) { archive = new ArchivePaths(context.CreateTempDirectory("archive")); diff --git a/tests/Chronos.Core.Tests/Archiving/TestPhotos.cs b/tests/Chronos.Core.Tests/Archiving/TestPhotos.cs index 97646872..aaefe1e9 100644 --- a/tests/Chronos.Core.Tests/Archiving/TestPhotos.cs +++ b/tests/Chronos.Core.Tests/Archiving/TestPhotos.cs @@ -8,7 +8,7 @@ namespace Chronos.Core.Tests.Archiving; internal static class TestPhotos { public static byte[] Jpeg(DateTime captured, int seed, string make = "Canon", string model = "Canon EOS R5", string? serial = "0431", - int width = 48, int height = 32, int orientation = 1) + int width = 48, int height = 32, int orientation = 1, string? lens = null) { using var image = new ImageBuffer(width, height); var random = new Random(seed); @@ -28,14 +28,16 @@ internal static class TestPhotos FNumber = 8, Iso = 100, Orientation = orientation, + LensModel = lens, }); return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] }); } - public static string Write(string path, DateTime captured, int seed, string make = "Canon", string model = "Canon EOS R5", string? serial = "0431") + public static string Write(string path, DateTime captured, int seed, string make = "Canon", string model = "Canon EOS R5", string? serial = "0431", + string? lens = null) { Directory.CreateDirectory(Path.GetDirectoryName(path)!); - File.WriteAllBytes(path, Jpeg(captured, seed, make, model, serial)); + File.WriteAllBytes(path, Jpeg(captured, seed, make, model, serial, lens: lens)); return path; } diff --git a/tests/Chronos.Core.Tests/Naming/FrameNamingTests.cs b/tests/Chronos.Core.Tests/Naming/FrameNamingTests.cs index 2f011079..69b30c4c 100644 --- a/tests/Chronos.Core.Tests/Naming/FrameNamingTests.cs +++ b/tests/Chronos.Core.Tests/Naming/FrameNamingTests.cs @@ -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)))); + } } diff --git a/tests/Chronos.Core.Tests/Naming/PathTemplateTests.cs b/tests/Chronos.Core.Tests/Naming/PathTemplateTests.cs index dd9c6cc1..00ec3664 100644 --- a/tests/Chronos.Core.Tests/Naming/PathTemplateTests.cs +++ b/tests/Chronos.Core.Tests/Naming/PathTemplateTests.cs @@ -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() { diff --git a/tests/Chronos.Data.Tests/Archiving/ArchiveFixture.cs b/tests/Chronos.Data.Tests/Archiving/ArchiveFixture.cs index d9276d78..2e2af950 100644 --- a/tests/Chronos.Data.Tests/Archiving/ArchiveFixture.cs +++ b/tests/Chronos.Data.Tests/Archiving/ArchiveFixture.cs @@ -113,11 +113,11 @@ internal sealed class ArchiveFixture : IAsyncDisposable } /// Stages a verified upload-like file for ingest: returns its path, size and SHA-256. - public (string Path, long Size, string Sha256) Stage(DateTime captured, int seed, string make = "Canon", string model = "Canon EOS R5") + public (string Path, long Size, string Sha256) Stage(DateTime captured, int seed, string? make = "Canon", string? model = "Canon EOS R5", string? lens = null) { var staging = Path.Combine(Paths.StagingDirectory, "uploads"); Directory.CreateDirectory(staging); - var bytes = Photos.Jpeg(captured, seed, make, model); + var bytes = Photos.Jpeg(captured, seed, make, model, lens); var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part"); File.WriteAllBytes(path, bytes); return (path, bytes.Length, Convert.ToHexStringLower(SHA256.HashData(bytes))); @@ -132,7 +132,7 @@ internal sealed class ArchiveFixture : IAsyncDisposable /// Small real JPEGs with Exif made by the in-house encoder; each seed gives distinct bytes. internal static class Photos { - public static byte[] Jpeg(DateTime captured, int seed, string make = "Canon", string model = "Canon EOS R5") + public static byte[] Jpeg(DateTime captured, int seed, string? make = "Canon", string? model = "Canon EOS R5", string? lens = null) { using var image = new ImageBuffer(48, 32); var random = new Random(seed); @@ -142,7 +142,7 @@ internal static class Photos var level = (float)random.NextDouble(); for (var i = 0; i < plane.Length; i++) plane[i] = level * (0.5f + 0.5f * (i % 48) / 48f); } - var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { Make = make, Model = model, BodySerial = "0431", CapturedAt = captured, Iso = 100 }); + var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { Make = make, Model = model, LensModel = lens, BodySerial = "0431", CapturedAt = captured, Iso = 100 }); return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] }); } diff --git a/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs b/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs index 3657c0de..41e06e02 100644 --- a/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs +++ b/tests/Chronos.Data.Tests/Archiving/ReorganizeTests.cs @@ -132,11 +132,85 @@ public sealed class ReorganizeTests(TestContext context) await fx.WaitForAsync((await engine.EnqueueAsync(ArchiveJobs.Import(fx.Archive, import, null), cancellationToken: fx.Ct)).Id, JobState.Done); Assert.Count(2, await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)); - var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, null, new RenameTemplates(File: fx.Archive.FileTemplate), fx.Ct); + var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, null, new RenameTemplates(fx.Archive.FolderTemplate, fx.Archive.FileTemplate), fx.Ct); Assert.Empty(plan.Steps); Assert.Count(2, plan.Subjects); } + [Test] + public async Task UploadsAreFiledWithTheirSequencesCameraAndLensAsReorganizeNamesThem() + { + await using var fx = await CreateAsync(context); + const string FolderTemplate = "{year}/{date} {camera} {ext}", FileTemplate = "{camera}_{lens}_{n:0000}"; + await fx.Service.UpdateSettingsAsync(fx.Archive.Id, s => s with { Templates = s.Templates with { Folder = FolderTemplate, File = FileTemplate } }, fx.Ct); + // A sequence created with the camera it is for, whose photos name none: the hint names them. + var hinted = new Sequence + { + Id = Guid.CreateVersion7(), + ArchiveId = fx.Archive.Id, + Title = "Roof", + Folder = string.Empty, + CameraModel = "GoPro HERO12 Black", + Origin = SequenceOrigin.Server, + UpdatedAt = DateTimeOffset.UtcNow, + }; + await fx.Db.Sequences.UpsertAsync(hinted, fx.Ct); + for (var i = 0; i < 2; i++) + { + var staged = fx.Stage(Start.AddSeconds(10 * i), 40 + i, make: null, model: null); + await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, hinted.Id, null, null, staged.Path, $"GOPR{i:0000}.JPG", staged.Size, + staged.Sha256, null, null), fx.Ct); + } + // A lens changed during a session: its photos take the first one's, as the sequence does. + Guid lensed = default; + foreach (var (lens, i) in new[] { "RF15-35mm", "RF24-70mm" }.Select((l, i) => (l, i))) + { + var staged = fx.Stage(Start.AddHours(2).AddSeconds(10 * i), 50 + i, lens: lens); + lensed = (await fx.Service.Ingest.CommitAsync(Upload(fx, staged, $"IMG_{i:0000}.JPG", "upload-2"), fx.Ct)).SequenceId; + } + + Assert.SequenceEqual(["originals/GoPro HERO12 Black__0001.JPG", "originals/GoPro HERO12 Black__0002.JPG"], await FilesAsync(fx, hinted.Id)); + Assert.Equal("2026/2026-09-28 GoPro HERO12 Black JPG", (await fx.Db.Sequences.GetAsync(hinted.Id))!.Folder); + Assert.SequenceEqual(["originals/Canon EOS R5_RF15-35mm_0001.JPG", "originals/Canon EOS R5_RF15-35mm_0002.JPG"], await FilesAsync(fx, lensed)); + var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, null, new RenameTemplates(FolderTemplate, FileTemplate), fx.Ct); + Assert.Count(2, plan.Subjects); + Assert.Empty(plan.Steps); + } + + [Test] + public async Task ASequenceWhoseSidecarCannotBeReadStaysAsItIsAndTheOthersMove() + { + await using var fx = await CreateAsync(context); + var good = await UploadAsync(fx, 2); + var torn = await UploadAsync(fx, 2, firstSeed: 20, firstSecond: 3600, key: "upload-2"); + var foreign = await UploadAsync(fx, 2, firstSeed: 30, firstSecond: 7200, key: "upload-3"); + var last = await UploadAsync(fx, 2, firstSeed: 40, firstSecond: 10800, key: "upload-4"); + async Task FolderOf(Guid id) => fx.Paths.Sequence((await fx.Db.Sequences.GetAsync(id))!.Folder); + File.WriteAllText((await FolderOf(torn)).ManifestPath, "{ \"schemaVersion\": 1, \"id\": "); + File.Copy((await FolderOf(good)).ManifestPath, (await FolderOf(foreign)).ManifestPath, overwrite: true); + var kept = new Dictionary(); + foreach (var id in new[] { torn, foreign }) kept[id] = (await fx.Db.Sequences.GetAsync(id))!.Folder; + + var templates = new RenameTemplates(Folder: "moved/{date}", Title: "{title} {hm}"); + var plan = await fx.Service.PlanReorganizeAsync(fx.Archive.Id, null, templates, fx.Ct); + foreach (var id in kept.Keys) + { + var step = plan.Steps.Single(s => s.SequenceId == id); + Assert.Equal((kept[id], step.FromTitle), (step.ToFolder, step.ToTitle)); + Assert.False(step.Changes); + Assert.Equal(RenameBlock.Unreadable, step.Blocked.Single().Reason); + } + // The one after them gets the " (2)" they never take. + Assert.Equal(("moved/2026-09-28 (2)", "21-00"), (plan.Steps.Single(s => s.SequenceId == last).ToFolder, plan.Steps.Single(s => s.SequenceId == last).ToTitle)); + + var result = await fx.Service.ApplyRenameAsync(plan, null, fx.Ct); + Assert.SequenceEqual([good, last], result.Changed); + Assert.Equal("moved/2026-09-28", (await fx.Db.Sequences.GetAsync(good))!.Folder); + foreach (var id in kept.Keys) Assert.Equal(kept[id], (await fx.Db.Sequences.GetAsync(id))!.Folder); + Assert.Empty(RenameJournal.ListPending(fx.Paths)); + Assert.Equal(0, await fx.Service.Renamer.ResumePendingAsync(fx.Archive, fx.Ct)); + } + [Test] public async Task AChainOfNewNamesIsRenamedThroughTemporaryNames() { diff --git a/tests/Chronos.Server.Tests/Api/AdminTests.cs b/tests/Chronos.Server.Tests/Api/AdminTests.cs index 53d74800..dcf2dd00 100644 --- a/tests/Chronos.Server.Tests/Api/AdminTests.cs +++ b/tests/Chronos.Server.Tests/Api/AdminTests.cs @@ -175,6 +175,18 @@ public sealed class AdminTests(TestContext context) using (var bad = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { FolderTemplate = "{year/{date" }, ProtocolJson.Default.ArchiveSettingsDto)) Assert.Equal("folderTemplate", (await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details); + // Templates that would hide sequences from scans, drop extensions or repeat names: refused as Reorganize refuses them. + foreach (var (refused, field) in new[] + { + (archive with { FolderTemplate = "Originals/{year}" }, "folderTemplate"), + (archive with { FileTemplate = "{n}{ext}" }, "fileTemplate"), + (archive with { FileTemplate = "{date}" }, "fileTemplate"), + (archive with { FileTemplate = " " }, "fileTemplate"), + }) + { + using var response = await admin.PutAsJsonAsync("/api/v1/archive/settings", refused, ProtocolJson.Default.ArchiveSettingsDto); + Assert.Equal(field, (await response.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details); + } using (var good = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { Name = "Studio", ProxyLongEdge = 2560 }, ProtocolJson.Default.ArchiveSettingsDto)) { var saved = await good.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto); diff --git a/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs b/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs index c72330ad..c1f1e3dd 100644 --- a/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs +++ b/tests/Chronos.Server.Tests/Api/ReorganizeTests.cs @@ -106,6 +106,18 @@ public sealed class ReorganizeTests(TestContext context) } using (var template = await admin.PostAsJsonAsync(Url, valid with { FileTemplate = "{nope}" }, ProtocolJson.Default.ReorganizeRequest)) Assert.Equal("fileTemplate", (await template.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details); + // The desktop's rules: no lost extension, names that tell the photos apart, no folder scans skip. + foreach (var (bad, field) in new[] + { + (valid with { FileTemplate = "{n}{ext}" }, "fileTemplate"), + (valid with { FileTemplate = "{title}" }, "fileTemplate"), + (valid with { FolderTemplate = "Exports/{title}" }, "folderTemplate"), + (valid with { FolderTemplate = ".hidden/{date}" }, "folderTemplate"), + }) + { + using var refused = await admin.PostAsJsonAsync(Url, bad, ProtocolJson.Default.ReorganizeRequest); + Assert.Equal(field, (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details); + } using (var blank = await admin.PostAsJsonAsync(Url, valid with { FolderTemplate = " " }, ProtocolJson.Default.ReorganizeRequest)) Assert.Equal("folderTemplate", (await blank.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details); using (var unknown = await admin.PostAsJsonAsync(Url, valid with { SequenceIds = [Guid.NewGuid()] }, ProtocolJson.Default.ReorganizeRequest))