fix(reorganize): never name a folder or title after a photo no longer first, and retry a kept job honestly
A Reorganize step whose photos changed after the plan kept its file names but still moved its folder and wrote the title the plan had computed from the old first photo, so a trashed first photo or an earlier photo filed since left the sequence named after a photo that is no longer first. The plan now journals its folder and title templates, and under the step's leases each folder move or retitle is expanded again from the settled sidecar: it is applied only when it gives what the plan gives (the folder itself or its " (n)"); otherwise the sequence stays as it was (Dropped, "changed after the plan"), or keeps its title and file names when an interrupted run moved its folder already (TitlesKept). Plans journaled before this change are applied as made. A Move job that ended failed with sequences kept, or whose held plan was given up, kept its plan:<id> checkpoint after the plan's journal was gone, so a retry logged "completed when the archive was opened" and ended done. Such a job now saves the checkpoint "applied": a retry plans again and applies only a plan that is still the one confirmed. A job that succeeds keeps plan:<id>, so a crash before it is marked done still finds the plan completed. Sequences a folder was to carry when that folder stayed (dropped, its title edited, its names no longer given, or a newer sidecar) were reported as "changed after the plan", and a newer-format carrier failed the job with ReorganizeKept. They are now a separate outcome, CarriedKept (journaled), logged with the new Archive.Log.ReorganizeCarriedKept (en+it) and not counted in the job's error, which follows the carrier's own reason. When a step's sidecar cannot be read or written after its folder moved, every sequence of the step is told (sequenceChanged "reorganized") before the failure is thrown, since the retry that would tell them may never come. Tests: Core APlanKeepsItsTemplatesSoTheNamesOfAFirstPhotoNoLongerFirstAreTold; Data AFolderOrTitleNamedAfterAPhotoNoLongerFirstIsNeverGiven, ARetryOfAJobThatKeptSequencesPlansAgain, ASequenceAFolderWasToCarryIsToldItStayedWithIt, AnArchiveOpensWhenAnInterruptedRenameMeetsASidecarOfANewerFormat, AResumedStepWhoseFolderMovedKeepsATitleEditedSinceAndTheNamesFromIt, and AMovedFolderWhoseSidecarCannotBeReadThereHasItsIndexRowFollowAndIsTold, which breaks the sidecar with a relative symbolic link instead of a read-only file, so it also runs on the Linux runner as root. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 130701afab224d47ea066eb7cc8debb16d1fb432)
This commit is contained in:
1 parent
37800b28ef
commit
8127373fd5
9 files changed
+455
-77
No files matched your search
@@ -144,6 +144,15 @@ public sealed record RenamePlan(Guid Id, Guid ArchiveId, IReadOnlyList<RenameSte
|
||||
/// <summary>The file template the steps' file names come from; null when files keep their names.</summary>
|
||||
public string? FileTemplate { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The folder template the steps' folders come from; null when folders stay, or in a plan journaled before it was kept
|
||||
/// (whose steps' folders are then taken as planned).
|
||||
/// </summary>
|
||||
public string? FolderTemplate { get; init; }
|
||||
|
||||
/// <summary>The title template the steps' titles come from; null when titles stay, or in a plan journaled before it was kept.</summary>
|
||||
public string? TitleTemplate { get; init; }
|
||||
|
||||
/// <summary>The sequences the plan was made for, in the order they were given (the order that decides which new folder gets " (2)").</summary>
|
||||
public IReadOnlyList<Guid> Subjects { get; init; } = [];
|
||||
|
||||
@@ -173,7 +182,9 @@ public sealed record RenamePlan(Guid Id, Guid ArchiveId, IReadOnlyList<RenameSte
|
||||
/// sequences changed, the frames whose file got a new name, and the sequences not changed as planned: because they changed
|
||||
/// since, left as they were (<see cref="Dropped"/>), their file names kept (<see cref="FilesKept"/>) or their title and file
|
||||
/// names kept (<see cref="TitlesKept"/>); their file names kept because a photo was in use or a new name was taken
|
||||
/// (<see cref="FilesBusy"/>); their title and file names kept because a newer Chronos wrote their sidecar (<see cref="Newer"/>).
|
||||
/// (<see cref="FilesBusy"/>); their title and file names kept because a newer Chronos wrote their sidecar (<see cref="Newer"/>);
|
||||
/// left as they were because the folder around them that was to carry them stayed, for its own reason
|
||||
/// (<see cref="CarriedKept"/>).
|
||||
/// </summary>
|
||||
public sealed record RenameTally
|
||||
{
|
||||
@@ -184,6 +195,7 @@ public sealed record RenameTally
|
||||
public IReadOnlyList<Guid> TitlesKept { get; init; } = [];
|
||||
public IReadOnlyList<Guid> FilesBusy { get; init; } = [];
|
||||
public IReadOnlyList<Guid> Newer { get; init; } = [];
|
||||
public IReadOnlyList<Guid> CarriedKept { get; init; } = [];
|
||||
}
|
||||
|
||||
/// <summary>A plan being applied, with the steps already completed; lets an interrupted rename resume (BRIEF §7).</summary>
|
||||
@@ -275,10 +287,10 @@ public static class RenamePlanner
|
||||
continue;
|
||||
}
|
||||
var context = Context(s);
|
||||
var newTitle = title is null || context is null ? s.Title : Blank(title.ExpandText(context)) ?? s.Title;
|
||||
var newTitle = NewTitle(context, s.Title, 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 && context 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 && NewFolder(context, newTitle, folder) is { } expanded
|
||||
&& !expanded.Equals(s.Folder, StringComparison.Ordinal))
|
||||
{
|
||||
var inside = stuck.Where(u => IsInside(s.Folder, u.Folder)).Select(u => u.SequenceId).ToList();
|
||||
@@ -323,9 +335,53 @@ public static class RenamePlanner
|
||||
steps.Add((step, 0));
|
||||
}
|
||||
// Stable: subjects keep their order, sequences inside moving folders move out first, carried ones come last.
|
||||
return new RenamePlan(Guid.CreateVersion7(), archiveId, [.. steps.OrderBy(s => s.Order).Select(s => s.Step)]) { FileTemplate = fileTemplate, Subjects = [.. subjects.Select(s => s.SequenceId)] };
|
||||
return new RenamePlan(Guid.CreateVersion7(), archiveId, [.. steps.OrderBy(s => s.Order).Select(s => s.Step)])
|
||||
{
|
||||
FileTemplate = fileTemplate,
|
||||
FolderTemplate = folderTemplate,
|
||||
TitleTemplate = titleTemplate,
|
||||
Subjects = [.. subjects.Select(s => s.SequenceId)],
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The title and the folder the templates give a sequence from its sidecar (<paramref name="manifest"/>) as
|
||||
/// <see cref="Plan"/> gives them, where <c>{title}</c> of the title template is <paramref name="title"/>: the title is
|
||||
/// <paramref name="title"/> without a title template or when it expands to nothing; the folder is the expansion itself,
|
||||
/// before any <c>" (2)"</c> (<see cref="Gives"/>), null without a folder template or when it expands to nothing.
|
||||
/// </summary>
|
||||
public static (string? Title, string? Folder) Names(SequenceManifest manifest, string? title, string? folderTemplate, string? titleTemplate)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(manifest);
|
||||
var context = Context(manifest, title);
|
||||
var newTitle = NewTitle(context, title, titleTemplate is null ? null : PathTemplate.Parse(titleTemplate));
|
||||
return (newTitle, folderTemplate is null ? null : NewFolder(context, newTitle, PathTemplate.Parse(folderTemplate)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether <paramref name="folder"/> is a folder a plan gives for the expansion <paramref name="expanded"/>
|
||||
/// (<see cref="Names"/>): that one, or the <c>" (n)"</c> of it that a folder of that name already there made it take.
|
||||
/// </summary>
|
||||
public static bool Gives(string expanded, string folder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(expanded);
|
||||
ArgumentNullException.ThrowIfNull(folder);
|
||||
return folder.Equals(expanded, StringComparison.Ordinal) || (Numbered(folder, expanded, StringComparison.Ordinal) is { } n && n >= 2
|
||||
&& folder.Equals(string.Create(CultureInfo.InvariantCulture, $"{expanded} ({n})"), StringComparison.Ordinal));
|
||||
}
|
||||
|
||||
/// <summary>The <c>n</c> of <paramref name="folder"/> when it is <paramref name="expanded"/> followed by <c>" (n)"</c>; null otherwise.</summary>
|
||||
private static int? Numbered(string folder, string expanded, StringComparison comparison) =>
|
||||
folder.Length > expanded.Length + 3 && folder.StartsWith(expanded + " (", comparison) && folder[^1] == ')'
|
||||
&& int.TryParse(folder.AsSpan(expanded.Length + 2, folder.Length - expanded.Length - 3), NumberStyles.None, CultureInfo.InvariantCulture, out var n)
|
||||
? n
|
||||
: null;
|
||||
|
||||
private static string? NewTitle(NamingContext? context, string? title, PathTemplate? template) =>
|
||||
template is null || context is null ? title : Blank(template.ExpandText(context)) ?? title;
|
||||
|
||||
private static string? NewFolder(NamingContext context, string? newTitle, PathTemplate template) => Blank(template.ExpandFolder(context with { Title = newTitle }));
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
@@ -334,20 +390,7 @@ public static class RenamePlanner
|
||||
/// </summary>
|
||||
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.Files?.Manifest is { } manifest) return Context(manifest, s.Title);
|
||||
if (s.FirstCapture is not { } first) return null;
|
||||
return new NamingContext
|
||||
{
|
||||
@@ -360,6 +403,22 @@ public static class RenamePlanner
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>What the folder and title templates see in a sidecar: its first frame (<see cref="Context(RenameSubject)"/>).</summary>
|
||||
private static NamingContext Context(SequenceManifest manifest, string? title)
|
||||
{
|
||||
if (manifest.Frames.Count > 0) return FrameNaming.Context(manifest.Frames[0], manifest, 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 = title,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The step with the file renames of its sequence (<see cref="FileRenames.Plan"/>).</summary>
|
||||
private static RenameStep WithFiles(RenameStep step, SequenceFiles files, PathTemplate template, string? title)
|
||||
{
|
||||
@@ -455,10 +514,7 @@ public static class RenamePlanner
|
||||
{
|
||||
string? same = null;
|
||||
if (current.Equals(expanded, StringComparison.OrdinalIgnoreCase)) same = expanded;
|
||||
else if (current.Length > expanded.Length + 3 && current.StartsWith(expanded + " (", StringComparison.OrdinalIgnoreCase) && current[^1] == ')'
|
||||
&& int.TryParse(current.AsSpan(expanded.Length + 2, current.Length - expanded.Length - 3), NumberStyles.None, CultureInfo.InvariantCulture,
|
||||
out var n) && n >= 2)
|
||||
same = string.Create(CultureInfo.InvariantCulture, $"{expanded} ({n})");
|
||||
else if (Numbered(current, expanded, StringComparison.OrdinalIgnoreCase) is { } 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) || given.Contains(same)) return null;
|
||||
if (same.Equals(current, StringComparison.Ordinal)) return same;
|
||||
|
||||
Reference in new issue
Block a user