using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using Titano.Core.Diagnostics; using Titano.Core.Imaging; using Titano.Core.IO; using Titano.Core.Model; using Titano.Core.Render; using Titano.Data.Processing; using Titano.Protocol; namespace Titano.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 { internal RemoteSequence(string server, Sequence sequence, IReadOnlyList frames, IReadOnlyList 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. internal sealed record ProxyCacheIndex(SequenceDto Sequence, string? SettingsJson, int FramesVersion, IReadOnlyList Frames, IReadOnlyList Files); [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) { 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)); /// 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); var path = Path.Combine(folder, 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; } if (index is null || index.Files.Count != index.Frames.Count) 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); } /// 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); } } } }