From 66c36629540ce20af7fb6b3ca60c3c2e0ea04880 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 27 Oct 2026 11:58:18 +0100 Subject: [PATCH] fix(server): trash and photo-delete API answers, body limit and audit - frames/delete carries its own request body limit (about 4.8 MB), so the documented 100,000 ids fit; the server's 1 MiB stopped at about 26,900. - Restore tells a damaged or foreign sidecar of the target sequence (409 sidecar_mismatch) from an entry whose files no longer match its record (422 trash_mismatch, now a TrashMismatchException); frames/delete answers sidecar_mismatch instead of 500, and 404 when the sequence left the index between plan and delete, also on the re-plan. - With the archive offline, the trash list and the empty plan answer 503 archive_offline instead of an empty trash; a read-only archive still lists and plans but cannot be emptied. - The empty request is audited as trash_empty_requested: the job checks the plan again and may erase nothing, so the entry no longer claims an erase that has not happened. - API tests for these paths, a named subset emptied, sequence-entry restore and sequence_in_library, photos restored into a new sequence, viewer 403 on bulk routes and admin deletes of another user's photos. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit fa35831d55c44746f7f76e0cff262ed6aecf8e72) --- i18n/en/Admin.resx | 4 +- i18n/it/Admin.it.resx | 4 +- .../Archiving/FrameTrash.Restore.cs | 4 +- src/Chronos.Data/Archiving/SequenceTrash.cs | 6 + src/Chronos.Server/Admin/web/audit.js | 2 +- src/Chronos.Server/Api/LibraryEndpoints.cs | 59 ++++- src/Chronos.Server/Api/TrashEndpoints.cs | 43 ++- .../Archiving/FrameTrashTests.cs | 2 +- .../Chronos.Server.Tests/Api/LibraryTests.cs | 250 ++++++++++++++++++ 9 files changed, 350 insertions(+), 24 deletions(-) diff --git a/i18n/en/Admin.resx b/i18n/en/Admin.resx index d75b9771..57550370 100644 --- a/i18n/en/Admin.resx +++ b/i18n/en/Admin.resx @@ -25,7 +25,7 @@ {0} deleted {1}long {0} edited a sequence · {1}long {0} changed the server settingslong - {0} emptied the trash · {1}long + {0} asked to empty the trash · {1}long {0} restored {1} from the trashlong {0} created user {1}long {0} deleted user {1}long @@ -360,7 +360,7 @@ Sequence edited Diagnostics exported Server settings - Trash emptied + Emptying requested Restored from trash User created User deleted diff --git a/i18n/it/Admin.it.resx b/i18n/it/Admin.it.resx index f4d521af..42664f87 100644 --- a/i18n/it/Admin.it.resx +++ b/i18n/it/Admin.it.resx @@ -25,7 +25,7 @@ {0} ha eliminato {1} {0} ha modificato una sequenza · {1} {0} ha modificato le impostazioni del server - {0} ha svuotato il cestino · {1}long + {0} ha chiesto di svuotare il cestino · {1}long {0} ha ripristinato {1} dal cestinolong {0} ha creato l'utente {1} {0} ha eliminato l'utente {1} @@ -360,7 +360,7 @@ Sequenza modificata Diagnostica esportata Impostazioni server - Cestino svuotato + Svuotamento richiesto Ripristino dal cestino Utente creato Utente eliminato diff --git a/src/Chronos.Data/Archiving/FrameTrash.Restore.cs b/src/Chronos.Data/Archiving/FrameTrash.Restore.cs index e7959759..3311b98b 100644 --- a/src/Chronos.Data/Archiving/FrameTrash.Restore.cs +++ b/src/Chronos.Data/Archiving/FrameTrash.Restore.cs @@ -32,6 +32,8 @@ public sealed partial class FrameTrash /// /// The entry's (photos-…). /// The entry is not in the trash. + /// No file of the entry matches its record; nothing moved. + /// The sequence's sidecar is damaged or belongs to another sequence; nothing moved. public async Task RestoreAsync(Archive archive, string name, JobEngine? followUps = null, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(archive); @@ -55,7 +57,7 @@ public sealed partial class FrameTrash throw new DirectoryNotFoundException($"{name} is not in the trash of {archive.Name}."); var (intact, kept) = await CheckAsync(entryFolder, record, cancellationToken).ConfigureAwait(false); // Nothing that matches its record: the entry stays as it is. - if (intact.Count == 0) throw new InvalidDataException(Core.Localization.Loc.L("Archive.Error.TrashMismatch")); + if (intact.Count == 0) throw new TrashMismatchException(); var had = into?.Status ?? SequenceStatus.None; PhotoRestoreResult result; List stays; diff --git a/src/Chronos.Data/Archiving/SequenceTrash.cs b/src/Chronos.Data/Archiving/SequenceTrash.cs index 24502cd9..4940e3d7 100644 --- a/src/Chronos.Data/Archiving/SequenceTrash.cs +++ b/src/Chronos.Data/Archiving/SequenceTrash.cs @@ -82,6 +82,12 @@ public sealed class SequenceInLibraryException(Guid sequenceId, string title) : public Guid SequenceId { get; } = sequenceId; } +/// +/// No file of a photo entry matches its record any more (each one re-hashed); the entry stays as it is and nothing moved. +/// A restore's is about a sidecar instead (damaged, or another sequence's). +/// +public sealed class TrashMismatchException() : IOException(Loc.L("Archive.Error.TrashMismatch")); + /// /// Deleting sequences without erasing anything (BRIEF §0.2 rule 8, §12.2, §12.5): is the dry /// run; moves each sequence folder whole — originals, proxies, exports and its sidecars — into diff --git a/src/Chronos.Server/Admin/web/audit.js b/src/Chronos.Server/Admin/web/audit.js index d3fd5d61..b803569e 100644 --- a/src/Chronos.Server/Admin/web/audit.js +++ b/src/Chronos.Server/Admin/web/audit.js @@ -70,7 +70,7 @@ export function detailText(entry) { const [, ...title] = details.split(" · "); return title.length > 0 ? title.join(" · ") : details; } - case "trash_emptied": { + case "trash_empty_requested": { const [count, size] = details.split(" · "); return /^\d+$/.test(count) && /^\d+$/.test(size ?? "") ? `${plural("Admin.Trash.EntryCount", Number(count))} · ${formatBytes(Number(size))}` : details; } diff --git a/src/Chronos.Server/Api/LibraryEndpoints.cs b/src/Chronos.Server/Api/LibraryEndpoints.cs index 00bef0ca..a9feed05 100644 --- a/src/Chronos.Server/Api/LibraryEndpoints.cs +++ b/src/Chronos.Server/Api/LibraryEndpoints.cs @@ -1,4 +1,5 @@ using System.Globalization; +using Microsoft.AspNetCore.Http.Metadata; using Microsoft.AspNetCore.Mvc; using Chronos.Core.Archiving; using Chronos.Core.Imaging.Decoders; @@ -32,6 +33,12 @@ public static class LibraryEndpoints /// Photos one delete request may name. public const int MaxFrameIds = 100_000; + /// + /// The body a delete request of photos may take: 48 bytes an id (36 characters, quotes, a + /// comma and some indentation) and 4 KiB for the rest, about 4.8 MB, above the server's 1 MiB for other JSON bodies. + /// + public const long FrameDeleteBodyLimit = MaxFrameIds * 48L + 4096; + private const string Ok = "ok"; private const string Skipped = "skipped"; private const string Regenerable = "private, max-age=3600"; @@ -41,7 +48,7 @@ public static class LibraryEndpoints var sequences = api.MapGroup("/sequences"); sequences.MapGet("/facets", FacetsAsync).Requires(Permissions.Read); sequences.MapGet("/{id:guid}/thumb", ThumbAsync).Requires(Permissions.Read); - sequences.MapPost("/{id:guid}/frames/delete", DeleteFramesAsync).Requires(Permissions.Delete); + sequences.MapPost("/{id:guid}/frames/delete", DeleteFramesAsync).Requires(Permissions.Delete).WithMetadata(new FrameDeleteBodyLimitMetadata()); sequences.MapPost("/bulk/delete", BulkDeleteAsync).Requires(Permissions.Delete); sequences.MapPost("/bulk/retag", BulkRetagAsync).Requires(Permissions.Edit); sequences.MapPost("/bulk/jobs", BulkJobsAsync).Requires(Permissions.Edit); @@ -90,10 +97,12 @@ public static class LibraryEndpoints /// /// Moves photos of a sequence into the archive's trash (; BRIEF §0.2 rule 8: nothing is erased), /// or with dryRun says what would go. Owners and admins only, like deleting the sequence. 400 bad_request - /// (frameIds); 409 frames_conflict (the frames changed since expectedFramesVersion, or a photo named - /// is not the sequence's; details: the frames version now), sequence_busy, would_empty (every photo would - /// go: delete the sequence instead), folder_in_use; 503 archive_offline. Publishes sequenceChanged - /// frames; the thumbnails, proxies and analyses follow as jobs. + /// (frameIds: none, or more than ); 404 not_found (sequence, also when it + /// left the index meanwhile); 409 frames_conflict (the frames changed since expectedFramesVersion, or a + /// photo named is not the sequence's; details: the frames version now), sequence_busy, would_empty (every + /// photo would go: delete the sequence instead), sidecar_mismatch (the sidecar in its folder is damaged or another + /// sequence's), folder_in_use; 413 too_large (a body above ); 503 + /// archive_offline. Publishes sequenceChanged frames; the thumbnails, proxies and analyses follow as jobs. /// private static async Task DeleteFramesAsync(HttpContext context, Caller caller, Guid id, FrameDeleteRequest request, ChronosDatabase db, [FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit, EventHub events, IHostApplicationLifetime lifetime) @@ -127,11 +136,20 @@ public static class LibraryEndpoints { return Refused(context, FrameDeleteBlock.Offline, plan.Sequence); } + catch (ArgumentException) + { + // The sequence left the index since the plan. + return ApiErrors.NotFound(context, "sequence"); + } catch (Exception ex) when (ex is StaleDeletePlanException or LeaseUnavailableException) { - var again = await engine.PlanDeleteAsync(id, ids, plan.Sequence.FramesVersion, ct); - return Refused(context, again.Blocked.Count > 0 ? again.Blocked[0] : ex is LeaseUnavailableException ? FrameDeleteBlock.Busy : FrameDeleteBlock.FramesChanged, - again.Sequence); + return await RefusedAgainAsync(context, engine, id, ids, plan.Sequence.FramesVersion, + ex is LeaseUnavailableException ? FrameDeleteBlock.Busy : FrameDeleteBlock.FramesChanged, ct); + } + catch (InvalidDataException) + { + // The sidecar in the sequence's folder is damaged or another sequence's: nothing moved. + return ApiErrors.Conflict(context, "sidecar_mismatch"); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { @@ -152,6 +170,22 @@ public static class LibraryEndpoints _ => ApiErrors.Conflict(context, "frames_conflict", sequence.FramesVersion.ToString(CultureInfo.InvariantCulture)), }; + /// A deletion refused under the lease, said as the plan is now ( when nothing blocks it); 404 when the sequence is gone. + private static async Task RefusedAgainAsync(HttpContext context, FrameTrash engine, Guid id, IReadOnlyList ids, int framesVersion, + FrameDeleteBlock otherwise, CancellationToken ct) + { + FrameDeletePlan again; + try + { + again = await engine.PlanDeleteAsync(id, ids, framesVersion, ct); + } + catch (ArgumentException) + { + return ApiErrors.NotFound(context, "sequence"); + } + return Refused(context, again.Blocked.Count > 0 ? again.Blocked[0] : otherwise, again.Sequence); + } + /// /// Moves many sequences into the trash as DELETE /sequences/{id} does each (or, with dryRun, plans them): /// one item per id, ok with the trash folder, or skipped with the code that says why (not_found, @@ -390,4 +424,13 @@ public static class LibraryEndpoints var ok = ordered.Count(i => i.Outcome == Ok); return new BulkResultDto(ordered, ok, ordered.Count - ok, dryRun, photos, bytes); } + + /// + /// The photo delete route's body limit (), applied by routing as it selects the + /// endpoint, as for proxies (): the server's 1 MiB stops at about 26,900 ids. + /// + private sealed class FrameDeleteBodyLimitMetadata : IRequestSizeLimitMetadata + { + public long? MaxRequestBodySize => FrameDeleteBodyLimit; + } } diff --git a/src/Chronos.Server/Api/TrashEndpoints.cs b/src/Chronos.Server/Api/TrashEndpoints.cs index eb2f6c48..fc4be3a2 100644 --- a/src/Chronos.Server/Api/TrashEndpoints.cs +++ b/src/Chronos.Server/Api/TrashEndpoints.cs @@ -35,9 +35,11 @@ public static partial class TrashEndpoints public static TrashEntryDto Entry(TrashEntry e) => new(e.Name, e.Kind.ToString().ToLowerInvariant(), e.SequenceId, e.Title, e.Folder, e.Frames, e.Files, e.Bytes, e.DeletedBy, e.DeletedAt); - /// The entries newest first with their total size; an archive offline lists none. + /// The entries newest first with their total size (a read-only archive lists them too); 503 archive_offline. private static async Task ListAsync(HttpContext context, ChronosDatabase db, ServerArchive archive, [FromServices] ArchiveService? archives) { + // An archive that cannot be reached shows no trash folder, which is not an empty trash. + if (Availability(archive) == ArchiveAvailability.Offline) return Offline(context); var entries = await LibraryEndpoints.Engine(archives, db).Trash.ListAsync(archive.Current, context.RequestAborted); return Results.Json(new TrashListDto([.. entries.Select(Entry)], entries.Sum(e => e.Bytes)), ProtocolJson.Default.TrashListDto); } @@ -45,9 +47,10 @@ public static partial class TrashEndpoints /// /// Brings an entry back: a sequence into its folder and the library, photos into their sequence (a new one when it is /// gone), each photo re-hashed against its record first. 404 not_found (trash_entry); 409 - /// sequence_in_library (that sequence is in the library already), sequence_busy, folder_in_use; 422 - /// trash_mismatch (no file matches its record any more); 503 archive_offline. Publishes - /// sequenceChanged: created for a sequence, frames for photos. + /// sequence_in_library (that sequence is in the library already), sequence_busy, sidecar_mismatch + /// (the sidecar of the sequence the photos go back into is damaged or another sequence's), folder_in_use; 422 + /// trash_mismatch (no file matches its record any more, or a deleted sequence's own sidecar no longer names it); + /// 503 archive_offline. Publishes sequenceChanged: created for a sequence, frames for photos. /// private static async Task RestoreAsync(HttpContext context, Caller caller, string name, ChronosDatabase db, ServerArchive archive, [FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit, EventHub events, IHostApplicationLifetime lifetime) @@ -68,15 +71,25 @@ public static partial class TrashEndpoints } catch (ArchiveOfflineException) { - return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline"); + return Offline(context); } catch (LeaseUnavailableException) { return ApiErrors.Conflict(context, "sequence_busy"); } + catch (TrashMismatchException) + { + return Mismatch(context); + } + catch (InvalidDataException) when (!FrameTrash.IsPhotoEntry(name)) + { + // A deleted sequence's own sidecar is damaged or names another sequence: the entry is not what its record says. + return Mismatch(context); + } catch (InvalidDataException) { - return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "trash_mismatch"); + // The sidecar of the sequence the photos go back into is damaged or another sequence's: nothing moved. + return ApiErrors.Conflict(context, "sidecar_mismatch"); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { @@ -94,14 +107,19 @@ public static partial class TrashEndpoints /// Empties the trash, or the entries names names. With dryRun the plan: the entries measured on disk, their /// totals and a fingerprint, nothing touched. Otherwise the request carries that fingerprint, and the server queues an /// empty-trash job (202) that erases exactly those entries; the job fails, erasing nothing, when the trash changed - /// before it ran. 400 bad_request (names, fingerprint); 409 trash_changed (details: the - /// fingerprint now), job_unavailable. + /// before it ran. The audit log records the request (trash_empty_requested: the entries planned, their size and + /// the job); what the job erased is in its log. 400 bad_request (names, fingerprint); 409 + /// trash_changed (details: the fingerprint now), job_unavailable; 503 archive_offline (offline, + /// or read-only for the erase itself). /// private static async Task EmptyAsync(HttpContext context, Caller caller, TrashEmptyRequest request, ChronosDatabase db, ServerArchive archive, [FromServices] ArchiveService? archives, JobEngineHost host, AuditLog audit) { var ct = context.RequestAborted; if (request.Names is { } names && (names.Count == 0 || names.Any(n => !IsEntryName(n)))) return ApiErrors.BadRequest(context, "bad_request", "names"); + // Offline, the trash cannot be measured (it would plan as empty); read-only, it can be measured but not erased. + var availability = Availability(archive); + if (availability == ArchiveAvailability.Offline || (!request.DryRun && availability != ArchiveAvailability.Online)) return Offline(context); var plan = await LibraryEndpoints.Engine(archives, db).PlanEmptyTrashAsync(archive.Id, request.Names, ct); if (request.DryRun) { @@ -112,11 +130,18 @@ public static partial class TrashEndpoints if (plan.Fingerprint != request.Fingerprint || plan.Unknown.Count > 0) return ApiErrors.Conflict(context, "trash_changed", plan.Fingerprint); if (!host.CanRun(JobKind.EmptyTrash)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(JobKind.EmptyTrash)); var job = await host.Engine.EnqueueAsync(ArchiveJobs.EmptyTrash(archive.Current, plan), caller.Id, ct); - await audit.WriteAsync(caller, context, "trash_emptied", "job", job.Id.ToString(), + // A request, not an outcome: the job still checks the plan when it runs and erases nothing if the trash changed. + await audit.WriteAsync(caller, context, "trash_empty_requested", "job", job.Id.ToString(), $"{plan.Entries.Count} · {plan.Bytes} · {string.Join(", ", plan.Entries.Take(5).Select(e => e.Name))}"); return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted); } + private static ArchiveAvailability Availability(ServerArchive archive) => ArchiveVolume.Probe(new ArchivePaths(archive.Root)); + + private static IResult Offline(HttpContext context) => ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline"); + + private static IResult Mismatch(HttpContext context) => ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "trash_mismatch"); + [GeneratedRegex("^(photos-)?[0-9a-f]{32}(-[0-9]{14}(-[0-9]{1,6})?)?$", RegexOptions.CultureInvariant)] private static partial Regex EntryName(); } diff --git a/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs b/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs index 4efc1249..8dea50ef 100644 --- a/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs +++ b/tests/Chronos.Data.Tests/Archiving/FrameTrashTests.cs @@ -266,7 +266,7 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex Assert.FileExists(damaged); // Nothing left that matches: the entry stays as it is. - await Assert.ThrowsAsync(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct)); + await Assert.ThrowsAsync(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct)); Assert.FileExists(damaged); } diff --git a/tests/Chronos.Server.Tests/Api/LibraryTests.cs b/tests/Chronos.Server.Tests/Api/LibraryTests.cs index 4db6e52b..965409e0 100644 --- a/tests/Chronos.Server.Tests/Api/LibraryTests.cs +++ b/tests/Chronos.Server.Tests/Api/LibraryTests.cs @@ -130,12 +130,250 @@ public sealed class LibraryTests(TestContext context) job = await empty.ReadAsync(ProtocolJson.Default.JobDto); } Assert.Equal("empty-trash", job.Kind); + // The audit log records the request: the job checks the plan again when it runs and may erase nothing. + var requests = (await server.Database.Audit.ListAsync(null, "trash_empty_requested", 50, null, context.Cancellation)).Items; + Assert.Count(1, requests); + Assert.Equal(("job", job.Id.ToString()), (requests[0].TargetKind, requests[0].TargetId)); + Assert.True(requests[0].Details?.StartsWith($"2 · {plan.Bytes} · ", StringComparison.Ordinal) == true, requests[0].Details); + Assert.Empty((await server.Database.Audit.ListAsync(null, "trash_emptied", 50, null, context.Cancellation)).Items); await UntilJobAsync(admin, job.Id, "done"); using (var list = await admin.GetAsync("/api/v1/archive/trash")) Assert.Empty((await list.ReadAsync(ProtocolJson.Default.TrashListDto)).Entries); Assert.Empty(Directory.EnumerateFiles(server.Options.TrashPath, "*", SearchOption.AllDirectories)); Assert.Count(1, await server.Database.Frames.ListBySequenceAsync(b)); } + [Test] + public async Task EmptyingNamedEntriesErasesOnlyThose() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var admin = await server.AdminAsync(); + var a = (await Uploads.UploadAsync(editor, Photo(50), "a.jpg", sessionKey: "a")).SequenceId; + Guid b = default; + for (var i = 0; i < 2; i++) b = (await Uploads.UploadAsync(editor, Photo(51 + i), $"b{i}.jpg", sessionKey: "b")).SequenceId; + await UntilIdleAsync(server); + using (var deleted = await editor.DeleteAsync($"/api/v1/sequences/{a}")) await deleted.ExpectAsync(HttpStatusCode.OK); + var photo = await DeletePhotoAsync(editor, b, (await server.Database.Frames.ListBySequenceAsync(b))[0].Id); + await UntilIdleAsync(server); + + TrashEmptyPlanDto plan; + using (var dry = await admin.PostAsJsonAsync("/api/v1/archive/trash/empty", new TrashEmptyRequest([photo], true, null), ProtocolJson.Default.TrashEmptyRequest)) + plan = await dry.ReadAsync(ProtocolJson.Default.TrashEmptyPlanDto); + Assert.SequenceEqual([photo], plan.Entries.Select(e => e.Name)); + using (var empty = await admin.PostAsJsonAsync("/api/v1/archive/trash/empty", new TrashEmptyRequest([photo], false, plan.Fingerprint), + ProtocolJson.Default.TrashEmptyRequest)) + await UntilJobAsync(admin, (await empty.ReadAsync(ProtocolJson.Default.JobDto)).Id, "done"); + + // Only the named entry went: the deleted sequence is still listed, its folder still there. + using (var list = await admin.GetAsync("/api/v1/archive/trash")) + Assert.SequenceEqual([a.ToString("N")], (await list.ReadAsync(ProtocolJson.Default.TrashListDto)).Entries.Select(e => e.Name)); + Assert.True(Directory.Exists(Path.Combine(server.Options.TrashPath, a.ToString("N")))); + Assert.False(Directory.Exists(PhotoEntryFolder(server, photo))); + } + + [Test] + public async Task TheDeletedSequenceComesBackAndASecondCopyIsRefused() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var admin = await server.AdminAsync(); + Guid id = default; + for (var i = 0; i < 2; i++) id = (await Uploads.UploadAsync(editor, Photo(60 + i), $"IMG_{i + 1:0000}.JPG", sessionKey: "s")).SequenceId; + await UntilIdleAsync(server); + var row = (await server.Database.Sequences.GetAsync(id))!; + using (var deleted = await editor.DeleteAsync($"/api/v1/sequences/{id}")) await deleted.ExpectAsync(HttpStatusCode.OK); + var name = id.ToString("N"); + + // The index holds that sequence again (scanned back, or restored elsewhere): this copy cannot come back over it. + await server.Database.Sequences.UpsertAsync(row); + using (var twice = await admin.PostAsync($"/api/v1/archive/trash/{name}/restore", null)) await twice.ExpectErrorAsync(HttpStatusCode.Conflict, "sequence_in_library"); + Assert.True(Directory.Exists(Path.Combine(server.Options.TrashPath, name))); + await server.Database.Sequences.DeleteAsync(id); + + using (var restored = await admin.PostAsync($"/api/v1/archive/trash/{name}/restore", null)) + { + var result = await restored.ReadAsync(ProtocolJson.Default.TrashRestoreDto); + Assert.Equal(("sequence", id, 2, false), (result.Kind, result.SequenceId, result.Photos, result.NewSequence)); + } + Assert.Count(2, await server.Database.Frames.ListBySequenceAsync(id)); + Assert.False(Directory.Exists(Path.Combine(server.Options.TrashPath, name))); + } + + [Test] + public async Task PhotosOfADeletedSequenceComeBackAsANewOne() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var admin = await server.AdminAsync(); + Guid id = default; + for (var i = 0; i < 3; i++) id = (await Uploads.UploadAsync(editor, Photo(70 + i), $"IMG_{i + 1:0000}.JPG", sessionKey: "gone")).SequenceId; + await UntilIdleAsync(server); + var frames = await server.Database.Frames.ListBySequenceAsync(id); + var name = await DeletePhotoAsync(editor, id, frames[1].Id); + await UntilIdleAsync(server); + using (var deleted = await editor.DeleteAsync($"/api/v1/sequences/{id}")) await deleted.ExpectAsync(HttpStatusCode.OK); + + TrashRestoreDto result; + using (var restored = await admin.PostAsync($"/api/v1/archive/trash/{name}/restore", null)) result = await restored.ReadAsync(ProtocolJson.Default.TrashRestoreDto); + Assert.Equal(("photos", 1, true), (result.Kind, result.Photos, result.NewSequence)); + Assert.NotEqual(id, result.SequenceId); + Assert.SequenceEqual([frames[1].Sha256], (await server.Database.Frames.ListBySequenceAsync(result.SequenceId)).Select(f => f.Sha256)); + } + + [Test] + public async Task ADamagedSidecarOrEntryIsAnsweredWithItsOwnCode() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var admin = await server.AdminAsync(); + var photos = Enumerable.Range(0, 3).Select(i => Photo(80 + i)).ToList(); + Guid s = default; + for (var i = 0; i < photos.Count; i++) s = (await Uploads.UploadAsync(editor, photos[i], $"IMG_{i + 1:0000}.JPG", sessionKey: "s")).SequenceId; + var t = (await Uploads.UploadAsync(editor, Photo(90), "other.jpg", sessionKey: "t")).SequenceId; + await UntilIdleAsync(server); + var frames = await server.Database.Frames.ListBySequenceAsync(s); + var name = await DeletePhotoAsync(editor, s, frames[1].Id); + await UntilIdleAsync(server); + var archive = new Chronos.Core.Archiving.ArchivePaths(server.Options.ArchivePath); + var sidecar = archive.Sequence((await server.Database.Sequences.GetAsync(s))!.Folder).ManifestPath; + var other = archive.Sequence((await server.Database.Sequences.GetAsync(t))!.Folder).ManifestPath; + var own = await File.ReadAllBytesAsync(sidecar, context.Cancellation); + + // The sequence's folder holds another sequence's sidecar: said as such, not as a damaged trash entry or a server error. + File.Copy(other, sidecar, overwrite: true); + try + { + using (var restore = await admin.PostAsync($"/api/v1/archive/trash/{name}/restore", null)) await restore.ExpectErrorAsync(HttpStatusCode.Conflict, "sidecar_mismatch"); + using (var delete = await editor.PostAsJsonAsync($"/api/v1/sequences/{s}/frames/delete", new FrameDeleteRequest([frames[0].Id], null, false), + ProtocolJson.Default.FrameDeleteRequest)) + await delete.ExpectErrorAsync(HttpStatusCode.Conflict, "sidecar_mismatch"); + } + finally + { + await File.WriteAllBytesAsync(sidecar, own, context.Cancellation); + } + Assert.Count(2, await server.Database.Frames.ListBySequenceAsync(s)); + Assert.FileExists(Path.Combine(server.Options.ArchivePath, frames[0].RelativePath)); + + // The entry's own file no longer matches its record: that is the trash mismatch, and the entry stays. + var trashed = Path.Combine(PhotoEntryFolder(server, name), "originals", Path.GetFileName(frames[1].RelativePath)); + var bytes = await File.ReadAllBytesAsync(trashed, context.Cancellation); + bytes[^10] ^= 0x01; + await File.WriteAllBytesAsync(trashed, bytes, context.Cancellation); + using (var mismatch = await admin.PostAsync($"/api/v1/archive/trash/{name}/restore", null)) + await mismatch.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "trash_mismatch"); + Assert.FileExists(trashed); + + // A deleted sequence whose own sidecar names another sequence does not match its record either; its folder stays in the trash. + using (var deleted = await editor.DeleteAsync($"/api/v1/sequences/{t}")) await deleted.ExpectAsync(HttpStatusCode.OK); + var entry = t.ToString("N"); + File.Copy(sidecar, archive.Sequence(".chronos/trash/" + entry).ManifestPath, overwrite: true); + using (var foreign = await admin.PostAsync($"/api/v1/archive/trash/{entry}/restore", null)) + await foreign.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "trash_mismatch"); + Assert.True(Directory.Exists(Path.Combine(server.Options.TrashPath, entry))); + Assert.Null(await server.Database.Sequences.GetAsync(t)); + } + + [Test] + public async Task APhotoDeleteTakesAsManyIdsAsItsLimit() + { + // 100,000 ids are about 3.9 MB of JSON, above the server's 1 MiB for other bodies: the route has its own limit. + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var id = (await Uploads.UploadAsync(editor, Photo(100), "IMG_0001.JPG", sessionKey: "many")).SequenceId; + await Uploads.UploadAsync(editor, Photo(101), "IMG_0002.JPG", sessionKey: "many"); + await UntilIdleAsync(server); + var url = $"/api/v1/sequences/{id}/frames/delete"; + var ids = Enumerable.Range(0, Chronos.Server.Api.LibraryEndpoints.MaxFrameIds).Select(_ => Guid.CreateVersion7()).ToList(); + + // Read whole and planned: those ids are not the sequence's photos. + using (var most = await editor.PostAsJsonAsync(url, new FrameDeleteRequest(ids, null, true), ProtocolJson.Default.FrameDeleteRequest)) + await most.ExpectErrorAsync(HttpStatusCode.Conflict, "frames_conflict"); + ids.Add(Guid.CreateVersion7()); + using (var tooMany = await editor.PostAsJsonAsync(url, new FrameDeleteRequest(ids, null, true), ProtocolJson.Default.FrameDeleteRequest)) + Assert.Equal("frameIds", (await tooMany.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request")).Details); + Assert.Count(2, await server.Database.Frames.ListBySequenceAsync(id)); + } + + [Test] + public async Task WithTheArchiveOfflineTheTrashAndDeletesAnswerArchiveOffline() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var admin = await server.AdminAsync(); + Guid id = default; + for (var i = 0; i < 2; i++) id = (await Uploads.UploadAsync(editor, Photo(110 + i), $"IMG_{i + 1:0000}.JPG", sessionKey: "off")).SequenceId; + await UntilIdleAsync(server); + var frames = await server.Database.Frames.ListBySequenceAsync(id); + var name = await DeletePhotoAsync(editor, id, frames[0].Id); + await UntilIdleAsync(server); + + // The archive drive is gone: the trash is not empty, it cannot be read. + var manifest = new Chronos.Core.Archiving.ArchivePaths(server.Options.ArchivePath).ManifestPath; + File.Move(manifest, manifest + ".away"); + try + { + using (var list = await admin.GetAsync("/api/v1/archive/trash")) await list.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, "archive_offline"); + using (var dry = await admin.PostAsJsonAsync("/api/v1/archive/trash/empty", new TrashEmptyRequest(null, true, null), ProtocolJson.Default.TrashEmptyRequest)) + await dry.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, "archive_offline"); + using (var empty = await admin.PostAsJsonAsync("/api/v1/archive/trash/empty", new TrashEmptyRequest(null, false, "0123"), ProtocolJson.Default.TrashEmptyRequest)) + await empty.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, "archive_offline"); + using (var restore = await admin.PostAsync($"/api/v1/archive/trash/{name}/restore", null)) + await restore.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, "archive_offline"); + using (var delete = await editor.PostAsJsonAsync($"/api/v1/sequences/{id}/frames/delete", new FrameDeleteRequest([frames[1].Id], null, true), + ProtocolJson.Default.FrameDeleteRequest)) + await delete.ExpectErrorAsync(HttpStatusCode.ServiceUnavailable, "archive_offline"); + using (var bulk = await editor.PostAsJsonAsync("/api/v1/sequences/bulk/delete", new BulkDeleteRequest([id], false), ProtocolJson.Default.BulkDeleteRequest)) + { + var items = (await bulk.ReadAsync(ProtocolJson.Default.BulkResultDto)).Items; + Assert.Count(1, items); + Assert.Equal(("skipped", "archive_offline"), (items[0].Outcome, items[0].Reason)); + } + } + finally + { + File.Move(manifest + ".away", manifest); + } + Assert.Empty((await server.Database.Jobs.ListAsync(JobState.Queued)).Items.Where(j => j.Kind == JobKind.EmptyTrash)); + using (var back = await admin.GetAsync("/api/v1/archive/trash")) + Assert.SequenceEqual([name], (await back.ReadAsync(ProtocolJson.Default.TrashListDto)).Entries.Select(e => e.Name)); + Assert.NotNull(await server.Database.Sequences.GetAsync(id)); + } + + [Test] + public async Task OnlyTheirOwnersAndAdminsChangeSequencesInBulk() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var (viewer, _, _) = await server.UserAsync("vera", "viewer"); + var admin = await server.AdminAsync(); + Guid a = default; + for (var i = 0; i < 2; i++) a = (await Uploads.UploadAsync(editor, Photo(120 + i), $"IMG_{i + 1:0000}.JPG", sessionKey: "a")).SequenceId; + var b = (await Uploads.UploadAsync(editor, Photo(130), "b.jpg", sessionKey: "b")).SequenceId; + await UntilIdleAsync(server); + + using (var delete = await viewer.PostAsJsonAsync("/api/v1/sequences/bulk/delete", new BulkDeleteRequest([b], true), ProtocolJson.Default.BulkDeleteRequest)) + await delete.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var retag = await viewer.PostAsJsonAsync("/api/v1/sequences/bulk/retag", new BulkRetagRequest([b], ["x"], null), ProtocolJson.Default.BulkRetagRequest)) + await retag.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var jobs = await viewer.PostAsJsonAsync("/api/v1/sequences/bulk/jobs", new BulkJobsRequest([b], "thumbnails"), ProtocolJson.Default.BulkJobsRequest)) + await jobs.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var photos = await viewer.PostAsJsonAsync($"/api/v1/sequences/{a}/frames/delete", + new FrameDeleteRequest([(await server.Database.Frames.ListBySequenceAsync(a))[0].Id], null, true), ProtocolJson.Default.FrameDeleteRequest)) + await photos.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + + // An admin deletes photos and sequences of any user. + var frame = (await server.Database.Frames.ListBySequenceAsync(a))[0].Id; + using (var photo = await admin.PostAsJsonAsync($"/api/v1/sequences/{a}/frames/delete", new FrameDeleteRequest([frame], null, false), + ProtocolJson.Default.FrameDeleteRequest)) + Assert.NotNull((await photo.ReadAsync(ProtocolJson.Default.FrameDeleteResultDto)).TrashEntry); + await UntilIdleAsync(server); + using (var bulk = await admin.PostAsJsonAsync("/api/v1/sequences/bulk/delete", new BulkDeleteRequest([b], false), ProtocolJson.Default.BulkDeleteRequest)) + Assert.SequenceEqual(["ok"], (await bulk.ReadAsync(ProtocolJson.Default.BulkResultDto)).Items.Select(i => i.Outcome)); + Assert.Null(await server.Database.Sequences.GetAsync(b)); + Assert.Count(1, await server.Database.Frames.ListBySequenceAsync(a)); + } + [Test] public async Task BulkOperationsAnswerForEachSequence() { @@ -293,6 +531,18 @@ public sealed class LibraryTests(TestContext context) Assert.Equal(HttpStatusCode.NotFound, unknown.StatusCode); } + /// Deletes one photo of a sequence into the trash; returns the entry's name. + private static async Task DeletePhotoAsync(HttpClient client, Guid sequenceId, Guid frameId) + { + using var response = await client.PostAsJsonAsync($"/api/v1/sequences/{sequenceId}/frames/delete", new FrameDeleteRequest([frameId], null, false), + ProtocolJson.Default.FrameDeleteRequest); + return Assert.NotNull((await response.ReadAsync(ProtocolJson.Default.FrameDeleteResultDto)).TrashEntry); + } + + /// The folder of a photo entry in the archive's trash. + private static string PhotoEntryFolder(TestServer server, string name) => + Path.Combine(server.Options.TrashPath, Chronos.Data.Archiving.FrameTrash.FolderName, name[Chronos.Data.Archiving.FrameTrash.EntryPrefix.Length..]); + /// A thumbnail once its pass has drawn it (404 until then). private static async Task ThumbnailAsync(HttpClient client, string url) {