fix(proxies): get proxies of undecodable originals from clients and nodes
The server decodes with managed decoders only, so HEIF originals had no
proxies, thumbnails from their small previews and no pixels for analysis or
managed renders, and nothing could send it proxies (ENG-05). Manifests now
carry the archive's proxy spec and, per frame, its flags and whether the
server wants its proxy. PUT /api/v1/frames/{id}/proxy (Edit or RenderNode)
stores a JPEG of at most 64 MiB that decodes and whose Exif capture time is
the frame's, then queues thumbnails and the proxy pass. An upload from the
desktop makes and sends the wanted proxies after the originals and counts
them as local changes; a render node sends those it can make from the
originals it fetched once the job is done. The server never makes such a
proxy from a preview, drops one left by a renumbered frame, takes thumbnails
from a proxy, and analyses and renders those frames from their proxies.
Render nodes fetched every frame of the manifest: frames missing, corrupt or
excluded on the server failed the download, and originals not hashed yet
failed the SHA-256 check (ENG-07). Nodes now skip those frames, fetch unhashed
originals into the job folder with a size check, judge decodability with their
own decoders, and use the proxy of a frame they cannot decode.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
28 files changed
+1005
-94
No files matched your search
@@ -132,6 +132,7 @@
|
||||
<data name="Archive.Log.ImportFailed" xml:space="preserve"><value>Not copied: {0} ({1})</value><comment>long: job log line</comment></data>
|
||||
<data name="Archive.Log.MaintenanceDone" xml:space="preserve"><value>{0} stale locks, {1} temporary files, {2} packs compacted, {3} duplicate flags changed</value><comment>long: job log line</comment></data>
|
||||
<data name="Archive.Log.Orphans" xml:space="preserve"><value>{0} unindexed photos, {1} missing photos</value><comment>long: job log line</comment></data>
|
||||
<data name="Archive.Log.ProxiesSent" xml:space="preserve"><value>Proxies for the server: {0} sent, {1} not sent</value><comment>long: job log line; {0} proxies sent; {1} proxies this PC could not make or the server refused</comment></data>
|
||||
<data name="Archive.Log.ScanDone" xml:space="preserve"><value>{0} folders, {1} indexed, {2} adopted, {3} removed</value><comment>long: job log line</comment></data>
|
||||
<data name="Archive.Log.SequenceMissing" xml:space="preserve"><value>Sequence not found: {0}</value><comment>long: job log line</comment></data>
|
||||
<data name="Archive.Log.VerifyDone" xml:space="preserve"><value>{0} photos checked, {1} intact, {2} hashed, {3} with problems</value><comment>long: job log line</comment></data>
|
||||
|
||||
@@ -54,6 +54,7 @@
|
||||
<data name="Server.Error.InvalidJobKind" xml:space="preserve"><value>This job kind cannot be started here.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.InvalidParameters" xml:space="preserve"><value>The job parameters are not valid.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.InvalidPriority" xml:space="preserve"><value>The priority must be background, normal or user.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.InvalidProxy" xml:space="preserve"><value>The proxy is not a JPEG the server can read.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.InvalidRole" xml:space="preserve"><value>The role must be admin, editor, uploader or viewer.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.InvalidSetting" xml:space="preserve"><value>The setting {0} is not valid.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.InvalidSettings" xml:space="preserve"><value>The settings must be valid JSON.</value><comment>long</comment></data>
|
||||
@@ -67,6 +68,8 @@
|
||||
<data name="Server.Error.NotFound" xml:space="preserve"><value>Not found.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.NotTitano" xml:space="preserve"><value>That address is not a Titano Server.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.PasswordChangeRequired" xml:space="preserve"><value>Change your password first.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.ProxiesDisabled" xml:space="preserve"><value>This archive makes no proxies.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.ProxyMismatch" xml:space="preserve"><value>The proxy was made from another photo.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.QuotaExceeded" xml:space="preserve"><value>This upload would exceed your quota.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.RateLimited" xml:space="preserve"><value>Too many attempts; wait a minute and try again.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.SamePassword" xml:space="preserve"><value>Choose a password different from the current one.</value><comment>long</comment></data>
|
||||
|
||||
@@ -127,6 +127,7 @@
|
||||
<data name="Archive.Log.ImportFailed" xml:space="preserve"><value>Non copiata: {0} ({1})</value></data>
|
||||
<data name="Archive.Log.MaintenanceDone" xml:space="preserve"><value>{0} blocchi scaduti, {1} file temporanei, {2} pacchetti compattati, {3} segni di duplicato cambiati</value></data>
|
||||
<data name="Archive.Log.Orphans" xml:space="preserve"><value>{0} foto non indicizzate, {1} foto mancanti</value></data>
|
||||
<data name="Archive.Log.ProxiesSent" xml:space="preserve"><value>Proxy per il server: {0} inviati, {1} non inviati</value></data>
|
||||
<data name="Archive.Log.ScanDone" xml:space="preserve"><value>{0} cartelle, {1} indicizzate, {2} adottate, {3} rimosse</value></data>
|
||||
<data name="Archive.Log.SequenceMissing" xml:space="preserve"><value>Sequenza non trovata: {0}</value></data>
|
||||
<data name="Archive.Log.VerifyDone" xml:space="preserve"><value>{0} foto controllate, {1} integre, {2} con impronta nuova, {3} con problemi</value></data>
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
<data name="Server.Error.InvalidJobKind" xml:space="preserve"><value>Questo tipo di job non si può avviare qui.</value></data>
|
||||
<data name="Server.Error.InvalidParameters" xml:space="preserve"><value>I parametri del job non sono validi.</value></data>
|
||||
<data name="Server.Error.InvalidPriority" xml:space="preserve"><value>La priorità deve essere background, normal o user.</value></data>
|
||||
<data name="Server.Error.InvalidProxy" xml:space="preserve"><value>Il proxy non è un JPEG che il server sa leggere.</value></data>
|
||||
<data name="Server.Error.InvalidRole" xml:space="preserve"><value>Il ruolo deve essere admin, editor, uploader o viewer.</value></data>
|
||||
<data name="Server.Error.InvalidSetting" xml:space="preserve"><value>L'impostazione {0} non è valida.</value></data>
|
||||
<data name="Server.Error.InvalidSettings" xml:space="preserve"><value>Le impostazioni devono essere JSON valido.</value></data>
|
||||
@@ -65,6 +66,8 @@
|
||||
<data name="Server.Error.NotFound" xml:space="preserve"><value>Non trovato.</value></data>
|
||||
<data name="Server.Error.NotTitano" xml:space="preserve"><value>Questo indirizzo non è un Titano Server.</value></data>
|
||||
<data name="Server.Error.PasswordChangeRequired" xml:space="preserve"><value>Cambia prima la password.</value></data>
|
||||
<data name="Server.Error.ProxiesDisabled" xml:space="preserve"><value>Questo archivio non crea proxy.</value></data>
|
||||
<data name="Server.Error.ProxyMismatch" xml:space="preserve"><value>Il proxy è stato creato da un'altra foto.</value></data>
|
||||
<data name="Server.Error.QuotaExceeded" xml:space="preserve"><value>Questo caricamento supererebbe la tua quota.</value></data>
|
||||
<data name="Server.Error.RateLimited" xml:space="preserve"><value>Troppi tentativi; attendi un minuto e riprova.</value></data>
|
||||
<data name="Server.Error.SamePassword" xml:space="preserve"><value>Scegli una password diversa da quella attuale.</value></data>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Diagnostics;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Codecs.Jpeg;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.IO;
|
||||
using Titano.Core.Metadata;
|
||||
using Titano.Core.Model;
|
||||
@@ -9,14 +11,35 @@ namespace Titano.Data.Archiving;
|
||||
/// <summary>Frames processed by one thumbnail or proxy pass over a sequence.</summary>
|
||||
public sealed record ImagePassResult(int Built, int Reused, int Undecodable);
|
||||
|
||||
/// <summary>What became of a proxy sent by a client or render node (<see cref="ArchiveImages.StoreProxy"/>).</summary>
|
||||
public enum ProxyStoreResult
|
||||
{
|
||||
Stored,
|
||||
|
||||
/// <summary>The archive's proxy policy makes no proxies.</summary>
|
||||
Disabled,
|
||||
|
||||
/// <summary>Not a JPEG that decodes, or larger than <see cref="ArchiveImages.MaxProxyBytes"/>.</summary>
|
||||
Invalid,
|
||||
|
||||
/// <summary>Its Exif capture time is not the frame's: it was made from another photo.</summary>
|
||||
WrongFrame,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Thumbnails (<c>thumbs.pack</c>) and proxies (<c>proxies/0001.jpg</c>) of sequences (BRIEF §8.5). Both are
|
||||
/// regenerable and self-validating: a thumbnail carries its source fingerprint, a proxy its capture time, so after
|
||||
/// frames were renumbered the stale ones are rebuilt and the rest reused.
|
||||
/// frames were renumbered the stale ones are rebuilt and the rest reused. A frame whose original this host cannot
|
||||
/// decode (<see cref="NeedsProxy(ImageDecoder, ImageFileFormat)"/>) gets its proxy from a client or render node that can
|
||||
/// (<see cref="StoreProxy"/>, BRIEF §2.3) and its thumbnail from that proxy.
|
||||
/// </summary>
|
||||
public sealed class ArchiveImages
|
||||
{
|
||||
/// <summary>Largest proxy a client or render node may send: a full-size proxy of a large sensor at q92 fits.</summary>
|
||||
public const long MaxProxyBytes = 64L << 20;
|
||||
|
||||
private const int CommitFrames = 200;
|
||||
private const int ProbeLongEdge = 64;
|
||||
private static readonly TimeSpan CommitInterval = TimeSpan.FromSeconds(10);
|
||||
|
||||
private readonly TitanoDatabase _db;
|
||||
@@ -32,6 +55,81 @@ public sealed class ArchiveImages
|
||||
_renderer = new FrameImageRenderer(writer.Options.Decoder);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a host with <paramref name="decoder"/> takes the pixels of a <paramref name="format"/> original from its
|
||||
/// proxy: no decoder reads the format, or only previews of a HEIF, which embeds thumbnails at best (BRIEF §2.3,
|
||||
/// §8.1: the server keeps HEIF originals and relies on proxies made by a Windows client or render node). Never for a
|
||||
/// JPEG (proxies are JPEGs: a host that reads them reads it) or a file no format was recognised in.
|
||||
/// </summary>
|
||||
public static bool NeedsProxy(ImageDecoder decoder, ImageFileFormat format)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(decoder);
|
||||
if (format is ImageFileFormat.Unknown or ImageFileFormat.Jpeg) return false;
|
||||
var tier = decoder.TierFor(format);
|
||||
return tier == DecodeTier.Unavailable || (tier != DecodeTier.Native && ImageFormatInfo.Of(format).Family == ImageFamily.Heif);
|
||||
}
|
||||
|
||||
/// <summary><see cref="NeedsProxy(ImageDecoder, ImageFileFormat)"/> with this archive engine's decoders.</summary>
|
||||
public bool NeedsProxy(ImageFileFormat format) => NeedsProxy(_writer.Options.Decoder, format);
|
||||
|
||||
/// <summary>
|
||||
/// A proxy of the original at <paramref name="path"/> for a server that cannot decode it (<see cref="StoreProxy"/>):
|
||||
/// at <paramref name="longEdge"/> (0 = full size) and <paramref name="quality"/>, with <paramref name="frame"/>'s capture
|
||||
/// data (the server's record, which it checks) and <paramref name="camera"/> in its Exif. Null when
|
||||
/// <paramref name="decoder"/> cannot decode the original either.
|
||||
/// </summary>
|
||||
/// <exception cref="IOException">The original cannot be read.</exception>
|
||||
public static byte[]? MakeProxy(ImageDecoder decoder, string path, Frame frame, SidecarCamera? camera, int longEdge, int quality,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(frame);
|
||||
if (NeedsProxy(decoder, frame.Format)) return null;
|
||||
// The original's folder stands in for a sequence folder: the renderer reads frames relative to one.
|
||||
var folder = new ArchivePaths(Path.GetDirectoryName(Path.GetFullPath(path))!).Sequence(string.Empty);
|
||||
var source = new SidecarFrame
|
||||
{
|
||||
Id = frame.Id,
|
||||
I = frame.Index,
|
||||
File = Path.GetFileName(path),
|
||||
Size = frame.Size,
|
||||
CapturedAt = frame.CapturedAt,
|
||||
UtcOffset = frame.UtcOffsetMinutes,
|
||||
Exposure = frame.ExposureTime,
|
||||
FNumber = frame.FNumber,
|
||||
Iso = frame.Iso,
|
||||
Focal = frame.FocalLength,
|
||||
Bias = frame.ExposureBias,
|
||||
Orientation = frame.Orientation,
|
||||
Format = frame.Format,
|
||||
};
|
||||
var policy = new ProxyPolicy { LongEdge = longEdge, Quality = quality };
|
||||
return new FrameImageRenderer(decoder).Proxy(folder, source, camera, policy, cancellationToken)?.Jpeg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Files a proxy made elsewhere (a client or render node whose decoders read the original) as the frame's proxy,
|
||||
/// replacing the one there: a JPEG of at most <see cref="MaxProxyBytes"/> that decodes, whose Exif capture time is the
|
||||
/// frame's (proxies carry it, so the proxy counts as current and cannot belong to another photo), written atomically.
|
||||
/// </summary>
|
||||
/// <exception cref="ArchiveOfflineException">The archive is offline or read-only.</exception>
|
||||
public ProxyStoreResult StoreProxy(Archive archive, Sequence sequence, Frame frame, byte[] jpeg)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(jpeg);
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
ArchiveVolume.EnsureWritable(paths);
|
||||
if (ManifestStore.ReadArchive(paths.ManifestPath).ProxyPolicy.LongEdge == ProxyPolicy.Disabled) return ProxyStoreResult.Disabled;
|
||||
if (jpeg.Length is 0 or > (int)MaxProxyBytes || !Decodes(jpeg)) return ProxyStoreResult.Invalid;
|
||||
if (frame.CapturedAt is { } captured)
|
||||
{
|
||||
using var stream = new MemoryStream(jpeg, writable: false);
|
||||
if (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);
|
||||
return ProxyStoreResult.Stored;
|
||||
}
|
||||
|
||||
/// <param name="startFrame">Resume point inside the sequence (frames before it were done by an earlier run).</param>
|
||||
/// <param name="checkpoint">Called with the next frame index whenever the pack has been committed.</param>
|
||||
public async Task<ImagePassResult> BuildThumbnailsAsync(Archive archive, Sequence sequence, bool force, int startFrame,
|
||||
@@ -53,12 +151,22 @@ public sealed class ArchiveImages
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var frame = manifest.Frames[i];
|
||||
var fingerprint = ThumbPack.Fingerprint(frame.Sha256, frame.File, frame.Size);
|
||||
if (!force && pack.Entries.TryGetValue(frame.I, out var entry) && entry.SourceHash.AsSpan().SequenceEqual(fingerprint))
|
||||
var entry = pack.Entries.GetValueOrDefault(frame.I);
|
||||
var edge = entry is null ? 0 : System.Math.Max(entry.Width, entry.Height);
|
||||
var current = !force && entry is not null && entry.SourceHash.AsSpan().SequenceEqual(fingerprint);
|
||||
// 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 (current && !fromProxy)
|
||||
{
|
||||
reused++;
|
||||
continue;
|
||||
}
|
||||
if (Render(() => _renderer.Thumbnail(folder, frame, cancellationToken)) is { } image)
|
||||
var source = fromProxy ? ProxySource(frame) : frame;
|
||||
if (Render(() => _renderer.Thumbnail(folder, source, cancellationToken)) is { } image)
|
||||
{
|
||||
pack.Append(frame.I, image.Width, image.Height, fingerprint, image.Jpeg);
|
||||
built++;
|
||||
@@ -97,12 +205,27 @@ public sealed class ArchiveImages
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var frame = manifest.Frames[i];
|
||||
var path = folder.Proxy(frame.I);
|
||||
if (!force && IsCurrent(path, frame))
|
||||
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.
|
||||
var check = ReadProxy(path, frame).Check;
|
||||
if (check is ProxyCheck.Current or ProxyCheck.Unverifiable)
|
||||
{
|
||||
reused++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (check == ProxyCheck.Stale) File.Delete(path);
|
||||
undecodable++;
|
||||
}
|
||||
}
|
||||
else if (!force && ReadProxy(path, frame).Check == ProxyCheck.Current)
|
||||
{
|
||||
reused++;
|
||||
continue;
|
||||
}
|
||||
if (Render(() => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken)) is { } image)
|
||||
else if (Render(() => _renderer.Proxy(folder, frame, manifest.Camera, policy, cancellationToken)) is { } image)
|
||||
{
|
||||
AtomicFile.WriteAllBytes(path, image.Jpeg);
|
||||
built++;
|
||||
@@ -117,15 +240,59 @@ public sealed class ArchiveImages
|
||||
return new ImagePassResult(built, reused, undecodable);
|
||||
}
|
||||
|
||||
/// <summary>A proxy is current when its Exif capture time is the frame's (frames renumbered since make it stale).</summary>
|
||||
private static bool IsCurrent(string proxy, SidecarFrame frame)
|
||||
/// <summary>Where a frame's proxy stands; only a capture time tells whether it is the frame's.</summary>
|
||||
private enum ProxyCheck
|
||||
{
|
||||
if (!File.Exists(proxy)) return false;
|
||||
Missing,
|
||||
|
||||
/// <summary>Its Exif capture time is the frame's.</summary>
|
||||
Current,
|
||||
|
||||
/// <summary>The frame has no capture time to compare with.</summary>
|
||||
Unverifiable,
|
||||
|
||||
/// <summary>Made from another frame (frames were renumbered since).</summary>
|
||||
Stale,
|
||||
}
|
||||
|
||||
/// <summary>Where the proxy at <paramref name="proxy"/> stands for <paramref name="frame"/>, and its long edge.</summary>
|
||||
private static (ProxyCheck Check, int LongEdge) ReadProxy(string proxy, SidecarFrame frame)
|
||||
{
|
||||
if (!File.Exists(proxy)) return (ProxyCheck.Missing, 0);
|
||||
try
|
||||
{
|
||||
return MetadataReader.Read(proxy).CapturedAt == frame.CapturedAt && frame.CapturedAt is not null;
|
||||
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;
|
||||
return (check, System.Math.Max(width, height));
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return (ProxyCheck.Missing, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The frame's proxy as the frame to draw: a JPEG already oriented, under <c>proxies/</c>.</summary>
|
||||
private static SidecarFrame ProxySource(SidecarFrame frame) => frame with
|
||||
{
|
||||
File = ArchiveLayout.ProxiesFolder + "/" + ArchiveLayout.ProxyFileName(frame.I),
|
||||
Format = ImageFileFormat.Jpeg,
|
||||
Orientation = 1,
|
||||
PreviewOffset = null,
|
||||
PreviewLength = null,
|
||||
};
|
||||
|
||||
/// <summary>A colour or grey JPEG whose headers parse and whose scans decode (at a small DCT-scaled size).</summary>
|
||||
private static bool Decodes(byte[] jpeg)
|
||||
{
|
||||
try
|
||||
{
|
||||
var info = JpegDecoder.ReadInfo(jpeg);
|
||||
if (info.Process == JpegProcess.Lossless || info.Components is not (1 or 3) || info.Width < 1 || info.Height < 1) return false;
|
||||
using var probe = JpegDecoder.DecodeToLinear(jpeg, new JpegDecodeOptions { MaxLongEdge = ProbeLongEdge });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidDataException or NotSupportedException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ internal sealed class RenderJobHandler(TitanoDatabase db, ProcessingOptions opti
|
||||
var targets = all.Select((t, i) => (i, t)).Where(t => !done.Contains(t.i)).Select(t => (t.i, adapt(t.t))).ToList();
|
||||
if (targets.Count == 0) return;
|
||||
|
||||
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, cancellation).ConfigureAwait(false);
|
||||
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, parameters.UseProxies, options.Archive.Decoder, cancellation).ConfigureAwait(false);
|
||||
IReadOnlyList<TargetOutcome> outcomes;
|
||||
await using (var render = new SequenceRender(frames, project, SequenceAnalyser.ReadReport(folder, frames.Count), options))
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.Export;
|
||||
using Titano.Core.Imaging;
|
||||
@@ -8,6 +9,7 @@ using Titano.Core.Localization;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Render;
|
||||
using Titano.Core.Video;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Protocol;
|
||||
using Titano.Protocol.Client;
|
||||
|
||||
@@ -47,8 +49,10 @@ public sealed record RenderNodeOptions
|
||||
/// <summary>
|
||||
/// "Offer this PC as render node" (BRIEF §14.7): registers this machine's single-file encoders with the server,
|
||||
/// heart-beats, claims one render job at a time that it can encode, downloads the frames it needs (originals
|
||||
/// hash-verified into a content-addressed cache, or proxies when the job asks; a dropped download resumes), renders the
|
||||
/// job's preset with the host's encoders and uploads the output, which the server files as a render of the sequence.
|
||||
/// hash-verified into a content-addressed cache, or proxies when the job asks or this PC cannot decode the original; a
|
||||
/// dropped download resumes; frames missing, corrupt or excluded on the server are left out), renders the job's preset
|
||||
/// with the host's encoders and uploads the output, which the server files as a render of the sequence. Then it sends
|
||||
/// the proxies the server asks for, made from the originals it fetched (BRIEF §2.3: the server cannot decode HEIF).
|
||||
/// Progress and render remarks go to the server; a job cancelled or reassigned there stops here; a failure is reported
|
||||
/// with its reason. Stopping abandons the job in flight without reporting it: the server requeues it when the node
|
||||
/// registers again or its heartbeats stop. Image sequences are not offered and batches are refused: the protocol uploads
|
||||
@@ -58,6 +62,12 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
{
|
||||
private const string LogCategory = "render-node";
|
||||
|
||||
/// <summary>
|
||||
/// Frames no node renders: absent on the server, not the archived file, or excluded. Undecodable is the server's
|
||||
/// verdict on its own decoders, so this PC's decoders try those frames.
|
||||
/// </summary>
|
||||
internal const FrameFlags NotFetched = FrameFlags.Missing | FrameFlags.Corrupt | FrameFlags.Excluded;
|
||||
|
||||
private readonly TitanoClient _client;
|
||||
private readonly ProcessingOptions _processing;
|
||||
private readonly RenderNodeOptions _options;
|
||||
@@ -127,7 +137,9 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(work);
|
||||
await RenderAsync(claimed, work, reporter, job.Token).ConfigureAwait(false);
|
||||
var input = await RenderAsync(claimed, work, reporter, job.Token).ConfigureAwait(false);
|
||||
// The job is done on the server; the proxies it asked for follow, on the node's own time.
|
||||
await SendProxiesAsync(claimed, input, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (job.IsCancellationRequested)
|
||||
{
|
||||
@@ -221,7 +233,8 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RenderAsync(ClaimedJob claimed, string work, NodeReporter reporter, CancellationToken cancellationToken)
|
||||
/// <summary>Renders the job and uploads its output; returns the frames it read.</summary>
|
||||
private async Task<SequenceFrames> RenderAsync(ClaimedJob claimed, string work, NodeReporter reporter, CancellationToken cancellationToken)
|
||||
{
|
||||
var export = ProcessingJobs.ReadExport(claimed.PresetJson);
|
||||
var resolved = ExportResolver.Resolve(export);
|
||||
@@ -232,9 +245,14 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
var project = ProjectStore.Parse(claimed.SettingsJson) with { Export = export };
|
||||
|
||||
var sequence = ToSequence(await _client.GetSequenceAsync(claimed.SequenceId, cancellationToken).ConfigureAwait(false));
|
||||
var frames = await ListFramesAsync(claimed.SequenceId, cancellationToken).ConfigureAwait(false);
|
||||
var files = await DownloadAsync(claimed, work, reporter, cancellationToken).ConfigureAwait(false);
|
||||
var input = new SequenceFrames(sequence, frames, [.. frames.Select(f => files.GetValueOrDefault(f.Id))]);
|
||||
// This PC's decoders judge what they can read: the server's Undecodable does not keep a frame out of the render here.
|
||||
var frames = (await ListFramesAsync(claimed.SequenceId, cancellationToken).ConfigureAwait(false))
|
||||
.ConvertAll(f => f with { Flags = f.Flags & ~FrameFlags.Undecodable });
|
||||
var files = await DownloadAsync(claimed, frames, work, reporter, cancellationToken).ConfigureAwait(false);
|
||||
var input = new SequenceFrames(sequence, frames, [.. frames.Select(f => files.GetValueOrDefault(f.Id).Path)])
|
||||
{
|
||||
Proxied = [.. frames.Select(f => files.GetValueOrDefault(f.Id).Proxy)],
|
||||
};
|
||||
|
||||
IReadOnlyList<TargetOutcome> outcomes;
|
||||
await using (var render = new SequenceRender(input, project, null, _processing))
|
||||
@@ -253,6 +271,56 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
if (outcomes[0].Error is { } error) throw new InvalidOperationException(error);
|
||||
return input;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the proxies the server asks for (<see cref="ManifestFrame.WantsProxy"/>) that this PC can make from the
|
||||
/// originals it fetched, as the server's proxy policy says; a failure is logged and never fails the finished job.
|
||||
/// </summary>
|
||||
private async Task SendProxiesAsync(ClaimedJob claimed, SequenceFrames input, CancellationToken cancellationToken)
|
||||
{
|
||||
if (claimed.Proxies is not { } spec) return;
|
||||
var wanted = claimed.Frames.Where(f => f.WantsProxy).Select(f => f.Id).ToHashSet();
|
||||
if (wanted.Count == 0) return;
|
||||
var camera = new SidecarCamera { Make = input.Sequence.CameraMake, Model = input.Sequence.CameraModel };
|
||||
int sent = 0, unsent = 0;
|
||||
for (var i = 0; i < input.Count; i++)
|
||||
{
|
||||
var frame = input.Frames[i];
|
||||
if (!wanted.Remove(frame.Id)) continue;
|
||||
if (input.Proxied?[i] == true || input.Files[i] is not { } original)
|
||||
{
|
||||
unsent++;
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{
|
||||
var jpeg = await Task.Run(() => ArchiveImages.MakeProxy(_processing.Archive.Decoder, original, frame, camera, spec.LongEdge, spec.Quality,
|
||||
cancellationToken), cancellationToken).ConfigureAwait(false);
|
||||
if (jpeg is null)
|
||||
{
|
||||
unsent++;
|
||||
continue;
|
||||
}
|
||||
await _client.UploadProxyAsync(frame.Id, jpeg, cancellationToken).ConfigureAwait(false);
|
||||
sent++;
|
||||
}
|
||||
catch (TitanoApiException ex) when (ex.Status is HttpStatusCode.Conflict or HttpStatusCode.Forbidden)
|
||||
{
|
||||
// Proxies turned off on the server, or this account may no longer send them: none of the rest would go either.
|
||||
Log.Warning(LogCategory, $"The server refused the proxies of job {claimed.Job.Id}", ex);
|
||||
return;
|
||||
}
|
||||
catch (Exception ex) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
// Best effort (a timed-out upload included): the job is done, and a proxy the server still lacks is asked for
|
||||
// again with the next job.
|
||||
Log.Warning(LogCategory, $"The proxy of {frame.FileName} could not be sent", ex);
|
||||
unsent++;
|
||||
}
|
||||
}
|
||||
Log.Info(LogCategory, $"Proxies of job {claimed.Job.Id}: {sent} sent, {unsent + wanted.Count} not");
|
||||
}
|
||||
|
||||
private async Task UploadAsync(Guid nodeId, Guid jobId, ExportTarget target, RenderResult result, CancellationToken cancellationToken)
|
||||
@@ -280,27 +348,33 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The files of the job's frames: originals verified against their SHA-256 into the content-addressed cache (reused
|
||||
/// across jobs, fetched once per content), or proxies into the job folder when the job asks for them and the server
|
||||
/// has them.
|
||||
/// The files of the job's frames (none for frames missing, corrupt or excluded on the server): originals verified
|
||||
/// against their SHA-256 into the content-addressed cache (reused across jobs, fetched once per content), originals the
|
||||
/// server has not hashed yet into the job folder, or proxies into the job folder when the job asks for them or this PC
|
||||
/// cannot decode the original, and the server has them.
|
||||
/// </summary>
|
||||
private async Task<Dictionary<Guid, string>> DownloadAsync(ClaimedJob claimed, string work, NodeReporter reporter, CancellationToken cancellationToken)
|
||||
private async Task<Dictionary<Guid, (string Path, bool Proxy)>> DownloadAsync(ClaimedJob claimed, IReadOnlyList<Frame> rows, string work,
|
||||
NodeReporter reporter, CancellationToken cancellationToken)
|
||||
{
|
||||
var files = new Dictionary<Guid, string>();
|
||||
var formats = rows.ToDictionary(f => f.Id, f => f.Format);
|
||||
bool Proxy(ManifestFrame f) =>
|
||||
f.HasProxy && (claimed.UseProxies || (formats.TryGetValue(f.Id, out var format) && ArchiveImages.NeedsProxy(_processing.Archive.Decoder, format)));
|
||||
var files = new Dictionary<Guid, (string, bool)>();
|
||||
var fetched = claimed.Frames.Where(f => ((FrameFlags)f.Flags & NotFetched) == 0).ToList();
|
||||
var done = 0;
|
||||
var total = claimed.Frames.Count;
|
||||
var total = fetched.Count;
|
||||
await reporter.ReportAsync(0, total, Loc.L("Export.Job.Downloading")).ConfigureAwait(false);
|
||||
var downloads = claimed.Frames.GroupBy(f => claimed.UseProxies && f.HasProxy ? f.Id.ToString("N") : f.Sha256.ToLowerInvariant()).ToList();
|
||||
var downloads = fetched.GroupBy(f => Proxy(f) || f.Sha256.Length == 0 ? f.Id.ToString("N") : f.Sha256.ToLowerInvariant()).ToList();
|
||||
await Parallel.ForEachAsync(downloads, new ParallelOptions { MaxDegreeOfParallelism = _options.Downloads, CancellationToken = cancellationToken },
|
||||
async (frames, ct) =>
|
||||
{
|
||||
var frame = frames.First();
|
||||
var path = claimed.UseProxies && frame.HasProxy
|
||||
var file = Proxy(frame)
|
||||
? await DownloadProxyAsync(frame, work, ct).ConfigureAwait(false)
|
||||
: await DownloadOriginalAsync(frame, ct).ConfigureAwait(false);
|
||||
: (await DownloadOriginalAsync(frame, work, ct).ConfigureAwait(false), false);
|
||||
lock (files)
|
||||
{
|
||||
foreach (var f in frames) files[f.Id] = path;
|
||||
foreach (var f in frames) files[f.Id] = file;
|
||||
}
|
||||
await reporter.ReportAsync(Interlocked.Add(ref done, frames.Count()), total, null).ConfigureAwait(false);
|
||||
}).ConfigureAwait(false);
|
||||
@@ -312,25 +386,29 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
/// fetches it whole, since a proxy rebuilt meanwhile must not be resumed from the old one); the original when the server
|
||||
/// has none for the frame.
|
||||
/// </summary>
|
||||
private async Task<string> DownloadProxyAsync(ManifestFrame frame, string work, CancellationToken cancellationToken)
|
||||
private async Task<(string Path, bool Proxy)> DownloadProxyAsync(ManifestFrame frame, string work, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = Path.Combine(work, "proxies", frame.Id.ToString("N") + ".jpg");
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
try
|
||||
{
|
||||
await FetchAsync(frame, "proxy", path, resume: false, cancellationToken).ConfigureAwait(false);
|
||||
return path;
|
||||
return (path, true);
|
||||
}
|
||||
catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.NotFound)
|
||||
{
|
||||
File.Delete(path);
|
||||
return await DownloadOriginalAsync(frame, cancellationToken).ConfigureAwait(false);
|
||||
return (await DownloadOriginalAsync(frame, work, cancellationToken).ConfigureAwait(false), false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A cached copy, or a download (resumed from a partial one) that must hash to the manifest's SHA-256.</summary>
|
||||
private async Task<string> DownloadOriginalAsync(ManifestFrame frame, CancellationToken cancellationToken)
|
||||
/// <summary>
|
||||
/// A cached copy, or a download (resumed from a partial one) that must hash to the manifest's SHA-256; an original the
|
||||
/// server has not hashed yet goes into the job folder and must have the manifest's size.
|
||||
/// </summary>
|
||||
private async Task<string> DownloadOriginalAsync(ManifestFrame frame, string work, CancellationToken cancellationToken)
|
||||
{
|
||||
if (frame.Sha256.Length == 0) return await DownloadUnhashedAsync(frame, work, cancellationToken).ConfigureAwait(false);
|
||||
var sha = frame.Sha256.ToLowerInvariant();
|
||||
if (sha.Length != 64 || !sha.All(char.IsAsciiHexDigitLower)) throw new InvalidDataException(Loc.L("Errors.Render.Download", frame.FileName));
|
||||
var path = Path.Combine(_options.CacheDirectory, "originals", sha[..2], sha + Path.GetExtension(frame.FileName).ToLowerInvariant());
|
||||
@@ -355,6 +433,23 @@ public sealed class RenderNodeClient : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An original without a SHA-256 on the server (adopted, not verified yet): nothing to check it against but its size,
|
||||
/// so it is neither cached nor shared with other frames; a copy of another size is fetched again once.
|
||||
/// </summary>
|
||||
private async Task<string> DownloadUnhashedAsync(ManifestFrame frame, string work, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = Path.Combine(work, "originals", frame.Id.ToString("N") + Path.GetExtension(frame.FileName).ToLowerInvariant());
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
if (!File.Exists(path) || new FileInfo(path).Length < frame.Size) await FetchAsync(frame, "original", path, resume: true, cancellationToken).ConfigureAwait(false);
|
||||
if (new FileInfo(path).Length == frame.Size) return path;
|
||||
File.Delete(path);
|
||||
if (attempt == 2) throw new InvalidDataException(Loc.L("Errors.Render.Download", frame.FileName));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches a frame resource into <paramref name="path"/>. A dropped connection or a busy server is retried (up to
|
||||
/// <see cref="RenderNodeOptions.DownloadRetries"/> times, the wait doubling from <see cref="RenderNodeOptions.RetryDelay"/>);
|
||||
|
||||
@@ -36,7 +36,7 @@ internal sealed class SequenceAnalyser(TitanoDatabase db, ProcessingOptions opti
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
ArchiveVolume.EnsureWritable(paths);
|
||||
var folder = paths.Sequence(sequence.Folder);
|
||||
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, cancellationToken).ConfigureAwait(false);
|
||||
var frames = await SequenceFrames.LoadAsync(db, archive, sequence, useProxies: false, options.Archive.Decoder, cancellationToken).ConfigureAwait(false);
|
||||
var metadata = frames.LightFrames();
|
||||
var light = LightAnalyzer.Analyze(metadata);
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using Titano.Core.Analysis;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Metadata;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data.Archiving;
|
||||
|
||||
namespace Titano.Data.Processing;
|
||||
|
||||
@@ -20,22 +22,39 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList<Frame> Fr
|
||||
|
||||
public int Count => Frames.Count;
|
||||
|
||||
/// <summary>Every frame row of <paramref name="sequence"/> with its archive file (the proxy when asked and built).</summary>
|
||||
public static async Task<SequenceFrames> LoadAsync(TitanoDatabase db, Archive archive, Sequence sequence, bool useProxies,
|
||||
/// <summary>
|
||||
/// Per frame, whether its file is its proxy. <see cref="FrameFlags.Undecodable"/> is this host's verdict on the
|
||||
/// original, so it does not keep a frame read from its proxy out of a render. Null: none is.
|
||||
/// </summary>
|
||||
public IReadOnlyList<bool>? Proxied { get; init; }
|
||||
|
||||
/// <summary>The originals, for metadata (proxies carry no GPS); null when <see cref="Files"/> are the originals.</summary>
|
||||
public IReadOnlyList<string?>? Originals { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Every frame row of <paramref name="sequence"/> with its archive file: the proxy when asked, and whenever the
|
||||
/// original is one <paramref name="decoder"/> cannot decode (<see cref="ArchiveImages.NeedsProxy(ImageDecoder, ImageFileFormat)"/>:
|
||||
/// HEIF on the server, BRIEF §2.3, §8.5), as long as the proxy exists; else the original.
|
||||
/// </summary>
|
||||
public static async Task<SequenceFrames> LoadAsync(TitanoDatabase db, Archive archive, Sequence sequence, bool useProxies, ImageDecoder decoder,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var frames = await ListAsync(db, sequence.Id, cancellationToken).ConfigureAwait(false);
|
||||
var paths = new ArchivePaths(archive.RootPath);
|
||||
var folder = paths.Sequence(sequence.Folder);
|
||||
var files = new string?[frames.Count];
|
||||
var originals = new string?[frames.Count];
|
||||
var proxied = new bool[frames.Count];
|
||||
for (var i = 0; i < files.Length; i++)
|
||||
{
|
||||
var frame = frames[i];
|
||||
if ((frame.Flags & FrameFlags.Missing) != 0) continue;
|
||||
var proxy = useProxies ? folder.Proxy(frame.Index) : null;
|
||||
files[i] = proxy is not null && File.Exists(proxy) ? proxy : paths.FromRelative(frame.RelativePath);
|
||||
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);
|
||||
files[i] = proxied[i] ? proxy : originals[i];
|
||||
}
|
||||
return new SequenceFrames(sequence, frames, files);
|
||||
return new SequenceFrames(sequence, frames, files) { Proxied = proxied, Originals = originals };
|
||||
}
|
||||
|
||||
public static async Task<List<Frame>> ListAsync(TitanoDatabase db, Guid sequenceId, CancellationToken cancellationToken)
|
||||
@@ -80,15 +99,17 @@ internal sealed record SequenceFrames(Sequence Sequence, IReadOnlyList<Frame> Fr
|
||||
{
|
||||
for (var i = 0; i < Frames.Count; i++)
|
||||
{
|
||||
if (Files[i] is null || (Frames[i].Flags & NotRendered) != 0) yield return i;
|
||||
var skip = Proxied is { } proxied && proxied[i] ? NotRendered & ~FrameFlags.Undecodable : NotRendered;
|
||||
if (Files[i] is null || (Frames[i].Flags & skip) != 0) yield return i;
|
||||
}
|
||||
}
|
||||
|
||||
private (int Index, GpsPosition Gps)? FirstGpsFix()
|
||||
{
|
||||
for (var i = 0; i < Files.Count; i++)
|
||||
var files = Originals ?? Files;
|
||||
for (var i = 0; i < files.Count; i++)
|
||||
{
|
||||
if (Files[i] is not { } file || (Frames[i].Flags & NotAnalysed) != 0) continue;
|
||||
if (files[i] is not { } file || (Frames[i].Flags & NotAnalysed) != 0) continue;
|
||||
try
|
||||
{
|
||||
return MetadataReader.Read(file).Gps is { } gps ? (i, gps) : null;
|
||||
|
||||
@@ -91,7 +91,10 @@ public sealed record SyncLink
|
||||
public int? DiffLocalRenders { get; init; }
|
||||
public int? DiffRemoteRenders { get; init; }
|
||||
|
||||
/// <summary>Local originals the server does not have.</summary>
|
||||
/// <summary>
|
||||
/// Local originals the server does not have, and proxies it asks for that this PC can make (it cannot decode those
|
||||
/// originals): an upload sends both.
|
||||
/// </summary>
|
||||
public int MissingRemote { get; init; }
|
||||
|
||||
/// <summary>Server originals this PC does not have.</summary>
|
||||
|
||||
@@ -42,6 +42,13 @@ public interface ISyncRemote : IDisposable
|
||||
/// <summary>Streams a frame's original, proxy or thumb, resuming with Range from the destination's length.</summary>
|
||||
Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Sends the proxy of a frame the server cannot decode (<see cref="ManifestFrame.WantsProxy"/>); a 422
|
||||
/// <see cref="TitanoApiException"/> means it was refused (not a JPEG of that frame), 409 that the server makes no
|
||||
/// proxies. A remote without the call (the screenshots' sample server) throws <see cref="NotSupportedException"/>.
|
||||
/// </summary>
|
||||
Task UploadProxyAsync(Guid frameId, byte[] jpeg, CancellationToken cancellationToken) => throw new NotSupportedException();
|
||||
|
||||
/// <summary>Streams a render's file, resuming with Range from the destination's length; returns the file name the server gives it.</summary>
|
||||
Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken);
|
||||
|
||||
@@ -116,6 +123,8 @@ public sealed class TitanoSyncRemote(TitanoClient client, bool ownsClient) : ISy
|
||||
public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
|
||||
client.DownloadFrameAsync(frameId, kind, destination, null, cancellationToken);
|
||||
|
||||
public Task UploadProxyAsync(Guid frameId, byte[] jpeg, CancellationToken cancellationToken) => client.UploadProxyAsync(frameId, jpeg, cancellationToken);
|
||||
|
||||
public Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
|
||||
client.DownloadRenderAsync(renderId, destination, cancellationToken);
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@ using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Project;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Data.Sync;
|
||||
@@ -130,6 +132,18 @@ public static class SyncRules
|
||||
return (hashed.Count - matched + Math.Max(0, unhashed - unmatchedRemote), Math.Max(0, unmatchedRemote - unhashed));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Frames whose proxy the server asks for (it cannot decode their originals, BRIEF §2.3) that this PC holds and its
|
||||
/// <paramref name="decoder"/> reads: an upload makes and sends them.
|
||||
/// </summary>
|
||||
public static int ProxiesToSend(SequenceManifest manifest, IReadOnlyCollection<Frame> local, ImageDecoder decoder)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(manifest);
|
||||
var readable = local.Where(f => !string.IsNullOrEmpty(f.Sha256) && !ArchiveImages.NeedsProxy(decoder, f.Format))
|
||||
.Select(f => f.Sha256!.ToLowerInvariant()).ToHashSet(StringComparer.Ordinal);
|
||||
return manifest.Frames.Count(f => f.WantsProxy && readable.Contains(f.Sha256.ToLowerInvariant()));
|
||||
}
|
||||
|
||||
internal static string Blank(string? text) => text?.Trim() ?? string.Empty;
|
||||
|
||||
private static JsonNode? Parse(string json)
|
||||
|
||||
@@ -43,8 +43,9 @@ public sealed record SyncOptions
|
||||
/// conflict the user settles with <see cref="ResolveAsync"/>, against the versions the user was shown. Uploads and
|
||||
/// downloads run as jobs (<see cref="CreateJobHandlers"/>): uploads use the chunked, hash-verified, resumable protocol;
|
||||
/// downloaded originals are verified and committed through the archive ingest, so sidecars stay right; proxies go to a
|
||||
/// cache and open as a <see cref="RemoteSequence"/>; renders travel on request. Proxies and thumbnails never travel
|
||||
/// upward. A local copy and a server sequence holding the same originals are linked by content when no link joins
|
||||
/// cache and open as a <see cref="RemoteSequence"/>; renders travel on request. Thumbnails never travel upward, and
|
||||
/// proxies only for originals the server cannot decode (BRIEF §2.3: HEIF), made here and counted with the originals the
|
||||
/// server lacks. A local copy and a server sequence holding the same originals are linked by content when no link joins
|
||||
/// them. The state of each local sequence is written to its <c>sync_state</c> for the Library. A failure while
|
||||
/// comparing one sequence is recorded on its link and does not stop the others.
|
||||
/// </summary>
|
||||
@@ -494,8 +495,10 @@ public sealed class SyncService
|
||||
manifest = await remote.GetManifestAsync(link.RemoteId, cancellationToken).ConfigureAwait(false);
|
||||
var frames = await SequenceFrames.ListAsync(_db, local.Id, cancellationToken).ConfigureAwait(false);
|
||||
// A frame flagged missing here counts as absent: downloading the originals restores it from the server.
|
||||
var (missingRemote, missingLocal) = SyncRules.CompareOriginals(
|
||||
[.. frames.Where(f => (f.Flags & FrameFlags.Missing) == 0).Select(f => f.Sha256)], [.. manifest.Frames.Select(f => f.Sha256)]);
|
||||
var present = frames.Where(f => (f.Flags & FrameFlags.Missing) == 0).ToList();
|
||||
var (missingRemote, missingLocal) = SyncRules.CompareOriginals([.. present.Select(f => f.Sha256)], [.. manifest.Frames.Select(f => f.Sha256)]);
|
||||
// Proxies the server asks for that this PC can make from its originals travel with an upload too (BRIEF §2.3).
|
||||
if (policy.CanEdit && manifest.Proxies is not null) missingRemote += SyncRules.ProxiesToSend(manifest, present, Options.Archive.Decoder);
|
||||
link = await CompareExportsAsync(link, local, manifest.Renders, cancellationToken).ConfigureAwait(false) with
|
||||
{
|
||||
DiffLocalFrames = local.FramesVersion,
|
||||
|
||||
@@ -16,7 +16,8 @@ namespace Titano.Data.Sync;
|
||||
/// <summary>
|
||||
/// The work of upload and download jobs. Uploads send every local original the server lacks through the chunked,
|
||||
/// resumable protocol after hashing it again (an original that no longer matches its archived hash is never sent),
|
||||
/// creating the server sequence first and sending its settings when the upload made it. Downloads fetch originals the
|
||||
/// creating the server sequence first and sending its settings when the upload made it, then the proxies the server asks
|
||||
/// for (originals it cannot decode, BRIEF §2.3), made here from this PC's originals. Downloads fetch originals the
|
||||
/// local copy lacks into the archive's staging folder (resuming with Range), require the manifest's SHA-256 and commit
|
||||
/// them through the archive ingest, which names them and writes the sidecar; the first original creates the local
|
||||
/// sequence, which then takes the server's settings. Frames of the local copy not hashed yet are hashed first (so the
|
||||
@@ -73,12 +74,90 @@ internal sealed class SyncTransfers(SyncService sync)
|
||||
Interlocked.Increment(ref skipped);
|
||||
await progress.StepAsync().ConfigureAwait(false);
|
||||
}).ConfigureAwait(false);
|
||||
await SendProxiesAsync(context, remote, target, local, paths, frames).ConfigureAwait(false);
|
||||
|
||||
if (created) await sync.PushAllAsync(remote, target, ct).ConfigureAwait(false);
|
||||
await sync.RecompareAsync(remote, target, ct).ConfigureAwait(false);
|
||||
if (skipped > 0) throw new InvalidDataException(Loc.Plural("Errors.Sync.Unreadable", skipped));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The proxies the server asks for (<see cref="ManifestFrame.WantsProxy"/>) that this PC's decoders can make from its
|
||||
/// originals, made as the server's proxy policy says with the server's capture data, and sent. One the server refuses
|
||||
/// is counted in the job log; a server that makes no proxies, or an account that may not send them, ends the pass; a
|
||||
/// server busy or out of reach fails the job like an original would.
|
||||
/// </summary>
|
||||
private async Task SendProxiesAsync(JobContext context, ISyncRemote remote, Guid remoteId, Sequence local, ArchivePaths paths, IReadOnlyList<Frame> frames)
|
||||
{
|
||||
var ct = context.Cancellation;
|
||||
var manifest = await remote.GetManifestAsync(remoteId, ct).ConfigureAwait(false);
|
||||
var wanted = manifest.Frames.Where(f => f.WantsProxy).ToList();
|
||||
if (manifest.Proxies is not { } spec || wanted.Count == 0) return;
|
||||
var originals = frames.Where(f => f.Sha256 is not null).GroupBy(f => f.Sha256!.ToLowerInvariant()).ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal);
|
||||
var records = (await remote.ListFramesAsync(remoteId, ct).ConfigureAwait(false)).ToDictionary(f => f.Id);
|
||||
var camera = new SidecarCamera { Make = local.CameraMake, Model = local.CameraModel, Serial = local.CameraSerial };
|
||||
var decoder = sync.Options.Archive.Decoder;
|
||||
var progress = new TransferProgress(context, wanted.Count, 0);
|
||||
await progress.ReportAsync().ConfigureAwait(false);
|
||||
int sent = 0, unsent = 0;
|
||||
using var refused = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
try
|
||||
{
|
||||
await Parallel.ForEachAsync(wanted, Workers(sync.Options.Uploads, refused.Token), async (frame, token) =>
|
||||
{
|
||||
var jpeg = originals.TryGetValue(frame.Sha256.ToLowerInvariant(), out var original) && records.TryGetValue(frame.Id, out var record)
|
||||
? await Task.Run(() => MakeProxy(decoder, paths.FromRelative(original.RelativePath), RenderNodeClient.ToFrame(record), camera, spec, frame.FileName,
|
||||
token), token).ConfigureAwait(false)
|
||||
: null;
|
||||
try
|
||||
{
|
||||
if (jpeg is not null)
|
||||
{
|
||||
await remote.UploadProxyAsync(frame.Id, jpeg, token).ConfigureAwait(false);
|
||||
Interlocked.Increment(ref sent);
|
||||
}
|
||||
else
|
||||
{
|
||||
Interlocked.Increment(ref unsent);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is NotSupportedException or TitanoApiException { Status: HttpStatusCode.Conflict or HttpStatusCode.Forbidden })
|
||||
{
|
||||
// The server takes no proxies (or none from this account): nothing more to send.
|
||||
Core.Diagnostics.Log.Info("sync", $"Proxies of {local.DisplayTitle} not sent: {ex.Message}");
|
||||
await refused.CancelAsync().ConfigureAwait(false);
|
||||
}
|
||||
catch (TitanoApiException ex) when (!ex.IsTransient)
|
||||
{
|
||||
// This proxy only (not a JPEG of that frame, a frame gone meanwhile): the others still go.
|
||||
Core.Diagnostics.Log.Warning("sync", $"The proxy of {frame.FileName} was refused", ex);
|
||||
Interlocked.Increment(ref unsent);
|
||||
}
|
||||
await progress.StepAsync().ConfigureAwait(false);
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (refused.IsCancellationRequested && !ct.IsCancellationRequested)
|
||||
{
|
||||
unsent = wanted.Count - sent;
|
||||
}
|
||||
context.Log(Loc.L("Archive.Log.ProxiesSent", sent, unsent));
|
||||
}
|
||||
|
||||
/// <summary>A proxy made here for the server; null (logged) when this PC cannot decode or read the original either.</summary>
|
||||
private static byte[]? MakeProxy(Core.Imaging.ImageDecoder decoder, string path, Frame frame, SidecarCamera camera, ProxySpec spec, string name,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ArchiveImages.MakeProxy(decoder, path, frame, camera, spec.LongEdge, spec.Quality, cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or NotSupportedException)
|
||||
{
|
||||
Core.Diagnostics.Log.Warning("sync", $"No proxy of {name} could be made", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DownloadOriginalsAsync(JobContext context, ISyncRemote remote, Guid remoteId, Guid? targetArchive)
|
||||
{
|
||||
var ct = context.Cancellation;
|
||||
|
||||
@@ -142,6 +142,25 @@ public sealed class TitanoClient : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends the proxy of a frame whose original the server cannot decode (<see cref="ManifestFrame.WantsProxy"/>): a JPEG
|
||||
/// with the frame's capture time in its Exif, replacing the server's. A transient failure is retried.
|
||||
/// </summary>
|
||||
public Task UploadProxyAsync(Guid frameId, byte[] jpeg, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(jpeg);
|
||||
return RetryAsync(async () =>
|
||||
{
|
||||
await Bandwidth.AcquireAsync(jpeg.Length, ct).ConfigureAwait(false);
|
||||
using var content = new ByteArrayContent(jpeg);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
|
||||
using var response = await SendRawAsync(new HttpRequestMessage(HttpMethod.Put, $"api/v1/frames/{frameId}/proxy") { Content = content }, ct)
|
||||
.ConfigureAwait(false);
|
||||
await EnsureSuccessAsync(response, ct).ConfigureAwait(false);
|
||||
return true;
|
||||
}, ct);
|
||||
}
|
||||
|
||||
// ---------------- Uploads ----------------
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -37,7 +37,10 @@ public sealed record NodeEncoder(string Codec, string Name, bool Hardware, bool
|
||||
public sealed record RenderNodeDto(Guid Id, string ClientName, string Username, IReadOnlyList<NodeEncoder> Encoders, DateTimeOffset LastHeartbeat, string State);
|
||||
|
||||
/// <summary><c>POST /api/v1/nodes/{id}/claim</c>: the job and everything needed to run it, or 204 when none matches.</summary>
|
||||
public sealed record ClaimedJob(JobDto Job, Guid SequenceId, string SettingsJson, string PresetJson, bool UseProxies, IReadOnlyList<ManifestFrame> Frames);
|
||||
/// <param name="Frames">Every frame of the sequence; a node renders none flagged missing, corrupt or excluded.</param>
|
||||
/// <param name="Proxies">How to make the proxies the server asks for (<see cref="ManifestFrame.WantsProxy"/>); null when it makes none.</param>
|
||||
public sealed record ClaimedJob(JobDto Job, Guid SequenceId, string SettingsJson, string PresetJson, bool UseProxies, IReadOnlyList<ManifestFrame> Frames,
|
||||
ProxySpec? Proxies = null);
|
||||
|
||||
/// <summary><c>POST /api/v1/nodes/{id}/jobs/{jobId}/progress</c>.</summary>
|
||||
public sealed record NodeProgress(long Done, long Total, double? ItemsPerSecond, string? LogLine);
|
||||
|
||||
@@ -65,6 +65,7 @@ public sealed record FrameDto(
|
||||
long Flags);
|
||||
|
||||
/// <summary><c>GET /api/v1/sequences/{id}/manifest</c>: what a client needs to diff a sequence (BRIEF §14.5).</summary>
|
||||
/// <param name="Proxies">How to make the proxies the server asks for (<see cref="ManifestFrame.WantsProxy"/>); null when it makes none.</param>
|
||||
public sealed record SequenceManifest(
|
||||
Guid SequenceId,
|
||||
int FramesVersion,
|
||||
@@ -72,9 +73,21 @@ public sealed record SequenceManifest(
|
||||
string? SettingsJson,
|
||||
IReadOnlyList<string> Tags,
|
||||
IReadOnlyList<ManifestFrame> Frames,
|
||||
IReadOnlyList<RenderDto> Renders);
|
||||
IReadOnlyList<RenderDto> Renders,
|
||||
ProxySpec? Proxies = null);
|
||||
|
||||
public sealed record ManifestFrame(Guid Id, int Index, string FileName, long Size, string Sha256, bool HasProxy, bool HasThumb);
|
||||
/// <summary>One frame of a manifest: its content, whether the server has a proxy and thumbnail of it, and its flags.</summary>
|
||||
/// <param name="Sha256">Lower-case hex SHA-256; empty while the server has not hashed it (a folder adopted and not verified yet).</param>
|
||||
/// <param name="Flags">The frame's <c>FrameFlags</c> bits (missing, corrupt, excluded…).</param>
|
||||
/// <param name="WantsProxy">
|
||||
/// The server cannot decode this original and has no proxy of it (BRIEF §2.3, §8.1: HEIF): a client or render node that
|
||||
/// decodes it sends one with <c>PUT /api/v1/frames/{id}/proxy</c>, made as <see cref="SequenceManifest.Proxies"/> says.
|
||||
/// </param>
|
||||
public sealed record ManifestFrame(Guid Id, int Index, string FileName, long Size, string Sha256, bool HasProxy, bool HasThumb, long Flags = 0,
|
||||
bool WantsProxy = false);
|
||||
|
||||
/// <summary>How the server's proxies are made (BRIEF §8.5): long edge in pixels (0 = full size) and JPEG quality.</summary>
|
||||
public sealed record ProxySpec(int LongEdge, int Quality);
|
||||
|
||||
public sealed record RenderDto(
|
||||
Guid Id,
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
using System.Globalization;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
@@ -8,7 +14,8 @@ namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Streamed archive files with Range support (BRIEF §14.3): originals, proxies, thumbnails and exports. Paths come
|
||||
/// from the database by id and are resolved under the archive root only.
|
||||
/// from the database by id and are resolved under the archive root only. Proxies of frames the server cannot decode
|
||||
/// arrive from clients and render nodes (<c>PUT /frames/{id}/proxy</c>, BRIEF §2.3).
|
||||
/// </summary>
|
||||
public static class FileEndpoints
|
||||
{
|
||||
@@ -21,6 +28,8 @@ public static class FileEndpoints
|
||||
api.MapGet("/frames/{id:guid}/original", OriginalAsync).Requires(Permissions.Edit | Permissions.RenderNode);
|
||||
api.MapGet("/frames/{id:guid}/proxy", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Proxy, db, a, l))
|
||||
.Requires(Permissions.Read | Permissions.RenderNode);
|
||||
// Made by clients that edit and by render nodes, which decode what the server cannot.
|
||||
api.MapPut("/frames/{id:guid}/proxy", StoreProxyAsync).Requires(Permissions.Edit | Permissions.RenderNode);
|
||||
api.MapGet("/frames/{id:guid}/thumb", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Thumbnail, db, a, l))
|
||||
.Requires(Permissions.Read);
|
||||
api.MapGet("/renders/{id:guid}/file", RenderAsync).Requires(Permissions.Read);
|
||||
@@ -48,6 +57,47 @@ public static class FileEndpoints
|
||||
?? ApiErrors.NotFound(context, "asset_missing");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Files the body (a JPEG made from the original, its Exif capture time the frame's, at most
|
||||
/// <see cref="ArchiveImages.MaxProxyBytes"/>) as the frame's proxy; the sequence's thumbnails and proxy state follow as
|
||||
/// background jobs. 204; 413 <c>too_large</c>, 422 <c>invalid_proxy</c> or <c>proxy_mismatch</c>, 409
|
||||
/// <c>proxies_disabled</c>, 503 <c>archive_offline</c>.
|
||||
/// </summary>
|
||||
private static async Task<IResult> StoreProxyAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive,
|
||||
[FromServices] ArchiveService? archives, SequenceJobs follow)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Frames.GetAsync(id, ct) is not { } frame || await db.Sequences.GetAsync(frame.SequenceId, ct) is not { } sequence)
|
||||
return ApiErrors.NotFound(context, "frame");
|
||||
if (context.Request.ContentLength is > ArchiveImages.MaxProxyBytes) return ApiErrors.Result(context, StatusCodes.Status413PayloadTooLarge, "too_large");
|
||||
// Proxies are larger than the JSON bodies Kestrel is limited to; reading past this answers 413.
|
||||
if (context.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = ArchiveImages.MaxProxyBytes;
|
||||
using var body = new MemoryStream();
|
||||
await context.Request.Body.CopyToAsync(body, ct);
|
||||
|
||||
var images = (archives ?? new ArchiveService(db, new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer(), Owner = ArchiveModule.Owner })).Images;
|
||||
ProxyStoreResult result;
|
||||
try
|
||||
{
|
||||
result = images.StoreProxy(archive.Current, sequence, frame, body.ToArray());
|
||||
}
|
||||
catch (ArchiveOfflineException)
|
||||
{
|
||||
return ApiErrors.Result(context, StatusCodes.Status503ServiceUnavailable, "archive_offline");
|
||||
}
|
||||
switch (result)
|
||||
{
|
||||
case ProxyStoreResult.Disabled:
|
||||
return ApiErrors.Conflict(context, "proxies_disabled");
|
||||
case ProxyStoreResult.Invalid:
|
||||
return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "invalid_proxy");
|
||||
case ProxyStoreResult.WrongFrame:
|
||||
return ApiErrors.Result(context, StatusCodes.Status422UnprocessableEntity, "proxy_mismatch");
|
||||
}
|
||||
await follow.ProxyArrivedAsync(sequence, ct);
|
||||
return Results.NoContent();
|
||||
}
|
||||
|
||||
private static async Task<IResult> RenderAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
|
||||
{
|
||||
if (await db.Renders.GetAsync(id, context.RequestAborted) is not { } render) return ApiErrors.NotFound(context, "render");
|
||||
|
||||
@@ -213,29 +213,24 @@ public static class SequenceEndpoints
|
||||
_ => ApiErrors.Conflict(context, "folder_missing"),
|
||||
};
|
||||
|
||||
/// <summary>Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5); ETag over the version counters.</summary>
|
||||
private static async Task<IResult> ManifestAsync(HttpContext context, Guid id, TitanoDatabase db)
|
||||
/// <summary>
|
||||
/// Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5), with the proxies the server asks
|
||||
/// for (<see cref="ServerProxies"/>); ETag over the version counters and the number of proxies asked for.
|
||||
/// </summary>
|
||||
private static async Task<IResult> ManifestAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
|
||||
var etag = string.Create(CultureInfo.InvariantCulture, $"\"m{sequence.SettingsVersion}-{sequence.FramesVersion}-{sequence.RenderedCount}\"");
|
||||
var spec = ServerProxies.Spec(archive.Current);
|
||||
var frames = await ServerProxies.FramesAsync(db, archive.Current, sequence, spec, ct);
|
||||
var etag = string.Create(CultureInfo.InvariantCulture,
|
||||
$"\"m{sequence.SettingsVersion}-{sequence.FramesVersion}-{sequence.RenderedCount}-{frames.Count(f => f.WantsProxy)}\"");
|
||||
if (context.Request.Headers.IfNoneMatch == etag) return Results.StatusCode(StatusCodes.Status304NotModified);
|
||||
|
||||
var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
|
||||
var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
|
||||
var frames = new List<ManifestFrame>(sequence.FrameCount);
|
||||
var after = -1;
|
||||
while (true)
|
||||
{
|
||||
var batch = await db.Frames.ListBySequenceAsync(id, after, 1000, ct);
|
||||
frames.AddRange(batch.Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb)));
|
||||
if (batch.Count < 1000) break;
|
||||
after = batch[^1].Index;
|
||||
}
|
||||
var renders = await db.Renders.ListBySequenceAsync(id, ct);
|
||||
context.Response.Headers.ETag = etag;
|
||||
return Results.Json(new SequenceManifest(id, sequence.FramesVersion, sequence.SettingsVersion, sequence.SettingsJson, sequence.Tags, frames,
|
||||
[.. renders.Select(Dto.Render)]), ProtocolJson.Default.SequenceManifest);
|
||||
[.. renders.Select(Dto.Render)], spec), ProtocolJson.Default.SequenceManifest);
|
||||
}
|
||||
|
||||
private static async Task<IResult> FramesAsync(HttpContext context, Guid id, string? cursor, int? limit, TitanoDatabase db)
|
||||
|
||||
@@ -16,11 +16,14 @@ namespace Titano.Server.Jobs;
|
||||
/// <summary>
|
||||
/// Render nodes (BRIEF §14.7): clients with the render-node right register their encoders, heartbeat, claim queued
|
||||
/// render jobs they can encode, report progress, and upload the output in resumable chunks that become a render of
|
||||
/// the sequence. A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>).
|
||||
/// the sequence. A claimed job lists every frame with its flags (a node renders none missing, corrupt or excluded) and
|
||||
/// the proxies the server asks for (<see cref="ServerProxies"/>), which the node sends from the originals it fetched.
|
||||
/// A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>).
|
||||
/// </summary>
|
||||
public sealed class RenderNodeService(
|
||||
TitanoDatabase db,
|
||||
ServerOptions options,
|
||||
ServerArchive archive,
|
||||
UploadService uploads,
|
||||
UploadStore store,
|
||||
RenderStore renders,
|
||||
@@ -84,8 +87,9 @@ public sealed class RenderNodeService(
|
||||
continue;
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
var spec = ServerProxies.Spec(archive.Current);
|
||||
return new ClaimedJob(Dto.Job(job), sequence.Id, parameters.SettingsJson ?? sequence.SettingsJson ?? "{}", parameters.PresetJson,
|
||||
parameters.UseProxies, await ManifestFramesAsync(sequence, cancellationToken));
|
||||
parameters.UseProxies, await ServerProxies.FramesAsync(db, archive.Current, sequence, spec, cancellationToken), spec);
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
return (ClaimedJob?)null;
|
||||
@@ -234,22 +238,6 @@ public sealed class RenderNodeService(
|
||||
if (state != node.State) events.Publish(new NodeStateEvent(node.Id, node.ClientName, state));
|
||||
}
|
||||
|
||||
private async Task<List<ManifestFrame>> ManifestFramesAsync(Sequence sequence, CancellationToken cancellationToken)
|
||||
{
|
||||
var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
|
||||
var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
|
||||
var frames = new List<ManifestFrame>(sequence.FrameCount);
|
||||
var after = -1;
|
||||
while (true)
|
||||
{
|
||||
var batch = await db.Frames.ListBySequenceAsync(sequence.Id, after, 1000, cancellationToken);
|
||||
frames.AddRange(batch.Where(f => !f.Flags.HasFlag(FrameFlags.Excluded))
|
||||
.Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb)));
|
||||
if (batch.Count < 1000) return frames;
|
||||
after = batch[^1].Index;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The node has an encoder for the job's codec that handles its resolution: the encoder's own limit, else the node's
|
||||
/// (0 = no announced limit).
|
||||
|
||||
@@ -4,21 +4,27 @@ using Titano.Data;
|
||||
namespace Titano.Server.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Follow-up work after frames arrive (BRIEF §14.4): thumbnails, proxies and light analysis of the sequence, queued at
|
||||
/// background priority once per sequence while one is still waiting, and only for kinds this server can run.
|
||||
/// 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
|
||||
/// priority once per sequence while one is still waiting, and only for kinds this server can run.
|
||||
/// </summary>
|
||||
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 readonly SemaphoreSlim _gate = new(1, 1);
|
||||
|
||||
public async Task FramesAddedAsync(Sequence sequence, CancellationToken cancellationToken)
|
||||
public Task FramesAddedAsync(Sequence sequence, CancellationToken cancellationToken) => QueueAsync(sequence, AfterNewFrames, cancellationToken);
|
||||
|
||||
public Task ProxyArrivedAsync(Sequence sequence, CancellationToken cancellationToken) => QueueAsync(sequence, AfterProxy, cancellationToken);
|
||||
|
||||
private async Task QueueAsync(Sequence sequence, JobKind[] kinds, CancellationToken cancellationToken)
|
||||
{
|
||||
await _gate.WaitAsync(cancellationToken);
|
||||
try
|
||||
{
|
||||
foreach (var kind in AfterNewFrames)
|
||||
foreach (var kind in kinds)
|
||||
{
|
||||
if (!jobs.CanRun(kind) || await db.Jobs.IsQueuedAsync(kind, sequence.Id, cancellationToken)) continue;
|
||||
await jobs.Engine.EnqueueAsync(kind, [sequence.Id], sequence.DisplayTitle, priority: JobPriority.Background, cancellationToken: cancellationToken);
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Proxies the server asks for (BRIEF §2.3, §8.5): it decodes with managed decoders only, so the frames whose originals
|
||||
/// it cannot decode (HEIF) get their proxies from a Windows client or render node, made as the archive's proxy policy
|
||||
/// says. Manifests (sync and render nodes) list every frame with its flags and whether its proxy is wanted.
|
||||
/// </summary>
|
||||
public static class ServerProxies
|
||||
{
|
||||
private const int Page = 1000;
|
||||
|
||||
/// <summary>The server's decoders (BRIEF §4.2: managed only).</summary>
|
||||
private static readonly ImageDecoder Decoder = DecoderFactory.CreateServer();
|
||||
|
||||
/// <summary>The server takes this format's pixels from proxies made elsewhere.</summary>
|
||||
public static bool CannotDecode(ImageFileFormat format) => ArchiveImages.NeedsProxy(Decoder, format);
|
||||
|
||||
/// <summary>How proxies of the archive are made; null when it makes none (or its manifest cannot be read now).</summary>
|
||||
public static ProxySpec? Spec(Archive archive)
|
||||
{
|
||||
try
|
||||
{
|
||||
var policy = ManifestStore.ReadArchive(new ArchivePaths(archive.RootPath).ManifestPath).ProxyPolicy;
|
||||
return policy.LongEdge == ProxyPolicy.Disabled ? null : new ProxySpec(policy.LongEdge, policy.Quality);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every frame of <paramref name="sequence"/> 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 <paramref name="spec"/> is null.
|
||||
/// </summary>
|
||||
public static async Task<List<ManifestFrame>> FramesAsync(TitanoDatabase db, Archive archive, Sequence sequence, ProxySpec? spec,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var proxies = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
|
||||
var thumbs = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
|
||||
var folder = new ArchivePaths(archive.RootPath).Sequence(sequence.Folder);
|
||||
var frames = new List<ManifestFrame>(sequence.FrameCount);
|
||||
var after = -1;
|
||||
while (true)
|
||||
{
|
||||
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 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));
|
||||
}
|
||||
if (batch.Count < Page) return frames;
|
||||
after = batch[^1].Index;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using Titano.Core.Archiving;
|
||||
using Titano.Core.Codecs.Jpeg;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Metadata;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Testing;
|
||||
using static Titano.Data.Tests.Archiving.ArchiveFixture;
|
||||
|
||||
namespace Titano.Data.Tests.Archiving;
|
||||
|
||||
/// <summary>Proxies of originals a host cannot decode (BRIEF §2.3, §8.5): sent by a client or render node, checked, kept, thumbnailed.</summary>
|
||||
[Category("archive")]
|
||||
public sealed class ProxyTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public void OnlyFormatsAHostCannotDecodeTakeTheirPixelsFromProxies()
|
||||
{
|
||||
var server = DecoderFactory.CreateServer();
|
||||
Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.Heif), "HEIF previews are thumbnails at best");
|
||||
Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.Avif));
|
||||
Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.WebP), "no decoder at all");
|
||||
Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Cr3), "a RAW's embedded full-size preview");
|
||||
Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Dng));
|
||||
Assert.False(ArchiveImages.NeedsProxy(new ImageDecoder([]), ImageFileFormat.Jpeg), "proxies are JPEGs");
|
||||
Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Unknown));
|
||||
// A desktop whose decoders read HEIF makes the proxies a server asks for.
|
||||
Assert.False(ArchiveImages.NeedsProxy(new ImageDecoder([new NativeHeif()]), ImageFileFormat.Heif));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProxySentForAFrameThisHostCannotDecodeIsCheckedKeptAndThumbnailed()
|
||||
{
|
||||
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.Invalid, images.StoreProxy(fx.Archive, sequence, frames[0], "not a photo"u8.ToArray()));
|
||||
Assert.Equal(ProxyStoreResult.WrongFrame, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[1].CapturedAt!.Value, 1)));
|
||||
Assert.FileMissing(folder.Proxy(0));
|
||||
var proxy = Proxy(frames[0].CapturedAt!.Value, 2);
|
||||
Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], proxy));
|
||||
Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct));
|
||||
|
||||
// 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.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct));
|
||||
Assert.FileMissing(folder.Proxy(1));
|
||||
|
||||
// The thumbnail comes from the proxy; the frame without one has none yet.
|
||||
var thumbs = await images.BuildThumbnailsAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((1, 0, 1), (thumbs.Built, thumbs.Reused, thumbs.Undecodable));
|
||||
using (var pack = ThumbPackReader.Open(folder.ThumbsPackPath))
|
||||
{
|
||||
var entry = pack.Entries[0];
|
||||
Assert.Equal((FrameImageRenderer.ThumbnailLongEdge, 240), (entry.Width, entry.Height));
|
||||
}
|
||||
var again = await images.BuildThumbnailsAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((0, 1, 1), (again.Built, again.Reused, again.Undecodable));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AProxyLeftByARenumberedFrameIsAskedForAgain()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (sequence, frames) = await HeifSequenceAsync(fx);
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
Directory.CreateDirectory(folder.Proxies);
|
||||
await File.WriteAllBytesAsync(folder.Proxy(1), Proxy(frames[0].CapturedAt!.Value, 3), fx.Ct);
|
||||
|
||||
var proxies = await fx.Service.Images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
|
||||
Assert.Equal(2, proxies.Undecodable);
|
||||
Assert.FileMissing(folder.Proxy(1));
|
||||
Assert.False((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnArchiveWithoutProxiesRefusesThem()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (sequence, frames) = await HeifSequenceAsync(fx);
|
||||
var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath);
|
||||
ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { ProxyPolicy = new ProxyPolicy { LongEdge = ProxyPolicy.Disabled } });
|
||||
|
||||
Assert.Equal(ProxyStoreResult.Disabled, fx.Service.Images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 4)));
|
||||
Assert.FileMissing(fx.Paths.Sequence(sequence.Folder).Proxy(0));
|
||||
}
|
||||
|
||||
/// <summary>Two ingested frames whose originals the archive engine takes for HEIF (its decoders read none).</summary>
|
||||
private static async Task<(Sequence Sequence, IReadOnlyList<Frame> Frames)> HeifSequenceAsync(ArchiveFixture fx)
|
||||
{
|
||||
Guid id = default;
|
||||
for (var i = 0; i < 2; i++)
|
||||
{
|
||||
var staged = fx.Stage(Start.AddSeconds(10 * i), 40 + i);
|
||||
id = (await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, null, null, "heif", staged.Path, $"IMG_{i:0000}.HEIC",
|
||||
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 sidecar = ManifestStore.ReadSequence(folder.ManifestPath);
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, sidecar with { Frames = [.. sidecar.Frames.Select(f => f with { Format = ImageFileFormat.Heif })] });
|
||||
return (sequence, await fx.Db.Frames.ListBySequenceAsync(id));
|
||||
}
|
||||
|
||||
/// <summary>A 320 × 240 JPEG proxy with <paramref name="captured"/> in its Exif, as a client makes it.</summary>
|
||||
private static byte[] Proxy(DateTime captured, int seed)
|
||||
{
|
||||
using var image = new ImageBuffer(320, 240);
|
||||
var level = 0.2f + seed % 5 * 0.1f;
|
||||
for (var c = 0; c < 3; c++) image.Plane(c).Fill(level + c * 0.05f);
|
||||
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Titano" });
|
||||
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] });
|
||||
}
|
||||
|
||||
/// <summary>A provider that decodes HEIF natively (WIC with the HEIF extension on a desktop).</summary>
|
||||
private sealed class NativeHeif : IImageDecoderProvider
|
||||
{
|
||||
public string Name => "native-heif";
|
||||
|
||||
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Heif ? DecodeTier.Native : null;
|
||||
|
||||
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,33 @@ public sealed class RenderJobTests(TestContext context)
|
||||
Assert.Equal(3, (await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.RenderedCount);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FramesThisHostCannotDecodeRenderFromTheirProxies()
|
||||
{
|
||||
await using var fx = await ProcessingFixture.CreateAsync(context);
|
||||
var sequence = await fx.AddAsync(Scene());
|
||||
var folder = fx.Folder(sequence);
|
||||
Directory.CreateDirectory(folder.Proxies);
|
||||
foreach (var frame in await fx.Db.Frames.ListBySequenceAsync(sequence.Id, cancellationToken: fx.Ct))
|
||||
{
|
||||
// The originals are HEIF this host cannot read (BRIEF §2.3): only the proxies a client sent have pixels here.
|
||||
var original = fx.Archive.Paths.FromRelative(frame.RelativePath);
|
||||
File.Copy(original, folder.Proxy(frame.Index));
|
||||
await File.WriteAllBytesAsync(original, [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8], fx.Ct);
|
||||
}
|
||||
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, sequence.Id], fx.Ct);
|
||||
await fx.StartEngineAsync();
|
||||
|
||||
// The job does not ask for proxies: the host takes them because it cannot decode the originals.
|
||||
var job = await fx.EnqueueAsync(ProcessingJobs.Render(sequence, Mjpeg, 96, 64));
|
||||
await fx.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
Assert.FileExists(Path.Combine(folder.Exports, "clip-MJPEG.avi"));
|
||||
var decoded = fx.Decoder.Requests.Where(r => r.Purpose == DecodePurpose.Render).Select(r => Path.GetDirectoryName(r.Path)).Distinct().ToList();
|
||||
Assert.SequenceEqual([folder.Proxies], decoded);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACancelledRenderLeavesNothingBehind()
|
||||
{
|
||||
|
||||
@@ -38,6 +38,9 @@ internal sealed class FakeSyncRemote : ISyncRemote
|
||||
/// <summary>Proxy and thumbnail downloads fail with a server error.</summary>
|
||||
public bool FailAssets { get; set; }
|
||||
|
||||
/// <summary>Proxies clients sent, by frame id (for sequences the server cannot decode).</summary>
|
||||
public Dictionary<Guid, byte[]> SentProxies { get; } = [];
|
||||
|
||||
public FakeSequence Add(string? title, IEnumerable<byte[]> originals, string? settingsJson = null,
|
||||
SequenceStatus status = SequenceStatus.ProxiesBuilt | SequenceStatus.ThumbsBuilt)
|
||||
{
|
||||
@@ -96,7 +99,7 @@ internal sealed class FakeSyncRemote : ISyncRemote
|
||||
{
|
||||
ManifestCalls++;
|
||||
if (Refused.Contains(id)) throw new TitanoApiException(HttpStatusCode.Forbidden, new ApiError("forbidden", "refused"));
|
||||
return Task.FromResult(Find(id).Manifest());
|
||||
return Task.FromResult(Find(id).Manifest(SentProxies.ContainsKey));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,6 +174,18 @@ internal sealed class FakeSyncRemote : ISyncRemote
|
||||
await destination.WriteAsync(bytes.AsMemory(offset), cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Like the server: only a frame whose proxy it asks for takes one, and only a JPEG.</summary>
|
||||
public Task UploadProxyAsync(Guid frameId, byte[] jpeg, CancellationToken cancellationToken)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (!Sequences.Values.Any(s => s.Undecodable && s.Frames.Any(f => f.Id == frameId))) throw NotFound();
|
||||
if (jpeg is not [0xFF, 0xD8, ..]) throw new TitanoApiException(HttpStatusCode.UnprocessableEntity, new ApiError("invalid_proxy", "not a JPEG"));
|
||||
SentProxies[frameId] = jpeg;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken)
|
||||
{
|
||||
FakeRender render;
|
||||
@@ -225,6 +240,10 @@ internal sealed class FakeSequence
|
||||
public int SettingsVersion { get; set; } = 1;
|
||||
public int FramesVersion { get; set; }
|
||||
public SequenceStatus Status { get; set; }
|
||||
|
||||
/// <summary>The server cannot decode these originals (HEIF): its manifest asks for proxies of them.</summary>
|
||||
public bool Undecodable { get; set; }
|
||||
|
||||
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
|
||||
public DateTime? FirstCapture { get; set; }
|
||||
public string? CameraModel { get; set; } = "Canon EOS R5";
|
||||
@@ -243,9 +262,10 @@ internal sealed class FakeSequence
|
||||
public SequenceDto Dto() => new(Id, Title, Title ?? "Untitled", "2026/" + Id.ToString("N"), "Canon", CameraModel, null, FirstCapture, null, Frames.Count, 10,
|
||||
Frames.Sum(f => (long)f.Bytes.Length), (long)Status, Renders.Count, null, Tags, Rating, Notes, SettingsVersion, FramesVersion, 0, null, UpdatedAt);
|
||||
|
||||
public SequenceManifest Manifest() => new(Id, FramesVersion, SettingsVersion, SettingsJson, Tags,
|
||||
[.. Frames.Select((f, i) => new ManifestFrame(f.Id, i, f.FileName, f.Bytes.Length, f.Sha256, (Status & SequenceStatus.ProxiesBuilt) != 0,
|
||||
(Status & SequenceStatus.ThumbsBuilt) != 0))], [.. Renders.Select(r => r.Dto())]);
|
||||
public SequenceManifest Manifest(Func<Guid, bool> proxySent) => new(Id, FramesVersion, SettingsVersion, SettingsJson, Tags,
|
||||
[.. Frames.Select((f, i) => new ManifestFrame(f.Id, i, f.FileName, f.Bytes.Length, f.Sha256, (Status & SequenceStatus.ProxiesBuilt) != 0 || proxySent(f.Id),
|
||||
(Status & SequenceStatus.ThumbsBuilt) != 0, WantsProxy: Undecodable && !proxySent(f.Id)))], [.. Renders.Select(r => r.Dto())],
|
||||
Undecodable ? new ProxySpec(1280, 90) : null);
|
||||
}
|
||||
|
||||
internal sealed record FakeRender(Guid Id, string FileName, string PresetName, byte[] Bytes)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Titano.Core.Codecs.Jpeg;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Imaging.Decoders;
|
||||
using Titano.Core.Localization;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Render;
|
||||
using Titano.Data.Sync;
|
||||
@@ -32,6 +34,34 @@ public sealed class SyncTransferTests(TestContext context)
|
||||
Assert.Equal(remote.Id, entry.Link!.RemoteId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnUploadSendsTheProxiesTheServerCannotMakeItself()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Holy grail", photos);
|
||||
// The server has the originals already (the Uploader sent them) but cannot decode them (HEIF, BRIEF §2.3).
|
||||
var remote = f.Remote.Add("Holy grail", photos);
|
||||
remote.Undecodable = true;
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.RefreshAsync();
|
||||
var ahead = await f.EntryAsync("Holy grail");
|
||||
Assert.Equal((SyncStatus.LocalAhead, 3), (ahead.Status, ahead.Link!.MissingRemote));
|
||||
var engine = await f.StartEngineAsync();
|
||||
|
||||
var job = await f.Sync.EnqueueUploadAsync(engine, f.Server, local);
|
||||
var done = await f.Archive.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
Assert.True(f.Remote.SentProxies.Keys.ToHashSet().SetEquals(remote.Frames.Select(r => r.Id)), "a proxy of every frame");
|
||||
foreach (var proxy in f.Remote.SentProxies.Values)
|
||||
{
|
||||
var info = JpegDecoder.ReadInfo(proxy);
|
||||
Assert.True(info.Width <= 1280 && info.Height <= 1280, "made as the server's policy says");
|
||||
}
|
||||
Assert.Contains(Loc.L("Archive.Log.ProxiesSent", 3, 0), done.LogTail);
|
||||
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AResumedUploadSendsOnlyWhatTheServerLacks()
|
||||
{
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Core.Archiving;
|
||||
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.Localization;
|
||||
using Titano.Core.Metadata;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Project;
|
||||
using Titano.Data.Archiving;
|
||||
@@ -57,6 +61,40 @@ public sealed class RenderNodeClientTests(TestContext context)
|
||||
Assert.False(Directory.EnumerateFileSystemEntries(Path.Combine(cache, "jobs")).Any(), "job folder removed");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ANodeSkipsMissingFramesFetchesUnhashedOnesAndSendsTheProxiesAskedFor()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (editor, _, token) = await server.UserAsync("alberto", "editor", renderNode: true);
|
||||
var sequenceId = await UploadSequenceAsync(editor, Photos()[..3]);
|
||||
await Uploads.UploadAsync(editor, [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8, .. Uploads.Photo(2000, 9)], "IMG_0004.HEIC", sequenceId);
|
||||
var frames = await server.Database.Frames.ListBySequenceAsync(sequenceId);
|
||||
// Frame 1 is gone from the server, frame 2 was adopted and never hashed, frame 4 is a HEIF the server cannot decode.
|
||||
File.Delete(Path.Combine(server.Options.ArchivePath, frames[0].RelativePath));
|
||||
await server.Database.Frames.UpdateFlagsAsync([frames[0].Id], FrameFlags.Missing, FrameFlags.None);
|
||||
await using (var session = await server.Database.Database.OpenAsync(context.Cancellation))
|
||||
{
|
||||
await session.ExecuteAsync("UPDATE frames SET sha256 = NULL WHERE id = $1", [frames[1].Id], context.Cancellation);
|
||||
await session.ExecuteAsync("UPDATE frames SET format = $1 WHERE id = $2", [(int)ImageFileFormat.Heif, frames[3].Id], context.Cancellation);
|
||||
}
|
||||
var job = await CreateRenderAsync(server, editor, sequenceId);
|
||||
|
||||
var requests = new RecordPaths(new SocketsHttpHandler());
|
||||
using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "studio-pc", requests) { Token = token };
|
||||
// This PC decodes HEIF (WIC with the HEIF extension on Windows).
|
||||
await using var node = Node(client, context.CreateTempDirectory("node"), decoder: new ImageDecoder([new SolidHeif(), new ManagedDecoderProvider()]));
|
||||
Assert.True(await node.RunOnceAsync(context.Cancellation), "a job was claimed");
|
||||
|
||||
var done = await JobApiTests.GetAsync(editor, job.Id);
|
||||
Assert.Equal("done", done.State, done.Error);
|
||||
Assert.DoesNotContain($"/api/v1/frames/{frames[0].Id}/original", requests.Paths);
|
||||
Assert.Contains($"/api/v1/frames/{frames[1].Id}/original", requests.Paths);
|
||||
var sequence = (await server.Database.Sequences.GetAsync(sequenceId))!;
|
||||
var proxy = new ArchivePaths(server.Options.ArchivePath).Sequence(sequence.Folder).Proxy(frames[3].Index);
|
||||
Assert.Equal(frames[3].CapturedAt, MetadataReader.Read(proxy).CapturedAt, "the proxy the server asked for, with the frame's capture time");
|
||||
Assert.Equal((96, 64), MetadataReader.Read(proxy).OrientedSize);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ANodeReportsWhyItCouldNotRender()
|
||||
{
|
||||
@@ -263,8 +301,8 @@ public sealed class RenderNodeClientTests(TestContext context)
|
||||
Assert.FileExists(part[..^".part".Length]);
|
||||
}
|
||||
|
||||
private static RenderNodeClient Node(TitanoClient client, string cache, TimeSpan? poll = null, TimeSpan? heartbeat = null) =>
|
||||
new(client, new ProcessingOptions { Archive = new ArchiveServiceOptions { Decoder = DecoderFactory.CreateServer() } },
|
||||
private static RenderNodeClient Node(TitanoClient client, string cache, TimeSpan? poll = null, TimeSpan? heartbeat = null, ImageDecoder? decoder = null) =>
|
||||
new(client, new ProcessingOptions { Archive = new ArchiveServiceOptions { Decoder = decoder ?? DecoderFactory.CreateServer() } },
|
||||
new RenderNodeOptions
|
||||
{
|
||||
ClientName = "studio-pc",
|
||||
@@ -314,6 +352,35 @@ public sealed class RenderNodeClientTests(TestContext context)
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Records the path of every request.</summary>
|
||||
private sealed class RecordPaths(HttpMessageHandler inner) : DelegatingHandler(inner)
|
||||
{
|
||||
private readonly ConcurrentQueue<string> _paths = new();
|
||||
|
||||
public IReadOnlyCollection<string> Paths => _paths;
|
||||
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
_paths.Enqueue(request.RequestUri!.AbsolutePath);
|
||||
return base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Decodes every HEIF as a mid-grey 96 × 64 frame, as a native HEIF decoder would decode a real one.</summary>
|
||||
private sealed class SolidHeif : IImageDecoderProvider
|
||||
{
|
||||
public string Name => "heif-test";
|
||||
|
||||
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Heif ? DecodeTier.Native : null;
|
||||
|
||||
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var image = new ImageBuffer(96, 64);
|
||||
for (var c = 0; c < 3; c++) image.Plane(c).Fill(0.2f);
|
||||
return new DecodedImage(image, DecodeTier.Native, Name, 96, 64);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Counts the node's claims the server answered.</summary>
|
||||
private sealed class CountClaims(HttpMessageHandler inner) : DelegatingHandler(inner)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using Titano.Core.Codecs.Jpeg;
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Metadata;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Storage;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Server.Tests.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Proxies of originals the server cannot decode (BRIEF §2.3: HEIF) arrive from clients and render nodes through
|
||||
/// <c>PUT /api/v1/frames/{id}/proxy</c> and become the frame's proxy and thumbnail.
|
||||
/// </summary>
|
||||
[Category("server")]
|
||||
public sealed class ProxyUploadTests(TestContext context)
|
||||
{
|
||||
[Test]
|
||||
public async Task AProxySentForAFrameTheServerCannotDecodeIsCheckedKeptAndThumbnailed()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var (viewer, _, _) = await server.UserAsync("vera", "viewer");
|
||||
var upload = await Uploads.UploadAsync(editor, Heif(1), "IMG_0001.HEIC", sessionKey: "roof");
|
||||
var frame = Assert.NotNull(await server.Database.Frames.GetAsync(upload.FrameId));
|
||||
Assert.Equal(ImageFileFormat.Heif, frame.Format);
|
||||
|
||||
var manifest = await ManifestAsync(editor, upload.SequenceId);
|
||||
Assert.Equal((true, false), (manifest.Frames.Single().WantsProxy, manifest.Frames.Single().HasProxy));
|
||||
Assert.Equal(new ProxySpec(1920, 92), manifest.Proxies);
|
||||
|
||||
using (var garbage = await PutAsync(editor, frame.Id, "not a photo"u8.ToArray()))
|
||||
await garbage.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "invalid_proxy");
|
||||
using (var other = await PutAsync(editor, frame.Id, Proxy(frame.CapturedAt!.Value.AddSeconds(1))))
|
||||
await other.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "proxy_mismatch");
|
||||
var proxy = Proxy(frame.CapturedAt!.Value);
|
||||
using (var refused = await PutAsync(viewer, frame.Id, proxy))
|
||||
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
using (var missing = await PutAsync(editor, Guid.CreateVersion7(), proxy))
|
||||
await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
using (var stored = await PutAsync(editor, frame.Id, proxy))
|
||||
await stored.ExpectAsync(HttpStatusCode.NoContent);
|
||||
|
||||
await Http.BytesEqualAsync(proxy, editor.GetByteArrayAsync($"/api/v1/frames/{frame.Id}/proxy"));
|
||||
var after = (await ManifestAsync(editor, upload.SequenceId)).Frames.Single();
|
||||
Assert.Equal((false, true), (after.WantsProxy, after.HasProxy));
|
||||
|
||||
// The thumbnail job the proxy queued draws the frame from it.
|
||||
var until = DateTime.UtcNow + TimeSpan.FromSeconds(30);
|
||||
while (true)
|
||||
{
|
||||
using var thumb = await editor.GetAsync($"/api/v1/frames/{frame.Id}/thumb");
|
||||
if (thumb.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
var jpeg = await thumb.Content.ReadAsByteArrayAsync();
|
||||
Assert.Equal((320, 240), (JpegDecoder.ReadInfo(jpeg).Width, JpegDecoder.ReadInfo(jpeg).Height));
|
||||
break;
|
||||
}
|
||||
Assert.True(DateTime.UtcNow < until, $"thumbnail not built: {(int)thumb.StatusCode}");
|
||||
await Task.Delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FramesTheServerDecodesAskForNoProxy()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context, services: ArchiveModule.Register);
|
||||
var (editor, _, _) = await server.UserAsync("alberto", "editor");
|
||||
var upload = await Uploads.UploadAsync(editor, Proxy(new DateTime(2026, 9, 28, 18, 2, 11)), "IMG_0001.JPG", sessionKey: "roof");
|
||||
|
||||
Assert.False((await ManifestAsync(editor, upload.SequenceId)).Frames.Single().WantsProxy);
|
||||
}
|
||||
|
||||
/// <summary>A HEIF file as far as signature sniffing goes (an <c>ftyp</c> box naming heic), with no pixels the server can read.</summary>
|
||||
private static byte[] Heif(int seed) => [0, 0, 0, 24, .. "ftypheic"u8, 0, 0, 0, 0, .. "mif1heic"u8, .. Uploads.Photo(2000, seed)];
|
||||
|
||||
/// <summary>A 320 × 240 JPEG with <paramref name="captured"/> in its Exif, as a client makes a proxy.</summary>
|
||||
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 Task<HttpResponseMessage> PutAsync(HttpClient client, Guid frameId, byte[] body)
|
||||
{
|
||||
var content = new ByteArrayContent(body);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
|
||||
return client.PutAsync($"/api/v1/frames/{frameId}/proxy", content);
|
||||
}
|
||||
|
||||
private static async Task<SequenceManifest> ManifestAsync(HttpClient client, Guid sequenceId)
|
||||
{
|
||||
using var response = await client.GetAsync($"/api/v1/sequences/{sequenceId}/manifest");
|
||||
return await response.ReadAsync(ProtocolJson.Default.SequenceManifest);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user