diff --git a/i18n/en/Server.resx b/i18n/en/Server.resx
index 84cf4086..3166661a 100644
--- a/i18n/en/Server.resx
+++ b/i18n/en/Server.resx
@@ -29,6 +29,7 @@
Another program is changing the archive's settings; try again shortly.long
The archive's archive.json is damaged, from a newer Titano or of another archive; check the archive folder.long
The archive is offline or read-only; try again when it is back.long
+ The archive's archive.json has a thumbnail or proxy setting Titano cannot use; correct it in the archive folder.long
Its photos are the archive's root folder, which never moves to the trash.long
Wrong user name or password.long
The request is not valid.long
diff --git a/i18n/it/Server.it.resx b/i18n/it/Server.it.resx
index b0aa9af3..fc9c78d1 100644
--- a/i18n/it/Server.it.resx
+++ b/i18n/it/Server.it.resx
@@ -27,6 +27,7 @@
Un altro programma sta modificando le impostazioni dell'archivio; riprova tra poco.
Il file archive.json dell'archivio è danneggiato, di un Titano più recente o di un altro archivio; controlla la cartella dell'archivio.
L'archivio non è raggiungibile o è di sola lettura; riprova quando torna disponibile.
+ Il file archive.json dell'archivio ha un'impostazione delle miniature o dei proxy che Titano non può usare; correggila nella cartella dell'archivio.
Le sue foto sono la cartella principale dell'archivio, che non va mai nel cestino.
Nome utente o password errati.
La richiesta non è valida.
diff --git a/src/Titano.Server/Api/ArchiveEndpoints.cs b/src/Titano.Server/Api/ArchiveEndpoints.cs
index 953df725..c71e1df6 100644
--- a/src/Titano.Server/Api/ArchiveEndpoints.cs
+++ b/src/Titano.Server/Api/ArchiveEndpoints.cs
@@ -67,7 +67,9 @@ public static class ArchiveEndpoints
/// Writes the name, templates and proxy policy into the archive's archive.json through the archive engine (so a
/// restart and the proxy jobs see them), then into the index. 503 archive_offline while the archive cannot be
/// written; 409 archive_busy while another process holds its settings; 409 archive_manifest 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 archive_policy when it holds a
+ /// thumbnail or proxy setting this API cannot set and Titano cannot use (details: thumbnailPolicy or
+ /// proxyPolicy, the property to correct in the file). Nothing is written.
///
private static async Task 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.
diff --git a/src/Titano.Server/Api/UploadEndpoints.cs b/src/Titano.Server/Api/UploadEndpoints.cs
index 56755b08..74bd66f0 100644
--- a/src/Titano.Server/Api/UploadEndpoints.cs
+++ b/src/Titano.Server/Api/UploadEndpoints.cs
@@ -54,7 +54,8 @@ public static class UploadEndpoints
///
/// 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.
+ /// 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.
///
private static async Task ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service)
{
diff --git a/src/Titano.Server/Storage/UploadService.cs b/src/Titano.Server/Storage/UploadService.cs
index 22f799bc..a468bad2 100644
--- a/src/Titano.Server/Storage/UploadService.cs
+++ b/src/Titano.Server/Storage/UploadService.cs
@@ -101,14 +101,17 @@ public sealed class UploadService(
public async Task> 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() 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(404, "not_found", "upload");
if (upload.State != UploadState.Receiving) return Fail(409, "upload_state");
if (index < 0 || index >= upload.ChunkCount) return Fail(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(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/Api/AdminTests.cs b/tests/Titano.Server.Tests/Api/AdminTests.cs
index ea8aebd3..744d20a2 100644
--- a/tests/Titano.Server.Tests/Api/AdminTests.cs
+++ b/tests/Titano.Server.Tests/Api/AdminTests.cs
@@ -177,5 +177,21 @@ public sealed class AdminTests(TestContext context)
{
await File.WriteAllBytesAsync(path, saved, context.Cancellation);
}
+
+ // A policy this form cannot set, written out of range by another tool, is named so the admin can correct the file.
+ var current = Titano.Core.Archiving.ManifestStore.ReadArchive(path);
+ foreach (var (odd, property) in new[]
+ {
+ (current with { ThumbnailPolicy = new Titano.Core.Archiving.ThumbnailPolicy { LongEdge = 2048 } }, "thumbnailPolicy"),
+ (current with { ProxyPolicy = current.ProxyPolicy with { Quality = 0 } }, "proxyPolicy"),
+ })
+ {
+ Titano.Core.Archiving.ManifestStore.WriteArchive(path, odd);
+ var before = await File.ReadAllBytesAsync(path, context.Cancellation);
+ using var refused = await admin.PutAsJsonAsync("/api/v1/archive/settings", changed with { ProxyLongEdge = 1280 }, ProtocolJson.Default.ArchiveSettingsDto);
+ Assert.Equal(property, (await refused.ExpectErrorAsync(HttpStatusCode.Conflict, "archive_policy")).Details);
+ Assert.SequenceEqual(before, await File.ReadAllBytesAsync(path, context.Cancellation));
+ }
+ await File.WriteAllBytesAsync(path, saved, context.Cancellation);
}
}
diff --git a/tests/Titano.Server.Tests/Storage/UploadTests.cs b/tests/Titano.Server.Tests/Storage/UploadTests.cs
index aea2324e..b449139f 100644
--- a/tests/Titano.Server.Tests/Storage/UploadTests.cs
+++ b/tests/Titano.Server.Tests/Storage/UploadTests.cs
@@ -170,6 +170,29 @@ public sealed class UploadTests(TestContext context)
await oversized.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_size");
}
+ [Test]
+ public async Task AnswersThatLeaveALargeChunkUnreadReachTheClient()
+ {
+ // Chunks far above the 1 MiB other requests may send: more than the connection buffers while the server reads nothing.
+ const int large = 32 * 1024 * 1024;
+ await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = large);
+ var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
+ var data = Uploads.Photo(24 * 1024 * 1024, 16);
+ var id = (await Uploads.StartAsync(uploader, data)).UploadId!.Value;
+ using (var first = await Uploads.PutChunkAsync(uploader, id, 0, data)) await first.ExpectAsync(HttpStatusCode.OK);
+
+ // The client lost the answer and sends the chunk again: the same answer, not a closed connection.
+ using (var again = await Uploads.PutChunkAsync(uploader, id, 0, data))
+ {
+ await again.ExpectAsync(HttpStatusCode.OK);
+ Assert.SequenceEqual([0], (await again.ReadAsync(ProtocolJson.Default.ChunkReceived)).Received);
+ }
+ using (var unknown = await Uploads.PutChunkAsync(uploader, Guid.CreateVersion7(), 0, data)) await unknown.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
+ using (var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null)) await complete.ExpectAsync(HttpStatusCode.OK);
+ using var late = await Uploads.PutChunkAsync(uploader, id, 0, data);
+ await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
+ }
+
[Test]
public async Task RenderUploadsNeedTheEditPermission()
{