fix(archive): a case-only target that is the sequence's own folder is kept; stored templates never fail other edits

Reorganize refused a case-only folder target whenever a folder of that exact
name was on disk. On a case-insensitive file system that folder can be the
sequence's own, when the disk spells it as the template does and the index
does not (renamed in Explorer, or an outer level a change of case left as it
was), so the plan gave a needless " (n)" folder. RenamePlanner.Kept now
refuses only when both exact names exist, the condition MassRenamer.TargetTaken
and MoveFolder already use.

ArchiveSettings.Problem held both templates to the template rules on every
UpdateSettingsAsync, so an archive.json saved before the rules made every
desktop Settings > Archive edit fail. A template the file already holds is now
only checked for being usable; a template the edit gives, and both templates
of a new archive, meet every rule. A broken rule throws TemplateRuleException
(an ArgumentException naming the field), and the desktop toast shows that
rule's sentence instead of "Something went wrong".

Also corrects comments that ignored RenameBlock.Unreadable (the whole
sequence stays as it is) and the ARCHITECTURE Naming row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit ac7a0c36df83b6d43651caf0225c530466242c97)
This commit is contained in:
Alby96 committed 2026-10-27 19:16:27 +01:00
1 parent 8127373fd5
commit 0377040a19
13 files changed
+193 -31

No files matched your search

