feat(archive): reorganize existing sequences with folder, file and title templates
Owner review 2, R7 asks for renaming and moving after import, on the web panel and the desktop. This is the engine and the server API both UIs build on. - FrameNaming (Core/Naming) is the one naming context and unique-name rule of a frame: imports, uploads and Reorganize now produce identical names (behaviour of the ingest and the import planner unchanged). - RenamePlanner takes a file template: each step counts its renamed frames, shows the first three and the frames version they were planned at, and lists what keeps its name (photos in place outside originals/, never hashed, missing, unreadable sidecar). Names are given in index order with _2, _3 as filing does; XMP companions follow their photo. - MassRenamer renames a step's files under the same leases after its folder moved, checks the frames version, journals .chronos/rename-files.json and moves files in two phases through temporary names so chains and swaps never meet; then sidecar, index (FrameRepository.RenameFilesAsync, both dialects) and journal. A failure puts every file back; a crash is completed by the next writer or the plan's resume, the index following. Plans a paused or cancelled job holds are left to that job. - JobKind.Move gets MoveJobHandler: it plans again and applies only the confirmed plan (fingerprint and frames versions), with progress and checkpoints per sequence. - POST /api/v1/archive/reorganize: dry run with the plan and its fingerprint, otherwise a 202 move job; validated templates, audited, sequenceChanged reorganized events. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit aae1c3eb18ea932b4a6251c5e2a282b9295a5785)
This commit is contained in:
1 parent
81cf58227d
commit
eb2395d9b0
36 files changed
+2238
-164
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Localization;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Naming;
|
||||
using Chronos.Data;
|
||||
@@ -13,12 +14,16 @@ using Chronos.Server.Storage;
|
||||
|
||||
namespace Chronos.Server.Api;
|
||||
|
||||
/// <summary><c>/api/v1/archive</c> (admin): templates and proxy policy, scan/rebuild/verify/maintenance jobs, statistics.</summary>
|
||||
/// <summary>
|
||||
/// <c>/api/v1/archive</c> (admin): templates and proxy policy, scan/rebuild/verify/maintenance jobs, statistics, and
|
||||
/// Reorganize (owner review 2, R7): new templates applied to sequences already in the archive.
|
||||
/// </summary>
|
||||
public static class ArchiveEndpoints
|
||||
{
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var archive = api.MapGroup("/archive").Requires(Permissions.ManageArchive);
|
||||
archive.MapPost("/reorganize", ReorganizeAsync);
|
||||
// The form saves every field: it starts from what archive.json holds now, also a desktop's change since the last poll.
|
||||
archive.MapGet("/settings", async (HttpContext c, ServerArchive a, ChronosDatabase db, [FromServices] ArchiveService? archives) =>
|
||||
Results.Json(Settings(archives is null ? a.Current : await a.RefreshAsync(archives, db, c.RequestAborted)), ProtocolJson.Default.ArchiveSettingsDto));
|
||||
@@ -122,6 +127,77 @@ public static class ArchiveEndpoints
|
||||
return Results.Json(Settings(updated), ProtocolJson.Default.ArchiveSettingsDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reorganize: folder, file and title templates applied to <c>sequenceIds</c> or, with <c>all</c>, to every sequence of the
|
||||
/// archive (BRIEF §12.5). With <c>dryRun</c> the plan, nothing touched: per sequence its folder and title before and after,
|
||||
/// the photos renamed with the first of them, and the photos that keep their names and why. Otherwise the request carries
|
||||
/// 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>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 every sequence it changes as <c>sequenceChanged</c>
|
||||
/// (<c>reorganized</c>).
|
||||
/// </summary>
|
||||
private static async Task<IResult> ReorganizeAsync(HttpContext context, Caller caller, ReorganizeRequest request, ChronosDatabase db, ServerArchive archive,
|
||||
[FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (request.All == (request.SequenceIds is not null) || request.SequenceIds is { Count: 0 or > LibraryEndpoints.MaxBulkIds })
|
||||
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 (!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");
|
||||
RenamePlan plan;
|
||||
try
|
||||
{
|
||||
plan = await LibraryEndpoints.Engine(archives, db).PlanReorganizeAsync(archive.Id, request.All ? null : request.SequenceIds, templates, ct);
|
||||
}
|
||||
catch (NestedRenameException ex)
|
||||
{
|
||||
return ApiErrors.Conflict(context, "nested_target", ex.Target);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return ApiErrors.NotFound(context, "sequence");
|
||||
}
|
||||
var fingerprint = plan.Fingerprint();
|
||||
if (request.DryRun) return Results.Json(Plan(context, plan, fingerprint), ProtocolJson.Default.ReorganizePlanDto);
|
||||
if (!string.Equals(fingerprint, request.Fingerprint, StringComparison.Ordinal)) return ApiErrors.Conflict(context, "plan_changed", fingerprint);
|
||||
if (!host.CanRun(JobKind.Move)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(JobKind.Move));
|
||||
var job = await host.Engine.EnqueueAsync(ArchiveJobs.Reorganize(archive.Current, plan, templates), caller.Id, ct);
|
||||
await audit.WriteAsync(caller, context, "archive_reorganized", "job", job.Id.ToString(),
|
||||
$"{plan.Steps.Count(s => s.Changes)} · {plan.Steps.Sum(s => s.FileChanges)} · {templates.Folder} · {templates.File} · {templates.Title}");
|
||||
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>
|
||||
private static ReorganizePlanDto Plan(HttpContext context, RenamePlan plan, string fingerprint)
|
||||
{
|
||||
var culture = RequestCulture.Of(context);
|
||||
return new ReorganizePlanDto(
|
||||
[.. plan.Steps.Select(s => new ReorganizeStepDto(s.SequenceId, s.FromFolder, s.ToFolder, s.FromTitle, s.ToTitle, s.Carried, s.FramesVersion,
|
||||
s.FileChanges, [.. s.FileSamples.Select(f => new ReorganizeFileDto(f.From, f.To))],
|
||||
[.. s.Blocked.Select(b => new ReorganizeBlockedDto(Wire(b.Reason), b.Frames, Loc.Get("Archive.Reorganize.Blocked." + b.Reason, culture) ?? Wire(b.Reason)))]))],
|
||||
plan.Steps.Count(s => s.Changes),
|
||||
plan.Steps.Sum(s => s.FileChanges),
|
||||
plan.Steps.Sum(s => s.Blocked.Sum(b => b.Frames)),
|
||||
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>
|
||||
public static string Wire(RenameBlock reason) => reason switch
|
||||
{
|
||||
RenameBlock.InPlace => "in_place",
|
||||
RenameBlock.Unhashed => "unhashed",
|
||||
RenameBlock.Missing => "missing",
|
||||
_ => "unreadable",
|
||||
};
|
||||
|
||||
private static async Task<IResult> EnqueueAsync(HttpContext context, Caller caller, ServerArchive archive, JobEngineHost host, AuditLog audit, JobKind kind)
|
||||
{
|
||||
if (!host.CanRun(kind)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(kind));
|
||||
|
||||
Reference in new issue
Block a user