fix(archive): Reorganize never plans a move onto a folder of the exact name, keeps long names' extensions, says why a template is refused

Second fix round of owner review 2, phase 4a (planner findings).

- A sequence keeps a folder that differs from its own in case only while no
  other folder holds that exact name (RenamePlanner.ExistsExactly, every level
  compared exactly). On a case-sensitive file system a stray "Trip (2)" beside
  the sequence at "trip (2)" no longer gives a case-only step that MoveFolder
  refuses at every archive open: the plan gives "Trip", and a plan whose
  target appeared since is refused under the step's leases before anything
  moves (or dropped when it resumes). A change of case in an outer level no
  longer looks like two folders on Windows.
- A file name longer than 200 characters is shortened before the ".{ext}" the
  template ends with, never through it, so a long title never strips the
  photo's extension.
- The archive engine's own settings check applies the template rules the
  server and the desktop apply (a folder scans skip, a lost extension, names
  that repeat), so archive.json can no longer take such a template.
- The server's invalid_template answer names the rule after the field
  ("fileTemplate:extension") with the desktop's sentence as its message; both
  admin panels show it on the field.
- FrameNaming keeps only the naming overloads production uses.
- Comments about Unreadable say the whole sequence stays as it is.
- Tests pin filing against Reorganize: an upload without a capture time, a
  card's second session with {session}, a duplicate skipped mid-session, and a
  whole folder of two cameras named with the first one's camera.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 50fe59aec1e180daa1733bcb951892a79dceba6c)
This commit is contained in:
Alby96 committed 2026-10-27 18:08:08 +01:00
1 parent 9e1f109b58
commit 765f0ad36b
20 files changed
+446 -141

No files matched your search

+28 -6
View File
@@ -70,7 +70,9 @@ public static class ArchiveEndpoints
/// <summary>
/// Writes the name, templates and proxy policy into the archive's <c>archive.json</c> through the archive engine (so a
/// restart and the proxy jobs see them), then into the index. 503 <c>archive_offline</c> while the archive cannot be
/// restart and the proxy jobs see them), then into the index. 400 <c>bad_request</c> (details <c>name</c>,
/// <c>proxyLongEdge</c>), 400 <c>invalid_template</c> (<see cref="InvalidTemplate"/>: <c>folderTemplate</c> or
/// <c>fileTemplate</c>, with the rule it breaks); 503 <c>archive_offline</c> while the archive cannot be
/// written; 409 <c>archive_busy</c> while another process holds its settings; 409 <c>archive_manifest</c> when its
/// archive.json is damaged, written by a newer Chronos or of another archive; 409 <c>archive_policy</c> when it holds a
/// thumbnail or proxy setting this API cannot set and Chronos cannot use (details: <c>thumbnailPolicy</c> or
@@ -84,7 +86,7 @@ public static class ArchiveEndpoints
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 (new RenameTemplates(request.FolderTemplate, request.FileTemplate).Check() is { } problem) return InvalidTemplate(context, problem);
if (!ProxyPolicy.IsValidLongEdge(request.ProxyLongEdge)) return ApiErrors.BadRequest(context, "bad_request", "proxyLongEdge");
var engine = archives ?? new ArchiveService(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner });
@@ -134,7 +136,8 @@ public static class ArchiveEndpoints
/// that plan's fingerprint and the server queues a <c>move</c> job (202) that applies exactly that plan, refused when the
/// archive changed before it ran. 400 <c>bad_request</c> (details <c>sequenceIds</c>: neither or both of them, none, or
/// more than <see cref="LibraryEndpoints.MaxBulkIds"/>; <c>templates</c>: none; <c>fingerprint</c>), 400
/// <c>invalid_template</c> (details: <c>folderTemplate</c>, <c>fileTemplate</c> or <c>titleTemplate</c>); 404
/// <c>invalid_template</c> (<see cref="InvalidTemplate"/>: <c>folderTemplate</c>, <c>fileTemplate</c> or
/// <c>titleTemplate</c>, with the rule it breaks); 404
/// <c>not_found</c> (<c>sequence</c>: not in the archive); 409 <c>nested_target</c> (details: the folder that would lie
/// inside a sequence's folder), <c>plan_changed</c> (details: the fingerprint now), <c>job_unavailable</c>; 503
/// <c>archive_offline</c>. Audited; the job tells each sequence it changes as <c>sequenceChanged</c> (<c>reorganized</c>)
@@ -152,7 +155,7 @@ public static class ArchiveEndpoints
return ApiErrors.BadRequest(context, "bad_request", "sequenceIds");
var templates = new RenameTemplates(request.FolderTemplate, request.FileTemplate, request.TitleTemplate);
if (templates.IsEmpty) return ApiErrors.BadRequest(context, "bad_request", "templates");
if (templates.Problem() is { } field) return ApiErrors.BadRequest(context, "invalid_template", field);
if (templates.Check() is { } problem) return InvalidTemplate(context, problem);
if (!request.DryRun && string.IsNullOrWhiteSpace(request.Fingerprint)) return ApiErrors.BadRequest(context, "bad_request", "fingerprint");
if (ArchiveService.GetAvailability(archive.Current) != ArchiveAvailability.Online)
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
@@ -183,7 +186,23 @@ public static class ArchiveEndpoints
return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
}
/// <summary>The dry run as the API answers it, with each reason photos keep their names in the request's language.</summary>
/// <summary>
/// 400 <c>invalid_template</c> for a template that cannot be used. The details name its field (<c>fileTemplate</c>); when
/// it parses but breaks a rule of the archive's templates (<see cref="TemplateRules"/>), the rule follows after a colon
/// (<c>folderTemplate:reserved_folder</c>, <c>fileTemplate:file_separator</c>, <c>fileTemplate:extension</c>,
/// <c>fileTemplate:not_unique</c>) and the message says why in the request's language, as the desktop's fields do
/// (<c>Errors.Template.&lt;Rule&gt;</c>); otherwise the message is that the template is not valid.
/// </summary>
private static IResult InvalidTemplate(HttpContext context, TemplateProblem problem)
{
if (problem.Rule is not { } rule) return ApiErrors.BadRequest(context, "invalid_template", problem.Field);
var message = Loc.Get("Errors.Template." + rule, RequestCulture.Of(context)) ?? ApiErrors.Message(context, "invalid_template");
var wire = string.Concat(rule.Select((c, i) => char.IsUpper(c) ? (i > 0 ? "_" : string.Empty) + char.ToLowerInvariant(c) : c.ToString()));
return Results.Json(new ApiError("invalid_template", message, $"{problem.Field}:{wire}"), ProtocolJson.Default.ApiError,
statusCode: StatusCodes.Status400BadRequest);
}
/// <summary>The dry run as the API answers it, with each reason something stays as it is in the request's language.</summary>
private static ReorganizePlanDto Plan(HttpContext context, RenamePlan plan, string fingerprint)
{
var culture = RequestCulture.Of(context);
@@ -197,7 +216,10 @@ public static class ArchiveEndpoints
fingerprint);
}
/// <summary>Wire name of a reason photos keep their names: <c>in_place</c>, <c>unhashed</c>, <c>missing</c>, <c>unreadable</c>.</summary>
/// <summary>
/// Wire name of a reason something stays as it is: photos that keep their names (<c>in_place</c>, <c>unhashed</c>,
/// <c>missing</c>), or the whole sequence (<c>unreadable</c>).
/// </summary>
public static string Wire(RenameBlock reason) => reason switch
{
RenameBlock.InPlace => "in_place",