fix(proxies): get proxies of undecodable originals from clients and nodes

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
This commit is contained in:
Claude committed 2026-10-12 09:58:58 +00:00
1 parent 078cac75e4
commit 9588ed7f4b
28 files changed
+1005 -94

No files matched your search

+51 -1
View File
@@ -1,6 +1,12 @@
using System.Globalization;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Titano.Core.Archiving;
using Titano.Core.Imaging.Decoders;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Server.Jobs;
using Titano.Server.Security;
using Titano.Server.Storage;
@@ -8,7 +14,8 @@ namespace Titano.Server.Api;
/// <summary>
/// Streamed archive files with Range support (BRIEF §14.3): originals, proxies, thumbnails and exports. Paths come
/// from the database by id and are resolved under the archive root only.
/// from the database by id and are resolved under the archive root only. Proxies of frames the server cannot decode
/// arrive from clients and render nodes (<c>PUT /frames/{id}/proxy</c>, BRIEF §2.3).
/// </summary>
public static class FileEndpoints
{
@@ -21,6 +28,8 @@ public static class FileEndpoints
api.MapGet("/frames/{id:guid}/original", OriginalAsync).Requires(Permissions.Edit | Permissions.RenderNode);
api.MapGet("/frames/{id:guid}/proxy", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Proxy, db, a, l))
.Requires(Permissions.Read | Permissions.RenderNode);
// Made by clients that edit and by render nodes, which decode what the server cannot.
api.MapPut("/frames/{id:guid}/proxy", StoreProxyAsync).Requires(Permissions.Edit | Permissions.RenderNode);
api.MapGet("/frames/{id:guid}/thumb", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Thumbnail, db, a, l))
.Requires(Permissions.Read);
api.MapGet("/renders/{id:guid}/file", RenderAsync).Requires(Permissions.Read);
@@ -48,6 +57,47 @@ public static class FileEndpoints
?? ApiErrors.NotFound(context, "asset_missing");
}
/// <summary>
/// Files the body (a JPEG made from the original, its Exif capture time the frame's, at most
/// <see cref="ArchiveImages.MaxProxyBytes"/>) as the frame's proxy; the sequence's thumbnails and proxy state follow as
/// background jobs. 204; 413 <c>too_large</c>, 422 <c>invalid_proxy</c> or <c>proxy_mismatch</c>, 409
/// <c>proxies_disabled</c>, 503 <c>archive_offline</c>.
/// </summary>
private static async Task<IResult> StoreProxyAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive,
[FromServices] ArchiveService? archives, SequenceJobs follow)
{
var ct = context.RequestAborted;
if (await db.Frames.GetAsync(id, ct) is not { } frame || await db.Sequences.GetAsync(frame.SequenceId, ct) is not { } sequence)
return ApiErrors.NotFound(context, "frame");
if (context.Request.ContentLength is > ArchiveImages.MaxProxyBytes) return ApiErrors.Result(context, StatusCodes.Status413PayloadTooLarge, "too_large");
// Proxies are larger than the JSON bodies Kestrel is limited to; reading past this answers 413.
if (context.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = ArchiveImages.MaxProxyBytes;
using var body = new MemoryStream();
await context.Request.Body.CopyToAsync(body, ct);
var images = (archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner })).Images;
ProxyStoreResult result;
try
{
result = images.StoreProxy(archive.Current, sequence, frame, body.ToArray());
}
catch (ArchiveOfflineException)
{
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
}
switch (result)
{
case ProxyStoreResult.Disabled:
return ApiErrors.Conflict(context, "proxies_disabled");
case ProxyStoreResult.Invalid:
return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "invalid_proxy");
case ProxyStoreResult.WrongFrame:
return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "proxy_mismatch");
}
await follow.ProxyArrivedAsync(sequence, ct);
return Results.NoContent();
}
private static async Task<IResult> RenderAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
{
if (await db.Renders.GetAsync(id, context.RequestAborted) is not { } render) return ApiErrors.NotFound(context, "render");
+10 -15
View File
@@ -213,29 +213,24 @@ public static class SequenceEndpoints
_ => ApiErrors.Conflict(context, "folder_missing"),
};
/// <summary>Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5); ETag over the version counters.</summary>
private static async Task<IResult> ManifestAsync(HttpContext context, Guid id, TitanoDatabase db)
/// <summary>
/// Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5), with the proxies the server asks
/// for (<see cref="ServerProxies"/>); ETag over the version counters and the number of proxies asked for.
/// </summary>
private static async Task<IResult> ManifestAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
{
var ct = context.RequestAborted;
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
var etag = string.Create(CultureInfo.InvariantCulture, $"\"m{sequence.SettingsVersion}-{sequence.FramesVersion}-{sequence.RenderedCount}\"");
var spec = ServerProxies.Spec(archive.Current);
var frames = await ServerProxies.FramesAsync(db, archive.Current, sequence, spec, ct);
var etag = string.Create(CultureInfo.InvariantCulture,
$"\"m{sequence.SettingsVersion}-{sequence.FramesVersion}-{sequence.RenderedCount}-{frames.Count(f => f.WantsProxy)}\"");
if (context.Request.Headers.IfNoneMatch == etag) return Results.StatusCode(StatusCodes.Status304NotModified);
var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
var frames = new List<ManifestFrame>(sequence.FrameCount);
var after = -1;
while (true)
{
var batch = await db.Frames.ListBySequenceAsync(id, after, 1000, ct);
frames.AddRange(batch.Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb)));
if (batch.Count < 1000) break;
after = batch[^1].Index;
}
var renders = await db.Renders.ListBySequenceAsync(id, ct);
context.Response.Headers.ETag = etag;
return Results.Json(new SequenceManifest(id, sequence.FramesVersion, sequence.SettingsVersion, sequence.SettingsJson, sequence.Tags, frames,
[.. renders.Select(Dto.Render)]), ProtocolJson.Default.SequenceManifest);
[.. renders.Select(Dto.Render)], spec), ProtocolJson.Default.SequenceManifest);
}
private static async Task<IResult> FramesAsync(HttpContext context, Guid id, string? cursor, int? limit, TitanoDatabase db)
+6 -18
View File
@@ -16,11 +16,14 @@ namespace Titano.Server.Jobs;
/// <summary>
/// Render nodes (BRIEF §14.7): clients with the render-node right register their encoders, heartbeat, claim queued
/// render jobs they can encode, report progress, and upload the output in resumable chunks that become a render of
/// the sequence. A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>).
/// the sequence. A claimed job lists every frame with its flags (a node renders none missing, corrupt or excluded) and
/// the proxies the server asks for (<see cref="ServerProxies"/>), which the node sends from the originals it fetched.
/// A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>).
/// </summary>
public sealed class RenderNodeService(
TitanoDatabase db,
ServerOptions options,
ServerArchive archive,
UploadService uploads,
UploadStore store,
RenderStore renders,
@@ -84,8 +87,9 @@ public sealed class RenderNodeService(
continue;
}
await TouchAsync(node, cancellationToken);
var spec = ServerProxies.Spec(archive.Current);
return new ClaimedJob(Dto.Job(job), sequence.Id, parameters.SettingsJson ?? sequence.SettingsJson ?? "{}", parameters.PresetJson,
parameters.UseProxies, await ManifestFramesAsync(sequence, cancellationToken));
parameters.UseProxies, await ServerProxies.FramesAsync(db, archive.Current, sequence, spec, cancellationToken), spec);
}
await TouchAsync(node, cancellationToken);
return (ClaimedJob?)null;
@@ -234,22 +238,6 @@ public sealed class RenderNodeService(
if (state != node.State) events.Publish(new NodeStateEvent(node.Id, node.ClientName, state));
}
private async Task<List<ManifestFrame>> ManifestFramesAsync(Sequence sequence, CancellationToken cancellationToken)
{
var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
var frames = new List<ManifestFrame>(sequence.FrameCount);
var after = -1;
while (true)
{
var batch = await db.Frames.ListBySequenceAsync(sequence.Id, after, 1000, cancellationToken);
frames.AddRange(batch.Where(f => !f.Flags.HasFlag(FrameFlags.Excluded))
.Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb)));
if (batch.Count < 1000) return frames;
after = batch[^1].Index;
}
}
/// <summary>
/// The node has an encoder for the job's codec that handles its resolution: the encoder's own limit, else the node's
/// (0 = no announced limit).
+10 -4
View File
@@ -4,21 +4,27 @@ using Titano.Data;
namespace Titano.Server.Jobs;
/// <summary>
/// Follow-up work after frames arrive (BRIEF §14.4): thumbnails, proxies and light analysis of the sequence, queued at
/// background priority once per sequence while one is still waiting, and only for kinds this server can run.
/// Follow-up work after frames arrive (BRIEF §14.4): thumbnails, proxies and light analysis of the sequence; after a
/// proxy sent by a client or render node arrives, the thumbnails and the sequence's proxy state. Queued at background
/// priority once per sequence while one is still waiting, and only for kinds this server can run.
/// </summary>
public sealed class SequenceJobs(TitanoDatabase db, JobEngineHost jobs)
{
private static readonly JobKind[] AfterNewFrames = [JobKind.Thumbs, JobKind.Proxies, JobKind.LightAnalyse];
private static readonly JobKind[] AfterProxy = [JobKind.Thumbs, JobKind.Proxies];
private readonly SemaphoreSlim _gate = new(1, 1);
public async Task FramesAddedAsync(Sequence sequence, CancellationToken cancellationToken)
public Task FramesAddedAsync(Sequence sequence, CancellationToken cancellationToken) => QueueAsync(sequence, AfterNewFrames, cancellationToken);
public Task ProxyArrivedAsync(Sequence sequence, CancellationToken cancellationToken) => QueueAsync(sequence, AfterProxy, cancellationToken);
private async Task QueueAsync(Sequence sequence, JobKind[] kinds, CancellationToken cancellationToken)
{
await _gate.WaitAsync(cancellationToken);
try
{
foreach (var kind in AfterNewFrames)
foreach (var kind in kinds)
{
if (!jobs.CanRun(kind) || await db.Jobs.IsQueuedAsync(kind, sequence.Id, cancellationToken)) continue;
await jobs.Engine.EnqueueAsync(kind, [sequence.Id], sequence.DisplayTitle, priority: JobPriority.Background, cancellationToken: cancellationToken);
@@ -0,0 +1,66 @@
using Titano.Core.Archiving;
using Titano.Core.Imaging;
using Titano.Core.Imaging.Decoders;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Protocol;
namespace Titano.Server.Storage;
/// <summary>
/// Proxies the server asks for (BRIEF §2.3, §8.5): it decodes with managed decoders only, so the frames whose originals
/// it cannot decode (HEIF) get their proxies from a Windows client or render node, made as the archive's proxy policy
/// says. Manifests (sync and render nodes) list every frame with its flags and whether its proxy is wanted.
/// </summary>
public static class ServerProxies
{
private const int Page = 1000;
/// <summary>The server's decoders (BRIEF §4.2: managed only).</summary>
private static readonly ImageDecoder Decoder = DecoderFactory.CreateServer();
/// <summary>The server takes this format's pixels from proxies made elsewhere.</summary>
public static bool CannotDecode(ImageFileFormat format) => ArchiveImages.NeedsProxy(Decoder, format);
/// <summary>How proxies of the archive are made; null when it makes none (or its manifest cannot be read now).</summary>
public static ProxySpec? Spec(Archive archive)
{
try
{
var policy = ManifestStore.ReadArchive(new ArchivePaths(archive.RootPath).ManifestPath).ProxyPolicy;
return policy.LongEdge == ProxyPolicy.Disabled ? null : new ProxySpec(policy.LongEdge, policy.Quality);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException)
{
return null;
}
}
/// <summary>
/// Every frame of <paramref name="sequence"/> in index order, as a manifest lists it: a proxy is there when the server
/// built them all or one was sent for the frame; it is wanted for a frame the server cannot decode that has none
/// (and is not missing), unless <paramref name="spec"/> is null.
/// </summary>
public static async Task<List<ManifestFrame>> FramesAsync(TitanoDatabase db, Archive archive, Sequence sequence, ProxySpec? spec,
CancellationToken cancellationToken)
{
var proxies = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
var thumbs = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
var folder = new ArchivePaths(archive.RootPath).Sequence(sequence.Folder);
var frames = new List<ManifestFrame>(sequence.FrameCount);
var after = -1;
while (true)
{
var batch = await db.Frames.ListBySequenceAsync(sequence.Id, after, Page, cancellationToken);
foreach (var f in batch)
{
var sent = !proxies && CannotDecode(f.Format) && File.Exists(folder.Proxy(f.Index));
var wanted = spec is not null && !proxies && !sent && CannotDecode(f.Format) && (f.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) == 0;
frames.Add(new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, proxies || sent, thumbs, (long)f.Flags, wanted));
}
if (batch.Count < Page) return frames;
after = batch[^1].Index;
}
}
}