feat(data): delete sequences into the archive's trash and restore them

SequenceTrash plans (files, bytes, trash folder, blocked reasons) and moves
each sequence folder whole into .titano/trash/<id> under its leases, next to a
record of its index row and renders, then drops the index rows; restore moves
it back (to a free folder when the old one is taken) and indexes it again from
its sidecar with the same ids. The server's DELETE /sequences/{id} now uses it
and refuses a sequence a job is working on (409 sequence_busy).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-03 11:27:49 +00:00
1 parent 90321a7e53
commit 453fb63687
10 files changed
+700 -20

No files matched your search

+24 -17
View File
@@ -3,7 +3,9 @@ using System.Globalization;
using System.Text;
using System.Text.Json;
using Titano.Core.Model;
using Microsoft.AspNetCore.Mvc;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
using Titano.Protocol;
using Titano.Server.Configuration;
@@ -141,30 +143,35 @@ public static class SequenceEndpoints
}
/// <summary>
/// Removes a sequence from the index and moves its folder into the archive's trash (BRIEF §0.2 rule 8: nothing is
/// erased). <c>?dryRun=true</c> only reports what would happen. Owners and admins only.
/// Moves a sequence's folder into the archive's trash and removes it from the index through the archive engine's
/// <see cref="SequenceTrash"/> (BRIEF §0.2 rule 8: nothing is erased, the trash keeps it restorable).
/// <c>?dryRun=true</c> only reports what would happen. Owners and admins only; a sequence a job is working on is refused (409).
/// </summary>
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid id, bool? dryRun, TitanoDatabase db, ServerArchive archive,
ServerOptions options, AuditLog audit, EventHub events)
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid id, bool? dryRun, TitanoDatabase db,
[FromServices] ArchiveService? archives, AuditLog audit, EventHub events)
{
if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
var ct = context.RequestAborted;
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
if (!caller.IsAdmin && sequence.OwnerUserId != caller.Id) return ApiErrors.Forbidden(context);
var folder = string.IsNullOrEmpty(sequence.Folder) ? null : ArchivePathGuard.Resolve(archive.Root, sequence.Folder);
var trash = Path.Combine(options.TrashPath, id.ToString("N"));
var hasFolder = folder is not null && Directory.Exists(folder);
var plan = new SequenceDeletePlan(id, sequence.Folder, hasFolder ? Path.GetRelativePath(archive.Root, trash).Replace('\\', '/') : null,
sequence.FrameCount, sequence.TotalBytes, sequence.RenderedCount, dryRun == true);
if (dryRun == true) return Results.Json(plan, ProtocolJson.Default.SequenceDeletePlan);
if (hasFolder)
var trash = archives?.Trash ?? new SequenceTrash(db, new ArchiveServiceOptions { Owner = ArchiveModule.Owner });
DeletePlan plan;
try
{
Directory.CreateDirectory(options.TrashPath);
Directory.Move(folder!, trash);
plan = await trash.PlanAsync([id], ct);
if (plan.Blocked is [{ } blocked]) return ApiErrors.Conflict(context, "sequence_busy", blocked.Reason.ToString());
if (dryRun != true) await trash.DeleteAsync(plan, caller.User.Username, null, ct);
}
await db.Sequences.DeleteAsync(id, context.RequestAborted);
catch (StaleDeletePlanException)
{
return ApiErrors.Conflict(context, "sequence_busy");
}
var step = plan.Steps[0];
var result = new SequenceDeletePlan(id, sequence.Folder, step.TrashFolder, sequence.FrameCount, sequence.TotalBytes, sequence.RenderedCount, dryRun == true);
if (dryRun == true) return Results.Json(result, ProtocolJson.Default.SequenceDeletePlan);
await audit.WriteAsync(caller, context, "sequence_deleted", "sequence", id.ToString(), sequence.DisplayTitle);
events.Publish(new SequenceChangedEvent(id, "deleted", sequence.FramesVersion, sequence.SettingsVersion));
return Results.Json(plan, ProtocolJson.Default.SequenceDeletePlan);
return Results.Json(result, ProtocolJson.Default.SequenceDeletePlan);
}
/// <summary>Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5); ETag over the version counters.</summary>