+58 -13
View File
@@ -2,6 +2,7 @@ using System.Text.Json;
using Chronos.Core.Archiving;
using Chronos.Core.Diagnostics;
using Chronos.Core.Import;
using Chronos.Core.Localization;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using Chronos.Data.Archiving.Jobs;
@@ -104,13 +105,14 @@ public sealed class ArchiveService : IJobHandlerProvider
/// becomes the folder's name; a longer one than <see cref="ArchiveSettings.MaxNameLength"/> is shortened.
/// </summary>
/// <exception cref="InvalidOperationException">The folder already is an archive (open it instead).</exception>
/// <exception cref="ArgumentException">A template or policy is invalid (<see cref="ArgumentException.ParamName"/> names it).</exception>
/// <exception cref="ArgumentException">A template or policy is invalid (<see cref="ArgumentException.ParamName"/> names it;
/// <see cref="TemplateRuleException"/> for a template that breaks a rule).</exception>
public async Task<Archive> CreateAsync(string root, ArchiveSettings settings, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(settings);
var paths = new ArchivePaths(root);
settings = WithUsableName(settings, paths);
if (settings.Problem() is { } field) throw new ArgumentException($"The archive setting {field} is invalid.", field);
settings.Validate();
if (File.Exists(paths.ManifestPath)) throw new InvalidOperationException($"{paths.Root} already is a Chronos archive.");
Directory.CreateDirectory(paths.MetaDirectory);
var manifest = new ArchiveManifest
@@ -211,9 +213,12 @@ public sealed class ArchiveService : IJobHandlerProvider
/// a client sharing the archive never undo each other's change; the file is replaced atomically. Folders and files
/// already in the archive keep their names: templates and policies apply to the next imports, proxies and thumbnails.
/// A blank or overlong name in the file (archives made at a drive root, or before names were checked) is made usable
/// as <see cref="CreateAsync(string, ArchiveSettings, CancellationToken)"/> does, instead of failing every edit.
/// as <see cref="CreateAsync(string, ArchiveSettings, CancellationToken)"/> does, instead of failing every edit. Likewise
/// a template the file holds that breaks a rule of the archive's templates (saved before the rules existed) stays as it
/// is through an edit of anything else: only a template the edit gives is held to them (<see cref="ArchiveSettings.Problem"/>).
/// </summary>
/// <exception cref="ArgumentException">An edited setting is invalid (<see cref="ArgumentException.ParamName"/> names it).</exception>
/// <exception cref="ArgumentException">An edited setting is invalid (<see cref="ArgumentException.ParamName"/> names it;
/// <see cref="TemplateRuleException"/> for a template that breaks a rule).</exception>
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
/// <exception cref="LeaseUnavailableException">Another process kept the archive's settings busy.</exception>
public async Task<Archive> UpdateSettingsAsync(Guid archiveId, Func<ArchiveSettings, ArchiveSettings> edit, CancellationToken cancellationToken = default)
@@ -230,7 +235,8 @@ public sealed class ArchiveService : IJobHandlerProvider
if (current.Id != archive.Id) throw new InvalidDataException($"{paths.ManifestPath} belongs to archive {current.Id}, not {archive.Id}.");
// The name read is made usable before the edit sees it; a name the edit sets is checked as given.
var settings = edit(WithUsableName(ArchiveSettings.Of(current), paths));
if (settings.Problem() is { } field) throw new ArgumentException($"The archive setting {field} is invalid.", field);
// A template the file already holds stays as it is, rules or not: only one the edit gives is held to them.
settings.Validate(current.LayoutTemplates);
var manifest = current with
{
Name = settings.Name.Trim(),
@@ -536,21 +542,60 @@ public sealed record ArchiveSettings(string Name, LayoutTemplates Templates, Pro
/// <summary>
/// The first invalid field by its API name (<c>name</c>, <c>folderTemplate</c>, <c>fileTemplate</c>,
/// <c>proxyLongEdge</c>, <c>thumbnailLongEdge</c>); null when every field is valid. The templates are held to the rules
/// they meet wherever they are typed (<see cref="RenameTemplates.Problem"/>, <see cref="TemplateRules"/>): besides
/// parsing, a folder template may not write a level scans never enter, and a file template must keep the photo's
/// extension, tell a session's photos apart and hold no slash.
/// <c>proxyLongEdge</c>, <c>thumbnailLongEdge</c>); null when every field is valid. A template has to be usable
/// (<see cref="RenameTemplates.Check"/>), and one given anew is held to the rules it meets wherever it is typed
/// (<see cref="TemplateRules"/>): a folder template may not write a level scans never enter, and a file template must
/// keep the photo's extension, tell a session's photos apart and hold no slash.
/// </summary>
public string? Problem()
/// <param name="stored">The templates archive.json holds before an edit: one the edit leaves as it is, saved before the
/// rules existed (0.1.0-rc.5 and earlier), never fails an edit of another setting. Null holds both to every rule (a new
/// archive).</param>
public string? Problem(LayoutTemplates? stored = null)
{
if (string.IsNullOrWhiteSpace(Name) || Name.Trim().Length > MaxNameLength) return "name";
if (string.IsNullOrWhiteSpace(Templates.Folder)) return "folderTemplate";
if (string.IsNullOrWhiteSpace(Templates.File)) return "fileTemplate";
if (new RenameTemplates(Templates.Folder, Templates.File).Problem() is { } template) return template;
if (CheckTemplates(stored) is { } template) return template.Field;
if (!ProxyPolicy.IsValidLongEdge(Proxies.LongEdge) || Proxies.Quality is < 1 or > 100) return "proxyLongEdge";
if (!ThumbnailPolicy.IsValidLongEdge(Thumbnails.LongEdge) || Thumbnails.Quality is < 1 or > 100) return "thumbnailLongEdge";
return null;
}
/// <summary>
/// The first template that cannot be used (<see cref="Problem"/>), with the rule it breaks
/// (<see cref="TemplateProblem.Rule"/>); null when both can. A rule broken by a template <paramref name="stored"/>
/// already holds does not count.
/// </summary>
public TemplateProblem? CheckTemplates(LayoutTemplates? stored = null)
{
if (string.IsNullOrWhiteSpace(Templates.Folder)) return new TemplateProblem("folderTemplate", null);
if (string.IsNullOrWhiteSpace(Templates.File)) return new TemplateProblem("fileTemplate", null);
static TemplateProblem? Given(RenameTemplates template, bool kept) => template.Check() is { } problem && (problem.Rule is null || !kept) ? problem : null;
return Given(new RenameTemplates(Folder: Templates.Folder), Templates.Folder == stored?.Folder)
?? Given(new RenameTemplates(File: Templates.File), Templates.File == stored?.File);
}
/// <summary>Refuses these settings when a field is invalid (<see cref="Problem"/>).</summary>
/// <exception cref="TemplateRuleException">A template breaks a rule of the archive's templates.</exception>
/// <exception cref="ArgumentException">Another field is invalid (<see cref="ArgumentException.ParamName"/> names it).</exception>
internal void Validate(LayoutTemplates? stored = null)
{
if (Problem(stored) is not { } field) return;
if (CheckTemplates(stored) is { Rule: { } rule } template && template.Field == field) throw new TemplateRuleException(field, rule);
throw new ArgumentException($"The archive setting {field} is invalid.", field);
}
}
/// <summary>
/// An archive setting's template breaks a rule of the archive's templates (<see cref="TemplateRules"/>): the message says
/// which in one sentence (<c>Errors.Template.&lt;Rule&gt;</c>, in the language of the moment it was refused), and
/// <see cref="ParamName"/> names the template by its API name (<c>folderTemplate</c>, <c>fileTemplate</c>).
/// </summary>
public sealed class TemplateRuleException(string field, string rule) : ArgumentException(Loc.L("Errors.Template." + rule))
{
// Not the base's: its message would end in "(Parameter '…')".
public override string? ParamName { get; } = field;
/// <summary>The rule broken (<see cref="TemplateProblem.Rule"/>).</summary>
public string Rule { get; } = rule;
}
/// <summary>Builds the archive jobs; enqueue with <see cref="EnqueueAsync"/>.</summary>