From b4f1fa32d5639c0a4da764fd6f535e83aaf52ea3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 16:07:31 +0000 Subject: [PATCH 1/2] fix(proxies): use only a frame's own proxy and let tiers, reports and built flags follow it - 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- i18n/en/Archive.resx | 1 + i18n/it/Archive.it.resx | 1 + .../Analysis/Deep/DeepAnalysisReport.cs | 7 + src/Titano.Core/Archiving/SequenceFrames.cs | 16 ++- src/Titano.Core/Archiving/SequenceManifest.cs | 3 +- src/Titano.Data/Archiving/ArchiveImages.cs | 84 +++++++---- src/Titano.Data/Archiving/FrameTiers.cs | 22 ++- .../Archiving/Jobs/ImageJobHandler.cs | 4 +- .../Processing/Jobs/RenderJobHandler.cs | 2 +- .../Processing/SequenceAnalyser.cs | 45 ++++-- src/Titano.Data/Processing/SequenceFrames.cs | 21 ++- src/Titano.Server/Api/FileEndpoints.cs | 4 +- src/Titano.Server/Jobs/SequenceJobs.cs | 5 +- .../Storage/ArchiveAssetLocator.cs | 6 +- src/Titano.Server/Storage/ServerProxies.cs | 7 +- .../Archiving/DecodeTierTests.cs | 18 +++ .../Archiving/ArchiveJobTests.cs | 17 ++- tests/Titano.Data.Tests/Archiving/HeifFile.cs | 90 ++++++++++++ .../Titano.Data.Tests/Archiving/ProxyTests.cs | 132 +++++++++++++++++- .../Processing/AnalysisJobTests.cs | 113 +++++++++++++++ .../Storage/ProcessingModuleTests.cs | 35 +++++ .../Storage/ProxyUploadTests.cs | 27 ++++ 22 files changed, 601 insertions(+), 59 deletions(-) create mode 100644 tests/Titano.Data.Tests/Archiving/HeifFile.cs diff --git a/i18n/en/Archive.resx b/i18n/en/Archive.resx index bdf2a374..c2862663 100644 --- a/i18n/en/Archive.resx +++ b/i18n/en/Archive.resx @@ -135,6 +135,7 @@ Index compacted: {0} freedlong: job log line Stale locks: {0}, temporary files: {1}, packs compacted: {2}, duplicate flags changed: {3}long: job log line Unindexed photos: {0}, missing photos: {1}long: job log line + {0}: made {1}, reused {2}, unreadable {3}, waiting {4}long: job log line; {4} frames whose proxy this host waits for from a client or render node Proxies for the server: {0} sent, {1} not sentlong: job log line; {0} proxies sent; {1} proxies this PC could not make or the server refused Folders: {0}, indexed: {1}, adopted: {2}, removed: {3}long: job log line Sequence not found: {0}long: job log line diff --git a/i18n/it/Archive.it.resx b/i18n/it/Archive.it.resx index c1522a34..d8359d69 100644 --- a/i18n/it/Archive.it.resx +++ b/i18n/it/Archive.it.resx @@ -130,6 +130,7 @@ Indice compattato: {0} liberati Blocchi scaduti: {0}, file temporanei: {1}, pacchetti compattati: {2}, segni di duplicato cambiati: {3} Foto non indicizzate: {0}, foto mancanti: {1} + {0}: create {1}, riusate {2}, illeggibili {3}, in attesa {4} Proxy per il server: {0} inviati, {1} non inviati Cartelle: {0}, indicizzate: {1}, adottate: {2}, rimosse: {3} Sequenza non trovata: {0} diff --git a/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs b/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs index 89866d9e..5a479ce9 100644 --- a/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs +++ b/src/Titano.Core/Analysis/Deep/DeepAnalysisReport.cs @@ -161,6 +161,13 @@ public sealed record DeepAnalysisReport /// Suggested hero frame: sharp, well exposed, near golden hour or mid-sequence, not excluded. public int? HeroIndex { get; init; } + /// + /// Frames measured from their proxies, ascending, because the analysing host cannot decode their originals (HEIF on the + /// server, BRIEF §2.3); null when none were. An unreadable verdict on a frame read from its original no longer holds + /// once the frame is read from a proxy. + /// + public int[]? ProxyFrames { get; init; } + public IReadOnlyList Findings { get; init; } = []; public string ToJson() => JsonSerializer.Serialize(this, DeepAnalysisJson.Default.DeepAnalysisReport); diff --git a/src/Titano.Core/Archiving/SequenceFrames.cs b/src/Titano.Core/Archiving/SequenceFrames.cs index 5f72d16b..11dbf2c7 100644 --- a/src/Titano.Core/Archiving/SequenceFrames.cs +++ b/src/Titano.Core/Archiving/SequenceFrames.cs @@ -57,10 +57,12 @@ public static class SequenceFrames /// For the unavailable tier: whether that proves the file unreadable. False when the host has no /// native decoder for the format, so a client or render node with one (or a proxy) may still produce its pixels: the /// tier is kept and is left as it was. - public static SidecarFrame WithTier(SidecarFrame frame, FileTier tier, bool damaged = true) + /// Whether the host takes the frame's pixels from its proxy (an original it cannot decode, whose + /// proxy is there): see . + public static SidecarFrame WithTier(SidecarFrame frame, FileTier tier, bool damaged = true, bool proxied = false) { var flags = frame.Flags & ~(FrameFlags.PreviewOnly | FrameFlags.Undecodable); - if (tier.Tier == DecodeTier.EmbeddedPreview) flags |= FrameFlags.PreviewOnly; + if (tier.Tier == DecodeTier.EmbeddedPreview) flags |= proxied ? FrameFlags.None : FrameFlags.PreviewOnly; else if (tier.Tier == DecodeTier.Unavailable) flags |= damaged ? FrameFlags.Undecodable : frame.Flags & FrameFlags.Undecodable; var (w, h) = tier.Tier == DecodeTier.EmbeddedPreview && tier.Width > 0 && tier.Height > 0 ? PixelConvert.OrientedSize(tier.Width, tier.Height, frame.Orientation) @@ -68,6 +70,16 @@ public static class SequenceFrames return frame with { Tier = tier.Tier, Flags = flags, PreviewW = Positive(w), PreviewH = Positive(h) }; } + /// + /// The frame with following its proxy, for a host that takes the pixels of the + /// frame's original from its proxy (HEIF on the server, BRIEF §2.3, §8.1): an embedded-preview frame is read from its + /// preview, so preview-only, only while is false. The tier and the preview size stay the + /// original's; other tiers are left as they are. + /// + public static SidecarFrame WithProxy(SidecarFrame frame, bool proxied) => frame.Tier != DecodeTier.EmbeddedPreview + ? frame + : frame with { Flags = proxied ? frame.Flags & ~FrameFlags.PreviewOnly : frame.Flags | FrameFlags.PreviewOnly }; + private static int? Positive(int? value) => value > 0 ? value : null; /// diff --git a/src/Titano.Core/Archiving/SequenceManifest.cs b/src/Titano.Core/Archiving/SequenceManifest.cs index 7d7c7b6f..e962f31e 100644 --- a/src/Titano.Core/Archiving/SequenceManifest.cs +++ b/src/Titano.Core/Archiving/SequenceManifest.cs @@ -117,7 +117,8 @@ public sealed record SidecarFrame /// /// Decode tier of the file (BRIEF §8.2) as the host that indexed or decoded it last found it; omitted until it is - /// known. and follow it. + /// known. (unless that host reads the frame from its proxy) and + /// follow it. /// [JsonConverter(typeof(TolerantEnumConverter))] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] diff --git a/src/Titano.Data/Archiving/ArchiveImages.cs b/src/Titano.Data/Archiving/ArchiveImages.cs index dd7851e1..15bd65a8 100644 --- a/src/Titano.Data/Archiving/ArchiveImages.cs +++ b/src/Titano.Data/Archiving/ArchiveImages.cs @@ -10,7 +10,9 @@ using Titano.Core.Model; namespace Titano.Data.Archiving; /// Frames processed by one thumbnail or proxy pass over a sequence. -public sealed record ImagePassResult(int Built, int Reused, int Undecodable); +/// Frames of a proxy pass whose originals this host cannot decode and whose proxy it waits for from a +/// client or render node that can (BRIEF §2.3): unlike ones, nothing is wrong with them. +public sealed record ImagePassResult(int Built, int Reused, int Undecodable, int Waiting = 0); /// What became of a proxy sent by a client or render node (). public enum ProxyStoreResult @@ -86,6 +88,15 @@ public sealed class ArchiveImages /// public static bool ServerNeedsProxy(ImageFileFormat format) => NeedsProxy(ServerDecoders, format); + /// + /// Whether the file at (a sequence's proxies/0001.jpg) is the proxy of the frame + /// captured at : its Exif capture time is the frame's, or the frame has none to compare + /// with. Proxies are named by frame index, so one left by a frame renumbered since (frames inserted before it) is + /// another photo's: renders, analyses and the server's manifests and downloads treat it as missing, and the proxy + /// pass removes it (). + /// + public static bool IsProxyOf(string proxy, DateTime? capturedAt) => ReadProxy(proxy, capturedAt).Check is ProxyCheck.Current or ProxyCheck.Unverifiable; + /// /// A proxy of the original at for a server that cannot decode it (): /// at (0 = full size) and , with 's capture @@ -148,7 +159,8 @@ public sealed class ArchiveImages return ProxyStoreResult.Stored; } - /// Resume point inside the sequence (frames before it were done by an earlier run). + /// Resume point inside the sequence (frames before it were done by an earlier run: the + /// sequence's built status counts them as they are now). /// Called with the next frame index whenever the pack has been committed. public async Task BuildThumbnailsAsync(Archive archive, Sequence sequence, bool force, int startFrame, Func checkpoint, CancellationToken cancellationToken = default) @@ -159,6 +171,7 @@ public sealed class ArchiveImages var folder = paths.Sequence(sequence.Folder); var manifest = ManifestStore.ReadSequence(folder.ManifestPath); int built = 0, reused = 0, undecodable = 0; + var complete = true; var tiers = new Dictionary(); var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false); // Thumbnail writers exclude each other only; ingest keeps committing meanwhile. @@ -166,6 +179,12 @@ public sealed class ArchiveImages _writer.Options.LeaseDuration, cancellationToken).ConfigureAwait(false)) using (var pack = ThumbPackWriter.Open(folder.ThumbsPackPath)) { + for (var i = 0; i < startFrame && i < manifest.Frames.Count; i++) + { + var frame = manifest.Frames[i]; + complete &= pack.Entries.GetValueOrDefault(frame.I) is { } entry + && entry.SourceHash.AsSpan().SequenceEqual(ThumbPack.Fingerprint(frame.Sha256, frame.File, frame.Size)); + } var cadence = new Cadence(); for (var i = startFrame; i < manifest.Frames.Count; i++) { @@ -178,9 +197,9 @@ public sealed class ArchiveImages // A frame this host cannot decode shows its proxy once one arrived; until then its original's embedded // preview, whose smaller thumbnail the proxy's replaces. var fromProxy = false; - if (NeedsProxy(frame.Format) && !(current && edge >= FrameImageRenderer.ThumbnailLongEdge) - && ReadProxy(folder.Proxy(frame.I), frame) is { Check: ProxyCheck.Current or ProxyCheck.Unverifiable } proxy) - fromProxy = !current || edge < System.Math.Min(FrameImageRenderer.ThumbnailLongEdge, proxy.LongEdge); + if (NeedsProxy(frame.Format) && !(current && edge >= policy.LongEdge) + && ReadProxy(folder.Proxy(frame.I), frame.CapturedAt) is { Check: ProxyCheck.Current or ProxyCheck.Unverifiable } proxy) + fromProxy = !current || edge < System.Math.Min(policy.LongEdge, proxy.LongEdge); if (current && !fromProxy) { reused++; @@ -209,11 +228,17 @@ public sealed class ArchiveImages pack.Commit(); } await _tiers.RecordAsync(archive, sequence, tiers, cancellationToken).ConfigureAwait(false); - await MarkAsync(archive, paths, sequence, SequenceStatus.ThumbsBuilt, undecodable == 0, cancellationToken).ConfigureAwait(false); + await MarkAsync(archive, paths, sequence, SequenceStatus.ThumbsBuilt, complete && undecodable == 0, cancellationToken).ConfigureAwait(false); return new ImagePassResult(built, reused, undecodable); } /// + /// + /// is set only when every frame has its proxy: a server asks clients and + /// render nodes for none while it is set (BRIEF §2.3), so a pass resumed at checks the + /// proxies of the frames an earlier run did again (their headers only). The flag + /// of frames this host reads through their proxies follows whether they have one (). + /// public async Task BuildProxiesAsync(Archive archive, Sequence sequence, bool force, int startFrame, Func checkpoint, CancellationToken cancellationToken = default) { @@ -224,33 +249,41 @@ public sealed class ArchiveImages var folder = paths.Sequence(sequence.Folder); var manifest = ManifestStore.ReadSequence(folder.ManifestPath); Directory.CreateDirectory(folder.Proxies); - int built = 0, reused = 0, undecodable = 0; + int built = 0, reused = 0, undecodable = 0, waiting = 0; + var complete = true; var tiers = new Dictionary(); + var proxied = new Dictionary(); var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false); var cadence = new Cadence(); - for (var i = startFrame; i < manifest.Frames.Count; i++) + for (var i = 0; i < manifest.Frames.Count; i++) { cancellationToken.ThrowIfCancellationRequested(); var frame = manifest.Frames[i]; var path = folder.Proxy(frame.I); + var done = i < startFrame; if (NeedsProxy(frame.Format)) { // Only a client or render node that decodes the original makes this proxy: a preview would make one the // size of a thumbnail here. One left from a frame renumbered since goes, so it is asked for again, and so // does one smaller than the policy's (made here from a preview by an earlier version). - var (check, longEdge) = ReadProxy(path, frame); + var (check, longEdge) = ReadProxy(path, frame.CapturedAt); if (check is ProxyCheck.Current or ProxyCheck.Unverifiable && TooSmall(longEdge, frame.W, frame.H, policy)) check = ProxyCheck.Stale; - if (check is ProxyCheck.Current or ProxyCheck.Unverifiable) - { - reused++; - } - else - { - if (check == ProxyCheck.Stale) File.Delete(path); - undecodable++; - } + var usable = check is ProxyCheck.Current or ProxyCheck.Unverifiable; + if (check == ProxyCheck.Stale) File.Delete(path); + proxied[frame.Id] = usable; + complete &= usable; + if (done) continue; + if (usable) reused++; + else if ((frame.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) != 0) undecodable++; + else waiting++; } - else if (!force && ReadProxy(path, frame).Check == ProxyCheck.Current) + else if (done) + { + // Made by the earlier run, unless the frame did not decode then. + complete &= ReadProxy(path, frame.CapturedAt).Check is ProxyCheck.Current or ProxyCheck.Unverifiable; + continue; + } + else if (!force && ReadProxy(path, frame.CapturedAt).Check == ProxyCheck.Current) { reused++; continue; @@ -264,12 +297,13 @@ public sealed class ArchiveImages else { undecodable++; + complete = false; } if (cadence.Due()) await checkpoint(i + 1, manifest.Frames.Count).ConfigureAwait(false); } - await _tiers.RecordAsync(archive, sequence, tiers, cancellationToken).ConfigureAwait(false); - await MarkAsync(archive, paths, sequence, SequenceStatus.ProxiesBuilt, undecodable == 0, cancellationToken).ConfigureAwait(false); - return new ImagePassResult(built, reused, undecodable); + await _tiers.RecordAsync(archive, sequence, tiers, proxied, cancellationToken).ConfigureAwait(false); + await MarkAsync(archive, paths, sequence, SequenceStatus.ProxiesBuilt, complete, cancellationToken).ConfigureAwait(false); + return new ImagePassResult(built, reused, undecodable, waiting); } /// Where a frame's proxy stands; only a capture time tells whether it is the frame's. @@ -287,15 +321,15 @@ public sealed class ArchiveImages Stale, } - /// Where the proxy at stands for , and its long edge. - private static (ProxyCheck Check, int LongEdge) ReadProxy(string proxy, SidecarFrame frame) + /// Where the proxy at stands for the frame captured at , and its long edge. + private static (ProxyCheck Check, int LongEdge) ReadProxy(string proxy, DateTime? capturedAt) { if (!File.Exists(proxy)) return (ProxyCheck.Missing, 0); try { var metadata = MetadataReader.Read(proxy); var (width, height) = metadata.OrientedSize; - var check = frame.CapturedAt is null ? ProxyCheck.Unverifiable : metadata.CapturedAt == frame.CapturedAt ? ProxyCheck.Current : ProxyCheck.Stale; + var check = capturedAt is null ? ProxyCheck.Unverifiable : metadata.CapturedAt == capturedAt ? ProxyCheck.Current : ProxyCheck.Stale; return (check, System.Math.Max(width, height)); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) diff --git a/src/Titano.Data/Archiving/FrameTiers.cs b/src/Titano.Data/Archiving/FrameTiers.cs index 8b7ae94d..e31649e3 100644 --- a/src/Titano.Data/Archiving/FrameTiers.cs +++ b/src/Titano.Data/Archiving/FrameTiers.cs @@ -12,7 +12,9 @@ namespace Titano.Data.Archiving; /// shows a warning at once; its next light analysis lists them. Tiers describe the host that probed or decoded the files /// last. Only a file in a format this host decodes natively is flagged undecodable (it is damaged); one beyond this /// host's decoders (HEIF or an unsupported RAW without a preview on the server) keeps its tier but no flag, so render -/// nodes and proxies can still provide its pixels, and a host that decodes a flagged frame clears the flag. +/// nodes and proxies can still provide its pixels, and a host that decodes a flagged frame clears the flag. A frame whose +/// original this host reads through its proxy () is preview-only +/// only while it has none: with its proxy it is not rendered from the embedded preview, so it caps nothing. /// internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, ArchiveIndexer indexer) { @@ -49,15 +51,22 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi return tiers; } + /// + public Task> RecordAsync(Archive archive, Sequence sequence, IReadOnlyDictionary tiers, + CancellationToken cancellationToken) => RecordAsync(archive, sequence, tiers, null, cancellationToken); + /// /// Records (by frame id) where they change what the sidecar says, and returns the frames that /// changed as they are now. Frames no longer in the sequence are ignored. /// + /// By frame id, for frames whose original this host reads through its proxy, whether the frame's + /// own proxy is there now (); their follows + /// it even where no tier is recorded. /// The archive is offline or read-only. public async Task> RecordAsync(Archive archive, Sequence sequence, IReadOnlyDictionary tiers, - CancellationToken cancellationToken) + IReadOnlyDictionary? proxied, CancellationToken cancellationToken) { - if (tiers.Count == 0) return []; + if (tiers.Count == 0 && proxied is not { Count: > 0 }) return []; var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); var changed = new List(); @@ -70,8 +79,11 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi { var frame = m.Frames[i]; frames[i] = frame; - if (!tiers.TryGetValue(frame.Id, out var tier)) continue; - var updated = SequenceFrames.WithTier(frame, tier, DecodesNatively(frame.Format)); + bool? hasProxy = proxied is not null && proxied.TryGetValue(frame.Id, out var found) ? found : null; + SidecarFrame updated; + if (tiers.TryGetValue(frame.Id, out var tier)) updated = SequenceFrames.WithTier(frame, tier, DecodesNatively(frame.Format), hasProxy == true); + else if (hasProxy is { } proxy) updated = SequenceFrames.WithProxy(frame, proxy); + else continue; if (updated.Tier == frame.Tier && updated.Flags == frame.Flags && updated.PreviewW == frame.PreviewW && updated.PreviewH == frame.PreviewH) continue; gained |= (updated.Flags & ~frame.Flags & FrameFlags.Undecodable) != 0; diff --git a/src/Titano.Data/Archiving/Jobs/ImageJobHandler.cs b/src/Titano.Data/Archiving/Jobs/ImageJobHandler.cs index 5d6af4b5..32538a60 100644 --- a/src/Titano.Data/Archiving/Jobs/ImageJobHandler.cs +++ b/src/Titano.Data/Archiving/Jobs/ImageJobHandler.cs @@ -34,7 +34,9 @@ internal sealed class ImageJobHandler(TitanoDatabase db, ArchiveImages images, J var result = kind == JobKind.Thumbs ? await images.BuildThumbnailsAsync(archive, sequence, force, start, Checkpoint, context.Cancellation).ConfigureAwait(false) : await images.BuildProxiesAsync(archive, sequence, force, start, Checkpoint, context.Cancellation).ConfigureAwait(false); - context.Log(Loc.L("Archive.Log.ImagesDone", sequence.DisplayTitle, result.Built, result.Reused, result.Undecodable)); + context.Log(kind == JobKind.Thumbs + ? Loc.L("Archive.Log.ImagesDone", sequence.DisplayTitle, result.Built, result.Reused, result.Undecodable) + : Loc.L("Archive.Log.ProxiesDone", sequence.DisplayTitle, result.Built, result.Reused, result.Undecodable, result.Waiting)); await context.CheckpointAsync(string.Create(CultureInfo.InvariantCulture, $"{s + 1}:0"), s + 1, targets.Count).ConfigureAwait(false); } } diff --git a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs index d58f5b2b..c155ef6b 100644 --- a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs +++ b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs @@ -67,7 +67,7 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, options.Archive.Decoder, cancellation).ConfigureAwait(false); var companions = await GprCompanions.FindAsync(db, archive, frames.Frames, cancellation).ConfigureAwait(false); IReadOnlyList outcomes; - await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options, originals: !parameters.UseProxies, + await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames), options, originals: !parameters.UseProxies, companions)) { outcomes = await render.RenderAllAsync( diff --git a/src/Titano.Data/Processing/SequenceAnalyser.cs b/src/Titano.Data/Processing/SequenceAnalyser.cs index b89a9fd9..5f225ceb 100644 --- a/src/Titano.Data/Processing/SequenceAnalyser.cs +++ b/src/Titano.Data/Processing/SequenceAnalyser.cs @@ -45,21 +45,23 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti ArchiveVolume.EnsureWritable(paths); var folder = paths.Sequence(sequence.Folder); var frames = await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, options.Archive.Decoder, cancellationToken).ConfigureAwait(false); - // Format tiers (BRIEF §10.1): frames indexed since the last analysis are probed with this host's decoders. + // Format tiers (BRIEF §10.1): frames indexed since the last analysis are probed with this host's decoders. A frame + // read from its proxy is not preview-only whatever its original's tier, so it caps nothing (BRIEF §8.1, §8.2). var probed = await _tiers.ProbeUnknownAsync(archive, folder, ManifestStore.ReadSequence(folder.ManifestPath), cancellationToken).ConfigureAwait(false); - frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, probed, cancellationToken).ConfigureAwait(false)); + var proxied = frames.ProxyStates(options.Archive.Decoder); + frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, probed, proxied, cancellationToken).ConfigureAwait(false)); var metadata = frames.LightFrames(); DeepAnalysisReport? report; if (deep) { (report, var decoded) = await AnalyseDeepAsync(archive, frames, metadata, folder, progress, cancellationToken).ConfigureAwait(false); - frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, decoded, cancellationToken).ConfigureAwait(false)); + frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, decoded, proxied, cancellationToken).ConfigureAwait(false)); metadata = [.. metadata.Select((m, i) => m with { Flags = frames.Frames[i].Flags })]; } else { - report = ReadReport(folder, frames.Count); + report = ReadReport(folder, frames); } var light = LightAnalyzer.Analyze(metadata); var context = SequenceContext.FromFrames(frames.Frames, options.AdvisorCodecs); @@ -85,7 +87,7 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti // then either sees that report or runs before it is recorded, and never clears what it recorded. await using (await writer.LockAsync(paths, sequence.Id, cancellationToken).ConfigureAwait(false)) { - report ??= ReadReport(folder, frames.Count); + report ??= ReadReport(folder, frames); warnings = Advisor.Recommend(light, report, context).Findings.Count(f => f.Severity >= FindingSeverity.Warning); var set = SequenceStatus.AnalyzedLight | (report is null ? SequenceStatus.None : SequenceStatus.AnalyzedDeep) | (warnings > 0 ? SequenceStatus.HasWarnings : SequenceStatus.None); @@ -103,7 +105,25 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti return new AnalysisOutcome(sequence, frames.Count, warnings, changed, unreadable, presets); } - /// The saved deep report when it describes the current frames; null when absent, stale or unreadable. + /// + /// The saved deep report when it describes as this host reads them now: also stale when a + /// frame it found unreadable has been read from its proxy since (one sent for an original this host cannot decode, + /// BRIEF §2.3), so that verdict and the frame's missing measurements no longer hold. + /// + public static DeepAnalysisReport? ReadReport(SequencePaths folder, SequenceFrames frames) + { + var report = ReadReport(folder, frames.Count); + if (report is null || frames.Proxied is not { } proxied) return report; + HashSet measured = report.ProxyFrames is { } read ? [.. read] : []; + foreach (var index in report.Issues.Undecodable) + { + if (index < frames.Count && proxied[index] && !measured.Contains(index) && (frames.Frames[index].Flags & SequenceFrames.NotAnalysed) == 0) + return null; + } + return report; + } + + /// The saved deep report when it covers frames; null when absent, stale or unreadable. public static DeepAnalysisReport? ReadReport(SequencePaths folder, int frameCount) { if (!File.Exists(folder.AnalysisPath)) return null; @@ -179,6 +199,9 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti .ConfigureAwait(false); decoded = Decoded(frames, report, source, companions, cancellationToken); } + // An unreadable verdict on a frame read from its original goes stale once the frame has a proxy (ReadReport). + int[] fromProxies = [.. Enumerable.Range(0, frames.Count).Where(i => frames.Proxied?[i] == true)]; + report = report with { ProxyFrames = fromProxies.Length > 0 ? fromProxies : null }; report.Save(folder.AnalysisPath); file.Delete(); @@ -191,11 +214,17 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti } /// Identity of the analysed content: frames replaced or renumbered since a checkpoint was written (with the - /// same count) make it stale, like changed options do. + /// same count), or now read from their proxies where they were read from their originals (or the other way round), + /// make it stale, like changed options do. private static string ContentKey(SequenceFrames frames) { using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); - foreach (var f in frames.Frames) hash.AppendData(Encoding.UTF8.GetBytes($"{f.Id:N}/{f.Sha256}/{f.Size}/{f.Flags & SequenceFrames.NotAnalysed};")); + for (var i = 0; i < frames.Count; i++) + { + var f = frames.Frames[i]; + var source = frames.Proxied is { } proxied && proxied[i] ? "proxy" : "original"; + hash.AppendData(Encoding.UTF8.GetBytes($"{f.Id:N}/{f.Sha256}/{f.Size}/{f.Flags & SequenceFrames.NotAnalysed}/{source};")); + } return Convert.ToHexStringLower(hash.GetHashAndReset())[..16]; } diff --git a/src/Titano.Data/Processing/SequenceFrames.cs b/src/Titano.Data/Processing/SequenceFrames.cs index 98ea6134..5489cf70 100644 --- a/src/Titano.Data/Processing/SequenceFrames.cs +++ b/src/Titano.Data/Processing/SequenceFrames.cs @@ -34,7 +34,8 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList Fr /// /// Every frame row of with its archive file: the proxy when asked, and whenever the /// original is one cannot decode (: - /// HEIF on the server, BRIEF §2.3, §8.5), as long as the proxy exists; else the original. + /// HEIF on the server, BRIEF §2.3, §8.5), as long as the frame's own proxy is there (: + /// not one left at its index by a frame renumbered since); else the original. /// public static async Task LoadAsync(TitanoDatabase db, Archive archive, Sequence sequence, bool useProxies, ImageDecoder decoder, CancellationToken cancellationToken) @@ -51,7 +52,7 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList Fr if ((frame.Flags & FrameFlags.Missing) != 0) continue; originals[i] = paths.FromRelative(frame.RelativePath); var proxy = useProxies || ArchiveImages.NeedsProxy(decoder, frame.Format) ? folder.Proxy(frame.Index) : null; - proxied[i] = proxy is not null && File.Exists(proxy); + proxied[i] = proxy is not null && ArchiveImages.IsProxyOf(proxy, frame.CapturedAt); files[i] = proxied[i] ? proxy : originals[i]; } return new SequenceFrames(sequence, frames, files) { Proxied = proxied, Originals = originals }; @@ -94,6 +95,22 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList Fr return light; } + /// + /// By frame id, whether each frame whose original cannot decode (it is read from its proxy, + /// ) has its own proxy now: what its follows + /// (). + /// Missing frames are left out. + /// + public Dictionary ProxyStates(ImageDecoder decoder) + { + var states = new Dictionary(); + for (var i = 0; i < Frames.Count; i++) + { + if (Files[i] is not null && ArchiveImages.NeedsProxy(decoder, Frames[i].Format)) states[Frames[i].Id] = Proxied is { } proxied && proxied[i]; + } + return states; + } + /// Positions of the frames a render must skip besides the project's exclusions. public IEnumerable NotRenderable() { diff --git a/src/Titano.Server/Api/FileEndpoints.cs b/src/Titano.Server/Api/FileEndpoints.cs index d7090edb..e1cb76f1 100644 --- a/src/Titano.Server/Api/FileEndpoints.cs +++ b/src/Titano.Server/Api/FileEndpoints.cs @@ -60,8 +60,8 @@ public static class FileEndpoints /// /// 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 ) as the proxy of a frame the server cannot decode - /// (); the sequence's thumbnails and proxy state follow as background jobs. 204; 413 - /// too_large; 422 proxy_not_wanted (the server decodes that original itself, or it is missing or + /// (); the sequence's thumbnails, proxy state and light analysis follow as background jobs. + /// 204; 413 too_large; 422 proxy_not_wanted (the server decodes that original itself, or it is missing or /// damaged), invalid_proxy, proxy_mismatch or proxy_too_small, which concern that proxy only; 409 /// proxies_disabled, which concerns them all; 503 archive_offline. /// diff --git a/src/Titano.Server/Jobs/SequenceJobs.cs b/src/Titano.Server/Jobs/SequenceJobs.cs index d1743b04..197f6480 100644 --- a/src/Titano.Server/Jobs/SequenceJobs.cs +++ b/src/Titano.Server/Jobs/SequenceJobs.cs @@ -5,13 +5,14 @@ namespace Titano.Server.Jobs; /// /// 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 +/// proxy sent by a client or render node arrives, the thumbnails, the sequence's proxy state and the light analysis (a +/// frame read from its proxy is no longer preview-only, so the advisor's Auto values change). Queued at background /// priority once per sequence while one is still waiting, and only for kinds this server can run. /// 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 static readonly JobKind[] AfterProxy = [JobKind.Thumbs, JobKind.Proxies, JobKind.LightAnalyse]; private readonly SemaphoreSlim _gate = new(1, 1); diff --git a/src/Titano.Server/Storage/ArchiveAssetLocator.cs b/src/Titano.Server/Storage/ArchiveAssetLocator.cs index 513babac..559cd105 100644 --- a/src/Titano.Server/Storage/ArchiveAssetLocator.cs +++ b/src/Titano.Server/Storage/ArchiveAssetLocator.cs @@ -1,12 +1,14 @@ using Titano.Core.Archiving; using Titano.Core.Model; +using Titano.Data.Archiving; namespace Titano.Server.Storage; /// /// Proxies are files under the sequence's proxies/ folder; a thumbnail is the JPEG byte range of its entry in /// thumbs.pack. A thumbnail made from a different original (the frame was renumbered or replaced) counts as not -/// built yet, like a missing one. +/// built yet, like a missing one, and so does a proxy whose capture time is another frame's +/// (). /// internal sealed class ArchiveAssetLocator : IFrameAssetLocator { @@ -21,7 +23,7 @@ internal sealed class ArchiveAssetLocator : IFrameAssetLocator private static AssetLocation? Proxy(SequencePaths paths, Frame frame) { var file = new FileInfo(paths.Proxy(frame.Index)); - return file.Exists ? new AssetLocation(file.FullName, 0, file.Length, Jpeg) : null; + return file.Exists && ArchiveImages.IsProxyOf(file.FullName, frame.CapturedAt) ? new AssetLocation(file.FullName, 0, file.Length, Jpeg) : null; } private static AssetLocation? Thumbnail(SequencePaths paths, Frame frame) diff --git a/src/Titano.Server/Storage/ServerProxies.cs b/src/Titano.Server/Storage/ServerProxies.cs index 34e73cc3..9d644853 100644 --- a/src/Titano.Server/Storage/ServerProxies.cs +++ b/src/Titano.Server/Storage/ServerProxies.cs @@ -35,8 +35,9 @@ public static class ServerProxies /// /// Every frame of 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 is null. + /// built them all or one was sent for the frame (: one left at its index by a + /// frame renumbered since is another photo's); it is wanted for a frame the server cannot decode that has none (and is + /// not missing), unless is null. /// public static async Task> FramesAsync(TitanoDatabase db, Archive archive, Sequence sequence, ProxySpec? spec, CancellationToken cancellationToken) @@ -51,7 +52,7 @@ public static class ServerProxies 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 sent = !proxies && CannotDecode(f.Format) && ArchiveImages.IsProxyOf(folder.Proxy(f.Index), f.CapturedAt); 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)); } diff --git a/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs b/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs index 49f548b2..ff8dd3da 100644 --- a/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs +++ b/tests/Titano.Core.Tests/Archiving/DecodeTierTests.cs @@ -56,6 +56,24 @@ public sealed class DecodeTierTests(TestContext context) SequenceFrames.WithTier(none, new FileTier(ImageFileFormat.Cr2, DecodeTier.Unavailable, null, "nothing here", 0, 0), damaged: false).Flags); } + [Test] + public void AFrameReadFromItsProxyKeepsItsPreviewTierButIsNotPreviewOnly() + { + // A HEIF on the server: its decoders find only the Exif thumbnail, but renders read the proxy a client sent. + var heif = Sidecar() with { File = "originals/IMG_0001.HEIC", Format = ImageFileFormat.Heif, Flags = FrameFlags.Duplicate }; + var thumbnail = new FileTier(ImageFileFormat.Heif, DecodeTier.EmbeddedPreview, "managed-preview", "no managed decoder for Heif", 160, 120); + var proxied = SequenceFrames.WithTier(heif, thumbnail, damaged: false, proxied: true); + Assert.Equal((DecodeTier.EmbeddedPreview, FrameFlags.Duplicate, (int?)160, (int?)120), (proxied.Tier, proxied.Flags, proxied.PreviewW, proxied.PreviewH)); + + // The flag follows the proxy, the tier and preview size stay the original's. + var gone = SequenceFrames.WithProxy(proxied, proxied: false); + Assert.Equal((DecodeTier.EmbeddedPreview, FrameFlags.Duplicate | FrameFlags.PreviewOnly, (int?)160), (gone.Tier, gone.Flags, gone.PreviewW)); + Assert.Equal(FrameFlags.Duplicate, SequenceFrames.WithProxy(gone, proxied: true).Flags); + // Other tiers have no preview-only flag to follow. + var unavailable = SequenceFrames.WithTier(heif, new FileTier(ImageFileFormat.Heif, DecodeTier.Unavailable, null, "nothing", 0, 0), damaged: false); + Assert.Same(unavailable, SequenceFrames.WithProxy(unavailable, proxied: false)); + } + [Test] public void SidecarsKeepTheTierAndThePreviewSizeAndOlderOnesReadAsUnknown() { diff --git a/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs b/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs index 1a50f5d3..4c563d54 100644 --- a/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ArchiveJobTests.cs @@ -79,13 +79,28 @@ public sealed class ArchiveJobTests(TestContext context) Assert.NotNull(pack.Read(4)); } Assert.SequenceEqual(["0004.jpg", "0005.jpg", "0006.jpg"], Directory.GetFiles(sequence.Proxies).Select(Path.GetFileName).Order()); + // The resumed passes saw every frame after the resume point done, but the frames before it have nothing: the + // sequence has neither all its thumbnails nor all its proxies (a server would ask for none of the missing ones). var status = (await fx.Db.Sequences.GetAsync(a.Id))!.Status; - Assert.True(status.HasFlag(SequenceStatus.ThumbsBuilt) && status.HasFlag(SequenceStatus.ProxiesBuilt)); + Assert.False(status.HasFlag(SequenceStatus.ThumbsBuilt) || status.HasFlag(SequenceStatus.ProxiesBuilt), status.ToString()); // A full pass reuses what is current and builds the rest. var all = await engine.EnqueueAsync(ArchiveJobs.Thumbnails([a.Id]), cancellationToken: fx.Ct); var done = await fx.WaitForAsync(all.Id, JobState.Done); Assert.Contains("made 3, reused 3", done.LogTail); + var allProxies = await engine.EnqueueAsync(ArchiveJobs.Proxies([a.Id]), cancellationToken: fx.Ct); + Assert.Contains("made 3, reused 3, unreadable 0, waiting 0", (await fx.WaitForAsync(allProxies.Id, JobState.Done)).LogTail); + status = (await fx.Db.Sequences.GetAsync(a.Id))!.Status; + Assert.True(status.HasFlag(SequenceStatus.ThumbsBuilt) && status.HasFlag(SequenceStatus.ProxiesBuilt), status.ToString()); + + // Resumed once every frame has them, a pass sets both again. + await fx.Db.Sequences.UpdateStatusAsync(a.Id, SequenceStatus.None, SequenceStatus.ThumbsBuilt | SequenceStatus.ProxiesBuilt, fx.Ct); + var resumedThumbs = await fx.EnqueueWithCheckpointAsync(engine, ArchiveJobs.Thumbnails([a.Id]), "0:3"); + await fx.WaitForAsync(resumedThumbs.Id, JobState.Done); + var resumedProxies = await fx.EnqueueWithCheckpointAsync(engine, ArchiveJobs.Proxies([a.Id]), "0:3"); + await fx.WaitForAsync(resumedProxies.Id, JobState.Done); + status = (await fx.Db.Sequences.GetAsync(a.Id))!.Status; + Assert.True(status.HasFlag(SequenceStatus.ThumbsBuilt) && status.HasFlag(SequenceStatus.ProxiesBuilt), status.ToString()); } [Test] diff --git a/tests/Titano.Data.Tests/Archiving/HeifFile.cs b/tests/Titano.Data.Tests/Archiving/HeifFile.cs new file mode 100644 index 00000000..39c79aab --- /dev/null +++ b/tests/Titano.Data.Tests/Archiving/HeifFile.cs @@ -0,0 +1,90 @@ +using System.Buffers.Binary; +using System.Text; +using Titano.Core.Codecs.Jpeg; +using Titano.Core.Imaging; +using Titano.Core.Metadata; + +namespace Titano.Data.Tests.Archiving; + +/// +/// HEIF files as phones and cameras write them, as far as a host without a HEVC decoder reads them (the server, BRIEF +/// §8.1): the primary image's size (ispe) and an Exif item with the capture time and, in IFD1, a small JPEG +/// thumbnail, the only pixels such a host finds in the file. +/// +internal static class HeifFile +{ + /// A × HEIF captured at , its Exif + /// thumbnail × . + public static byte[] Create(DateTime captured, int width, int height, int thumbnailWidth = 160, int thumbnailHeight = 120) + { + using var small = new ImageBuffer(thumbnailWidth, thumbnailHeight); + for (var c = 0; c < 3; c++) small.Plane(c).Fill(0.2f + c * 0.2f); + var thumbnail = JpegEncoder.Encode(small, new JpegEncodeOptions { Quality = 80 }); + var tiff = WithThumbnail(ExifWriter.BuildTiff(new FrameMetadata { Make = "TestCam", Model = "H-1", CapturedAt = captured }), thumbnail); + byte[] exif = [.. U32(6), .. "Exif\0\0"u8, .. tiff]; + // The HEVC-coded primary image, which no managed decoder reads. + var coded = new byte[64]; + + byte[] Build(int mdat) => + [ + .. Box("ftyp", "heic"u8.ToArray(), U32(0), "mif1heic"u8.ToArray()), + .. FullBox("meta", 0, + FullBox("hdlr", 0, U32(0), "pict"u8.ToArray(), new byte[13]), + FullBox("pitm", 0, U16(1)), + FullBox("iinf", 0, U16(2), FullBox("infe", 2, U16(1), U16(0), "hvc1\0"u8.ToArray()), FullBox("infe", 2, U16(2), U16(0), "Exif\0"u8.ToArray())), + FullBox("iloc", 1, [0x44, 0x00], U16(2), + U16(1), U16(0), U16(0), U16(1), U32(mdat), U32(coded.Length), + U16(2), U16(0), U16(0), U16(1), U32(mdat + coded.Length), U32(exif.Length)), + Box("iprp", Box("ipco", FullBox("ispe", 0, U32(width), U32(height))), FullBox("ipma", 0, U32(1), U16(1), [1, 0x81]))), + ]; + + var head = Build(0); + head = Build(head.Length + 8); + return [.. head, .. Box("mdat", coded, exif)]; + } + + /// (a single IFD0 chain, as writes it) with an IFD1 holding . + private static byte[] WithThumbnail(byte[] tiff, byte[] thumbnail) + { + const int Entries = 3; + var ifd1 = (tiff.Length + 1) & ~1; + var jpeg = ifd1 + 2 + 12 * Entries + 4; + var result = new byte[jpeg + thumbnail.Length]; + tiff.CopyTo(result, 0); + var ifd0Entries = BinaryPrimitives.ReadUInt16LittleEndian(result.AsSpan(8)); + BinaryPrimitives.WriteUInt32LittleEndian(result.AsSpan(8 + 2 + 12 * ifd0Entries), (uint)ifd1); + BinaryPrimitives.WriteUInt16LittleEndian(result.AsSpan(ifd1), Entries); + Entry(result, ifd1 + 2, 0x0103, 3, 6); // Compression: JPEG + Entry(result, ifd1 + 14, 0x0201, 4, (uint)jpeg); // JPEGInterchangeFormat + Entry(result, ifd1 + 26, 0x0202, 4, (uint)thumbnail.Length); // JPEGInterchangeFormatLength + thumbnail.CopyTo(result, jpeg); + return result; + } + + /// A little-endian IFD entry with one value. + private static void Entry(byte[] data, int at, ushort tag, ushort type, uint value) + { + BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(at), tag); + BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(at + 2), type); + BinaryPrimitives.WriteUInt32LittleEndian(data.AsSpan(at + 4), 1); + BinaryPrimitives.WriteUInt32LittleEndian(data.AsSpan(at + 8), value); + } + + private static byte[] Box(string type, params byte[][] payload) => [.. U32(8 + payload.Sum(p => p.Length)), .. Encoding.ASCII.GetBytes(type), .. payload.SelectMany(p => p)]; + + private static byte[] FullBox(string type, byte version, params byte[][] payload) => Box(type, [[version, 0, 0, 0], .. payload]); + + private static byte[] U16(int value) + { + var bytes = new byte[2]; + BinaryPrimitives.WriteUInt16BigEndian(bytes, (ushort)value); + return bytes; + } + + private static byte[] U32(int value) + { + var bytes = new byte[4]; + BinaryPrimitives.WriteUInt32BigEndian(bytes, (uint)value); + return bytes; + } +} diff --git a/tests/Titano.Data.Tests/Archiving/ProxyTests.cs b/tests/Titano.Data.Tests/Archiving/ProxyTests.cs index 0fc493fa..bdb1e577 100644 --- a/tests/Titano.Data.Tests/Archiving/ProxyTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ProxyTests.cs @@ -46,7 +46,7 @@ public sealed class ProxyTests(TestContext context) // The proxy pass keeps what was sent and makes none from the originals here, even when forced. var proxies = await images.BuildProxiesAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); - Assert.Equal((0, 1, 1), (proxies.Built, proxies.Reused, proxies.Undecodable)); + Assert.Equal((0, 1, 0, 1), (proxies.Built, proxies.Reused, proxies.Undecodable, proxies.Waiting)); Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct)); Assert.FileMissing(folder.Proxy(1)); @@ -73,7 +73,7 @@ public sealed class ProxyTests(TestContext context) var proxies = await fx.Service.Images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); - Assert.Equal(2, proxies.Undecodable); + Assert.Equal((0, 2), (proxies.Undecodable, proxies.Waiting)); Assert.FileMissing(folder.Proxy(1)); Assert.False((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt)); } @@ -93,7 +93,7 @@ public sealed class ProxyTests(TestContext context) var proxies = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); - Assert.Equal((0, 0, 2), (proxies.Built, proxies.Reused, proxies.Undecodable)); + Assert.Equal((0, 0, 0, 2), (proxies.Built, proxies.Reused, proxies.Undecodable, proxies.Waiting)); Assert.FileMissing(folder.Proxy(0)); Assert.False((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt), "asked for again"); @@ -102,7 +102,7 @@ public sealed class ProxyTests(TestContext context) Assert.Equal(ProxyStoreResult.TooSmall, images.StoreProxy(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 6, 160, 120))); Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frame, Proxy(frame.CapturedAt!.Value, 7, 1920, 1440))); var again = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); - Assert.Equal((0, 1, 1), (again.Built, again.Reused, again.Undecodable)); + Assert.Equal((0, 1, 0, 1), (again.Built, again.Reused, again.Undecodable, again.Waiting)); Assert.FileExists(folder.Proxy(0)); } @@ -118,6 +118,128 @@ public sealed class ProxyTests(TestContext context) Assert.FileMissing(fx.Paths.Sequence(sequence.Folder).Proxy(0)); } + [Test] + public async Task OnlyAFramesOwnProxyIsReadForIt() + { + await using var fx = await CreateAsync(context); + var (sequence, frames) = await HeifSequenceAsync(fx); + var folder = fx.Paths.Sequence(sequence.Folder); + // Proxies are named by frame index: after a frame was inserted before both, frame 1's place holds frame 0's. + Directory.CreateDirectory(folder.Proxies); + await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[0].CapturedAt!.Value, 1), fx.Ct); + await File.WriteAllBytesAsync(folder.Proxy(1), Proxy(frames[0].CapturedAt!.Value, 2), fx.Ct); + + Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(0), frames[0].CapturedAt)); + Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(1), frames[1].CapturedAt), "another photo's"); + Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(1), null), "a frame without a capture time cannot tell"); + Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(2), frames[1].CapturedAt), "none there"); + + // Renders and analyses read frame 1 from its original, whether the host takes HEIF from proxies or was asked to. + foreach (var useProxies in new[] { false, true }) + { + var loaded = await Titano.Data.Processing.SequenceFrames.LoadAsync(fx.Db, fx.Archive, sequence, useProxies, new ImageDecoder([]), fx.Ct); + Assert.SequenceEqual([true, false], loaded.Proxied!); + Assert.SequenceEqual([folder.Proxy(0), fx.Paths.FromRelative(frames[1].RelativePath)], loaded.Files); + } + } + + [Test] + public async Task AResumedProxyPassSetsTheBuiltStatusOnlyWhenEveryFrameHasItsProxy() + { + await using var fx = await CreateAsync(context); + var (sequence, frames) = await HeifSequenceAsync(fx); + var images = fx.Service.Images; + Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[1], Proxy(frames[1].CapturedAt!.Value, 1))); + + // A pass resumed at frame 1 (paused, retried or restarted) finds that frame done, but frame 0 still waits: the + // status must not tell a server's clients that nothing is wanted. + var resumed = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 1, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.Equal((0, 1, 0, 0), (resumed.Built, resumed.Reused, resumed.Undecodable, resumed.Waiting)); + Assert.False(await ProxiesBuiltAsync(fx, sequence), "frame 0 has no proxy"); + + Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 2))); + await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 1, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.True(await ProxiesBuiltAsync(fx, sequence), "every frame has its proxy"); + } + + [Test] + public async Task AFrameReadFromItsProxyIsNotPreviewOnly() + { + await using var fx = await CreateAsync(context); + var (sequence, frames) = await HeifSequenceAsync(fx); + var folder = fx.Paths.Sequence(sequence.Folder); + var images = fx.Service.Images; + // As a server probes them: its decoders read these HEIF files only through a 160 × 120 Exif thumbnail. + var sidecar = ManifestStore.ReadSequence(folder.ManifestPath); + ManifestStore.WriteSequence(folder.ManifestPath, sidecar with + { + Frames = [.. sidecar.Frames.Select(f => f with { Tier = DecodeTier.EmbeddedPreview, Flags = f.Flags | FrameFlags.PreviewOnly, PreviewW = 160, PreviewH = 120 })], + }); + Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1))); + + // Frame 0 now renders from its proxy, not from the thumbnail: it caps nothing. Its tier stays the original's. + await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + var after = ManifestStore.ReadSequence(folder.ManifestPath).Frames; + Assert.Equal((DecodeTier.EmbeddedPreview, FrameFlags.None, (int?)160), (after[0].Tier, after[0].Flags & FrameFlags.PreviewOnly, after[0].PreviewW)); + Assert.Equal(FrameFlags.PreviewOnly, after[1].Flags & FrameFlags.PreviewOnly); + Assert.Equal(FrameFlags.None, (await fx.Db.Frames.GetAsync(frames[0].Id, fx.Ct))!.Flags & FrameFlags.PreviewOnly); + Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + + // Another photo's proxy in its place is no proxy of frame 0: the pass removes it, and the frame is preview-only again. + await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[1].CapturedAt!.Value, 2), fx.Ct); + await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.FileMissing(folder.Proxy(0)); + Assert.Equal(FrameFlags.PreviewOnly, ManifestStore.ReadSequence(folder.ManifestPath).Frames[0].Flags & FrameFlags.PreviewOnly); + Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + } + + [Test] + public async Task ThumbnailsDrawnFromProxiesFollowTheArchivesThumbnailSize() + { + await using var fx = await CreateAsync(context); + var (sequence, frames) = await HeifSequenceAsync(fx); + var folder = fx.Paths.Sequence(sequence.Folder); + var images = fx.Service.Images; + Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 1, 640, 480))); + Task ThumbnailsAsync(bool force = false) => + images.BuildThumbnailsAsync(fx.Archive, sequence, force, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + (int, int) Entry() + { + using var pack = ThumbPackReader.Open(folder.ThumbsPackPath); + return (pack.Entries[0].Width, pack.Entries[0].Height); + } + + var first = await ThumbnailsAsync(); + Assert.Equal((1, 0, 1), (first.Built, first.Reused, first.Undecodable)); + Assert.Equal((320, 240), Entry()); + + // A larger size in Settings › Archive: the proxy's thumbnail is drawn again at it, once. + SetThumbnailEdge(fx, 480); + var larger = await ThumbnailsAsync(); + Assert.Equal((1, 0), (larger.Built, larger.Reused)); + Assert.Equal((480, 360), Entry()); + var same = await ThumbnailsAsync(); + Assert.Equal((0, 1), (same.Built, same.Reused)); + + // A smaller one: once drawn at it, the thumbnail is reused, not drawn and appended to the pack on every pass. + SetThumbnailEdge(fx, 240); + Assert.Equal(1, (await ThumbnailsAsync(force: true)).Built); + Assert.Equal((240, 180), Entry()); + var length = new FileInfo(folder.ThumbsPackPath).Length; + var again = await ThumbnailsAsync(); + Assert.Equal((0, 1, 1), (again.Built, again.Reused, again.Undecodable)); + Assert.Equal(length, new FileInfo(folder.ThumbsPackPath).Length); + } + + private static void SetThumbnailEdge(ArchiveFixture fx, int longEdge) + { + var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath); + ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { ThumbnailPolicy = manifest.ThumbnailPolicy with { LongEdge = longEdge } }); + } + + private static async Task ProxiesBuiltAsync(ArchiveFixture fx, Sequence sequence) => + (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt); + /// /// Two ingested frames whose originals the archive engine takes for HEIF (its decoders read none), their size in the /// sidecar × when given. @@ -138,6 +260,8 @@ public sealed class ProxyTests(TestContext context) { Frames = [.. sidecar.Frames.Select(f => f with { Format = ImageFileFormat.Heif, W = width ?? f.W, H = height ?? f.H })], }); + await using (var session = await fx.Db.Database.OpenAsync(fx.Ct)) + await session.ExecuteAsync("UPDATE frames SET format = $1 WHERE sequence_id = $2", [(int)ImageFileFormat.Heif, id], fx.Ct); return (sequence, await fx.Db.Frames.ListBySequenceAsync(id)); } diff --git a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs index 0aa05b9f..e6fd04ff 100644 --- a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs @@ -6,15 +6,18 @@ using Titano.Core.Codecs.Dng; using Titano.Core.Codecs.Jpeg; using Titano.Core.Diagnostics.Synthetic; using Titano.Core.Imaging; +using Titano.Core.Imaging.Decoders; using Titano.Core.Import; using Titano.Core.Localization; using Titano.Core.Metadata; using Titano.Core.Model; using Titano.Core.Project; +using Titano.Data.Archiving; using Titano.Data.Archiving.Jobs; using Titano.Data.Processing; using Titano.Data.Processing.Jobs; using Titano.Data.Repositories; +using Titano.Data.Tests.Archiving; using Titano.Testing; namespace Titano.Data.Tests.Processing; @@ -115,6 +118,101 @@ public sealed class AnalysisJobTests(TestContext context) Assert.Equal(written, File.GetLastWriteTimeUtc(folder.ManifestPath)); } + [Test] + public async Task AHeifFrameTheServerReadsFromItsProxyCapsNothing() + { + // The server's decoders (BRIEF §4.2, §8.1): a HEIF gives them only the 160 × 120 JPEG thumbnail of its Exif. + await using var fx = await ProcessingFixture.CreateAsync(context, o => o with { Archive = o.Archive with { Decoder = DecoderFactory.CreateServer() } }); + var directory = Path.Combine(fx.Archive.Paths.Root, "Heif"); + Directory.CreateDirectory(directory); + var start = new DateTime(2026, 9, 28, 18, 0, 0); + for (var i = 0; i < 4; i++) + await File.WriteAllBytesAsync(Path.Combine(directory, $"IMG_{i + 1:0000}.HEIC"), HeifFile.Create(start.AddSeconds(10 * i), 4032, 3024), fx.Ct); + await fx.Archive.ScanAsync(ScanMode.Full); + var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Archive.Id, fx.Ct)).Single(l => l.Folder == "Heif"); + var sequence = (await fx.Db.Sequences.GetAsync(location.Id, fx.Ct))!; + var folder = fx.Folder(sequence); + await fx.StartEngineAsync(); + + // Without proxies the server has nothing better than those thumbnails: preview-only, and the advisor caps the output. + await LightAnalysisAsync(fx, sequence); + var probed = ManifestStore.ReadSequence(folder.ManifestPath).Frames; + Assert.True(probed.All(f => f is { Tier: DecodeTier.EmbeddedPreview, PreviewW: 160, PreviewH: 120 } && f.Flags.HasFlag(FrameFlags.PreviewOnly)), + "preview-only at the thumbnail's size"); + Assert.Equal(4, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + var rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct); + var capped = Advisor.Recommend(LightAnalyzer.Analyze(rows), null, SequenceContext.FromFrames(rows)); + Assert.Equal((AdvisorKeys.ResolutionPreview, "720p-preview"), (capped.Find(AdvisorParameters.Resolution)!.ReasonKey, capped.Find(AdvisorParameters.ExportPreset)!.Value as string)); + Assert.True(capped.Findings.Any(f => f.Key == FindingKeys.PreviewOnly), "preview-only finding"); + + // A client sends their proxies (BRIEF §2.3): the server renders the frames from them, so none is preview-only and + // the advice follows. The tier and preview size stay the originals'. + Directory.CreateDirectory(folder.Proxies); + foreach (var frame in rows) await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value), fx.Ct); + await LightAnalysisAsync(fx, sequence); + var proxied = ManifestStore.ReadSequence(folder.ManifestPath).Frames; + Assert.True(proxied.All(f => f is { Tier: DecodeTier.EmbeddedPreview, PreviewW: 160 } && !f.Flags.HasFlag(FrameFlags.PreviewOnly)), "not preview-only"); + Assert.Equal(0, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + rows = await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct); + Assert.True(rows.All(f => !f.Flags.HasFlag(FrameFlags.PreviewOnly)), "index flags"); + var advice = Advisor.Recommend(LightAnalyzer.Analyze(rows), null, SequenceContext.FromFrames(rows)); + Assert.Equal((AdvisorKeys.ResolutionSource, "4k-hevc"), (advice.Find(AdvisorParameters.Resolution)!.ReasonKey, advice.Find(AdvisorParameters.ExportPreset)!.Value as string)); + Assert.False(advice.Findings.Any(f => f.Key == FindingKeys.PreviewOnly), "no preview-only finding"); + } + + [Test] + public async Task AFrameTheDeepAnalysisCouldNotReadIsReadAgainOnceItsProxyArrives() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(Flickering()); + var folder = fx.Folder(sequence); + // Frame 4 is a HEIF this host reads only from its proxy (BRIEF §2.3); the proxy a client sends shows the photo. + var original = Path.Combine(folder.Folder, ProcessingFixture.FileName(3)); + var photo = await File.ReadAllBytesAsync(original, fx.Ct); + await File.WriteAllBytesAsync(original, [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8], fx.Ct); + var frame = (await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))[3]; + await using (var session = await fx.Db.Database.OpenAsync(fx.Ct)) + await session.ExecuteAsync("UPDATE frames SET format = $1 WHERE id = $2", [(int)ImageFileFormat.Heif, frame.Id], fx.Ct); + var sidecar = ManifestStore.ReadSequence(folder.ManifestPath); + ManifestStore.WriteSequence(folder.ManifestPath, sidecar with + { + Frames = [.. sidecar.Frames.Select(f => f.Id == frame.Id ? f with { Format = ImageFileFormat.Heif } : f)], + }); + fx.Decoder.HoldFrame(25); + await fx.StartEngineAsync(); + + // A deep analysis reads the original, finds no pixels, and is interrupted once its checkpoint holds that verdict. + var job = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); + await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct); + await fx.Engine.CancelAsync(job.Id, fx.Ct); + await fx.WaitForAsync(job.Id, JobState.Cancelled); + + // The proxy arrives meanwhile: the frame is no longer read as the checkpoint read it, so the resumed run starts over. + Directory.CreateDirectory(folder.Proxies); + await File.WriteAllBytesAsync(folder.Proxy(3), photo, fx.Ct); + fx.Decoder.Hold = null; + await fx.Engine.ResumeAsync(job.Id, fx.Ct); + await fx.WaitForAsync(job.Id, JobState.Done); + var report = SequenceAnalyser.ReadReport(folder, sequence.FrameCount)!; + Assert.Empty(report.Issues.Undecodable); + Assert.SequenceEqual([3], report.ProxyFrames!); + + // Without its proxy, a deep analysis lists the frame as unreadable, a sequence warning... + File.Delete(folder.Proxy(3)); + var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); + await fx.WaitForAsync(deep.Id, JobState.Done); + Assert.SequenceEqual([3], SequenceAnalyser.ReadReport(folder, sequence.FrameCount)!.Issues.Undecodable); + var unreadable = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; + Assert.True(unreadable.Status.HasFlag(SequenceStatus.AnalyzedDeep | SequenceStatus.HasWarnings), unreadable.Status.ToString()); + + // ...a verdict that no longer holds once the proxy is there: the next light analysis drops the report and its warning. + await File.WriteAllBytesAsync(folder.Proxy(3), photo, fx.Ct); + await LightAnalysisAsync(fx, sequence); + var row = (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!; + Assert.False(row.Status.HasFlag(SequenceStatus.AnalyzedDeep), "the report is stale"); + Assert.Less(row.WarningCount, unreadable.WarningCount); + } + [Test] public async Task DeepAnalysisFlagsFramesItCannotDecodeAndListsThemOnce() { @@ -386,6 +484,21 @@ public sealed class AnalysisJobTests(TestContext context) foreach (var text in new[] { null, "6", "2;3", "2;x", "a" }) Assert.Equal(0, AnalyseJobHandler.Parse(text, 5).Next); } + private static async Task LightAnalysisAsync(ProcessingFixture fx, Sequence sequence) + { + var job = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id])); + await fx.WaitForAsync(job.Id, JobState.Done); + } + + /// A 320 × 240 JPEG with in its Exif, as a client makes a proxy. + private static byte[] Proxy(DateTime captured) + { + using var image = new ImageBuffer(320, 240); + for (var c = 0; c < 3; c++) image.Plane(c).Fill(0.3f + c * 0.1f); + var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Titano" }); + return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] }); + } + private static async Task> AnalysedAsync(ProcessingFixture fx, params Sequence[] sequences) { var analysed = new List(); diff --git a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs index 6cb2d346..32516306 100644 --- a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs +++ b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs @@ -63,6 +63,28 @@ public sealed class ProcessingModuleTests(TestContext context) Assert.Contains(Drain(events), e => e is SequenceChangedEvent { Change: "settings" } changed && changed.SequenceId == sequenceId); } + [Test] + public async Task AProxyThatArrivesHasTheSequenceAnalysedAgain() + { + await using var server = await TestServer.StartAsync(context, services: Production); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + // A HEIF original the server cannot decode (BRIEF §2.3): what it renders depends on the proxy a client sends. + var upload = await Uploads.UploadAsync(editor, [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8, .. Uploads.Photo(2000, 1)], "IMG_0001.HEIC", + sessionKey: "roof"); + await LightAnalysesAsync(server, upload.SequenceId, 1); + var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId)); + + using var image = new Core.Imaging.ImageBuffer(320, 240); + var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = frame.CapturedAt, Software = "Titano" }); + using var proxy = new ByteArrayContent(JpegEncoder.Encode(image, new JpegEncodeOptions { App1Segments = [exif[4..]] })); + proxy.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg"); + using (var stored = await editor.PutAsync($"/api/v1/frames/{frame.Id}/proxy", proxy)) + await stored.ExpectAsync(HttpStatusCode.NoContent); + + // A frame read from its proxy is no longer preview-only, so the advisor's Auto values are worked out again. + await LightAnalysesAsync(server, upload.SequenceId, 2); + } + [Test] public async Task TheServerRendersManagedFormatsAndLeavesMp4ToRenderNodes() { @@ -196,6 +218,19 @@ public sealed class ProcessingModuleTests(TestContext context) return drained; } + /// Waits until light analyses of the sequence have finished. + private static async Task LightAnalysesAsync(TestServer server, Guid sequenceId, int count) + { + var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); + while (true) + { + var done = (await server.Database.Jobs.ListAsync(JobState.Done, JobKind.LightAnalyse, 200)).Items.Count(j => j.TargetIds.Contains(sequenceId)); + if (done >= count) return; + Assert.True(DateTime.UtcNow < until, $"light analyses finished: {done} of {count}"); + await Task.Delay(100); + } + } + /// Uploads a synthetic scene with Exif into one new sequence; returns its id. private static async Task UploadSceneAsync(HttpClient editor) { diff --git a/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs b/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs index 2e0811ba..dc603b66 100644 --- a/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs +++ b/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs @@ -62,6 +62,31 @@ public sealed class ProxyUploadTests(TestContext context) } } + [Test] + public async Task AnotherPhotosProxyInTheFramesPlaceIsAskedForAgainAndNeverServed() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var upload = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof"); + var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId)); + var sequence = Assert.NotNull(await server.Database.Sequences.GetAsync(upload.SequenceId)); + // Proxies are named by frame index: after a frame was inserted before this one, its place holds another photo's. + var path = new Core.Archiving.ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder).Proxy(frame.Index); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + await File.WriteAllBytesAsync(path, Proxy(frame.CapturedAt!.Value.AddSeconds(-10))); + + Assert.Equal((true, false), Wanted((await ManifestAsync(editor, upload.SequenceId)).Frames.Single())); + using (var served = await editor.GetAsync($"/api/v1/frames/{frame.Id}/proxy")) + Assert.Equal("asset_missing", (await served.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details); + + // The frame's own proxy takes its place. + var proxy = Proxy(frame.CapturedAt!.Value); + using (var stored = await PutAsync(editor, frame.Id, proxy)) + await stored.ExpectAsync(HttpStatusCode.NoContent); + Assert.Equal((false, true), Wanted((await ManifestAsync(editor, upload.SequenceId)).Frames.Single())); + await Http.BytesEqualAsync(proxy, editor.GetByteArrayAsync($"/api/v1/frames/{frame.Id}/proxy")); + } + [Test] public async Task FramesTheServerDecodesAskForNoProxy() { @@ -87,6 +112,8 @@ public sealed class ProxyUploadTests(TestContext context) return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] }); } + private static (bool WantsProxy, bool HasProxy) Wanted(ManifestFrame frame) => (frame.WantsProxy, frame.HasProxy); + private static Task PutAsync(HttpClient client, Guid frameId, byte[] body) { var content = new ByteArrayContent(body); From 4014b0e669026cf0f5476dca2881a3f9a95c3f7f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 16:52:30 +0000 Subject: [PATCH 2/2] fix(proxies): judge proxies by one rule and record tiers and reports as they stand - ArchiveImages.IsProxyOf compares capture times to the microsecond the index keeps (sidecars and Exif keep 100 ns ticks), and for frames read through proxies also applies the policy's size rule, so the proxy pass, uploads, thumbnails, renders, analyses, tiers, manifests and downloads agree. - Proxy headers are cached per file while length and write time are unchanged (and the file has settled), so manifests cost a stat per proxy. - FrameTiers.RecordAsync checks proxy presence when it writes the sidecar and returns the sequence's frames as they are, so a deep analysis or render that ran while proxies arrived no longer flags the frames preview-only again, and the analysis advises from the current flags. - Server manifests ignore ProxiesBuilt for frames the server cannot decode, so frames added since the last proxy pass are asked for their proxies. - GET /sequences/{id}/analysis/report answers 404 for a report that found frames unreadable before their proxies arrived (DeepReports.ReadCurrentAsync). - The deep checkpoint key marks only frames read from proxies, so checkpoints written by earlier versions still resume. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- docs/ARCHITECTURE.md | 2 +- src/Titano.Data/Archiving/ArchiveImages.cs | 120 +++++++++++++--- src/Titano.Data/Archiving/FrameTiers.cs | 39 +++--- src/Titano.Data/Processing/DeepReports.cs | 28 ++++ .../Processing/Jobs/RenderJobHandler.cs | 5 +- .../Processing/SequenceAnalyser.cs | 26 ++-- src/Titano.Data/Processing/SequenceFrames.cs | 26 +--- src/Titano.Server/Api/SequenceEndpoints.cs | 16 ++- .../Storage/ArchiveAssetLocator.cs | 14 +- src/Titano.Server/Storage/ServerProxies.cs | 25 +++- .../Titano.Data.Tests/Archiving/ProxyTests.cs | 129 +++++++++++++++++- .../Processing/AnalysisJobTests.cs | 77 ++++++++++- .../Processing/ProcessingFixture.cs | 16 ++- .../Api/RemoteEditingTests.cs | 6 +- tests/Titano.Server.Tests/Reports.cs | 20 +++ .../Storage/ProxyUploadTests.cs | 64 +++++++++ 16 files changed, 514 insertions(+), 99 deletions(-) create mode 100644 src/Titano.Data/Processing/DeepReports.cs create mode 100644 tests/Titano.Server.Tests/Reports.cs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index cfdc756a..c682ab03 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -57,7 +57,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | `Repositories` | `TitanoDatabase` facade: archives, sequences, frames, tags, renders, jobs, cards, users, tokens, uploads, render nodes, settings, audit; `DatabaseBackups` (daily `VACUUM INTO` copies of an SQLite index kept ×3, restore staged for the next start and carried out before the index opens, so it also recovers one that no longer opens) | | `Jobs` | `JobEngine` (its number of workers can change while it runs), `IJobHandler`, `IJobHandlerProvider`, `JobContext` (progress, checkpoints, follow-up jobs) | | `Archiving` | `ArchiveService` (create/adopt/open, archive settings written to `archive.json` then the index), `ArchiveIndexer` (scan, rebuild), `ArchiveIngest` (uploads and imports), `ImportService`, `ArchiveVerifier`, `ArchiveImages`, `FrameTiers` (decode tier, `PreviewOnly`/`Undecodable` flags and preview size of frames, written to sidecar and index by the light analysis, deep analysis, renders and image passes), `GprCompanions` (the JPEG of each GoPro GPR, found in the index when an import kept it in another sequence), `ArchiveMaintenance`, `MassRenamer`, `SequenceTrash` (delete into the archive's hidden trash with a dry run, restore; the folder moves back when the index step fails), job handlers | -| `Processing` | `ProcessingService`, `SequenceAnalyser`, `SequenceRender`, `ProjectStore`, `ProjectBundles` (a `.titano` file for a sequence, and the sequence a file names), `RenderNodeClient`, job handlers | +| `Processing` | `ProcessingService`, `SequenceAnalyser`, `DeepReports` (a saved deep report while it still describes the frames), `SequenceRender`, `ProjectStore`, `ProjectBundles` (a `.titano` file for a sequence, and the sequence a file names), `RenderNodeClient`, job handlers | | `Sync` | `SyncService` (sync table of one server: compare, link by content, settle conflicts, enqueue transfers), `SyncRules` (status, settings merge and differences, no I/O), `SyncLinkStore` (`sync_links`, `sync_state`), `SyncTransfers` and `SyncJobHandler` (resumable, hash-verified uploads and downloads as jobs; downloads commit through the archive ingest), `SyncAgent` (background comparison, server events, backoff, offline proxies), `ProxyCache` and `RemoteSequence` (server sequences opened with proxies), `ISyncRemote` (the server calls; `TitanoSyncRemote` over `TitanoClient`) | ### 2.3 Titano.App areas diff --git a/src/Titano.Data/Archiving/ArchiveImages.cs b/src/Titano.Data/Archiving/ArchiveImages.cs index 15bd65a8..adee216a 100644 --- a/src/Titano.Data/Archiving/ArchiveImages.cs +++ b/src/Titano.Data/Archiving/ArchiveImages.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Diagnostics; using Titano.Core.Archiving; using Titano.Core.Codecs.Jpeg; @@ -47,9 +48,26 @@ public sealed class ArchiveImages private const int CommitFrames = 200; private const int ProbeLongEdge = 64; + + /// Proxy headers kept at most (): some 30 MB, the proxies of a few large sequences. + private const int MaxHeaders = 1 << 17; + private static readonly TimeSpan CommitInterval = TimeSpan.FromSeconds(10); + + /// + /// Age of a proxy file under which its header is not kept: a file system's clock may give a file written again at + /// once (with the same length) the same write time, at a granularity of up to two seconds. + /// + private static readonly TimeSpan Settling = TimeSpan.FromSeconds(3); + private static readonly ImageDecoder ServerDecoders = DecoderFactory.CreateServer(); + /// + /// The capture time and size of proxies read lately, by path, while the file keeps its length and write time: the + /// server's manifests and tier records check every proxy of a sequence, so an unchanged one costs a stat. + /// + private static readonly ConcurrentDictionary Headers = new(StringComparer.Ordinal); + private readonly TitanoDatabase _db; private readonly SequenceWriter _writer; private readonly ArchiveIndexer _indexer; @@ -90,12 +108,18 @@ public sealed class ArchiveImages /// /// Whether the file at (a sequence's proxies/0001.jpg) is the proxy of the frame - /// captured at : its Exif capture time is the frame's, or the frame has none to compare - /// with. Proxies are named by frame index, so one left by a frame renumbered since (frames inserted before it) is - /// another photo's: renders, analyses and the server's manifests and downloads treat it as missing, and the proxy - /// pass removes it (). + /// captured at , of × : its Exif capture + /// time is the frame's to the microsecond (the index's precision), or the frame has none to compare with; and, for a + /// frame whose original this host takes from its proxy ( given: the archive's), it is not + /// smaller than the policy's (: one an earlier version made from an embedded preview). Proxies + /// are named by frame index, so one left by a frame renumbered since (frames inserted before it) is another photo's. + /// Renders, analyses, decode tiers and the server's manifests and downloads treat a proxy that fails this as missing, + /// and the proxy pass removes it (). /// - public static bool IsProxyOf(string proxy, DateTime? capturedAt) => ReadProxy(proxy, capturedAt).Check is ProxyCheck.Current or ProxyCheck.Unverifiable; + /// The archive's proxy policy for a frame whose original this host cannot decode (its proxy comes + /// from a client or render node); null for one this host decodes and makes the proxy of itself. + public static bool IsProxyOf(string proxy, DateTime? capturedAt, int width, int height, ProxyPolicy? policy) => + Check(proxy, capturedAt, width, height, policy).Check is ProxyCheck.Current or ProxyCheck.Unverifiable; /// /// A proxy of the original at for a server that cannot decode it (): @@ -151,11 +175,13 @@ public sealed class ArchiveImages if (frame.CapturedAt is { } captured) { using var stream = new MemoryStream(jpeg, writable: false); - if (MetadataReader.Read(stream, "proxy.jpg").CapturedAt != captured) return ProxyStoreResult.WrongFrame; + if (!SameCapture(MetadataReader.Read(stream, "proxy.jpg").CapturedAt, captured)) return ProxyStoreResult.WrongFrame; } var folder = paths.Sequence(sequence.Folder); Directory.CreateDirectory(folder.Proxies); - AtomicFile.WriteAllBytes(folder.Proxy(frame.Index), jpeg); + var path = folder.Proxy(frame.Index); + AtomicFile.WriteAllBytes(path, jpeg); + Headers.TryRemove(path, out _); return ProxyStoreResult.Stored; } @@ -167,7 +193,8 @@ public sealed class ArchiveImages { var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); - var policy = ManifestStore.ReadArchive(paths.ManifestPath).ThumbnailPolicy; + var archiveManifest = ManifestStore.ReadArchive(paths.ManifestPath); + var policy = archiveManifest.ThumbnailPolicy; var folder = paths.Sequence(sequence.Folder); var manifest = ManifestStore.ReadSequence(folder.ManifestPath); int built = 0, reused = 0, undecodable = 0; @@ -198,7 +225,7 @@ public sealed class ArchiveImages // preview, whose smaller thumbnail the proxy's replaces. var fromProxy = false; if (NeedsProxy(frame.Format) && !(current && edge >= policy.LongEdge) - && ReadProxy(folder.Proxy(frame.I), frame.CapturedAt) is { Check: ProxyCheck.Current or ProxyCheck.Unverifiable } proxy) + && Check(folder.Proxy(frame.I), frame.CapturedAt, frame.W, frame.H, archiveManifest.ProxyPolicy) is { Check: ProxyCheck.Current or ProxyCheck.Unverifiable } proxy) fromProxy = !current || edge < System.Math.Min(policy.LongEdge, proxy.LongEdge); if (current && !fromProxy) { @@ -227,7 +254,7 @@ public sealed class ArchiveImages } pack.Commit(); } - await _tiers.RecordAsync(archive, sequence, tiers, cancellationToken).ConfigureAwait(false); + await _tiers.RecordAsync(archive, sequence, tiers, proxies: false, cancellationToken).ConfigureAwait(false); await MarkAsync(archive, paths, sequence, SequenceStatus.ThumbsBuilt, complete && undecodable == 0, cancellationToken).ConfigureAwait(false); return new ImagePassResult(built, reused, undecodable); } @@ -252,7 +279,6 @@ public sealed class ArchiveImages int built = 0, reused = 0, undecodable = 0, waiting = 0; var complete = true; var tiers = new Dictionary(); - var proxied = new Dictionary(); var companions = await GprCompanions.FindAsync(_db, archive, manifest.Frames, cancellationToken).ConfigureAwait(false); var cadence = new Cadence(); for (var i = 0; i < manifest.Frames.Count; i++) @@ -266,11 +292,9 @@ public sealed class ArchiveImages // Only a client or render node that decodes the original makes this proxy: a preview would make one the // size of a thumbnail here. One left from a frame renumbered since goes, so it is asked for again, and so // does one smaller than the policy's (made here from a preview by an earlier version). - var (check, longEdge) = ReadProxy(path, frame.CapturedAt); - if (check is ProxyCheck.Current or ProxyCheck.Unverifiable && TooSmall(longEdge, frame.W, frame.H, policy)) check = ProxyCheck.Stale; + var check = Check(path, frame.CapturedAt, frame.W, frame.H, policy).Check; var usable = check is ProxyCheck.Current or ProxyCheck.Unverifiable; - if (check == ProxyCheck.Stale) File.Delete(path); - proxied[frame.Id] = usable; + if (check == ProxyCheck.Stale) Delete(path); complete &= usable; if (done) continue; if (usable) reused++; @@ -292,6 +316,7 @@ public sealed class ArchiveImages () => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken, companions.GetValueOrDefault(frame.Id))) is { } image) { AtomicFile.WriteAllBytes(path, image.Jpeg); + Headers.TryRemove(path, out _); built++; } else @@ -301,7 +326,7 @@ public sealed class ArchiveImages } if (cadence.Due()) await checkpoint(i + 1, manifest.Frames.Count).ConfigureAwait(false); } - await _tiers.RecordAsync(archive, sequence, tiers, proxied, cancellationToken).ConfigureAwait(false); + await _tiers.RecordAsync(archive, sequence, tiers, proxies: true, cancellationToken).ConfigureAwait(false); await MarkAsync(archive, paths, sequence, SequenceStatus.ProxiesBuilt, complete, cancellationToken).ConfigureAwait(false); return new ImagePassResult(built, reused, undecodable, waiting); } @@ -317,27 +342,80 @@ public sealed class ArchiveImages /// The frame has no capture time to compare with. Unverifiable, - /// Made from another frame (frames were renumbered since). + /// Made from another frame (frames were renumbered since), or too small (). Stale, } + /// A proxy's Exif capture time and oriented long edge, read when the file had this length and write time. + private readonly record struct ProxyHeader(long Length, long WriteTicks, DateTime? CapturedAt, int LongEdge); + + /// + /// Where the proxy at stands for the frame captured at , of + /// × , and its long edge: also when + /// is given and the proxy is for it (). + /// + private static (ProxyCheck Check, int LongEdge) Check(string proxy, DateTime? capturedAt, int width, int height, ProxyPolicy? policy) + { + var (check, longEdge) = ReadProxy(proxy, capturedAt); + if (check is ProxyCheck.Current or ProxyCheck.Unverifiable && policy is not null && TooSmall(longEdge, width, height, policy)) check = ProxyCheck.Stale; + return (check, longEdge); + } + /// Where the proxy at stands for the frame captured at , and its long edge. private static (ProxyCheck Check, int LongEdge) ReadProxy(string proxy, DateTime? capturedAt) { - if (!File.Exists(proxy)) return (ProxyCheck.Missing, 0); + if (ReadHeader(proxy) is not { } header) return (ProxyCheck.Missing, 0); + var check = capturedAt is not { } captured ? ProxyCheck.Unverifiable : SameCapture(header.CapturedAt, captured) ? ProxyCheck.Current : ProxyCheck.Stale; + return (check, header.LongEdge); + } + + /// + /// The header of the proxy at ; null when there is none or it cannot be read. Kept in + /// while the file's length and write time stay as they were when it was read (taken first, so + /// a file replaced meanwhile is read again next time), once the file is older than ; a full + /// cache starts over. + /// + private static ProxyHeader? ReadHeader(string proxy) + { try { + var file = new FileInfo(proxy); + if (!file.Exists) return null; + var (length, written) = (file.Length, file.LastWriteTimeUtc); + if (Headers.TryGetValue(proxy, out var cached) && cached.Length == length && cached.WriteTicks == written.Ticks) return cached; var metadata = MetadataReader.Read(proxy); var (width, height) = metadata.OrientedSize; - var check = capturedAt is null ? ProxyCheck.Unverifiable : metadata.CapturedAt == capturedAt ? ProxyCheck.Current : ProxyCheck.Stale; - return (check, System.Math.Max(width, height)); + var header = new ProxyHeader(length, written.Ticks, metadata.CapturedAt, System.Math.Max(width, height)); + if (DateTime.UtcNow - written < Settling) + { + Headers.TryRemove(proxy, out _); + return header; + } + if (Headers.Count >= MaxHeaders) Headers.Clear(); + Headers[proxy] = header; + return header; } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { - return (ProxyCheck.Missing, 0); + return null; } } + /// + /// Whether a proxy's Exif capture time is the frame's to the microsecond: the index keeps + /// capture times to the microsecond, sidecars and Exif to the 100 ns tick, so a proxy made from either record of the + /// frame (here from the sidecar, by a client from the server's index) matches both. + /// + private static bool SameCapture(DateTime? proxy, DateTime captured) => + proxy is { } time && time.Ticks / TimeSpan.TicksPerMicrosecond == captured.Ticks / TimeSpan.TicksPerMicrosecond; + + /// Removes the proxy at and what is known of it. + private static void Delete(string path) + { + File.Delete(path); + Headers.TryRemove(path, out _); + } + /// /// Whether a proxy whose long edge is falls short of the policy's for a frame of /// × (the policy's long edge, or the frame's own when smaller or diff --git a/src/Titano.Data/Archiving/FrameTiers.cs b/src/Titano.Data/Archiving/FrameTiers.cs index e31649e3..6b7d27ed 100644 --- a/src/Titano.Data/Archiving/FrameTiers.cs +++ b/src/Titano.Data/Archiving/FrameTiers.cs @@ -51,25 +51,26 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi return tiers; } - /// - public Task> RecordAsync(Archive archive, Sequence sequence, IReadOnlyDictionary tiers, - CancellationToken cancellationToken) => RecordAsync(archive, sequence, tiers, null, cancellationToken); - /// - /// Records (by frame id) where they change what the sidecar says, and returns the frames that - /// changed as they are now. Frames no longer in the sequence are ignored. + /// Records (by frame id) where they change what the sidecar says, and returns the sequence's + /// frames as the sidecar has them afterwards (none when there was nothing to record). Frames no longer in the + /// sequence are ignored. The flag of a frame whose original this host reads + /// through its proxy follows whether its own proxy is there when the sidecar is written + /// (, ), not when the caller looked: proxies + /// arrive while analyses and renders run, and another worker may have recorded them already. /// - /// By frame id, for frames whose original this host reads through its proxy, whether the frame's - /// own proxy is there now (); their follows - /// it even where no tier is recorded. + /// Whether every such frame follows its proxy even where no tier is recorded (analyses and the + /// proxy pass); otherwise only the frames in do. /// The archive is offline or read-only. public async Task> RecordAsync(Archive archive, Sequence sequence, IReadOnlyDictionary tiers, - IReadOnlyDictionary? proxied, CancellationToken cancellationToken) + bool proxies, CancellationToken cancellationToken) { - if (tiers.Count == 0 && proxied is not { Count: > 0 }) return []; + if (tiers.Count == 0 && !proxies) return []; var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); + var folder = paths.Sequence(sequence.Folder); var changed = new List(); + ProxyPolicy? policy = null; var manifest = await writer.UpdateAsync(paths, sequence.Id, sequence.Folder, m => { changed.Clear(); @@ -79,10 +80,15 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi { var frame = m.Frames[i]; frames[i] = frame; - bool? hasProxy = proxied is not null && proxied.TryGetValue(frame.Id, out var found) ? found : null; + var tier = tiers.GetValueOrDefault(frame.Id); + if (tier is null && !proxies) continue; + bool? proxied = (frame.Flags & FrameFlags.Missing) == 0 && ArchiveImages.NeedsProxy(writer.Options.Decoder, frame.Format) + ? ArchiveImages.IsProxyOf(folder.Proxy(frame.I), frame.CapturedAt, frame.W, frame.H, + policy ??= ManifestStore.ReadArchive(paths.ManifestPath).ProxyPolicy) + : null; SidecarFrame updated; - if (tiers.TryGetValue(frame.Id, out var tier)) updated = SequenceFrames.WithTier(frame, tier, DecodesNatively(frame.Format), hasProxy == true); - else if (hasProxy is { } proxy) updated = SequenceFrames.WithProxy(frame, proxy); + if (tier is not null) updated = SequenceFrames.WithTier(frame, tier, DecodesNatively(frame.Format), proxied == true); + else if (proxied is { } own) updated = SequenceFrames.WithProxy(frame, own); else continue; if (updated.Tier == frame.Tier && updated.Flags == frame.Flags && updated.PreviewW == frame.PreviewW && updated.PreviewH == frame.PreviewH) continue; @@ -98,14 +104,13 @@ internal sealed class FrameTiers(TitanoDatabase db, SequenceWriter writer, Archi UpdatedAt = DateTimeOffset.UtcNow, }; }, cancellationToken).ConfigureAwait(false); - if (changed.Count == 0) return []; + if (changed.Count == 0) return manifest.Frames; await db.Frames.UpdateTiersAsync(changed.ConvertAll(f => new FrameTierFields(f.Id, f.Flags, f.PreviewW ?? 0, f.PreviewH ?? 0)), cancellationToken) .ConfigureAwait(false); - var folder = paths.Sequence(sequence.Folder); if (await db.Sequences.GetAsync(sequence.Id, cancellationToken).ConfigureAwait(false) is { } existing) await db.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, folder, existing), cancellationToken).ConfigureAwait(false); await indexer.RecordStampsAsync(archive, folder, sequence.Id, cancellationToken).ConfigureAwait(false); - return changed; + return manifest.Frames; } /// diff --git a/src/Titano.Data/Processing/DeepReports.cs b/src/Titano.Data/Processing/DeepReports.cs new file mode 100644 index 00000000..78c10b93 --- /dev/null +++ b/src/Titano.Data/Processing/DeepReports.cs @@ -0,0 +1,28 @@ +using Titano.Core.Analysis.Deep; +using Titano.Core.Archiving; +using Titano.Core.Imaging; +using Titano.Core.Model; + +namespace Titano.Data.Processing; + +/// +/// Saved deep-analysis reports (.titano/analysis.json, BRIEF §10.2) for hosts that hand them on: a server serves +/// one to the editors of its clients only while it still describes the sequence (§14.5). +/// +public static class DeepReports +{ + /// + /// The saved report of when it describes its frames as a host with + /// reads them now; null when there is none, it covers another number of frames, or it + /// found unreadable a frame that has been read from its proxy since (an original that host cannot decode, whose proxy + /// arrived after the analysis): its verdict and its advice to exclude that frame no longer hold. + /// + public static async Task ReadCurrentAsync(TitanoDatabase db, Archive archive, Sequence sequence, ImageDecoder decoder, + CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(archive); + ArgumentNullException.ThrowIfNull(sequence); + var frames = await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, decoder, cancellationToken).ConfigureAwait(false); + return SequenceAnalyser.ReadReport(new ArchivePaths(archive.RootPath).Sequence(sequence.Folder), frames); + } +} diff --git a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs index c155ef6b..fee91028 100644 --- a/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs +++ b/src/Titano.Data/Processing/Jobs/RenderJobHandler.cs @@ -94,11 +94,12 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti if (render.StandIns.Count > 0) context.Log(Loc.L("Export.Log.StandIns", string.Join(", ", render.StandIns.Select(i => (i + 1).ToString(Loc.Culture))))); // Recorded without the job's token: what decoding found holds even if the job is cancelled meanwhile, and a - // sidecar that cannot be written now only leaves it to the next analysis or render. + // sidecar that cannot be written now only leaves it to the next analysis or render. A frame decoded from its + // preview whose proxy arrived during the render is not preview-only: FrameTiers looks for the proxy as it records. try { var decoded = render.Decoded.ToDictionary(kv => frames.Frames[kv.Key].Id, kv => kv.Value); - await tiers.RecordAsync(archive, sequence, decoded, CancellationToken.None).ConfigureAwait(false); + await tiers.RecordAsync(archive, sequence, decoded, proxies: false, CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or LeaseUnavailableException or ArchiveOfflineException) diff --git a/src/Titano.Data/Processing/SequenceAnalyser.cs b/src/Titano.Data/Processing/SequenceAnalyser.cs index 5f225ceb..46b73035 100644 --- a/src/Titano.Data/Processing/SequenceAnalyser.cs +++ b/src/Titano.Data/Processing/SequenceAnalyser.cs @@ -46,17 +46,18 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti var folder = paths.Sequence(sequence.Folder); var frames = await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, options.Archive.Decoder, cancellationToken).ConfigureAwait(false); // Format tiers (BRIEF §10.1): frames indexed since the last analysis are probed with this host's decoders. A frame - // read from its proxy is not preview-only whatever its original's tier, so it caps nothing (BRIEF §8.1, §8.2). + // read from its proxy is not preview-only whatever its original's tier, so it caps nothing (BRIEF §8.1, §8.2); the + // flags are taken as the sidecar has them once recorded, so ones another worker recorded meanwhile count too. var probed = await _tiers.ProbeUnknownAsync(archive, folder, ManifestStore.ReadSequence(folder.ManifestPath), cancellationToken).ConfigureAwait(false); - var proxied = frames.ProxyStates(options.Archive.Decoder); - frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, probed, proxied, cancellationToken).ConfigureAwait(false)); + frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, probed, proxies: true, cancellationToken).ConfigureAwait(false)); var metadata = frames.LightFrames(); DeepAnalysisReport? report; if (deep) { + // Proxies may have arrived during the pass: the advice below follows the frames as they are at its end. (report, var decoded) = await AnalyseDeepAsync(archive, frames, metadata, folder, progress, cancellationToken).ConfigureAwait(false); - frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, decoded, proxied, cancellationToken).ConfigureAwait(false)); + frames = WithTiers(frames, await _tiers.RecordAsync(archive, sequence, decoded, proxies: true, cancellationToken).ConfigureAwait(false)); metadata = [.. metadata.Select((m, i) => m with { Flags = frames.Frames[i].Flags })]; } else @@ -139,11 +140,11 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti } } - /// The frame rows with the tier fields of sidecar frames. - private static SequenceFrames WithTiers(SequenceFrames frames, IReadOnlyList changed) + /// The frame rows with the flags and preview sizes of the sidecar frames. + private static SequenceFrames WithTiers(SequenceFrames frames, IReadOnlyList current) { - if (changed.Count == 0) return frames; - var byId = changed.ToDictionary(f => f.Id); + if (current.Count == 0) return frames; + var byId = current.ToDictionary(f => f.Id); var rows = frames.Frames.Select(f => byId.TryGetValue(f.Id, out var s) ? f with { Flags = s.Flags, PreviewWidth = s.PreviewW ?? 0, PreviewHeight = s.PreviewH ?? 0 } : f).ToList(); @@ -215,15 +216,16 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti /// Identity of the analysed content: frames replaced or renumbered since a checkpoint was written (with the /// same count), or now read from their proxies where they were read from their originals (or the other way round), - /// make it stale, like changed options do. - private static string ContentKey(SequenceFrames frames) + /// make it stale, like changed options do. Only frames read from proxies are marked, so the key of a sequence read + /// from its originals is what it was before proxies counted, and checkpoints written then still resume. + internal static string ContentKey(SequenceFrames frames) { using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); for (var i = 0; i < frames.Count; i++) { var f = frames.Frames[i]; - var source = frames.Proxied is { } proxied && proxied[i] ? "proxy" : "original"; - hash.AppendData(Encoding.UTF8.GetBytes($"{f.Id:N}/{f.Sha256}/{f.Size}/{f.Flags & SequenceFrames.NotAnalysed}/{source};")); + var source = frames.Proxied is { } proxied && proxied[i] ? "/proxy" : ""; + hash.AppendData(Encoding.UTF8.GetBytes($"{f.Id:N}/{f.Sha256}/{f.Size}/{f.Flags & SequenceFrames.NotAnalysed}{source};")); } return Convert.ToHexStringLower(hash.GetHashAndReset())[..16]; } diff --git a/src/Titano.Data/Processing/SequenceFrames.cs b/src/Titano.Data/Processing/SequenceFrames.cs index 5489cf70..654d77c4 100644 --- a/src/Titano.Data/Processing/SequenceFrames.cs +++ b/src/Titano.Data/Processing/SequenceFrames.cs @@ -35,7 +35,8 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList Fr /// Every frame row of with its archive file: the proxy when asked, and whenever the /// original is one cannot decode (: /// HEIF on the server, BRIEF §2.3, §8.5), as long as the frame's own proxy is there (: - /// not one left at its index by a frame renumbered since); else the original. + /// not one left at its index by a frame renumbered since, nor, for an original this host cannot decode, one smaller + /// than the archive's policy); else the original. /// public static async Task LoadAsync(TitanoDatabase db, Archive archive, Sequence sequence, bool useProxies, ImageDecoder decoder, CancellationToken cancellationToken) @@ -46,13 +47,16 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList Fr var files = new string?[frames.Count]; var originals = new string?[frames.Count]; var proxied = new bool[frames.Count]; + ProxyPolicy? policy = null; for (var i = 0; i < files.Length; i++) { var frame = frames[i]; if ((frame.Flags & FrameFlags.Missing) != 0) continue; originals[i] = paths.FromRelative(frame.RelativePath); - var proxy = useProxies || ArchiveImages.NeedsProxy(decoder, frame.Format) ? folder.Proxy(frame.Index) : null; - proxied[i] = proxy is not null && ArchiveImages.IsProxyOf(proxy, frame.CapturedAt); + var needs = ArchiveImages.NeedsProxy(decoder, frame.Format); + var proxy = useProxies || needs ? folder.Proxy(frame.Index) : null; + proxied[i] = proxy is not null && ArchiveImages.IsProxyOf(proxy, frame.CapturedAt, frame.Width, frame.Height, + needs ? policy ??= ManifestStore.ReadArchive(paths.ManifestPath).ProxyPolicy : null); files[i] = proxied[i] ? proxy : originals[i]; } return new SequenceFrames(sequence, frames, files) { Proxied = proxied, Originals = originals }; @@ -95,22 +99,6 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList Fr return light; } - /// - /// By frame id, whether each frame whose original cannot decode (it is read from its proxy, - /// ) has its own proxy now: what its follows - /// (). - /// Missing frames are left out. - /// - public Dictionary ProxyStates(ImageDecoder decoder) - { - var states = new Dictionary(); - for (var i = 0; i < Frames.Count; i++) - { - if (Files[i] is not null && ArchiveImages.NeedsProxy(decoder, Frames[i].Format)) states[Frames[i].Id] = Proxied is { } proxied && proxied[i]; - } - return states; - } - /// Positions of the frames a render must skip besides the project's exclusions. public IEnumerable NotRenderable() { diff --git a/src/Titano.Server/Api/SequenceEndpoints.cs b/src/Titano.Server/Api/SequenceEndpoints.cs index 2071c602..f16959e7 100644 --- a/src/Titano.Server/Api/SequenceEndpoints.cs +++ b/src/Titano.Server/Api/SequenceEndpoints.cs @@ -254,7 +254,11 @@ public static class SequenceEndpoints _ => ApiErrors.NotFound(context, "analysis"), }; - /// The full deep-analysis report (.titano/analysis.json) a client's editor shows for a server sequence. + /// + /// The full deep-analysis report (.titano/analysis.json) a client's editor shows for a server sequence, while it + /// describes the frames as the server reads them now (): one that found + /// frames unreadable before their proxies arrived answers 404 analysis, so no editor advises excluding them. + /// private static async Task ReportAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive) { if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); @@ -262,6 +266,16 @@ public static class SequenceEndpoints var path = new Core.Archiving.ArchivePaths(archive.Root).Sequence(sequence.Folder).AnalysisPath; var file = new FileInfo(path); if (!file.Exists || file.LinkTarget is not null || !ArchivePathGuard.IsInside(archive.Root, path)) return ApiErrors.NotFound(context, "analysis"); + try + { + if (await DeepReports.ReadCurrentAsync(db, archive.Current, sequence, Core.Imaging.Decoders.DecoderFactory.CreateServer(), context.RequestAborted) is null) + return ApiErrors.NotFound(context, "analysis"); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException) + { + // archive.json cannot be read now, so whether the proxies count is not known. + return ApiErrors.NotFound(context, "analysis"); + } var etag = string.Create(CultureInfo.InvariantCulture, $"a-{file.Length}-{file.LastWriteTimeUtc.Ticks}"); return FileResponses.Serve(context, file, 0, file.Length, "application/json; charset=utf-8", "no-cache", etag) ?? ApiErrors.NotFound(context, "analysis"); } diff --git a/src/Titano.Server/Storage/ArchiveAssetLocator.cs b/src/Titano.Server/Storage/ArchiveAssetLocator.cs index 559cd105..d9d3d1c6 100644 --- a/src/Titano.Server/Storage/ArchiveAssetLocator.cs +++ b/src/Titano.Server/Storage/ArchiveAssetLocator.cs @@ -7,8 +7,8 @@ namespace Titano.Server.Storage; /// /// Proxies are files under the sequence's proxies/ folder; a thumbnail is the JPEG byte range of its entry in /// thumbs.pack. A thumbnail made from a different original (the frame was renumbered or replaced) counts as not -/// built yet, like a missing one, and so does a proxy whose capture time is another frame's -/// (). +/// built yet, like a missing one, and so does a proxy whose capture time is another frame's, or one smaller than the +/// archive's policy sent for a frame the server cannot decode (). /// internal sealed class ArchiveAssetLocator : IFrameAssetLocator { @@ -17,13 +17,17 @@ internal sealed class ArchiveAssetLocator : IFrameAssetLocator public ValueTask LocateAsync(Archive archive, Sequence sequence, Frame frame, FrameAsset asset, CancellationToken cancellationToken) { var paths = new ArchivePaths(archive.RootPath).Sequence(sequence.Folder); - return ValueTask.FromResult(asset == FrameAsset.Proxy ? Proxy(paths, frame) : Thumbnail(paths, frame)); + return ValueTask.FromResult(asset == FrameAsset.Proxy ? Proxy(archive, paths, frame) : Thumbnail(paths, frame)); } - private static AssetLocation? Proxy(SequencePaths paths, Frame frame) + private static AssetLocation? Proxy(Archive archive, SequencePaths paths, Frame frame) { var file = new FileInfo(paths.Proxy(frame.Index)); - return file.Exists && ArchiveImages.IsProxyOf(file.FullName, frame.CapturedAt) ? new AssetLocation(file.FullName, 0, file.Length, Jpeg) : null; + if (!file.Exists) return null; + var own = ServerProxies.CannotDecode(frame.Format) + ? ServerProxies.HasProxy(paths, frame, ServerProxies.Spec(archive)) + : ArchiveImages.IsProxyOf(file.FullName, frame.CapturedAt, frame.Width, frame.Height, policy: null); + return own ? new AssetLocation(file.FullName, 0, file.Length, Jpeg) : null; } private static AssetLocation? Thumbnail(SequencePaths paths, Frame frame) diff --git a/src/Titano.Server/Storage/ServerProxies.cs b/src/Titano.Server/Storage/ServerProxies.cs index 9d644853..4ebb9f6d 100644 --- a/src/Titano.Server/Storage/ServerProxies.cs +++ b/src/Titano.Server/Storage/ServerProxies.cs @@ -34,10 +34,20 @@ public static class ServerProxies } /// - /// Every frame of 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 (: one left at its index by a - /// frame renumbered since is another photo's); it is wanted for a frame the server cannot decode that has none (and is - /// not missing), unless is null. + /// Whether the proxy of , one the server cannot decode, is in : the + /// frame's own (: one left at its index by a frame renumbered since is another + /// photo's) and as large as asks (null: the archive makes none, or its manifest cannot be read + /// now, so the size is not judged). + /// + public static bool HasProxy(SequencePaths folder, Frame frame, ProxySpec? spec) => + ArchiveImages.IsProxyOf(folder.Proxy(frame.Index), frame.CapturedAt, frame.Width, frame.Height, + spec is null ? null : new ProxyPolicy { LongEdge = spec.LongEdge, Quality = spec.Quality }); + + /// + /// Every frame of in index order, as a manifest lists it. A frame the server cannot decode + /// has its proxy when its own is there (), whatever the sequence's built status says (frames + /// added or renumbered since keep it until the next proxy pass), and wants one otherwise (unless it is missing or + /// damaged, or is null); any other frame has one when the server built them all. /// public static async Task> FramesAsync(TitanoDatabase db, Archive archive, Sequence sequence, ProxySpec? spec, CancellationToken cancellationToken) @@ -52,9 +62,10 @@ public static class ServerProxies var batch = await db.Frames.ListBySequenceAsync(sequence.Id, after, Page, cancellationToken); foreach (var f in batch) { - var sent = !proxies && CannotDecode(f.Format) && ArchiveImages.IsProxyOf(folder.Proxy(f.Index), f.CapturedAt); - 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)); + var elsewhere = CannotDecode(f.Format); + var has = elsewhere ? HasProxy(folder, f, spec) : proxies; + var wanted = spec is not null && elsewhere && !has && (f.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) == 0; + frames.Add(new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, has, thumbs, (long)f.Flags, wanted)); } if (batch.Count < Page) return frames; after = batch[^1].Index; diff --git a/tests/Titano.Data.Tests/Archiving/ProxyTests.cs b/tests/Titano.Data.Tests/Archiving/ProxyTests.cs index bdb1e577..61735329 100644 --- a/tests/Titano.Data.Tests/Archiving/ProxyTests.cs +++ b/tests/Titano.Data.Tests/Archiving/ProxyTests.cs @@ -129,10 +129,11 @@ public sealed class ProxyTests(TestContext context) await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[0].CapturedAt!.Value, 1), fx.Ct); await File.WriteAllBytesAsync(folder.Proxy(1), Proxy(frames[0].CapturedAt!.Value, 2), fx.Ct); - Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(0), frames[0].CapturedAt)); - Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(1), frames[1].CapturedAt), "another photo's"); - Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(1), null), "a frame without a capture time cannot tell"); - Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(2), frames[1].CapturedAt), "none there"); + var (width, height) = (frames[0].Width, frames[0].Height); + Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(0), frames[0].CapturedAt, width, height, new ProxyPolicy())); + Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(1), frames[1].CapturedAt, width, height, new ProxyPolicy()), "another photo's"); + Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(1), null, width, height, new ProxyPolicy()), "a frame without a capture time cannot tell"); + Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(2), frames[1].CapturedAt, width, height, null), "none there"); // Renders and analyses read frame 1 from its original, whether the host takes HEIF from proxies or was asked to. foreach (var useProxies in new[] { false, true }) @@ -143,6 +144,120 @@ public sealed class ProxyTests(TestContext context) } } + [Test] + public async Task AProxyMatchesItsFrameToTheMicrosecondTheIndexKeeps() + { + await using var fx = await CreateAsync(context); + // A frame timed to the 100 ns tick (seven sub-second digits, or a file time): the sidecar keeps every digit, the + // index six. + var captured = Start.AddTicks(1_234_567); + var staged = fx.Stage(captured, 50); + var id = (await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, null, null, "ticks", staged.Path, "IMG_0001.JPG", + staged.Size, staged.Sha256, null, null), fx.Ct)).SequenceId; + var sequence = (await fx.Db.Sequences.GetAsync(id, fx.Ct))!; + var folder = fx.Paths.Sequence(sequence.Folder); + var frame = (await fx.Db.Frames.ListBySequenceAsync(id))[0]; + Assert.Equal(captured, ManifestStore.ReadSequence(folder.ManifestPath).Frames[0].CapturedAt); + Assert.Equal(captured.AddTicks(-7), frame.CapturedAt, "the index keeps microseconds"); + + // The proxy pass writes the sidecar's time into the proxy it makes; a render asked to use proxies takes it. + var images = fx.Service.Images; + var made = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.Equal((1, 0), (made.Built, made.Reused)); + Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(0), frame.CapturedAt, frame.Width, frame.Height, null)); + var loaded = await Titano.Data.Processing.SequenceFrames.LoadAsync(fx.Db, fx.Archive, sequence, useProxies: true, new ImageDecoder([]), fx.Ct); + Assert.SequenceEqual([folder.Proxy(0)], loaded.Files); + + // A client makes one from the index's time: it is kept, and the next pass reuses it rather than making its own. + var sent = Proxy(frame.CapturedAt!.Value, 1); + Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frame, sent)); + var again = await images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); + Assert.Equal((0, 1), (again.Built, again.Reused)); + Assert.SequenceEqual(sent, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct)); + } + + [Test] + public async Task AThumbnailSizedProxyIsNoProxyOfAFrameReadThroughProxies() + { + await using var fx = await CreateAsync(context); + var (sequence, frames) = await HeifSequenceAsync(fx, 4032, 3024); + var folder = fx.Paths.Sequence(sequence.Folder); + // An earlier version made frame 0's proxy here from the original's 160 × 120 Exif thumbnail. + Directory.CreateDirectory(folder.Proxies); + await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[0].CapturedAt!.Value, 1, 160, 120), fx.Ct); + + Assert.False(ArchiveImages.IsProxyOf(folder.Proxy(0), frames[0].CapturedAt, 4032, 3024, new ProxyPolicy()), "under the policy's 1920 px"); + Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(0), frames[0].CapturedAt, 4032, 3024, null), "a host that decodes the original judges no size"); + // Renders and analyses read the original as the proxy pass would see it: no proxy. + var loaded = await Titano.Data.Processing.SequenceFrames.LoadAsync(fx.Db, fx.Archive, sequence, useProxies: false, new ImageDecoder([]), fx.Ct); + Assert.SequenceEqual([false, false], loaded.Proxied!); + + await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[0].CapturedAt!.Value, 2, 1920, 1440), fx.Ct); + loaded = await Titano.Data.Processing.SequenceFrames.LoadAsync(fx.Db, fx.Archive, sequence, useProxies: false, new ImageDecoder([]), fx.Ct); + Assert.SequenceEqual([true, false], loaded.Proxied!); + } + + [Test] + public async Task PreviewOnlyFollowsTheProxyThereWhenTiersAreRecorded() + { + await using var fx = await CreateAsync(context); + var (sequence, frames) = await HeifSequenceAsync(fx, 4032, 3024); + var folder = fx.Paths.Sequence(sequence.Folder); + var writer = new SequenceWriter(new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) }); + var tiers = new FrameTiers(fx.Db, writer, new ArchiveIndexer(fx.Db, writer)); + // A render decoded both originals from their thumbnails. Frame 0's proxy arrived meanwhile; frame 1's place holds + // a thumbnail-sized one an earlier version made. + Directory.CreateDirectory(folder.Proxies); + await File.WriteAllBytesAsync(folder.Proxy(0), Proxy(frames[0].CapturedAt!.Value, 1, 1920, 1440), fx.Ct); + await File.WriteAllBytesAsync(folder.Proxy(1), Proxy(frames[1].CapturedAt!.Value, 2, 160, 120), fx.Ct); + var preview = new FileTier(ImageFileFormat.Heif, DecodeTier.EmbeddedPreview, "managed", null, 160, 120); + await tiers.RecordAsync(fx.Archive, sequence, new Dictionary { [frames[0].Id] = preview, [frames[1].Id] = preview }, + proxies: false, fx.Ct); + + var recorded = ManifestStore.ReadSequence(folder.ManifestPath).Frames; + Assert.Equal((DecodeTier.EmbeddedPreview, (int?)160, FrameFlags.None), (recorded[0].Tier, recorded[0].PreviewW, recorded[0].Flags & FrameFlags.PreviewOnly)); + Assert.Equal(FrameFlags.PreviewOnly, recorded[1].Flags & FrameFlags.PreviewOnly); + Assert.Equal(1, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + + // Once frame 0's proxy is gone, an analysis (every frame follows its proxy) makes it preview-only again, and + // hands back every frame as the sidecar has it. + File.Delete(folder.Proxy(0)); + var current = await tiers.RecordAsync(fx.Archive, sequence, new Dictionary(), proxies: true, fx.Ct); + Assert.True(current.Count == 2 && current.All(f => f.Flags.HasFlag(FrameFlags.PreviewOnly)), "both preview-only"); + Assert.Equal(2, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + Assert.Count(2, await tiers.RecordAsync(fx.Archive, sequence, new Dictionary(), proxies: true, fx.Ct)); + } + + [Test] + public async Task AnUnchangedProxyIsNotReadAgain() + { + await using var fx = await CreateAsync(context); + var (sequence, frames) = await HeifSequenceAsync(fx); + var path = fx.Paths.Sequence(sequence.Folder).Proxy(0); + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + var proxy = Proxy(frames[0].CapturedAt!.Value, 1); + bool Own() => ArchiveImages.IsProxyOf(path, frames[0].CapturedAt, frames[0].Width, frames[0].Height, null); + await File.WriteAllBytesAsync(path, proxy, fx.Ct); + var written = DateTime.UtcNow.AddMinutes(-5); + File.SetLastWriteTimeUtc(path, written); + Assert.True(Own()); + + // Same length and write time: a manifest listing thousands of proxies costs a stat per proxy, not a read. + await File.WriteAllBytesAsync(path, new byte[proxy.Length], fx.Ct); + File.SetLastWriteTimeUtc(path, written); + Assert.True(Own(), "known"); + File.SetLastWriteTimeUtc(path, written.AddSeconds(1)); + Assert.False(Own(), "written since: read again"); + + // A file written moments ago is read every time: its clock may give a quick second write the same time. + await File.WriteAllBytesAsync(path, proxy, fx.Ct); + var now = File.GetLastWriteTimeUtc(path); + Assert.True(Own()); + await File.WriteAllBytesAsync(path, new byte[proxy.Length], fx.Ct); + File.SetLastWriteTimeUtc(path, now); + Assert.False(Own(), "read again"); + } + [Test] public async Task AResumedProxyPassSetsTheBuiltStatusOnlyWhenEveryFrameHasItsProxy() { @@ -242,7 +357,7 @@ public sealed class ProxyTests(TestContext context) /// /// Two ingested frames whose originals the archive engine takes for HEIF (its decoders read none), their size in the - /// sidecar × when given. + /// sidecar and the index × when given. /// private static async Task<(Sequence Sequence, IReadOnlyList Frames)> HeifSequenceAsync(ArchiveFixture fx, int? width = null, int? height = null) { @@ -261,7 +376,11 @@ public sealed class ProxyTests(TestContext context) Frames = [.. sidecar.Frames.Select(f => f with { Format = ImageFileFormat.Heif, W = width ?? f.W, H = height ?? f.H })], }); await using (var session = await fx.Db.Database.OpenAsync(fx.Ct)) + { await session.ExecuteAsync("UPDATE frames SET format = $1 WHERE sequence_id = $2", [(int)ImageFileFormat.Heif, id], fx.Ct); + if (width is { } w && height is { } h) + await session.ExecuteAsync("UPDATE frames SET width = $1, height = $2 WHERE sequence_id = $3", [w, h, id], fx.Ct); + } return (sequence, await fx.Db.Frames.ListBySequenceAsync(id)); } diff --git a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs index e6fd04ff..b6947cca 100644 --- a/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs +++ b/tests/Titano.Data.Tests/Processing/AnalysisJobTests.cs @@ -1,3 +1,5 @@ +using System.Security.Cryptography; +using System.Text; using Titano.Core.Advice; using Titano.Core.Analysis; using Titano.Core.Analysis.Deep; @@ -5,6 +7,7 @@ using Titano.Core.Archiving; using Titano.Core.Codecs.Dng; using Titano.Core.Codecs.Jpeg; using Titano.Core.Diagnostics.Synthetic; +using Titano.Core.Export; using Titano.Core.Imaging; using Titano.Core.Imaging.Decoders; using Titano.Core.Import; @@ -145,10 +148,10 @@ public sealed class AnalysisJobTests(TestContext context) Assert.Equal((AdvisorKeys.ResolutionPreview, "720p-preview"), (capped.Find(AdvisorParameters.Resolution)!.ReasonKey, capped.Find(AdvisorParameters.ExportPreset)!.Value as string)); Assert.True(capped.Findings.Any(f => f.Key == FindingKeys.PreviewOnly), "preview-only finding"); - // A client sends their proxies (BRIEF §2.3): the server renders the frames from them, so none is preview-only and - // the advice follows. The tier and preview size stay the originals'. + // A client sends their proxies (BRIEF §2.3) at the policy's 1920 px: the server renders the frames from them, so none + // is preview-only and the advice follows. The tier and preview size stay the originals'. Directory.CreateDirectory(folder.Proxies); - foreach (var frame in rows) await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value), fx.Ct); + foreach (var frame in rows) await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value, 1920, 1440), fx.Ct); await LightAnalysisAsync(fx, sequence); var proxied = ManifestStore.ReadSequence(folder.ManifestPath).Frames; Assert.True(proxied.All(f => f is { Tier: DecodeTier.EmbeddedPreview, PreviewW: 160 } && !f.Flags.HasFlag(FrameFlags.PreviewOnly)), "not preview-only"); @@ -160,6 +163,49 @@ public sealed class AnalysisJobTests(TestContext context) Assert.False(advice.Findings.Any(f => f.Key == FindingKeys.PreviewOnly), "no preview-only finding"); } + [Test] + public async Task ProxiesThatArriveDuringADeepAnalysisStillCount() + { + // A server, whose advisor rules no codec out: it reads these HEIF files only through their 160 × 120 Exif + // thumbnails until proxies come (BRIEF §2.3). + await using var fx = await ProcessingFixture.CreateAsync(context, o => o with { Host = EncoderHost.Server }); + var directory = Path.Combine(fx.Archive.Paths.Root, "Heif"); + Directory.CreateDirectory(directory); + var start = new DateTime(2026, 9, 28, 18, 0, 0); + for (var i = 0; i < 4; i++) + await File.WriteAllBytesAsync(Path.Combine(directory, $"IMG_{i + 1:0000}.HEIC"), HeifFile.Create(start.AddSeconds(10 * i), 4032, 3024), fx.Ct); + await fx.Archive.ScanAsync(ScanMode.Full); + var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Archive.Id, fx.Ct)).Single(l => l.Folder == "Heif"); + var sequence = (await fx.Db.Sequences.GetAsync(location.Id, fx.Ct))!; + var folder = fx.Folder(sequence); + await fx.StartEngineAsync(workers: 2); + async Task PresetAsync() => ProjectSettings.FromJson(await File.ReadAllTextAsync(folder.ProjectPath, fx.Ct)).Export.Preset.AutoValue; + await LightAnalysisAsync(fx, sequence); + Assert.Equal(4, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + Assert.Equal("720p-preview", await PresetAsync()); + + // A deep analysis starts on the originals; while it runs, the proxies arrive and a light analysis on the other + // worker counts them. + fx.Decoder.Hold = "IMG_0001.HEIC"; + var deep = await fx.EnqueueAsync(ProcessingJobs.DeepAnalysis([sequence.Id])); + await fx.Decoder.Holding.Task.WaitAsync(TimeSpan.FromSeconds(60), fx.Ct); + Directory.CreateDirectory(folder.Proxies); + foreach (var frame in await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct)) + await File.WriteAllBytesAsync(folder.Proxy(frame.Index), Proxy(frame.CapturedAt!.Value, 1920, 1440), fx.Ct); + await LightAnalysisAsync(fx, sequence); + Assert.Equal(0, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + Assert.Equal("4k-hevc", await PresetAsync()); + fx.Decoder.Release(); + await fx.WaitForAsync(deep.Id, JobState.Done); + + // The deep pass decoded the originals from their thumbnails, yet the frames are read from their proxies by now: + // it flags none preview-only again, and its advice caps nothing. + Assert.True(ManifestStore.ReadSequence(folder.ManifestPath).Frames.All(f => f.Tier == DecodeTier.EmbeddedPreview && !f.Flags.HasFlag(FrameFlags.PreviewOnly)), + "not preview-only"); + Assert.Equal(0, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.PreviewOnlyCount); + Assert.Equal("4k-hevc", await PresetAsync()); + } + [Test] public async Task AFrameTheDeepAnalysisCouldNotReadIsReadAgainOnceItsProxyArrives() { @@ -213,6 +259,25 @@ public sealed class AnalysisJobTests(TestContext context) Assert.Less(row.WarningCount, unreadable.WarningCount); } + [Test] + public async Task ACheckpointOfASequenceReadFromItsOriginalsKeepsTheKeyEarlierVersionsWrote() + { + await using var fx = await ProcessingFixture.CreateAsync(context); + var sequence = await fx.AddAsync(Flickering(4)); + var frames = await Titano.Data.Processing.SequenceFrames.LoadAsync(fx.Db, fx.Archive.Archive, sequence, useProxies: false, new ImageDecoder([fx.Decoder]), fx.Ct); + Assert.SequenceEqual([false, false, false, false], frames.Proxied!); + + // A deep analysis interrupted before an upgrade resumes after it (a server restarted mid-run does not start over). + using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); + foreach (var f in frames.Frames) + hash.AppendData(Encoding.UTF8.GetBytes($"{f.Id:N}/{f.Sha256}/{f.Size}/{f.Flags & Titano.Data.Processing.SequenceFrames.NotAnalysed};")); + var earlier = Convert.ToHexStringLower(hash.GetHashAndReset())[..16]; + Assert.Equal(earlier, SequenceAnalyser.ContentKey(frames)); + + // A frame read from its proxy since makes the checkpoint stale. + Assert.NotEqual(earlier, SequenceAnalyser.ContentKey(frames with { Proxied = [false, true, false, false] })); + } + [Test] public async Task DeepAnalysisFlagsFramesItCannotDecodeAndListsThemOnce() { @@ -490,10 +555,10 @@ public sealed class AnalysisJobTests(TestContext context) await fx.WaitForAsync(job.Id, JobState.Done); } - /// A 320 × 240 JPEG with in its Exif, as a client makes a proxy. - private static byte[] Proxy(DateTime captured) + /// A JPEG (320 × 240 unless sized) with in its Exif, as a client makes a proxy. + private static byte[] Proxy(DateTime captured, int width = 320, int height = 240) { - using var image = new ImageBuffer(320, 240); + using var image = new ImageBuffer(width, height); for (var c = 0; c < 3; c++) image.Plane(c).Fill(0.3f + c * 0.1f); var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Titano" }); return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] }); diff --git a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs index 690ff8fd..4e64ba85 100644 --- a/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs +++ b/tests/Titano.Data.Tests/Processing/ProcessingFixture.cs @@ -95,7 +95,7 @@ internal sealed class ProcessingFixture : IAsyncDisposable } } -/// Managed decoding that records every request and can hold one file's decode until the job is cancelled. +/// Managed decoding that records every request and can hold one file's decode until the job is cancelled or the decode is released. internal sealed class GateDecoder : IImageDecoderProvider { private readonly ManagedDecoderProvider _managed = new(); @@ -116,13 +116,19 @@ internal sealed class GateDecoder : IImageDecoderProvider /// Completed when a decode of started waiting. public TaskCompletionSource Holding { get; private set; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + private TaskCompletionSource _released = new(TaskCreationOptions.RunContinuationsAsynchronously); + public void HoldFrame(int index, DecodePurpose? purpose = null, int? edge = null) { Holding = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _released = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); (HoldPurpose, HoldEdge) = (purpose, edge); Hold = ProcessingFixture.FileName(index); } + /// Lets the held decode go on, as a slow read would. + public void Release() => _released.TrySetResult(); + /// Tiers reports per file name instead of the managed provider's (a /// host that reads them only as previews, or not at all). public ConcurrentDictionary Tiers { get; } = new(); @@ -152,7 +158,13 @@ internal sealed class GateDecoder : IImageDecoderProvider && (HoldEdge ?? request.MaxLongEdge) == request.MaxLongEdge) { Holding.TrySetResult(); - cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(30)); + try + { + _released.Task.Wait(TimeSpan.FromSeconds(30), cancellationToken); + } + catch (OperationCanceledException) + { + } cancellationToken.ThrowIfCancellationRequested(); } if (Unreadable.ContainsKey(name)) throw new IOException($"The process cannot access the file '{request.Path}' because it is being used by another process."); diff --git a/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs b/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs index 9cd3f34b..de67f08a 100644 --- a/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs +++ b/tests/Titano.Server.Tests/Api/RemoteEditingTests.cs @@ -32,10 +32,14 @@ public sealed class RemoteEditingTests(TestContext context) var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!; var path = new ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder).AnalysisPath; Directory.CreateDirectory(Path.GetDirectoryName(path)!); - const string report = """{"version":1,"frameCount":1}"""; + var report = Reports.Minimal(1).ToJson(); await File.WriteAllTextAsync(path, report, context.Cancellation); Assert.Equal(report, await client.GetAnalysisReportAsync(frame.SequenceId, context.Cancellation)); + // One made for other frames is not this sequence's report. + await File.WriteAllTextAsync(path, Reports.Minimal(2).ToJson(), context.Cancellation); + Assert.Null(await client.GetAnalysisReportAsync(frame.SequenceId, context.Cancellation), "another frame count"); + var gone = await Assert.ThrowsAsync(() => client.GetAnalysisReportAsync(Guid.CreateVersion7(), context.Cancellation)); Assert.Equal(HttpStatusCode.NotFound, gone.Status); } diff --git a/tests/Titano.Server.Tests/Reports.cs b/tests/Titano.Server.Tests/Reports.cs new file mode 100644 index 00000000..3e040881 --- /dev/null +++ b/tests/Titano.Server.Tests/Reports.cs @@ -0,0 +1,20 @@ +using Titano.Core.Analysis.Deep; + +namespace Titano.Server.Tests; + +/// Deep-analysis reports as a server keeps them in .titano/analysis.json. +internal static class Reports +{ + /// A report of frames that found unreadable and measured nothing else. + public static DeepAnalysisReport Minimal(int frames, params int[] undecodable) => new() + { + FrameCount = frames, + Flicker = new FlickerSummary(0, 0, null, 0, 0, 1), + Exposure = new ExposureSummary(0, 0, null, null, 0, []), + WhiteBalance = new WhiteBalanceSummary(0, 0, null, null), + Shake = new ShakeSummary(0, 0, 0, 0, false, 0), + Motion = new MotionSummary(0, null, null, null), + Sky = new SkySummary(DayNight.Unknown, 0, 0, 0, null, false, null), + Issues = new FrameIssues(undecodable, [], [], [], []), + }; +} diff --git a/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs b/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs index dc603b66..90cddd9d 100644 --- a/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs +++ b/tests/Titano.Server.Tests/Storage/ProxyUploadTests.cs @@ -3,6 +3,7 @@ using System.Net.Http.Headers; using Titano.Core.Codecs.Jpeg; using Titano.Core.Imaging; using Titano.Core.Metadata; +using Titano.Core.Model; using Titano.Protocol; using Titano.Server.Storage; using Titano.Testing; @@ -87,6 +88,69 @@ public sealed class ProxyUploadTests(TestContext context) await Http.BytesEqualAsync(proxy, editor.GetByteArrayAsync($"/api/v1/frames/{frame.Id}/proxy")); } + [Test] + public async Task FramesAddedAfterEveryProxyArrivedAreAskedForTheirs() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var first = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof"); + var frame = Assert.NotNull(await server.Database.Frames.GetAsync(first.FrameId)); + using (var stored = await PutAsync(editor, frame.Id, Proxy(frame.CapturedAt!.Value))) + await stored.ExpectAsync(HttpStatusCode.NoContent); + + // A frame comes after the proxy pass found every proxy there: until the next pass the status still says so, yet + // the new frame is asked for its proxy and the first keeps its own. + var second = await Uploads.UploadAsync(editor, Heif(2), "IMG_0002.HEIC", sessionKey: "roof"); + Assert.Equal(first.SequenceId, second.SequenceId); + await server.Database.Sequences.UpdateStatusAsync(first.SequenceId, SequenceStatus.ProxiesBuilt, SequenceStatus.None, context.Cancellation); + var frames = (await ManifestAsync(editor, first.SequenceId)).Frames; + Assert.Equal((false, true), Wanted(frames.Single(f => f.Id == first.FrameId))); + Assert.Equal((true, false), Wanted(frames.Single(f => f.Id == second.FrameId))); + } + + [Test] + public async Task AProxyTheServerMadeIsServedForAFrameTimedToTheTick() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + // Seven sub-second digits: the sidecar, and the proxy made from it, keep them all; the index keeps six. + var upload = await Uploads.UploadAsync(editor, Proxy(new DateTime(2026, 9, 28, 18, 2, 11).AddTicks(1_234_567)), "IMG_0001.JPG", sessionKey: "roof"); + + // The proxy pass the upload queued makes the frame's proxy, and the server serves it. + var until = DateTime.UtcNow + TimeSpan.FromSeconds(30); + while (true) + { + using var proxy = await editor.GetAsync($"/api/v1/frames/{upload.FrameId}/proxy"); + if (proxy.StatusCode == HttpStatusCode.OK) break; + Assert.True(DateTime.UtcNow < until, $"proxy not served: {(int)proxy.StatusCode}"); + await Task.Delay(100); + } + } + + [Test] + public async Task AReportThatFoundAFrameUnreadableBeforeItsProxyCameIsNotServed() + { + await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var upload = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof"); + var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId)); + var sequence = Assert.NotNull(await server.Database.Sequences.GetAsync(upload.SequenceId)); + // A deep analysis ran before the proxy came and found no pixels in the HEIF original. + var path = new Core.Archiving.ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder).AnalysisPath; + Directory.CreateDirectory(Path.GetDirectoryName(path)!); + Reports.Minimal(1, undecodable: 0).Save(path); + var url = $"/api/v1/sequences/{sequence.Id}/analysis/report"; + using (var before = await editor.GetAsync(url)) + await before.ExpectAsync(HttpStatusCode.OK); + + // Once the frame is read from its proxy, that verdict and its advice to exclude the frame no longer hold, even + // before a light analysis has dropped the deep status. + using (var stored = await PutAsync(editor, frame.Id, Proxy(frame.CapturedAt!.Value))) + await stored.ExpectAsync(HttpStatusCode.NoContent); + using var after = await editor.GetAsync(url); + Assert.Equal("analysis", (await after.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details); + } + [Test] public async Task FramesTheServerDecodesAskForNoProxy() {