using System.Net; using System.Net.Http.Headers; using Microsoft.Extensions.DependencyInjection; using Chronos.Core.Model; using Chronos.Server.Storage; using Chronos.Testing; namespace Chronos.Server.Tests.Storage; [Category("server")] public sealed class FileTests(TestContext context) { [Test] public async Task OriginalsAreStreamedWithRangesAndEtags() { await using var server = await TestServer.StartAsync(context); var (editor, _, _) = await server.UserAsync("alberto", "editor"); var data = Uploads.Photo(10_000, 21); var frame = await Uploads.UploadAsync(editor, data); using (var full = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original")) { await full.ExpectAsync(HttpStatusCode.OK); Assert.Equal("bytes", full.Headers.AcceptRanges.Single()); Assert.Equal($"\"{Uploads.Sha(data)}\"", full.Headers.ETag!.Tag); await Http.BytesEqualAsync(data, full.Content.ReadAsByteArrayAsync()); } using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original"); range.Headers.Range = new RangeHeaderValue(100, 199); using (var partial = await editor.SendAsync(range)) { await partial.ExpectAsync(HttpStatusCode.PartialContent); Assert.Equal((100L, 199L, 10_000L), (partial.Content.Headers.ContentRange!.From!.Value, partial.Content.Headers.ContentRange.To!.Value, partial.Content.Headers.ContentRange.Length!.Value)); await Http.BytesEqualAsync(data[100..200], partial.Content.ReadAsByteArrayAsync()); } using var tail = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original"); tail.Headers.Range = new RangeHeaderValue(9_990, null); using (var end = await editor.SendAsync(tail)) await Http.BytesEqualAsync(data[9_990..], end.Content.ReadAsByteArrayAsync()); using var cached = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original"); cached.Headers.IfNoneMatch.Add(new EntityTagHeaderValue($"\"{Uploads.Sha(data)}\"")); using (var notModified = await editor.SendAsync(cached)) await notModified.ExpectAsync(HttpStatusCode.NotModified); var (viewer, _, _) = await server.UserAsync("marco", "viewer"); using var refused = await viewer.GetAsync($"/api/v1/frames/{frame.FrameId}/original"); await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); } [Test] public async Task ThumbnailsAndProxiesComeFromTheLocatedFiles() { await using var server = await TestServer.StartAsync(context); var (editor, _, _) = await server.UserAsync("alberto", "editor"); var first = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 22), "a.jpg", sessionKey: "s"); var second = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 23), "b.jpg", sessionKey: "s"); var sequence = (await server.Database.Sequences.GetAsync(first.SequenceId))!; var folder = Path.Combine(server.Options.ArchivePath, sequence.Folder); var pack = Uploads.Photo(FakeAssetLocator.ThumbnailSize * 2, 24); Directory.CreateDirectory(Path.Combine(folder, ".chronos")); await File.WriteAllBytesAsync(Path.Combine(folder, ".chronos", "thumbs.pack"), pack); using (var thumb = await editor.GetAsync($"/api/v1/frames/{second.FrameId}/thumb")) { await thumb.ExpectAsync(HttpStatusCode.OK); Assert.Equal("image/jpeg", thumb.Content.Headers.ContentType!.MediaType); await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(2 * FakeAssetLocator.ThumbnailSize)], thumb.Content.ReadAsByteArrayAsync()); } using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{second.FrameId}/thumb"); range.Headers.Range = new RangeHeaderValue(0, 9); using (var partial = await editor.SendAsync(range)) { await partial.ExpectAsync(HttpStatusCode.PartialContent); await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(FakeAssetLocator.ThumbnailSize + 10)], partial.Content.ReadAsByteArrayAsync()); } using (var missing = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy")) Assert.Equal("asset_missing", (await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details); var proxy = Uploads.Photo(500, 25); Directory.CreateDirectory(Path.Combine(folder, "proxies")); await File.WriteAllBytesAsync(Path.Combine(folder, "proxies", "0000.jpg"), proxy); using var found = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy"); await Http.BytesEqualAsync(proxy, found.Content.ReadAsByteArrayAsync()); } [Test] public async Task PathsOutsideTheArchiveAreNeverServed() { await using var server = await TestServer.StartAsync(context); var (editor, _, _) = await server.UserAsync("alberto", "editor"); var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 26)); var secret = Path.Combine(Path.GetDirectoryName(server.Options.ArchivePath)!, "secret.txt"); await File.WriteAllTextAsync(secret, "not for clients"); foreach (var hostile in new[] { "../secret.txt", "2026/../../secret.txt", "..\\secret.txt", secret, "/etc/passwd" }) { await using (var session = await server.Database.Database.OpenAsync(context.Cancellation)) await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", [hostile, frame.FrameId], context.Cancellation); using var response = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original"); await response.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); } var locator = server.Services.GetRequiredService(); locator.Override = new AssetLocation(secret, 0, 5, "text/plain"); using (var proxy = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/proxy")) await proxy.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); locator.Override = new AssetLocation(Path.Combine(server.Options.ArchivePath, "..", "secret.txt"), 0, 5, "text/plain"); using (var dotted = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/thumb")) await dotted.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); if (!OperatingSystem.IsWindows()) { var link = Path.Combine(server.Options.ArchivePath, "link.jpg"); File.CreateSymbolicLink(link, secret); await using (var session = await server.Database.Database.OpenAsync(context.Cancellation)) await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", ["link.jpg", frame.FrameId], context.Cancellation); using var linked = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original"); await linked.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); } } [Test] public async Task ExportsAreDownloadableWithRanges() { await using var server = await TestServer.StartAsync(context); var (viewer, _, _) = await server.UserAsync("marco", "viewer"); var (editor, _, _) = await server.UserAsync("alberto", "editor"); var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 27)); var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!; var video = Uploads.Photo(4096, 28); var relative = $"{sequence.Folder}/exports/Harbour sunset - 4K.mp4"; Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(server.Options.ArchivePath, relative))!); await File.WriteAllBytesAsync(Path.Combine(server.Options.ArchivePath, relative), video); var render = new Render { Id = Guid.CreateVersion7(), SequenceId = sequence.Id, PresetName = "4K HEVC", OutputPath = relative, Bytes = video.Length, Created = DateTimeOffset.UtcNow }; await server.Database.Renders.InsertAsync(render); using var request = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/renders/{render.Id}/file"); request.Headers.Range = new RangeHeaderValue(1000, 1999); using var response = await viewer.SendAsync(request); await response.ExpectAsync(HttpStatusCode.PartialContent); Assert.Equal("video/mp4", response.Content.Headers.ContentType!.MediaType); await Http.BytesEqualAsync(video[1000..2000], response.Content.ReadAsByteArrayAsync()); } }