Files
Chronos/src/Titano.Server/Api/FileEndpoints.cs
T
Claude ac9a150027 feat(server): run on the real archive engine
ArchiveModule registers the archive engine as the server's ingest, its
proxy/thumbnail locator and its job handlers (through the new
IJobHandlerProvider); Program.cs passes it to ServerHost.Build. The
server opens its archive through ArchiveService so archive.json always
exists. The server's path-safety helper is renamed ArchivePathGuard to
stop clashing with the archive layout's ArchivePaths, and the server
migration is renumbered 0003. An end-to-end test uploads a photo, finds
it in the layout and fetches the thumbnail built by the queued job.
CI's PostgreSQL jobs now run the server tests too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 13:00:38 +00:00

60 lines
3.8 KiB
C#

using System.Globalization;
using Titano.Core.Model;
using Titano.Data;
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.
/// </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);
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");
}
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");
}
}