- Renders, analyses, the server's manifests and proxy downloads take a proxy only when its Exif capture time is the frame's (ArchiveImages.IsProxyOf); one left at the index by a frame renumbered since counts as missing and is asked for again (V-24). - A frame whose original the host reads through its proxy (HEIF on the server) is preview-only only while it has none: the light analysis and the proxy pass keep the flag, the preview-only count, the light finding and the advisor's output cap in step with the proxies, and an arriving proxy queues a light analysis on the server (V-1, V-25). - The deep checkpoint's content key includes whether each frame is read from its original or its proxy, and analysis.json lists the frames measured from proxies; an unreadable verdict on a frame now read from its proxy makes the report stale (V-26). - Thumbnails drawn from proxies are compared with the archive's thumbnail size instead of 320 px (V-27). - A resumed thumbnail or proxy pass sets ThumbsBuilt/ProxiesBuilt only when the frames an earlier run did still have theirs, and the proxy pass logs frames waiting for a client's proxy apart from unreadable ones (V-28). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
118 lines
7.6 KiB
C#
118 lines
7.6 KiB
C#
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;
|
|
|
|
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. 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
|
|
{
|
|
private const string Immutable = "private, max-age=31536000, immutable";
|
|
private const string Regenerable = "private, max-age=3600";
|
|
|
|
public static void Map(RouteGroupBuilder api)
|
|
{
|
|
// Originals feed editing clients and render nodes; viewers browse proxies, thumbnails and exports.
|
|
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);
|
|
}
|
|
|
|
private static async Task<IResult> OriginalAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
|
|
{
|
|
if (await db.Frames.GetAsync(id, context.RequestAborted) is not { } frame) return ApiErrors.NotFound(context, "frame");
|
|
if (ArchivePathGuard.ExistingFile(archive.Root, frame.RelativePath) is not { } file) return ApiErrors.NotFound(context, "file_missing");
|
|
return FileResponses.Serve(context, file, 0, file.Length, FileResponses.ContentTypeOf(frame.FileName), Immutable, frame.Sha256, frame.FileName)
|
|
?? ApiErrors.NotFound(context, "file_missing");
|
|
}
|
|
|
|
private static async Task<IResult> AssetAsync(HttpContext context, Guid id, FrameAsset kind, TitanoDatabase db, ServerArchive archive, IFrameAssetLocator locator)
|
|
{
|
|
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");
|
|
var location = await locator.LocateAsync(archive.Current, sequence, frame, kind, ct);
|
|
if (location is null || !ArchivePathGuard.IsInside(archive.Root, location.Path)) return ApiErrors.NotFound(context, "asset_missing");
|
|
var file = new FileInfo(location.Path);
|
|
if (!file.Exists || file.LinkTarget is not null) return ApiErrors.NotFound(context, "asset_missing");
|
|
var etag = string.Create(CultureInfo.InvariantCulture, $"{kind}-{frame.Id:N}-{file.LastWriteTimeUtc.Ticks}-{location.Offset}");
|
|
return FileResponses.Serve(context, file, location.Offset, location.Length, location.ContentType, Regenerable, etag)
|
|
?? ApiErrors.NotFound(context, "asset_missing");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Files the body (a JPEG made from the original, its Exif capture time the frame's, as large as the archive's proxies,
|
|
/// at most <see cref="ArchiveImages.MaxProxyBytes"/>) as the proxy of a frame the server cannot decode
|
|
/// (<see cref="ServerProxies"/>); the sequence's thumbnails, proxy state and light analysis follow as background jobs.
|
|
/// 204; 413 <c>too_large</c>; 422 <c>proxy_not_wanted</c> (the server decodes that original itself, or it is missing or
|
|
/// damaged), <c>invalid_proxy</c>, <c>proxy_mismatch</c> or <c>proxy_too_small</c>, which concern that proxy only; 409
|
|
/// <c>proxies_disabled</c>, which concerns them all; 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");
|
|
// A proxy replaces the server's own of an original it decodes, which renders and remote views would then show.
|
|
if (!ServerProxies.CannotDecode(frame.Format) || (frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0)
|
|
return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "proxy_not_wanted");
|
|
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");
|
|
case ProxyStoreResult.TooSmall:
|
|
return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "proxy_too_small");
|
|
}
|
|
// The proxy is filed: its follow-up is queued even when the sender is gone by now.
|
|
await follow.ProxyArrivedAsync(sequence, CancellationToken.None);
|
|
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");
|
|
if (ArchivePathGuard.ExistingFile(archive.Root, render.OutputPath) is not { } file) return ApiErrors.NotFound(context, "file_missing");
|
|
var etag = string.Create(CultureInfo.InvariantCulture, $"r-{render.Id:N}-{file.Length}-{file.LastWriteTimeUtc.Ticks}");
|
|
return FileResponses.Serve(context, file, 0, file.Length, FileResponses.ContentTypeOf(file.Name), Immutable, etag, file.Name)
|
|
?? ApiErrors.NotFound(context, "file_missing");
|
|
}
|
|
}
|