diff --git a/i18n/en/Server.resx b/i18n/en/Server.resx index 708db05e..c0af70c1 100644 --- a/i18n/en/Server.resx +++ b/i18n/en/Server.resx @@ -26,14 +26,19 @@ Disconnect Not connected to a server Too many failed sign-ins; the account is locked for a while.long + The archive is offline or read-only; try again when it is back.long + Its photos are the archive's root folder, which never moves to the trash.long Wrong user name or password.long The request is not valid.long The server certificate changed; confirm it and sign in again.long This upload has no such chunk.long The chunk has the wrong size.long The file could not be stored in the archive; try again.long + Its folder holds other sequences; move them elsewhere first.long The security token is missing; reload the page.long The connection failed; the log has the details.long + A file of the sequence is open in another program; close it and try again.long + Its folder is no longer on disk; nothing is left to move to the trash.long Your account cannot do this.long The file arrived damaged; upload it again.long Open the admin panel over HTTPS to sign in.long @@ -66,10 +71,11 @@ Too many attempts; wait a minute and try again.long Choose a password different from the current one.long You cannot do this to your own account.long - A job is working on this sequence, or it cannot move to the trash; try again when it is free.long + A job is working on this sequence or its archive; try again when it is free.long The session expired; sign in again.long An environment variable sets this; change it there.long The sequence changed on the server; reload it and apply your edit again.long + Its folder holds another sequence; scan the archive first.long The request is larger than the server accepts.long Too many uploads at once; retry shortly.long Sign in to continue.long diff --git a/i18n/it/Server.it.resx b/i18n/it/Server.it.resx index aaf6c9ae..b001a673 100644 --- a/i18n/it/Server.it.resx +++ b/i18n/it/Server.it.resx @@ -24,14 +24,19 @@ Disconnetti Non connesso a un server Troppi accessi falliti; l'account è bloccato per un po'. + L'archivio non è raggiungibile o è di sola lettura; riprova quando torna disponibile. + Le sue foto sono la cartella principale dell'archivio, che non va mai nel cestino. Nome utente o password errati. La richiesta non è valida. Il certificato del server è cambiato; confermalo e accedi di nuovo. Questo caricamento non ha quel blocco. Il blocco ha la dimensione sbagliata. Il file non è stato salvato nell'archivio; riprova. + La sua cartella contiene altre sequenze; prima spostale altrove. Manca il token di sicurezza; ricarica la pagina. La connessione non è riuscita; i dettagli sono nel log. + Un file della sequenza è aperto in un altro programma; chiudilo e riprova. + La sua cartella non è più sul disco; non resta nulla da spostare nel cestino. Il tuo account non può farlo. Il file è arrivato danneggiato; caricalo di nuovo. Apri il pannello di amministrazione in HTTPS per accedere. @@ -64,10 +69,11 @@ Troppi tentativi; attendi un minuto e riprova. Scegli una password diversa da quella attuale. Non puoi farlo sul tuo account. - Un'attività sta lavorando su questa sequenza, oppure non può andare nel cestino; riprova quando è libera. + Un'attività sta lavorando su questa sequenza o sul suo archivio; riprova quando è libera. La sessione è scaduta; accedi di nuovo. È impostato da una variabile d'ambiente; modificala lì. La sequenza è cambiata sul server; ricaricala e riapplica la modifica. + La sua cartella contiene un'altra sequenza; prima esegui una scansione dell'archivio. La richiesta supera la dimensione accettata dal server. Troppi caricamenti insieme; riprova tra poco. Accedi per continuare. diff --git a/src/Titano.Server/Api/SequenceEndpoints.cs b/src/Titano.Server/Api/SequenceEndpoints.cs index 57fc2de5..28bf8982 100644 --- a/src/Titano.Server/Api/SequenceEndpoints.cs +++ b/src/Titano.Server/Api/SequenceEndpoints.cs @@ -145,7 +145,9 @@ public static class SequenceEndpoints /// /// Moves a sequence's folder into the archive's trash and removes it from the index through the archive engine's /// (BRIEF §0.2 rule 8: nothing is erased, the trash keeps it restorable). - /// ?dryRun=true only reports what would happen. Owners and admins only; a sequence a job is working on is refused (409). + /// ?dryRun=true only reports what would happen. Owners and admins only. A sequence that cannot move is refused + /// with the reason as its code: 409 sequence_busy (a job works on it or its archive), archive_root, + /// contains_sequences, sidecar_mismatch, folder_missing; 503 archive_offline. /// private static async Task DeleteAsync(HttpContext context, Caller caller, Guid id, bool? dryRun, TitanoDatabase db, [FromServices] ArchiveService? archives, AuditLog audit, EventHub events) @@ -157,13 +159,27 @@ public static class SequenceEndpoints DeletePlan plan; try { - plan = await trash.PlanAsync([id], ct); - if (plan.Blocked is [{ } blocked]) return ApiErrors.Conflict(context, "sequence_busy", blocked.Reason.ToString()); + plan = await trash.PlanAsync([id], null, ct); + if (plan.Blocked is [{ } blocked]) return Refused(context, blocked.Reason); if (dryRun != true) await trash.DeleteAsync(plan, caller.User.Username, null, ct); } - catch (StaleDeletePlanException) + catch (ArgumentException) { - return ApiErrors.Conflict(context, "sequence_busy"); + // Deleted by someone else between the lookup and the plan. + return ApiErrors.NotFound(context, "sequence"); + } + catch (Titano.Core.Archiving.ArchiveOfflineException) + { + return Refused(context, DeleteBlock.Offline); + } + catch (Exception ex) when (ex is StaleDeletePlanException or Titano.Core.Archiving.LeaseUnavailableException) + { + return Refused(context, DeleteBlock.Busy); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + // A file of the folder is open elsewhere: nothing moved. + return ApiErrors.Conflict(context, "folder_in_use"); } var step = plan.Steps[0]; var result = new SequenceDeletePlan(id, sequence.Folder, step.TrashFolder, sequence.FrameCount, sequence.TotalBytes, sequence.RenderedCount, dryRun == true); @@ -174,6 +190,16 @@ public static class SequenceEndpoints return Results.Json(result, ProtocolJson.Default.SequenceDeletePlan); } + private static IResult Refused(HttpContext context, DeleteBlock reason) => reason switch + { + DeleteBlock.Offline => ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline"), + DeleteBlock.Busy => ApiErrors.Conflict(context, "sequence_busy"), + DeleteBlock.ArchiveRoot => ApiErrors.Conflict(context, "archive_root"), + DeleteBlock.ContainsSequences => ApiErrors.Conflict(context, "contains_sequences"), + DeleteBlock.ForeignSidecar => ApiErrors.Conflict(context, "sidecar_mismatch"), + _ => ApiErrors.Conflict(context, "folder_missing"), + }; + /// Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5); ETag over the version counters. private static async Task ManifestAsync(HttpContext context, Guid id, TitanoDatabase db) { diff --git a/tests/Titano.Server.Tests/Api/SequenceTests.cs b/tests/Titano.Server.Tests/Api/SequenceTests.cs index 3fbeb2b2..b160c278 100644 --- a/tests/Titano.Server.Tests/Api/SequenceTests.cs +++ b/tests/Titano.Server.Tests/Api/SequenceTests.cs @@ -173,6 +173,40 @@ public sealed class SequenceTests(TestContext context) Assert.NotNull(await server.Database.Sequences.GetAsync(sequence.Id)); } + [Test] + public async Task ADeleteThatCannotMoveSaysWhy() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var a = (await server.Database.Sequences.GetAsync((await Uploads.UploadAsync(editor, Uploads.Photo(3000, 36), "a.jpg", sessionKey: "a")).SequenceId))!; + var b = (await server.Database.Sequences.GetAsync((await Uploads.UploadAsync(editor, Uploads.Photo(3000, 37), "b.jpg", sessionKey: "b")).SequenceId))!; + Assert.NotEqual(a.Folder, b.Folder); + var archive = new Titano.Core.Archiving.ArchivePaths(server.Options.ArchivePath); + + // The folder of b now holds a sidecar naming another sequence (copied over by hand): the index is out of date. + var sidecar = archive.Sequence(b.Folder).ManifestPath; + Directory.CreateDirectory(Path.GetDirectoryName(sidecar)!); + File.WriteAllText(sidecar, $$"""{"schemaVersion":1,"id":"{{a.Id}}"}"""); + await RefusedAsync(editor, b.Id, "sidecar_mismatch"); + Directory.Delete(archive.Sequence(a.Folder).Folder, recursive: true); + await RefusedAsync(editor, a.Id, "folder_missing"); + Assert.NotNull(await server.Database.Sequences.GetAsync(a.Id)); + Assert.NotNull(await server.Database.Sequences.GetAsync(b.Id)); + + // A sequence still waiting for its first photo has no folder: only its row goes. + using var created = await editor.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest("Waiting", null), ProtocolJson.Default.CreateSequenceRequest); + var empty = await created.ReadAsync(ProtocolJson.Default.SequenceDto); + using (var deleted = await editor.DeleteAsync($"/api/v1/sequences/{empty.Id}")) Assert.Null((await deleted.ReadAsync(ProtocolJson.Default.SequenceDeletePlan)).TrashFolder); + Assert.Null(await server.Database.Sequences.GetAsync(empty.Id)); + + static async Task RefusedAsync(HttpClient client, Guid id, string code) + { + using var response = await client.DeleteAsync($"/api/v1/sequences/{id}"); + var error = await response.ExpectErrorAsync(HttpStatusCode.Conflict, code); + Assert.NotEqual(code, error.Message, "the code has a sentence"); + } + } + [Test] public async Task FramesAndStatsArePaged() {