fix(archive): Reorganize names as filing does and refuses templates that lose or hide photos
Reorganize with the archive's own templates planned renames and moves for
photos just filed, and some templates could strip extensions, hide sequences
from scans or stall a dry run. Owner review 2, phase 4a findings.
- Filing and Reorganize share one context: uploads and imports name every
photo with the sequence's camera and lens (its first photo's, or the
creator's hint), an upload's folder from its first frame in the new
sequence, and a missing capture time from the sequence start instead of
the current time. The planner expands folder and title templates from the
first frame in the sidecar ({name}, {ext}, {iso}...), never today's date.
- Titles expand as text (ExpandText): "Paris: night" stays as typed.
- A sequence whose sidecar cannot be read (torn, another sequence's) keeps
its folder, title and names, listed with why, and so does every folder
around it; the dry run reads every sidecar, nested ones included.
- Kept never gives one folder to two sequences of a plan whatever the case,
and a case-only move onto another folder is refused instead of taken as
done.
- Shared template rules (TemplateRules) for the server's Reorganize and
archive settings and the desktop's fields: a file template must keep the
photo's extension ("{n}{ext}" refused) and tell a session's photos apart;
a folder template may not write a level scans skip (a leading dot,
originals, proxies, exports). Levels a title gives are escaped with "_".
- {ext} comes from the file, or from the original name when the file lost
a photo's extension, so a Reorganize can put it back.
- Unique names cost linear time (UniqueNames) with the same names as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6c734777e01476881a3f34b3188571cc3f7150ab)
This commit is contained in:
1 parent
13c2ac321c
commit
b253cac8d4
25 files changed
+777
-173
No files matched your search
@@ -217,12 +217,17 @@ public sealed class ArchiveIngest : IArchiveIngest
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A new folder for <paramref name="manifest"/>: the plan's folder or the archive's folder template, made unique.</summary>
|
||||
/// <summary>
|
||||
/// A new folder for <paramref name="manifest"/>: 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 (<see cref="FrameNaming.Context(SidecarFrame, SequenceManifest, string?)"/>).
|
||||
/// </summary>
|
||||
private async Task<SequencePaths> 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;
|
||||
}
|
||||
|
||||
/// <summary>Final file names, in capture order: the plan's name or the template with <c>{n}</c> = index + 1, made unique.</summary>
|
||||
/// <summary>
|
||||
/// Final file names, in capture order: the plan's name, or the template with each frame in the sequence it joins
|
||||
/// (<see cref="FrameNaming.Context(SidecarFrame, SequenceManifest, string?)"/>: <c>{n}</c> = index + 1, the sequence's
|
||||
/// camera and lens), the context Reorganize names it with; made unique.
|
||||
/// </summary>
|
||||
private static Dictionary<Guid, SidecarFrame> 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<Guid, SidecarFrame>();
|
||||
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);
|
||||
|
||||
@@ -80,10 +80,12 @@ public sealed class MassRenamer
|
||||
PlanAsync(archive, sequenceIds, new RenameTemplates(folderTemplate, null, titleTemplate), cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// The dry run of a Reorganize: the plan of <paramref name="templates"/> over <paramref name="sequenceIds"/>. 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 <paramref name="templates"/> over <paramref name="sequenceIds"/>. 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.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentException">A sequence is not in the archive.</exception>
|
||||
/// <exception cref="NestedRenameException">A new folder would lie inside a sequence's folder.</exception>
|
||||
@@ -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<RenameSubject>();
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// 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 <paramref name="listFiles"/>, the files beside its frames; marked
|
||||
/// <see cref="RenameSubject.Unreadable"/> (logged) when the sidecar cannot be read, is another sequence's, or is of a
|
||||
/// newer format this build never rewrites.
|
||||
/// </summary>
|
||||
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 };
|
||||
}
|
||||
|
||||
/// <summary>Applies a plan; each completed step is journaled before the next starts.</summary>
|
||||
|
||||
Reference in new issue
Block a user