fix(ingest): file uploads into API-made sequences and repair damaged frames

A sequence created with POST /api/v1/sequences had no folder, and the ingest
looked for its sidecar at the archive root: every upload into it failed with
503 commit_failed, so the Uploader's "New sequence" target never worked. Its
first frame now claims a folder by the rules of a sequence created by an
upload (folder template with its title, unique variant, sidecar with the
row's id, title, tags, rating, notes and project), and the row takes the
folder in one transaction (DOCKER-1).

A frame flagged missing or corrupt, or whose file is gone or has another
size, no longer stands for an upload: the start short-circuit and crash
recovery match only intact frames still on disk, and the ingest repairs such
a frame with the verified copy (same id and position, flags cleared, a damaged
file kept beside it as name.damaged-<time>) instead of calling it a
duplicate. The Uploader deletes a source only after the server received and
verified its bytes in that upload, never on "already on the server" (SRV-1).

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-12 09:40:27 +00:00
1 parent 23308c77dd
commit 94b13cc064
12 files changed
+441 -41

No files matched your search

@@ -8,7 +8,8 @@ namespace Titano.Server.Storage;
/// Upload housekeeping (BRIEF §14.4): at start, uploads a crash left half-committed are settled; then every half hour
/// abandoned uploads expire, orphaned partial and staging files are removed, old upload records and dead tokens purged.
/// </summary>
public sealed class UploadMaintenance(TitanoDatabase db, ServerOptions options, UploadStore store, TimeProvider time, ILogger<UploadMaintenance> logger)
public sealed class UploadMaintenance(TitanoDatabase db, ServerOptions options, ServerArchive archive, UploadStore store, TimeProvider time,
ILogger<UploadMaintenance> logger)
: BackgroundService
{
private static readonly TimeSpan Interval = TimeSpan.FromMinutes(30);
@@ -44,14 +45,16 @@ public sealed class UploadMaintenance(TitanoDatabase db, ServerOptions options,
}
/// <summary>
/// Uploads left committing by a crash: committed when the frame with their hash exists, otherwise back to receiving
/// (the partial file is intact, so the client simply completes again).
/// Uploads left committing by a crash: committed when an intact frame with their hash exists, otherwise back to
/// receiving (the partial file is intact, so the client simply completes again and the ingest files or repairs it).
/// </summary>
public async Task RecoverAsync(CancellationToken cancellationToken)
{
foreach (var upload in await db.Uploads.ListByStateAsync(UploadState.Committing, cancellationToken))
{
var frame = upload.JobId is null ? (await db.Frames.FindBySha256Async(upload.Sha256, cancellationToken)).FirstOrDefault() : null;
var frame = upload.JobId is null
? (await db.Frames.FindIntactBySha256Async(upload.Sha256, cancellationToken)).FirstOrDefault(f => UploadRules.IsOnDisk(archive.Root, f))
: null;
if (frame is not null)
{
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Committed, frame.Id, cancellationToken);
+10
View File
@@ -1,3 +1,5 @@
using Titano.Core.Model;
namespace Titano.Server.Storage;
/// <summary>Validation shared by frame and render uploads.</summary>
@@ -30,6 +32,14 @@ public static class UploadRules
return received;
}
/// <summary>
/// The frame's original is in the archive with its recorded size. Together with flags that say neither missing nor
/// corrupt (<c>FindIntactBySha256Async</c>), that is the copy an upload of the same content may stand for; a damaged
/// one is repaired by uploading the content again.
/// </summary>
public static bool IsOnDisk(string archiveRoot, Frame frame) =>
ArchivePathGuard.ExistingFile(archiveRoot, frame.RelativePath) is { } file && file.Length == frame.Size;
/// <summary>Free bytes on the volume holding <paramref name="path"/>, or null when the platform cannot tell.</summary>
public static long? FreeBytes(string path)
{
+6 -4
View File
@@ -16,9 +16,10 @@ using UploadStateDto = Titano.Protocol.UploadState;
namespace Titano.Server.Storage;
/// <summary>
/// Chunked, resumable, hash-verified frame uploads (BRIEF §14.4): start (short-circuit on a known SHA-256, resume an
/// unfinished upload, quota and disk checks), idempotent chunks, and completion that stages the file on the archive
/// volume, verifies its hash and hands it to <see cref="IArchiveIngest"/>.
/// Chunked, resumable, hash-verified frame uploads (BRIEF §14.4): start (short-circuit on a known SHA-256 held by an
/// intact frame, resume an unfinished upload, quota and disk checks), idempotent chunks, and completion that stages the
/// file on the archive volume, verifies its hash and hands it to <see cref="IArchiveIngest"/>, which repairs a missing or
/// damaged frame of that content.
/// </summary>
public sealed class UploadService(
TitanoDatabase db,
@@ -48,7 +49,8 @@ public sealed class UploadService(
return Fail<StartUploadResponse>(404, "not_found", "sequence");
var sha = request.Sha256.ToLowerInvariant();
var known = await db.Frames.FindBySha256Async(sha, cancellationToken);
// Only a copy that is still whole stands for the upload: a client may delete its file on this answer.
var known = (await db.Frames.FindIntactBySha256Async(sha, cancellationToken)).Where(f => UploadRules.IsOnDisk(archive.Root, f)).ToList();
if (known.Count > 0)
{
var frame = known.FirstOrDefault(f => f.SequenceId == request.SequenceId) ?? known[0];