The server decodes with managed decoders only, so HEIF originals had no
proxies, thumbnails from their small previews and no pixels for analysis or
managed renders, and nothing could send it proxies (ENG-05). Manifests now
carry the archive's proxy spec and, per frame, its flags and whether the
server wants its proxy. PUT /api/v1/frames/{id}/proxy (Edit or RenderNode)
stores a JPEG of at most 64 MiB that decodes and whose Exif capture time is
the frame's, then queues thumbnails and the proxy pass. An upload from the
desktop makes and sends the wanted proxies after the originals and counts
them as local changes; a render node sends those it can make from the
originals it fetched once the job is done. The server never makes such a
proxy from a preview, drops one left by a renumbered frame, takes thumbnails
from a proxy, and analyses and renders those frames from their proxies.
Render nodes fetched every frame of the manifest: frames missing, corrupt or
excluded on the server failed the download, and originals not hashed yet
failed the SHA-256 check (ENG-07). Nodes now skip those frames, fetch unhashed
originals into the job folder with a size check, judge decodability with their
own decoders, and use the proxy of a frame they cannot decode.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
100 lines
5.1 KiB
C#
100 lines
5.1 KiB
C#
using System.Net;
|
||
using System.Net.Http.Headers;
|
||
using Titano.Core.Codecs.Jpeg;
|
||
using Titano.Core.Imaging;
|
||
using Titano.Core.Metadata;
|
||
using Titano.Protocol;
|
||
using Titano.Server.Storage;
|
||
using Titano.Testing;
|
||
|
||
namespace Titano.Server.Tests.Storage;
|
||
|
||
/// <summary>
|
||
/// Proxies of originals the server cannot decode (BRIEF §2.3: HEIF) arrive from clients and render nodes through
|
||
/// <c>PUT /api/v1/frames/{id}/proxy</c> and become the frame's proxy and thumbnail.
|
||
/// </summary>
|
||
[Category("server")]
|
||
public sealed class ProxyUploadTests(TestContext context)
|
||
{
|
||
[Test]
|
||
public async Task AProxySentForAFrameTheServerCannotDecodeIsCheckedKeptAndThumbnailed()
|
||
{
|
||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||
var (viewer, _, _) = await server.UserAsync("vera", "viewer");
|
||
var upload = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
|
||
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId));
|
||
Assert.Equal(ImageFileFormat.Heif, frame.Format);
|
||
|
||
var manifest = await ManifestAsync(editor, upload.SequenceId);
|
||
Assert.Equal((true, false), (manifest.Frames.Single().WantsProxy, manifest.Frames.Single().HasProxy));
|
||
Assert.Equal(new ProxySpec(1920, 92), manifest.Proxies);
|
||
|
||
using (var garbage = await PutAsync(editor, frame.Id, "not a photo"u8.ToArray()))
|
||
await garbage.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "invalid_proxy");
|
||
using (var other = await PutAsync(editor, frame.Id, Proxy(frame.CapturedAt!.Value.AddSeconds(1))))
|
||
await other.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "proxy_mismatch");
|
||
var proxy = Proxy(frame.CapturedAt!.Value);
|
||
using (var refused = await PutAsync(viewer, frame.Id, proxy))
|
||
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||
using (var missing = await PutAsync(editor, Guid.CreateVersion7(), proxy))
|
||
await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||
using (var stored = await PutAsync(editor, frame.Id, proxy))
|
||
await stored.ExpectAsync(HttpStatusCode.NoContent);
|
||
|
||
await Http.BytesEqualAsync(proxy, editor.GetByteArrayAsync($"/api/v1/frames/{frame.Id}/proxy"));
|
||
var after = (await ManifestAsync(editor, upload.SequenceId)).Frames.Single();
|
||
Assert.Equal((false, true), (after.WantsProxy, after.HasProxy));
|
||
|
||
// The thumbnail job the proxy queued draws the frame from it.
|
||
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
|
||
while (true)
|
||
{
|
||
using var thumb = await editor.GetAsync($"/api/v1/frames/{frame.Id}/thumb");
|
||
if (thumb.StatusCode == HttpStatusCode.OK)
|
||
{
|
||
var jpeg = await thumb.Content.ReadAsByteArrayAsync();
|
||
Assert.Equal((320, 240), (JpegDecoder.ReadInfo(jpeg).Width, JpegDecoder.ReadInfo(jpeg).Height));
|
||
break;
|
||
}
|
||
Assert.True(DateTime.UtcNow < until, $"thumbnail not built: {(int)thumb.StatusCode}");
|
||
await Task.Delay(100);
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public async Task FramesTheServerDecodesAskForNoProxy()
|
||
{
|
||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||
var upload = await Uploads.UploadAsync(editor, Proxy(new DateTime(2026, 9, 28, 18, 2, 11)), "IMG_0001.JPG", sessionKey: "roof");
|
||
|
||
Assert.False((await ManifestAsync(editor, upload.SequenceId)).Frames.Single().WantsProxy);
|
||
}
|
||
|
||
/// <summary>A HEIF file as far as signature sniffing goes (an <c>ftyp</c> box naming heic), with no pixels the server can read.</summary>
|
||
private static byte[] Heif(int seed) => [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8, .. Uploads.Photo(2000, seed)];
|
||
|
||
/// <summary>A 320 × 240 JPEG with <paramref name="captured"/> in its Exif, as a client makes a proxy.</summary>
|
||
private static byte[] Proxy(DateTime captured)
|
||
{
|
||
using var image = new ImageBuffer(320, 240);
|
||
for (var c = 0; c < 3; c++) image.Plane(c).Fill(0.3f + c * 0.1f);
|
||
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Titano" });
|
||
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
|
||
}
|
||
|
||
private static Task<HttpResponseMessage> PutAsync(HttpClient client, Guid frameId, byte[] body)
|
||
{
|
||
var content = new ByteArrayContent(body);
|
||
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
|
||
return client.PutAsync($"/api/v1/frames/{frameId}/proxy", content);
|
||
}
|
||
|
||
private static async Task<SequenceManifest> ManifestAsync(HttpClient client, Guid sequenceId)
|
||
{
|
||
using var response = await client.GetAsync($"/api/v1/sequences/{sequenceId}/manifest");
|
||
return await response.ReadAsync(ProtocolJson.Default.SequenceManifest);
|
||
}
|
||
}
|