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 <noreply@anthropic.com> (cherry picked from commit fa35831d55c44746f7f76e0cff262ed6aecf8e72)
This commit is contained in:
1 parent
316c182b19
commit
66c3662954
9 files changed
+350
-24
No files matched your search
+2
-2
@@ -25,7 +25,7 @@
|
||||
<data name="Admin.Activity.SequenceDeleted" xml:space="preserve"><value>{0} deleted {1}</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.SequenceUpdated" xml:space="preserve"><value>{0} edited a sequence · {1}</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.ServerSettings" xml:space="preserve"><value>{0} changed the server settings</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.TrashEmptied" xml:space="preserve"><value>{0} emptied the trash · {1}</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.TrashEmptyRequested" xml:space="preserve"><value>{0} asked to empty the trash · {1}</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.TrashRestored" xml:space="preserve"><value>{0} restored {1} from the trash</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.UserCreated" xml:space="preserve"><value>{0} created user {1}</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.UserDeleted" xml:space="preserve"><value>{0} deleted user {1}</value><comment>long</comment></data>
|
||||
@@ -360,7 +360,7 @@
|
||||
<data name="Admin.Logs.Action.SequenceUpdated" xml:space="preserve"><value>Sequence edited</value></data>
|
||||
<data name="Admin.Logs.Action.ServerDiagnostics" xml:space="preserve"><value>Diagnostics exported</value></data>
|
||||
<data name="Admin.Logs.Action.ServerSettings" xml:space="preserve"><value>Server settings</value></data>
|
||||
<data name="Admin.Logs.Action.TrashEmptied" xml:space="preserve"><value>Trash emptied</value></data>
|
||||
<data name="Admin.Logs.Action.TrashEmptyRequested" xml:space="preserve"><value>Emptying requested</value></data>
|
||||
<data name="Admin.Logs.Action.TrashRestored" xml:space="preserve"><value>Restored from trash</value></data>
|
||||
<data name="Admin.Logs.Action.UserCreated" xml:space="preserve"><value>User created</value></data>
|
||||
<data name="Admin.Logs.Action.UserDeleted" xml:space="preserve"><value>User deleted</value></data>
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<data name="Admin.Activity.SequenceDeleted" xml:space="preserve"><value>{0} ha eliminato {1}</value></data>
|
||||
<data name="Admin.Activity.SequenceUpdated" xml:space="preserve"><value>{0} ha modificato una sequenza · {1}</value></data>
|
||||
<data name="Admin.Activity.ServerSettings" xml:space="preserve"><value>{0} ha modificato le impostazioni del server</value></data>
|
||||
<data name="Admin.Activity.TrashEmptied" xml:space="preserve"><value>{0} ha svuotato il cestino · {1}</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.TrashEmptyRequested" xml:space="preserve"><value>{0} ha chiesto di svuotare il cestino · {1}</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.TrashRestored" xml:space="preserve"><value>{0} ha ripristinato {1} dal cestino</value><comment>long</comment></data>
|
||||
<data name="Admin.Activity.UserCreated" xml:space="preserve"><value>{0} ha creato l'utente {1}</value></data>
|
||||
<data name="Admin.Activity.UserDeleted" xml:space="preserve"><value>{0} ha eliminato l'utente {1}</value></data>
|
||||
@@ -360,7 +360,7 @@
|
||||
<data name="Admin.Logs.Action.SequenceUpdated" xml:space="preserve"><value>Sequenza modificata</value></data>
|
||||
<data name="Admin.Logs.Action.ServerDiagnostics" xml:space="preserve"><value>Diagnostica esportata</value></data>
|
||||
<data name="Admin.Logs.Action.ServerSettings" xml:space="preserve"><value>Impostazioni server</value></data>
|
||||
<data name="Admin.Logs.Action.TrashEmptied" xml:space="preserve"><value>Cestino svuotato</value></data>
|
||||
<data name="Admin.Logs.Action.TrashEmptyRequested" xml:space="preserve"><value>Svuotamento richiesto</value></data>
|
||||
<data name="Admin.Logs.Action.TrashRestored" xml:space="preserve"><value>Ripristino dal cestino</value></data>
|
||||
<data name="Admin.Logs.Action.UserCreated" xml:space="preserve"><value>Utente creato</value></data>
|
||||
<data name="Admin.Logs.Action.UserDeleted" xml:space="preserve"><value>Utente eliminato</value></data>
|
||||
|
||||
@@ -32,6 +32,8 @@ public sealed partial class FrameTrash
|
||||
/// </summary>
|
||||
/// <param name="name">The entry's <see cref="TrashEntry.Name"/> (<c>photos-…</c>).</param>
|
||||
/// <exception cref="DirectoryNotFoundException">The entry is not in the trash.</exception>
|
||||
/// <exception cref="TrashMismatchException">No file of the entry matches its record; nothing moved.</exception>
|
||||
/// <exception cref="InvalidDataException">The sequence's sidecar is damaged or belongs to another sequence; nothing moved.</exception>
|
||||
public async Task<PhotoRestoreResult> 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<TrashedPhoto> stays;
|
||||
|
||||
@@ -82,6 +82,12 @@ public sealed class SequenceInLibraryException(Guid sequenceId, string title) :
|
||||
public Guid SequenceId { get; } = sequenceId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="InvalidDataException"/> is about a sidecar instead (damaged, or another sequence's).
|
||||
/// </summary>
|
||||
public sealed class TrashMismatchException() : IOException(Loc.L("Archive.Error.TrashMismatch"));
|
||||
|
||||
/// <summary>
|
||||
/// Deleting sequences without erasing anything (BRIEF §0.2 rule 8, §12.2, §12.5): <see cref="PlanAsync"/> is the dry
|
||||
/// run; <see cref="DeleteAsync"/> moves each sequence folder whole — originals, proxies, exports and its sidecars — into
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
/// <summary>Photos one delete request may name.</summary>
|
||||
public const int MaxFrameIds = 100_000;
|
||||
|
||||
/// <summary>
|
||||
/// The body a delete request of <see cref="MaxFrameIds"/> 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.
|
||||
/// </summary>
|
||||
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
|
||||
/// <summary>
|
||||
/// Moves photos of a sequence into the archive's trash (<see cref="FrameTrash"/>; BRIEF §0.2 rule 8: nothing is erased),
|
||||
/// or with <c>dryRun</c> says what would go. Owners and admins only, like deleting the sequence. 400 <c>bad_request</c>
|
||||
/// (<c>frameIds</c>); 409 <c>frames_conflict</c> (the frames changed since <c>expectedFramesVersion</c>, or a photo named
|
||||
/// is not the sequence's; details: the frames version now), <c>sequence_busy</c>, <c>would_empty</c> (every photo would
|
||||
/// go: delete the sequence instead), <c>folder_in_use</c>; 503 <c>archive_offline</c>. Publishes <c>sequenceChanged</c>
|
||||
/// <c>frames</c>; the thumbnails, proxies and analyses follow as jobs.
|
||||
/// (<c>frameIds</c>: none, or more than <see cref="MaxFrameIds"/>); 404 <c>not_found</c> (<c>sequence</c>, also when it
|
||||
/// left the index meanwhile); 409 <c>frames_conflict</c> (the frames changed since <c>expectedFramesVersion</c>, or a
|
||||
/// photo named is not the sequence's; details: the frames version now), <c>sequence_busy</c>, <c>would_empty</c> (every
|
||||
/// photo would go: delete the sequence instead), <c>sidecar_mismatch</c> (the sidecar in its folder is damaged or another
|
||||
/// sequence's), <c>folder_in_use</c>; 413 <c>too_large</c> (a body above <see cref="FrameDeleteBodyLimit"/>); 503
|
||||
/// <c>archive_offline</c>. Publishes <c>sequenceChanged</c> <c>frames</c>; the thumbnails, proxies and analyses follow as jobs.
|
||||
/// </summary>
|
||||
private static async Task<IResult> 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)),
|
||||
};
|
||||
|
||||
/// <summary>A deletion refused under the lease, said as the plan is now (<paramref name="otherwise"/> when nothing blocks it); 404 when the sequence is gone.</summary>
|
||||
private static async Task<IResult> RefusedAgainAsync(HttpContext context, FrameTrash engine, Guid id, IReadOnlyList<Guid> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves many sequences into the trash as <c>DELETE /sequences/{id}</c> does each (or, with <c>dryRun</c>, plans them):
|
||||
/// one item per id, <c>ok</c> with the trash folder, or <c>skipped</c> with the code that says why (<c>not_found</c>,
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The photo delete route's body limit (<see cref="FrameDeleteBodyLimit"/>), applied by routing as it selects the
|
||||
/// endpoint, as for proxies (<see cref="FileEndpoints"/>): the server's 1 MiB stops at about 26,900 ids.
|
||||
/// </summary>
|
||||
private sealed class FrameDeleteBodyLimitMetadata : IRequestSizeLimitMetadata
|
||||
{
|
||||
public long? MaxRequestBodySize => FrameDeleteBodyLimit;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
/// <summary>The entries newest first with their total size; an archive offline lists none.</summary>
|
||||
/// <summary>The entries newest first with their total size (a read-only archive lists them too); 503 <c>archive_offline</c>.</summary>
|
||||
private static async Task<IResult> 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
|
||||
/// <summary>
|
||||
/// 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 <c>not_found</c> (<c>trash_entry</c>); 409
|
||||
/// <c>sequence_in_library</c> (that sequence is in the library already), <c>sequence_busy</c>, <c>folder_in_use</c>; 422
|
||||
/// <c>trash_mismatch</c> (no file matches its record any more); 503 <c>archive_offline</c>. Publishes
|
||||
/// <c>sequenceChanged</c>: <c>created</c> for a sequence, <c>frames</c> for photos.
|
||||
/// <c>sequence_in_library</c> (that sequence is in the library already), <c>sequence_busy</c>, <c>sidecar_mismatch</c>
|
||||
/// (the sidecar of the sequence the photos go back into is damaged or another sequence's), <c>folder_in_use</c>; 422
|
||||
/// <c>trash_mismatch</c> (no file matches its record any more, or a deleted sequence's own sidecar no longer names it);
|
||||
/// 503 <c>archive_offline</c>. Publishes <c>sequenceChanged</c>: <c>created</c> for a sequence, <c>frames</c> for photos.
|
||||
/// </summary>
|
||||
private static async Task<IResult> 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 <c>names</c> names. With <c>dryRun</c> 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
|
||||
/// <c>empty-trash</c> job (202) that erases exactly those entries; the job fails, erasing nothing, when the trash changed
|
||||
/// before it ran. 400 <c>bad_request</c> (<c>names</c>, <c>fingerprint</c>); 409 <c>trash_changed</c> (details: the
|
||||
/// fingerprint now), <c>job_unavailable</c>.
|
||||
/// before it ran. The audit log records the request (<c>trash_empty_requested</c>: the entries planned, their size and
|
||||
/// the job); what the job erased is in its log. 400 <c>bad_request</c> (<c>names</c>, <c>fingerprint</c>); 409
|
||||
/// <c>trash_changed</c> (details: the fingerprint now), <c>job_unavailable</c>; 503 <c>archive_offline</c> (offline,
|
||||
/// or read-only for the erase itself).
|
||||
/// </summary>
|
||||
private static async Task<IResult> 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();
|
||||
}
|
||||
@@ -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<InvalidDataException>(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct));
|
||||
await Assert.ThrowsAsync<TrashMismatchException>(() => fx.Service.RestoreEntryAsync(fx.Archive.Id, deleted.Entry.Name, null, fx.Ct));
|
||||
Assert.FileExists(damaged);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>Deletes one photo of a sequence into the trash; returns the entry's name.</summary>
|
||||
private static async Task<string> 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);
|
||||
}
|
||||
|
||||
/// <summary>The folder of a photo entry in the archive's trash.</summary>
|
||||
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..]);
|
||||
|
||||
/// <summary>A thumbnail once its pass has drawn it (404 until then).</summary>
|
||||
private static async Task<byte[]> ThumbnailAsync(HttpClient client, string url)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user