using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using Chronos.Core.Diagnostics; using Chronos.Core.Imaging; using Chronos.Core.IO; using Chronos.Core.Model; using Chronos.Core.Render; using Chronos.Data.Processing; using Chronos.Protocol; namespace Chronos.Data.Sync; /// /// A server sequence opened with proxies (BRIEF ยง14.5 "edit with proxies, render where the originals are"): the /// sequence and frame rows as the server lists them, and for each frame its cached proxy (a thumbnail when the server /// had no proxy). Frames without a cached file are flagged , so analysis and renders /// skip them. Not part of the local index. /// public sealed class RemoteSequence { /// Absolute path of each frame's proxy in frame order; null (and the frame flagged missing) when it is not cached. public RemoteSequence(string server, Sequence sequence, IReadOnlyList frames, IReadOnlyList files) { if (files.Count != frames.Count) throw new ArgumentException("One file entry per frame is required.", nameof(files)); Server = server; Sequence = sequence; Frames = frames; Files = files; } /// The server it belongs to (). public string Server { get; } /// The server's sequence: its id, , its settings JSON and versions; no archive. public Sequence Sequence { get; } public IReadOnlyList Frames { get; } /// Absolute path of each frame's cached proxy, in frame order; null when it is not cached. public IReadOnlyList Files { get; } public int CachedCount => Files.Count(f => f is not null); /// /// The editor's frame source over the cached proxies, decoded by through the same /// read-ahead window as archive renders. The result also implements /// and ; dispose it when done. Frames are proxy-sized while the source reports the /// originals' size, which allows. /// public IRenderFrameSource OpenFrames(ImageDecoder decoder, FrameWindowOptions? window = null) { var frames = new SequenceFrames(Sequence, Frames, Files); return new SequenceFrameSource(frames, frames.LightFrames(), decoder, window); } } /// What the proxy cache keeps next to the files of one server sequence. /// /// What each file is, in frame order: or (a thumbnail standing in /// while the server has no proxy of the frame); null where no file is cached, and for a cache written before kinds were kept. /// internal sealed record ProxyCacheIndex(SequenceDto Sequence, string? SettingsJson, int FramesVersion, IReadOnlyList Frames, IReadOnlyList Files, IReadOnlyList? Kinds = null); [JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] [JsonSerializable(typeof(ProxyCacheIndex))] internal sealed partial class ProxyCacheJson : JsonSerializerContext; /// /// Proxies of server sequences on this PC: <root>/<server hash>/<sequence id>/ with one JPEG per /// frame (named by frame id, so renumbering never mixes them up) and remote.json. Everything here can be /// downloaded again, so the folder lives in the client's cache. /// internal sealed class ProxyCache(string root) { /// The asset kind of a cached proxy (the server's name for it). public const string Proxy = "proxy"; /// The asset kind of a thumbnail cached in place of a proxy the server did not have. public const string Thumb = "thumb"; private const string IndexFile = "remote.json"; public string Folder(string server, Guid remoteId) => Path.Combine(root, Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(server)))[..16], remoteId.ToString("N")); public static string FileName(Guid frameId) => frameId.ToString("N") + ".jpg"; public bool Exists(string server, Guid remoteId) => File.Exists(Path.Combine(Folder(server, remoteId), IndexFile)); public void Save(string server, Guid remoteId, ProxyCacheIndex index) => AtomicFile.WriteAllText(Path.Combine(Folder(server, remoteId), IndexFile), JsonSerializer.Serialize(index, ProxyCacheJson.Default.ProxyCacheIndex)); /// /// What each cached file of a server sequence is, by frame id (): none for a frame /// without a file, null for a file of a cache written before kinds were kept. /// public IReadOnlyDictionary Kinds(string server, Guid remoteId) { var kinds = new Dictionary(); if (ReadIndex(server, remoteId) is not { } index) return kinds; for (var i = 0; i < index.Frames.Count; i++) { if (index.Files[i] is not null) kinds[index.Frames[i].Id] = index.Kinds is { } known && i < known.Count ? known[i] : null; } return kinds; } /// The cached sequence, or null when it was never cached or the cache was cleared. public RemoteSequence? Load(string server, Guid remoteId) { var folder = Folder(server, remoteId); if (ReadIndex(server, remoteId) is not { } index) return null; var dto = index.Sequence; var sequence = RenderNodeClient.ToSequence(dto) with { Tags = dto.Tags, Rating = dto.Rating, Notes = dto.Notes, MedianInterval = dto.MedianInterval, TotalBytes = dto.TotalBytes, Status = (SequenceStatus)dto.Status, HeroFrameId = dto.HeroFrameId, SettingsJson = index.SettingsJson, SettingsVersion = dto.SettingsVersion, FramesVersion = index.FramesVersion, Origin = SequenceOrigin.Server, SyncState = SyncState.RemoteOnly, OwnerUserId = dto.OwnerUserId, UpdatedAt = dto.UpdatedAt, }; var files = index.Files.Select(f => f is not null && File.Exists(Path.Combine(folder, f)) ? Path.Combine(folder, f) : null).ToList(); var frames = index.Frames.Select((f, i) => { var frame = RenderNodeClient.ToFrame(f); return files[i] is null ? frame with { Flags = frame.Flags | FrameFlags.Missing } : frame; }).ToList(); return new RemoteSequence(server, sequence, frames, files); } /// The index of a cached sequence; null when there is none or it cannot be read. private ProxyCacheIndex? ReadIndex(string server, Guid remoteId) { var path = Path.Combine(Folder(server, remoteId), IndexFile); if (!File.Exists(path)) return null; ProxyCacheIndex? index; try { index = JsonSerializer.Deserialize(File.ReadAllText(path), ProxyCacheJson.Default.ProxyCacheIndex); } catch (Exception ex) when (ex is JsonException or IOException) { Log.Warning("sync", $"Proxy cache of {remoteId} is unreadable", ex); return null; } return index is null || index.Files.Count != index.Frames.Count ? null : index; } /// Removes the files of a sequence the server no longer has. public void Delete(string server, Guid remoteId) { var folder = Folder(server, remoteId); try { if (Directory.Exists(folder)) Directory.Delete(folder, recursive: true); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { Log.Warning("sync", $"Proxy cache {folder} could not be removed", ex); } } /// Deletes proxies of frames does not name (frames removed on the server). public static void Prune(string folder, IReadOnlySet keep) { foreach (var file in Directory.EnumerateFiles(folder, "*.jpg")) { if (keep.Contains(Path.GetFileName(file))) continue; try { File.Delete(file); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { Log.Warning("sync", $"Stale proxy {file} could not be removed", ex); } } } }