merge: server archive fixes (V-5, V-8, V-11, V-16, V-35, V-36)

Merge branch fix2/server-archive into the verification branch.

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 16:54:04 +00:00
commit 61c7e9e7e3
19 files changed
+671 -39

No files matched your search

+14 -1
View File
@@ -67,7 +67,9 @@ public static class ArchiveEndpoints
/// Writes the name, templates and proxy policy into the archive's <c>archive.json</c> through the archive engine (so a
/// restart and the proxy jobs see them), then into the index. 503 <c>archive_offline</c> while the archive cannot be
/// written; 409 <c>archive_busy</c> while another process holds its settings; 409 <c>archive_manifest</c> when its
/// archive.json is damaged, written by a newer Titano or of another archive (nothing is written).
/// archive.json is damaged, written by a newer Titano or of another archive; 409 <c>archive_policy</c> when it holds a
/// thumbnail or proxy setting this API cannot set and Titano cannot use (details: <c>thumbnailPolicy</c> or
/// <c>proxyPolicy</c>, the property to correct in the file). Nothing is written.
/// </summary>
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, ArchiveSettingsDto request, TitanoDatabase db, ServerArchive archive,
[FromServices] ArchiveService? archives, AuditLog audit)
@@ -99,6 +101,17 @@ public static class ArchiveEndpoints
{
return ApiErrors.Conflict(context, "archive_manifest");
}
catch (ArgumentException ex)
{
// The request's fields were checked above: what fails is a value read from archive.json (a thumbnail size, or a
// proxy quality, written by hand or by another tool), which the admin corrects in the file, not in this form.
return ApiErrors.Conflict(context, "archive_policy", ex.ParamName switch
{
"thumbnailLongEdge" => "thumbnailPolicy",
"proxyLongEdge" => "proxyPolicy",
var other => other,
});
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// ArchiveOfflineException included: nothing was written.
+18 -3
View File
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Http.Metadata;
using Titano.Core.Model;
using Titano.Protocol;
using Titano.Server.Security;
@@ -14,11 +15,14 @@ public static class UploadEndpoints
{
private const int MaxCardName = 200;
/// <summary>The largest chunk size a server can be set to (<see cref="Configuration.ServerOptions.ChunkSizeBytes"/>).</summary>
private const long MaxChunkBytes = 64L * 1024 * 1024;
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.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync).WithMetadata(new ChunkBodyLimit());
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) =>
@@ -53,12 +57,23 @@ public static class UploadEndpoints
}
/// <summary>
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
/// raises it to the chunk size of this upload (fixed when it started) before it reads the body.
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. Routing raises
/// it for this route (<see cref="ChunkBodyLimit"/>), and the service keeps it at least at the chunk size of this upload
/// (fixed when it started) and of this server, so a body left unread by any answer 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));
}
/// <summary>
/// The chunk route's body limit, applied by routing as it selects the endpoint, before authentication can answer 401
/// or 403 with the chunk unread. Kestrel drains an unread body only within the limit; above it, it closes the connection
/// and the client sees a network error instead of the answer (an Uploader would retry rather than ask to sign in).
/// </summary>
private sealed class ChunkBodyLimit : IRequestSizeLimitMetadata
{
public long? MaxRequestBodySize => MaxChunkBytes;
}
}
+4 -3
View File
@@ -8,8 +8,9 @@ namespace Titano.Server.Storage;
/// <summary>
/// The archive engine behind the server (BRIEF §7, §14.4): ingest of verified uploads, lookup of proxies and
/// thumbnails, its job handlers (scan, rebuild, verify, import, thumbnails, proxies, maintenance) and the periodic
/// incremental scan (<see cref="ArchivePolling"/>). Decoding is managed only (BRIEF §4.2). The daily backup of an
/// SQLite index runs with it (<see cref="DatabaseBackupService"/>).
/// incremental scan (<see cref="ArchivePolling"/>); a scan queues thumbnails and proxies of what it indexes, as an upload
/// does. Decoding is managed only (BRIEF §4.2). The daily backup of an SQLite index runs with it
/// (<see cref="DatabaseBackupService"/>).
/// </summary>
public static class ArchiveModule
{
@@ -19,7 +20,7 @@ public static class ArchiveModule
public static void Register(IServiceCollection services)
{
services.AddSingleton(sp => new ArchiveService(sp.GetRequiredService<TitanoDatabase>(),
new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = Owner }));
new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = Owner, ImagesAfterScan = true }));
services.AddSingleton<IArchiveIngest>(sp => sp.GetRequiredService<ArchiveService>().Ingest);
services.AddSingleton<IJobHandlerProvider>(sp => sp.GetRequiredService<ArchiveService>());
services.AddSingleton<IFrameAssetLocator, ArchiveAssetLocator>();
+5 -2
View File
@@ -101,14 +101,17 @@ public sealed class UploadService(
public async Task<Outcome<ChunkReceived>> ReceiveChunkAsync(Caller caller, Guid id, int index, HttpRequest request, CancellationToken cancellationToken)
{
var upload = await db.Uploads.GetAsync(id, cancellationToken);
// A body left unread by an answer below is drained after it: one of a chunk's size must not count as too large, or
// Kestrel would close the connection while the client still sends it and the client would never see the answer. The
// upload keeps the chunk size it started with: a smaller setting since a restart must not refuse its chunks (413).
if (request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false, MaxRequestBodySize: { } current } limit)
limit.MaxRequestBodySize = Math.Max(current, Math.Max(upload?.ChunkSize ?? 0, options.ChunkSizeBytes));
if (upload is null || upload.UserId != caller.Id) return Fail<ChunkReceived>(404, "not_found", "upload");
if (upload.State != UploadState.Receiving) return Fail<ChunkReceived>(409, "upload_state");
if (index < 0 || index >= upload.ChunkCount) return Fail<ChunkReceived>(400, "chunk_out_of_range");
var expected = Math.Min(upload.ChunkSize, upload.Size - (long)index * upload.ChunkSize);
if (request.ContentLength is { } announced && announced != expected) return Fail<ChunkReceived>(400, "chunk_size", expected.ToString(CultureInfo.InvariantCulture));
if (upload.HasChunk(index)) return new ChunkReceived(index, UploadRules.ReceivedChunks(upload));
// The upload keeps the chunk size it started with: a smaller setting since a restart must not refuse its chunks (413).
if (request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = upload.ChunkSize;
if (_chunksInFlight.AddOrUpdate(caller.Id, 1, (_, n) => n + 1) > options.ConcurrentChunksPerUser)
{