Files
Chronos/src/Titano.Data/Sync/ProxyCache.cs
T
Claude b79e4ed6d9 feat(sync): client sync engine with links, settings conflicts, transfers and background agent
Per-sequence state between the local index and a server from listings and
manifest diffs; version-counter settings sync with explicit conflicts; upload,
original and proxy download jobs; WebSocket-driven background agent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 22:23:23 +00:00

158 lines
6.8 KiB
C#

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;
/// <summary>
/// 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 <see cref="FrameFlags.Missing"/>, so analysis and renders
/// skip them. Not part of the local index.
/// </summary>
public sealed class RemoteSequence
{
internal RemoteSequence(string server, Sequence sequence, IReadOnlyList<Frame> frames, IReadOnlyList<string?> files)
{
Server = server;
Sequence = sequence;
Frames = frames;
Files = files;
}
/// <summary>The server it belongs to (<see cref="SyncService.ServerKey"/>).</summary>
public string Server { get; }
/// <summary>The server's sequence: its id, <see cref="SequenceOrigin.Server"/>, its settings JSON and versions; no archive.</summary>
public Sequence Sequence { get; }
public IReadOnlyList<Frame> Frames { get; }
/// <summary>Absolute path of each frame's cached proxy, in frame order; null when it is not cached.</summary>
public IReadOnlyList<string?> Files { get; }
public int CachedCount => Files.Count(f => f is not null);
/// <summary>
/// The editor's frame source over the cached proxies, decoded by <paramref name="decoder"/> through the same
/// read-ahead window as archive renders. The result also implements <see cref="Core.Analysis.Deep.IFrameSource"/>
/// and <see cref="IAsyncDisposable"/>; dispose it when done. Frames are proxy-sized while the source reports the
/// originals' size, which <see cref="IRenderFrameSource"/> allows.
/// </summary>
public IRenderFrameSource OpenFrames(ImageDecoder decoder, FrameWindowOptions? window = null)
{
var frames = new SequenceFrames(Sequence, Frames, Files);
return new SequenceFrameSource(frames, frames.LightFrames(), decoder, window);
}
}
/// <summary>What the proxy cache keeps next to the files of one server sequence.</summary>
internal sealed record ProxyCacheIndex(SequenceDto Sequence, string? SettingsJson, int FramesVersion, IReadOnlyList<FrameDto> Frames, IReadOnlyList<string?> Files);
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(ProxyCacheIndex))]
internal sealed partial class ProxyCacheJson : JsonSerializerContext;
/// <summary>
/// Proxies of server sequences on this PC: <c>&lt;root&gt;/&lt;server hash&gt;/&lt;sequence id&gt;/</c> with one JPEG per
/// frame (named by frame id, so renumbering never mixes them up) and <c>remote.json</c>. Everything here can be
/// downloaded again, so the folder lives in the client's cache.
/// </summary>
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));
/// <summary>The cached sequence, or null when it was never cached or the cache was cleared.</summary>
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);
}
/// <summary>Removes the files of a sequence the server no longer has.</summary>
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);
}
}
/// <summary>Deletes proxies of frames <paramref name="keep"/> does not name (frames removed on the server).</summary>
public static void Prune(string folder, IReadOnlySet<string> 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);
}
}
}
}