Files
Chronos/src/Titano.Server/Api/UploadEndpoints.cs
T
Claude 98f301f5fd fix(server): answer every chunk request and out-of-range archive.json policies
The chunk endpoint raised the request body limit only before reading a chunk. A
chunk sent again after a lost answer, or to an unknown or finished upload, was left
unread above Kestrel's 1 MiB default, so Kestrel closed the connection instead of
delivering the answer. The limit is now raised before any answer, to the larger of
the upload's and the server's chunk size, and never lowered.

PUT /api/v1/archive/settings answered 500 when archive.json held a thumbnail size or
proxy quality out of range, which the admin API cannot set. It now answers 409
archive_policy with the policy to correct in details (Server.Error.ArchivePolicy).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 15:58:48 +00:00

66 lines
4.1 KiB
C#

using Titano.Core.Model;
using Titano.Protocol;
using Titano.Server.Security;
using Titano.Server.Storage;
namespace Titano.Server.Api;
/// <summary>
/// <c>/api/v1/uploads</c> (BRIEF §14.4). Chunk, state and cancel calls also serve render nodes uploading their output,
/// which start and complete through the node endpoints. <c>POST /uploads/cards</c> takes the reports of a card import a
/// client streams through these uploads and tells every connected client (BRIEF §14.3 <c>cardEvent</c>).
/// </summary>
public static class UploadEndpoints
{
private const int MaxCardName = 200;
public static void Map(RouteGroupBuilder api)
{
var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode);
uploads.MapPost("", StartAsync).Requires(Permissions.Upload);
uploads.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync);
uploads.MapGet("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.GetStateAsync(caller, id, context.RequestAborted)).ToResult(context, s => Results.Json(s, ProtocolJson.Default.UploadState)));
uploads.MapDelete("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.CancelAsync(caller, id, context.RequestAborted)).ToResult(context, _ => Results.NoContent()));
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
(await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
.Requires(Permissions.Upload);
uploads.MapPost("/cards", ReportCard).Requires(Permissions.Upload);
}
private static async Task<IResult> StartAsync(HttpContext context, Caller caller, StartUploadRequest request, UploadService service)
{
var outcome = await service.StartAsync(caller, request, context.RequestAborted);
return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse,
statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created));
}
/// <summary>
/// A card import's start, progress or end, published as a <see cref="CardEvent"/> naming who imports it; 204, also for
/// progress dropped because it came within a second of the last (<see cref="UploadService.PublishCard"/>).
/// </summary>
private static IResult ReportCard(HttpContext context, Caller caller, CardImportReport report, UploadService service)
{
if (report.ImportId == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "importId");
var card = report.Card?.Trim();
if (string.IsNullOrEmpty(card) || card.Length > MaxCardName || card.Any(char.IsControl)) return ApiErrors.BadRequest(context, "bad_request", "card");
if (!CardImportStates.IsKnown(report.State)) return ApiErrors.BadRequest(context, "bad_request", "state");
if (report.Total < 0 || report.Done < 0 || report.Done > report.Total || report.Failed < 0 || report.Failed > report.Total)
return ApiErrors.BadRequest(context, "bad_request", "done");
return service.PublishCard(caller, new CardEvent(report.ImportId, card, report.State, report.Done, report.Total, report.Failed, caller.User.Username,
caller.ClientName)).ToResult(context, _ => Results.NoContent());
}
/// <summary>
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
/// raises it before any answer to at least the chunk size of this upload (fixed when it started) and of this server,
/// so a body it leaves unread can still be drained.
/// </summary>
private static async Task<IResult> ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service)
{
var outcome = await service.ReceiveChunkAsync(caller, id, index, context.Request, context.RequestAborted);
return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.ChunkReceived));
}
}