fix(archive): move sequences nested in a renamed folder with it

A mass rename or move of a sequence whose folder holds other sequences moved
their files without updating their index rows, and a target inside another
sequence's folder could nest one sequence in another. The plan now carries the
nested sequences (listed in the dry run, moved with the folder that holds them,
inner folders leaving first) and refuses a target inside a sequence folder with
a localized NestedRenameException (CD-4). The Archive screen counts only the
selected sequences in its summary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 09:56:54 +00:00
1 parent b0e3baa47c
commit 5d01d6ab34
7 files changed
+134 -12

No files matched your search

+50 -7
View File
@@ -1,6 +1,7 @@
using System.Globalization;
using System.Text.Json;
using Titano.Core.IO;
using Titano.Core.Localization;
using Titano.Core.Naming;
namespace Titano.Core.Archiving;
@@ -9,11 +10,20 @@ namespace Titano.Core.Archiving;
public sealed record RenameSubject(Guid SequenceId, string Folder, string? Title, DateTime? FirstCapture, string? CameraMake, string? CameraModel, string? Lens);
/// <summary>One sequence's change: its folder, its title, or both.</summary>
public sealed record RenameStep(Guid SequenceId, string FromFolder, string ToFolder, string? FromTitle, string? ToTitle)
/// <param name="Carried">The folder moves only because a folder it is inside moves (a sequence nested in another).</param>
public sealed record RenameStep(Guid SequenceId, string FromFolder, string ToFolder, string? FromTitle, string? ToTitle, bool Carried = false)
{
public bool MovesFolder => !string.Equals(FromFolder, ToFolder, StringComparison.Ordinal);
}
/// <summary>A planned folder would lie inside a sequence's folder (nesting one sequence in another); nothing is planned.</summary>
public sealed class NestedRenameException(string target, string folder)
: ArgumentException(Loc.L("Archive.Error.NestedTarget", target, folder))
{
public string Target { get; } = target;
public string Folder { get; } = folder;
}
/// <summary>The exact changes a mass rename will make (its dry run); applying it makes exactly these.</summary>
public sealed record RenamePlan(Guid Id, Guid ArchiveId, IReadOnlyList<RenameStep> Steps);
@@ -50,16 +60,21 @@ public static class RenamePlanner
/// Expands <paramref name="titleTemplate"/> (where <c>{title}</c> is the current title) and then
/// <paramref name="folderTemplate"/> (where <c>{title}</c> 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)"….
/// Null templates leave that part unchanged; subjects without a change produce no step.
/// Null templates leave that part unchanged; subjects without a change produce no step. A folder that moves takes
/// the sequences inside it along: each gets a <see cref="RenameStep.Carried"/> step after every move, so the index
/// follows its files; a selected sequence inside a moving folder that moves elsewhere leaves first.
/// </summary>
/// <param name="occupied">Every sequence folder of the archive (renamed or not).</param>
/// <param name="nested">The archive's other sequences inside a subject's folder, with their titles.</param>
/// <exception cref="NestedRenameException">A target lies inside a sequence's folder, or inside another target.</exception>
public static RenamePlan Plan(Guid archiveId, IReadOnlyList<RenameSubject> subjects, string? folderTemplate, string? titleTemplate,
IEnumerable<string> occupied, Func<string, bool> existsOnDisk)
IEnumerable<string> occupied, Func<string, bool> existsOnDisk, IReadOnlyList<RenameSubject>? nested = null)
{
var folder = folderTemplate is null ? null : PathTemplate.Parse(folderTemplate);
var title = titleTemplate is null ? null : PathTemplate.Parse(titleTemplate);
var claimed = new HashSet<string>(occupied, StringComparer.OrdinalIgnoreCase);
var steps = new List<RenameStep>();
var taken = occupied.ToList();
var claimed = new HashSet<string>(taken, StringComparer.OrdinalIgnoreCase);
var own = new List<RenameStep>(subjects.Count);
foreach (var s in subjects)
{
var context = new NamingContext
@@ -80,11 +95,39 @@ public static class RenamePlanner
newFolder = expanded.Equals(s.Folder, StringComparison.OrdinalIgnoreCase) ? expanded : Unique(expanded, claimed, existsOnDisk);
claimed.Add(newFolder);
}
if (newFolder != s.Folder || newTitle != s.Title) steps.Add(new RenameStep(s.SequenceId, s.Folder, newFolder, s.Title, newTitle));
own.Add(new RenameStep(s.SequenceId, s.Folder, newFolder, s.Title, newTitle));
}
return new RenamePlan(Guid.CreateVersion7(), archiveId, steps);
var moving = own.Where(s => s.MovesFolder).ToList();
foreach (var step in moving)
{
// Into a sequence's folder (its own included) or another target: one sequence would end up inside another.
var host = taken.Concat(moving.Where(m => m != step).Select(m => m.ToFolder)).FirstOrDefault(f => IsInside(f, step.ToFolder));
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))))
{
// 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);
if (carrier is not null)
steps.Add((step with { ToFolder = carrier.ToFolder + step.FromFolder[carrier.FromFolder.Length..], Carried = true }, 1));
else if (step.MovesFolder)
steps.Add((step, -moving.Count(m => m != step && IsInside(m.FromFolder, step.FromFolder))));
else if (step.FromTitle != step.ToTitle)
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)]);
}
/// <summary>
/// Whether <paramref name="path"/> lies inside <paramref name="folder"/> (archive-relative, forward slashes); the
/// archive root, a sequence only when a folder was adopted in place, counts as containing nothing.
/// </summary>
public static bool IsInside(string folder, string path) =>
folder.Length > 0 && path.Length > folder.Length + 1 && path[folder.Length] == '/' && path.StartsWith(folder, StringComparison.OrdinalIgnoreCase);
/// <summary>
/// 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).