using Chronos.Core.Archiving; using Chronos.Core.Imaging; using Chronos.Core.Model; using Chronos.Data; using Chronos.Data.Archiving; using Chronos.Protocol; namespace Chronos.Server.Storage; /// /// 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. /// public static class ServerProxies { private const int Page = 1000; /// The server takes this format's pixels from proxies made elsewhere (its decoders are managed only, BRIEF §4.2). public static bool CannotDecode(ImageFileFormat format) => ArchiveImages.ServerNeedsProxy(format); /// How proxies of the archive are made; null when it makes none (or its manifest cannot be read now). 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; } } /// /// Whether the proxy of , one the server cannot decode, is in : the /// frame's own (: one left at its index by a frame renumbered since is another /// photo's) and as large as asks (null: the archive makes none, or its manifest cannot be read /// now, so the size is not judged). /// public static bool HasProxy(SequencePaths folder, Frame frame, ProxySpec? spec) => ArchiveImages.IsProxyOf(folder.Proxy(frame.Index), frame.CapturedAt, frame.Width, frame.Height, spec is null ? null : new ProxyPolicy { LongEdge = spec.LongEdge, Quality = spec.Quality }); /// /// Every frame of in index order, as a manifest lists it. A frame the server cannot decode /// has its proxy when its own is there (), whatever the sequence's built status says (frames /// added or renumbered since keep it until the next proxy pass), and wants one otherwise (unless it is missing or /// damaged, or is null); any other frame has one when the server built them all. /// public static async Task> FramesAsync(ChronosRepositories 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(sequence.FrameCount); var after = -1; while (true) { var batch = await db.Frames.ListBySequenceAsync(sequence.Id, after, Page, cancellationToken); foreach (var f in batch) { var elsewhere = CannotDecode(f.Format); var has = elsewhere ? HasProxy(folder, f, spec) : proxies; var wanted = spec is not null && elsewhere && !has && (f.Flags & (FrameFlags.Missing | FrameFlags.Corrupt)) == 0; frames.Add(new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, has, thumbs, (long)f.Flags, wanted)); } if (batch.Count < Page) return frames; after = batch[^1].Index; } } }