diff --git a/src/Titano.Server/Api/SequenceEndpoints.cs b/src/Titano.Server/Api/SequenceEndpoints.cs index c84b82ad..819e90be 100644 --- a/src/Titano.Server/Api/SequenceEndpoints.cs +++ b/src/Titano.Server/Api/SequenceEndpoints.cs @@ -42,15 +42,16 @@ public static class SequenceEndpoints sequences.MapGet("/{id:guid}/analysis/report", ReportAsync).Requires(Permissions.Read); sequences.MapGet("/{id:guid}/stats", StatsAsync).Requires(Permissions.Read); sequences.MapGet("/{id:guid}/renders", RendersAsync).Requires(Permissions.Read); - // Renders made on a client, sent on request (BRIEF §14.5); chunks go through /uploads. + // Renders made on a client, sent on request (BRIEF §14.5) by the sync of an editor's copy; chunks go through /uploads. + // Edit, not Upload: render outputs count against no quota, so an upload-only account cannot add them. sequences.MapPost("/{id:guid}/renders", async (HttpContext c, Caller caller, Guid id, RenderUploadRequest request, RenderUploads uploads) => (await uploads.StartAsync(caller, id, request, c.RequestAborted)).ToResult(c, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse, - statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created))).Requires(Permissions.Upload | Permissions.Edit); + statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created))).Requires(Permissions.Edit); sequences.MapPost("/{id:guid}/renders/{uploadId:guid}/complete", async (HttpContext c, Caller caller, Guid id, Guid uploadId, RenderUploads uploads, IHostApplicationLifetime lifetime) => // The commit runs to the end even if the client disconnects meanwhile. (await uploads.CompleteAsync(caller, id, uploadId, lifetime.ApplicationStopping)).ToResult(c, r => Results.Json(r, ProtocolJson.Default.RenderDto))) - .Requires(Permissions.Upload | Permissions.Edit); + .Requires(Permissions.Edit); } private static async Task ListAsync(HttpContext context, TitanoDatabase db, ServerArchive archive, int? year, string? camera, string? status, diff --git a/src/Titano.Server/Api/UploadEndpoints.cs b/src/Titano.Server/Api/UploadEndpoints.cs index 92ccfa53..2bf2e615 100644 --- a/src/Titano.Server/Api/UploadEndpoints.cs +++ b/src/Titano.Server/Api/UploadEndpoints.cs @@ -1,7 +1,5 @@ -using Microsoft.AspNetCore.Http.Features; using Titano.Core.Model; using Titano.Protocol; -using Titano.Server.Configuration; using Titano.Server.Security; using Titano.Server.Storage; @@ -34,10 +32,12 @@ public static class UploadEndpoints statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created)); } - private static async Task ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service, ServerOptions options) + /// + /// 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. + /// + private static async Task ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service) { - // Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. - if (context.Features.Get() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = options.ChunkSizeBytes; var outcome = await service.ReceiveChunkAsync(caller, id, index, context.Request, context.RequestAborted); return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.ChunkReceived)); } diff --git a/src/Titano.Server/Storage/UploadService.cs b/src/Titano.Server/Storage/UploadService.cs index 327bd02d..0556474f 100644 --- a/src/Titano.Server/Storage/UploadService.cs +++ b/src/Titano.Server/Storage/UploadService.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Globalization; +using Microsoft.AspNetCore.Http.Features; using Titano.Data; using Titano.Data.Archiving; using Titano.Data.Repositories; @@ -97,6 +98,8 @@ public sealed class UploadService( var expected = Math.Min(upload.ChunkSize, upload.Size - (long)index * upload.ChunkSize); if (request.ContentLength is { } announced && announced != expected) return Fail(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() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = upload.ChunkSize; if (_chunksInFlight.AddOrUpdate(caller.Id, 1, (_, n) => n + 1) > options.ConcurrentChunksPerUser) { diff --git a/tests/Titano.Server.Tests/Storage/UploadTests.cs b/tests/Titano.Server.Tests/Storage/UploadTests.cs index bb072e69..ae889782 100644 --- a/tests/Titano.Server.Tests/Storage/UploadTests.cs +++ b/tests/Titano.Server.Tests/Storage/UploadTests.cs @@ -96,6 +96,55 @@ public sealed class UploadTests(TestContext context) Assert.Equal(frame.FrameId, again.FrameId); } + [Test] + public async Task AnUploadKeepsItsChunkSizeWhenTheSettingShrinks() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk * 2); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var data = Uploads.Photo(Chunk * 3, 14); + var start = await Uploads.StartAsync(uploader, data); + var id = Assert.NotNull(start.UploadId); + Assert.Equal(Chunk * 2, start.ChunkSize); + + // An administrator lowers the chunk size; the upload resumes with the size it started with. + server.Options.ChunkSizeBytes = Chunk; + var resumed = await Uploads.StartAsync(uploader, data); + Assert.Equal((id, Chunk * 2), (resumed.UploadId!.Value, resumed.ChunkSize)); + for (var i = 0; i < 2; i++) + { + using var put = await Uploads.PutChunkAsync(uploader, id, i, Uploads.Chunk(data, Chunk * 2, i)); + await put.ExpectAsync(HttpStatusCode.OK); + } + using var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null); + await complete.ExpectAsync(HttpStatusCode.OK); + + // A new upload takes the new size, and a body above it is refused. + var next = Uploads.Photo(Chunk * 2, 15); + var small = await Uploads.StartAsync(uploader, next); + Assert.Equal(Chunk, small.ChunkSize); + using var oversized = await Uploads.PutChunkAsync(uploader, small.UploadId!.Value, 0, next); + await oversized.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_size"); + } + + [Test] + public async Task RenderUploadsNeedTheEditPermission() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var frame = await Uploads.UploadAsync(uploader, Uploads.Photo(5000, 16), sessionKey: "roof"); + var output = Uploads.Photo(3000, 17); + var request = new RenderUploadRequest(Guid.CreateVersion7(), "Roof.mp4", output.Length, Uploads.Sha(output), "4K HEVC", "hevc", 3840, 2160, 30, 10, null); + + // Render outputs count against no quota: an upload-only account cannot add them to any sequence. + using (var refused = await uploader.PostAsJsonAsync($"/api/v1/sequences/{frame.SequenceId}/renders", request, ProtocolJson.Default.RenderUploadRequest)) + await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var complete = await uploader.PostAsync($"/api/v1/sequences/{frame.SequenceId}/renders/{Guid.CreateVersion7()}/complete", null)) + await complete.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using var accepted = await editor.PostAsJsonAsync($"/api/v1/sequences/{frame.SequenceId}/renders", request, ProtocolJson.Default.RenderUploadRequest); + await accepted.ExpectAsync(HttpStatusCode.Created); + } + [Test] public async Task TheProtocolClientUploadsAndDownloadsLikeTheUploader() {