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