using System.Collections.Concurrent; using System.Diagnostics; using System.Globalization; using System.Net; using System.Text.Json; using Titano.Core.Archiving; using Titano.Core.Diagnostics; using Titano.Core.IO; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Core.Project; using Titano.Data.Archiving; using Titano.Data.Jobs; using Titano.Data.Processing; using Titano.Data.Repositories; using Titano.Protocol; using Titano.Protocol.Client; using SequenceFrames = Titano.Data.Processing.SequenceFrames; using SequenceManifest = Titano.Protocol.SequenceManifest; namespace Titano.Data.Sync; /// Settings of a . public sealed record SyncOptions { /// Decoders, lease owner and lease timing for sidecar and project writes (shared with the archive engine). public ArchiveServiceOptions Archive { get; init; } = new(); /// Folder of the proxy cache of server sequences (inside the client's cache). public required string ProxyCacheDirectory { get; init; } /// Concurrent frame downloads of one job. public int Downloads { get; init; } = 4; /// Concurrent file uploads of one job (the server accepts 8 chunks in flight per user). public int Uploads { get; init; } = 2; } /// /// Client side of sync (BRIEF §4.4, §14.5). Compares every server sequence with its local copy from the server listing /// and, when frame counters or render counts moved, the manifest: originals are matched by SHA-256 and never conflict; /// renders by id. Settings, title, tags, rating and notes travel together under the settings version, so a change on /// one side is sent or taken (automatically or on request) and a change on both sides with different content is a /// conflict the user settles with , against the versions the user was shown. The settings' /// frame positions are translated by photo to the frames of the copy they go to (). Uploads and /// downloads run as jobs (): 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 ; 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 sync_state for the Library. A failure while /// comparing one sequence is recorded on its link and does not stop the others. /// public sealed class SyncService { private const string LogCategory = "sync"; private const string SessionKeyPrefix = "titano-sync:"; /// Recomputes states without sending or taking settings on the user's behalf. internal static readonly SyncPolicy CompareOnly = new(AutoSyncSettings: false); private readonly TitanoDatabase _db; private readonly SyncLinkStore _links; private readonly SequenceWriter _writer; private readonly SemaphoreSlim _gate = new(1, 1); /// Unlinked pairs whose manifests were compared at these frame versions without a match (not fetched again). private readonly ConcurrentDictionary _unmatched = new(StringComparer.Ordinal); public SyncService(TitanoDatabase db, ArchiveService archives, SyncOptions options) { _db = db; Archives = archives; Options = options; _links = new SyncLinkStore(db); _writer = new SequenceWriter(options.Archive); Proxies = new ProxyCache(options.ProxyCacheDirectory); } public SyncOptions Options { get; } /// The links or local states of a server (the argument) changed: refresh, resolution, transfer; raised on any thread. public event Action? Changed; internal ProxyCache Proxies { get; } internal SyncLinkStore Links => _links; internal TitanoDatabase Database => _db; internal ArchiveService Archives { get; } /// "https://nas.local:8443": the key links are stored under (scheme, lower-case host, port, path). public static string ServerKey(Uri address) => address.GetLeftPart(UriPartial.Path).TrimEnd('/').ToLowerInvariant(); /// What an upload or download job moves; null for any other job. public static SyncTransferKind? TransferKind(Job job) => SyncJobParameters.Read(job)?.Kind(job.Kind); /// Session key the local archive groups the downloaded originals of one server sequence by. internal static string SessionKey(Guid remoteId) => SessionKeyPrefix + remoteId.ToString("N"); // ---------------- Table ---------------- /// /// Lists the server, compares every sequence with its local copy, sends or takes settings changed on one side when /// is on, forgets sequences the server confirms deleted, links unlinked /// copies holding the same originals, and stores the states. /// public async Task RefreshAsync(ISyncRemote remote, SyncPolicy policy, CancellationToken cancellationToken) { var clock = Stopwatch.StartNew(); var listing = await remote.ListSequencesAsync(cancellationToken).ConfigureAwait(false); Dictionary locals; await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { var links = (await _links.ListAsync(remote.Server, cancellationToken).ConfigureAwait(false)).ToDictionary(l => l.RemoteId); locals = await LocalSequencesAsync(cancellationToken).ConfigureAwait(false); foreach (var dto in listing) { links.Remove(dto.Id, out var link); var local = await LocalOfAsync(link, locals, cancellationToken).ConfigureAwait(false); await ReconcileSafelyAsync(remote, Observe(link, remote.Server, dto), dto, local, locals, policy, cancellationToken).ConfigureAwait(false); } // Not in the listing: deleted on the server, or created or moved while its pages were read. Only a 404 forgets a link. foreach (var link in links.Values) await CompareOneAsync(remote, link.RemoteId, link, locals, policy, cancellationToken).ConfigureAwait(false); await MatchByContentAsync(remote, listing, locals, policy, cancellationToken).ConfigureAwait(false); } finally { _gate.Release(); } var entries = await StoreStatesAsync(remote.Server, locals, cancellationToken).ConfigureAwait(false); Changed?.Invoke(remote.Server); return new SyncSnapshot(remote.Server, entries, DateTimeOffset.UtcNow, clock.Elapsed); } /// Compares the given server sequences again (live events, finished transfers); a sequence gone from the server is forgotten. public async Task RefreshSequencesAsync(ISyncRemote remote, IReadOnlyCollection remoteIds, SyncPolicy policy, CancellationToken cancellationToken) { await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { foreach (var id in remoteIds) { var link = await _links.GetAsync(remote.Server, id, cancellationToken).ConfigureAwait(false); await CompareOneAsync(remote, id, link, null, policy, cancellationToken).ConfigureAwait(false); } } finally { _gate.Release(); } await StoreStatesAsync(remote.Server, null, cancellationToken).ConfigureAwait(false); Changed?.Invoke(remote.Server); } /// /// The sync table of as last compared: every server sequence and every local one, with /// active transfers; conflicts first, then transfers, then by title. /// public Task> ListAsync(string server, CancellationToken cancellationToken) => EntriesAsync(server, null, cancellationToken); /// /// Links a local sequence to a server sequence holding the same originals. Nothing was agreed yet, so the next /// comparison settles equal settings and reports different ones as a conflict. /// public async Task LinkAsync(string server, Guid remoteId, Guid localId, CancellationToken cancellationToken = default) { var local = await _db.Sequences.GetAsync(localId, cancellationToken).ConfigureAwait(false) ?? throw new ArgumentException($"Unknown sequence {localId}.", nameof(localId)); await LinkAsync(server, remoteId, local, cancellationToken).ConfigureAwait(false); } // ---------------- Settings ---------------- /// /// Settles a conflict as the user chose (BRIEF §12.7), against the versions the user was shown: when either side /// changed again since, nothing is written, the sequence is compared again and /// is thrown. Returns the file this PC's settings were saved to for , else null. /// public async Task ResolveAsync(ISyncRemote remote, Guid remoteId, SyncResolution resolution, CancellationToken cancellationToken) { string? saved = null; var changed = false; await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { var (link, local) = await LinkedAsync(remote.Server, remoteId, cancellationToken).ConfigureAwait(false); if (link is not { ConflictRemote: not null, ConflictLocalSettings: { } seenLocal, ConflictRemoteSettings: { } seenRemote } || local.SettingsVersion != seenLocal) { changed = true; } else if (resolution == SyncResolution.KeepMine) { try { var manifest = await remote.GetManifestAsync(remoteId, cancellationToken).ConfigureAwait(false); // Edited here since it was shown, or frames that kept changing while they were read: compared again. if (await RequestAsync(remote, local, manifest, seenRemote, cancellationToken).ConfigureAwait(false) is not { } request) { changed = true; } else { var updated = await remote.UpdateSequenceAsync(remoteId, request, cancellationToken).ConfigureAwait(false); link = Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion); } } catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict) { changed = true; } } else { // The server's version as it is now: the one shown, with its positions on the server's current frames. var dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false); var manifest = dto.SettingsVersion == seenRemote ? await remote.GetManifestAsync(remoteId, cancellationToken).ConfigureAwait(false) : null; if (manifest?.SettingsVersion != seenRemote) { changed = true; } else { if (resolution == SyncResolution.SaveBoth) saved = await SaveCopyAsync(local, cancellationToken).ConfigureAwait(false); var theirs = SyncPayload.Of(dto, manifest!.SettingsJson); if (await ApplyLocallyAsync(local, theirs, Positions(remote, local, manifest), cancellationToken).ConfigureAwait(false) is { } applied) { link = Agreed(Observe(link, remote.Server, dto), applied.SettingsVersion, seenRemote); } else { // Edited on this PC since it was read: the copy of a version the user did not choose to keep goes. changed = true; if (saved is not null) File.Delete(saved); saved = null; } } } if (!changed) { await _links.UpsertAsync(link with { SyncedAt = DateTimeOffset.UtcNow }, cancellationToken).ConfigureAwait(false); Log.Info(LogCategory, $"Conflict on {local.DisplayTitle} settled: {resolution}"); } } finally { _gate.Release(); } if (changed) { await RefreshSequencesAsync(remote, [remoteId], CompareOnly, cancellationToken).ConfigureAwait(false); throw new SyncConflictChangedException(Loc.L("Errors.Sync.ChangedAgain")); } await StoreStatesAsync(remote.Server, null, cancellationToken).ConfigureAwait(false); Changed?.Invoke(remote.Server); return saved; } /// Sends or takes settings changed on one side only (the row action while automatic sync is off). public Task SyncSettingsAsync(ISyncRemote remote, Guid remoteId, SyncPolicy policy, CancellationToken cancellationToken) => RefreshSequencesAsync(remote, [remoteId], policy with { AutoSyncSettings = true }, cancellationToken); // ---------------- Transfers ---------------- /// Queues the upload of a local sequence's originals and settings, unless one is already waiting or running. public Task EnqueueUploadAsync(JobEngine engine, string server, Sequence local, CancellationToken cancellationToken = default) => EnqueueAsync(engine, JobKind.UploadCommit, local.Id, local.DisplayTitle, new SyncJobParameters(server), JobPriority.User, cancellationToken); /// Queues sending the renders of a local copy the server lacks (BRIEF §14.5), unless that is already waiting or running. public Task EnqueueSendExportsAsync(JobEngine engine, string server, Sequence local, CancellationToken cancellationToken = default) => EnqueueAsync(engine, JobKind.UploadCommit, local.Id, local.DisplayTitle, new SyncJobParameters(server, Upload: SyncUploadMode.Exports), JobPriority.User, cancellationToken); /// /// Queues a download of a server sequence, unless the same one is already waiting or running: originals into /// (ignored when the sequence already has a local copy), proxies into the cache, or the /// server's renders into the local copy. A background proxy download gives way to the user's request for the same /// proxies (queued again at the user's priority) or for the originals. /// public Task EnqueueDownloadAsync(JobEngine engine, string server, Guid remoteId, string title, SyncDownloadMode mode, Guid? archiveId, JobPriority priority = JobPriority.User, CancellationToken cancellationToken = default) => EnqueueAsync(engine, JobKind.Download, remoteId, title, new SyncJobParameters(server, mode, archiveId), priority, cancellationToken); /// Handlers for and ; register them on the client's engine. public IReadOnlyList CreateJobHandlers(ISyncConnector connector) { var transfers = new SyncTransfers(this); return [new SyncJobHandler(JobKind.UploadCommit, transfers, connector), new SyncJobHandler(JobKind.Download, transfers, connector)]; } /// A server sequence from the proxy cache, for the editor; null when its proxies were never fetched or were cleared. public RemoteSequence? OpenRemote(string server, Guid remoteId) => Proxies.Load(server, remoteId); /// The link of a local sequence to a sequence of ; null when the server does not have it. public Task LinkOfAsync(string server, Guid localId, CancellationToken cancellationToken = default) => _links.FindByLocalAsync(server, localId, cancellationToken); // ---------------- Engine ---------------- /// Server ids of the sequences an upload or download job is working on. internal async Task> TransferringAsync(string server, CancellationToken cancellationToken) { var transfers = await _links.ActiveTransfersAsync(cancellationToken).ConfigureAwait(false); var links = await _links.ListAsync(server, cancellationToken).ConfigureAwait(false); return links.Where(l => transfers.ContainsKey(l.RemoteId) || (l.LocalId is { } id && transfers.ContainsKey(id))).Select(l => l.RemoteId).ToHashSet(); } /// Creates or updates the link of a local sequence to a server sequence (an upload or download made it). internal async Task LinkAsync(string server, Guid remoteId, Sequence local, CancellationToken cancellationToken) { await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { var link = await _links.GetAsync(server, remoteId, cancellationToken).ConfigureAwait(false) ?? new SyncLink { Server = server, RemoteId = remoteId, Title = local.DisplayTitle, RemoteUpdatedAt = DateTimeOffset.UtcNow }; if (link.LocalId != local.Id) link = Unlinked(link) with { LocalId = local.Id }; await _links.UpsertAsync(link, cancellationToken).ConfigureAwait(false); return link; } finally { _gate.Release(); } } /// Forgets a link whose server sequence an upload found deleted (its originals go into a new one). internal async Task ForgetGoneAsync(string server, Guid remoteId, CancellationToken cancellationToken) { await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (await _links.GetAsync(server, remoteId, cancellationToken).ConfigureAwait(false) is { } link) await ForgetAsync(link, cancellationToken).ConfigureAwait(false); } finally { _gate.Release(); } } /// /// Makes the server's copy equal to this PC's (an upload sends the settings with the originals of the sequence it /// created); the server's own processing may edit the new sequence meanwhile, so a refused edit is sent again. /// internal async Task PushAllAsync(ISyncRemote remote, Guid remoteId, CancellationToken cancellationToken) { await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { var (link, local) = await LinkedAsync(remote.Server, remoteId, cancellationToken).ConfigureAwait(false); for (var attempt = 1; ; attempt++) { var manifest = await remote.GetManifestAsync(remoteId, cancellationToken).ConfigureAwait(false); try { if (await RequestAsync(remote, local, manifest, manifest.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } request) { // An edit saved here meanwhile goes instead; frames that keep changing here leave it to the next comparison. if (attempt >= 3 || await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false) is not { } fresh) return; local = fresh; continue; } var updated = await remote.UpdateSequenceAsync(remoteId, request, cancellationToken).ConfigureAwait(false); await _links.UpsertAsync(Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion), cancellationToken) .ConfigureAwait(false); return; } catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict && attempt < 3) { Log.Info(LogCategory, $"{local.DisplayTitle} changed on the server while its settings were sent; sending again"); } } } finally { _gate.Release(); } } /// Makes this PC's copy equal to the server's (a sequence a download just created takes the server's settings). internal async Task TakeAllAsync(ISyncRemote remote, Guid remoteId, CancellationToken cancellationToken) { await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { var (link, local) = await LinkedAsync(remote.Server, remoteId, cancellationToken).ConfigureAwait(false); var dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false); var manifest = await remote.GetManifestAsync(remoteId, cancellationToken).ConfigureAwait(false); // Edited on this PC meanwhile: nothing is agreed yet, so the next comparison settles both sides' content. if (await ApplyLocallyAsync(local, SyncPayload.Of(dto, manifest.SettingsJson), Positions(remote, local, manifest), cancellationToken) .ConfigureAwait(false) is not { } applied) return; await _links.UpsertAsync(Agreed(Observe(link, remote.Server, dto), applied.SettingsVersion, manifest.SettingsVersion), cancellationToken) .ConfigureAwait(false); } finally { _gate.Release(); } } /// /// Compares a sequence again after a transfer, originals and renders included even when no counter moved (a /// restored original or a hash given by verify changes no frame version), without sending or taking settings. /// internal async Task RecompareAsync(ISyncRemote remote, Guid remoteId, CancellationToken cancellationToken) { await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (await _links.GetAsync(remote.Server, remoteId, cancellationToken).ConfigureAwait(false) is { } link) await _links.UpsertAsync(link with { DiffLocalFrames = null, DiffLocalRenders = null }, cancellationToken).ConfigureAwait(false); } finally { _gate.Release(); } await RefreshSequencesAsync(remote, [remoteId], CompareOnly, cancellationToken).ConfigureAwait(false); } /// /// Records what the proxy cache holds for a server sequence. Thumbnails standing in for proxies the server did not /// have leave the cache stale: it is filled again once the server has its proxies. /// internal async Task SetProxiesAsync(string server, SequenceDto dto, int cached, int thumbnails, int framesVersion, CancellationToken cancellationToken) { await _gate.WaitAsync(cancellationToken).ConfigureAwait(false); try { var link = Observe(await _links.GetAsync(server, dto.Id, cancellationToken).ConfigureAwait(false), server, dto); int? filledAt = thumbnails > 0 && !link.ServerHasProxies ? null : framesVersion; await _links.UpsertAsync(link with { ProxiesCached = cached, ProxiesFramesVersion = filledAt }, cancellationToken).ConfigureAwait(false); } finally { _gate.Release(); } Changed?.Invoke(server); } /// The file of a local render: archive-relative for outputs inside the archive, absolute otherwise. internal static string RenderFile(Archive archive, Render render) => Path.IsPathRooted(render.OutputPath) ? render.OutputPath : new ArchivePaths(archive.RootPath).FromRelative(render.OutputPath); /// /// Compares one server sequence (under the gate): fetches it, forgets its link on a 404, records a failure on its /// link. (when given) receives the local copy as it stands after the comparison. /// private async Task CompareOneAsync(ISyncRemote remote, Guid remoteId, SyncLink? link, Dictionary? locals, SyncPolicy policy, CancellationToken cancellationToken) { SequenceDto dto; try { dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false); } catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.NotFound) { if (link is not null) await ForgetAsync(link, cancellationToken).ConfigureAwait(false); return; } catch (Exception ex) when (IsIsolated(ex, cancellationToken)) { Log.Warning(LogCategory, $"Sequence {remoteId} could not be compared", ex); if (link is not null) await _links.UpsertAsync(link with { Error = ex.Message }, cancellationToken).ConfigureAwait(false); return; } var local = await LocalOfAsync(link, locals, cancellationToken).ConfigureAwait(false); await ReconcileSafelyAsync(remote, Observe(link, remote.Server, dto), dto, local, locals, policy, cancellationToken).ConfigureAwait(false); } private async Task ReconcileSafelyAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence? local, Dictionary? locals, SyncPolicy policy, CancellationToken cancellationToken) { try { (link, local) = await ReconcileAsync(remote, link, dto, local, policy, cancellationToken).ConfigureAwait(false); link = link with { Error = null }; if (local is not null && locals is not null) locals[local.Id] = local; } catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.NotFound) { // Deleted between the listing and the manifest: the next comparison forgets it. Log.Info(LogCategory, $"{dto.DisplayTitle} vanished from the server during the comparison"); } catch (Exception ex) when (IsIsolated(ex, cancellationToken)) { // The archive is offline or busy, the server refused this sequence, or its project is unreadable: it is // compared again next time and the other sequences go on. Log.Warning(LogCategory, $"{dto.DisplayTitle} could not be synced", ex); link = link with { Error = ex.Message }; } await _links.UpsertAsync(link, cancellationToken).ConfigureAwait(false); } /// A failure that concerns one sequence; a lost session (401) or the caller's cancellation stops the whole comparison. private static bool IsIsolated(Exception ex, CancellationToken cancellationToken) => ex switch { TitanoApiException api => api.Status != HttpStatusCode.Unauthorized, OperationCanceledException => !cancellationToken.IsCancellationRequested, _ => ex is HttpRequestException or JsonException or IOException or UnauthorizedAccessException or InvalidDataException or LeaseUnavailableException, }; /// Compares originals and renders when either side's moved, then settles the settings; returns the updated link and local copy. private async Task<(SyncLink Link, Sequence? Local)> ReconcileAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence? local, SyncPolicy policy, CancellationToken cancellationToken) { if (local is null) { // A cleared cache leaves nothing to open; a deleted local copy makes the sequence remote again. if (link.ProxiesCached > 0 && !Proxies.Exists(link.Server, link.RemoteId)) link = link with { ProxiesCached = 0, ProxiesFramesVersion = null }; return (link.LocalId is null ? link : Unlinked(link), null); } SequenceManifest? manifest = null; if (link.DiffLocalFrames != local.FramesVersion || link.DiffRemoteFrames != link.RemoteFramesVersion || link.DiffLocalRenders != local.RenderedCount || link.DiffRemoteRenders != link.RenderCount || (link.DiffRemoteUpdatedAt is { } compared && !SameMoment(compared, link.RemoteUpdatedAt))) { 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 present = frames.Where(f => (f.Flags & FrameFlags.Missing) == 0).ToList(); // A frame either side has not hashed yet (a folder adopted, not verified) is told apart by size and capture time. var captured = manifest.Frames.Any(f => f.Sha256.Length == 0) || present.Any(f => f.Sha256 is null) ? (await remote.ListFramesAsync(link.RemoteId, cancellationToken).ConfigureAwait(false)).ToDictionary(f => f.Id, f => f.CapturedAt) : null; var (missingRemote, missingLocal) = SyncRules.CompareOriginals([.. present.Select(f => new FramePhoto(f.Sha256, f.Size, f.CapturedAt))], [.. manifest.Frames.Select(f => new FramePhoto(f.Sha256, f.Size, captured?.GetValueOrDefault(f.Id)))]); // Proxies the server asks for that this PC can make from its originals travel with an upload too (BRIEF §2.3). var proxies = policy.CanEdit && manifest.Proxies is not null ? SyncRules.ProxiesToSend(manifest, present, Options.Archive.Decoder, captured) : 0; // Those change without the server's frame counters moving: while some may be made here, the server's next // change of the sequence brings a new comparison. var watch = policy.CanEdit && (proxies > 0 || SyncRules.MakesProxiesFor(present, Options.Archive.Decoder)); link = await CompareExportsAsync(link, local, manifest.Renders, cancellationToken).ConfigureAwait(false) with { DiffLocalFrames = local.FramesVersion, DiffRemoteFrames = manifest.FramesVersion, RemoteFramesVersion = manifest.FramesVersion, DiffLocalRenders = local.RenderedCount, DiffRemoteRenders = manifest.Renders.Count, DiffRemoteUpdatedAt = watch ? link.RemoteUpdatedAt : null, MissingRemote = missingRemote + proxies, MissingLocal = missingLocal, }; } (link, local) = await SettleSettingsAsync(remote, link, dto, local, manifest, policy, cancellationToken).ConfigureAwait(false); return (link with { SyncedAt = DateTimeOffset.UtcNow }, local); } /// Renders each side lacks, by id (a render keeps its id on both sides); local renders whose file is gone cannot be sent. private async Task CompareExportsAsync(SyncLink link, Sequence local, IReadOnlyList remoteRenders, CancellationToken cancellationToken) { var renders = await _db.Renders.ListBySequenceAsync(local.Id, cancellationToken).ConfigureAwait(false); var archive = await _db.Archives.GetAsync(local.ArchiveId, cancellationToken).ConfigureAwait(false); var here = renders.Select(r => r.Id).ToHashSet(); var there = remoteRenders.Select(r => r.Id).ToHashSet(); var toGet = remoteRenders.Where(r => !here.Contains(r.Id)).ToList(); var toSend = archive is null ? [] : renders.Where(r => !there.Contains(r.Id) && File.Exists(RenderFile(archive, r))).ToList(); return link with { ExportsToGet = toGet.Count, ExportsToGetBytes = toGet.Sum(r => r.Bytes), ExportsToSend = toSend.Count, ExportsToSendBytes = toSend.Sum(r => r.Bytes), }; } private async Task<(SyncLink Link, Sequence Local)> SettleSettingsAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence local, SequenceManifest? manifest, SyncPolicy policy, CancellationToken cancellationToken) { var localChanged = link.BaseLocalSettings != local.SettingsVersion; var remoteChanged = link.BaseRemoteSettings != link.RemoteSettingsVersion; if (!localChanged && !remoteChanged) return (link, local); // Frames inserted here since the copy was listed moved its positions without a new settings version. if (await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false) is { } current && current.SettingsVersion == local.SettingsVersion) local = current; if (localChanged && remoteChanged) { if (link.ConflictLocalSettings == local.SettingsVersion && link.ConflictRemoteSettings == link.RemoteSettingsVersion) return (link, local); manifest ??= await remote.GetManifestAsync(link.RemoteId, cancellationToken).ConfigureAwait(false); // The listing and the manifest disagree: the server changed during the comparison; settle it next time. if (manifest.SettingsVersion != dto.SettingsVersion) return (link, local); // Compared (and shown) with its positions on this copy's frames, against this copy as it is at those frames: the // same photos are the same settings. Edited here meanwhile, or frames that kept changing: compared next time. if (await Positions(remote, local, manifest).ToLocalAsync(manifest.SettingsJson, local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } translated) return (link, local); local = translated.Local; var mine = SyncPayload.Of(local); var theirs = SyncPayload.Of(dto, translated.SettingsJson); if (mine.SameAs(theirs)) return (Agreed(link, local.SettingsVersion, manifest.SettingsVersion), local); // Last writer wins only when one side changed; both changed differently is the user's choice (BRIEF §14.5). return (link with { ConflictLocalSettings = local.SettingsVersion, ConflictRemoteSettings = manifest.SettingsVersion, ConflictRemote = theirs with { SettingsJson = SyncRules.NormalizeSettings(theirs.SettingsJson) }, }, local); } if (!policy.AutoSyncSettings) return (link, local); if (localChanged) { if (!policy.CanEdit) return (link, local); manifest ??= await remote.GetManifestAsync(link.RemoteId, cancellationToken).ConfigureAwait(false); return (await PushAsync(remote, link, local, manifest, link.BaseRemoteSettings, cancellationToken).ConfigureAwait(false), local); } manifest ??= await remote.GetManifestAsync(link.RemoteId, cancellationToken).ConfigureAwait(false); if (manifest.SettingsVersion != dto.SettingsVersion) return (link, local); if (await ApplyLocallyAsync(local, SyncPayload.Of(dto, manifest.SettingsJson), Positions(remote, local, manifest), cancellationToken) .ConfigureAwait(false) is { } pulled) return (Agreed(link, pulled.SettingsVersion, manifest.SettingsVersion), pulled); // The local copy was read when the comparison began and an edit was saved on this PC since: both sides changed, // so the server's version must not replace it unseen (BRIEF §14.5). var fresh = await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false); if (fresh is null || fresh.SettingsVersion == local.SettingsVersion) return (link, local); return await SettleSettingsAsync(remote, link, dto, fresh, manifest, policy, cancellationToken).ConfigureAwait(false); } /// /// Sends this PC's payload unless the server moved past , which shows up as a conflict next, /// or either copy's frames changed since they were read (the positions are translated again next time). /// private async Task PushAsync(ISyncRemote remote, SyncLink link, Sequence local, SequenceManifest manifest, int? expected, CancellationToken cancellationToken) { try { if (await RequestAsync(remote, local, manifest, expected, cancellationToken).ConfigureAwait(false) is not { } request) return link; var updated = await remote.UpdateSequenceAsync(link.RemoteId, request, cancellationToken).ConfigureAwait(false); return Agreed(Observe(link, link.Server, updated), local.SettingsVersion, updated.SettingsVersion); } catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict) { return Observe(link, link.Server, await remote.GetSequenceAsync(link.RemoteId, cancellationToken).ConfigureAwait(false)); } } /// /// The PATCH that makes the server's copy equal to this copy at 's settings version (read again /// with its frames, which may have moved its positions since), its frame positions moved to the server's frames as /// lists them; the server refuses it when its frames changed since. Null when this copy /// was edited since was read, or either copy's frames cannot be read as one state now. /// private async Task RequestAsync(ISyncRemote remote, Sequence local, SequenceManifest manifest, int? expected, CancellationToken cancellationToken) { var positions = Positions(remote, local, manifest); if (await positions.ToServerAsync(local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } translated) return null; var frames = ProjectSettings.NamesFrames(translated.Local.SettingsJson) ? positions.ServerFrames : (int?)null; return (SyncPayload.Of(translated.Local) with { SettingsJson = translated.SettingsJson }).ToRequest(expected, frames); } private SyncPositions Positions(ISyncRemote remote, Sequence local, SequenceManifest manifest) => new(_db, remote, local.Id, manifest); /// /// Links local copies and server sequences that hold the same originals but no link joins (an Uploader upload of /// the same card, a link lost with an old index): candidates start at the same second with the same camera, and /// at least half of the smaller side's originals must match by SHA-256. A pair that does not match is not fetched /// again until either side's frames change. /// private async Task MatchByContentAsync(ISyncRemote remote, IReadOnlyList listing, Dictionary locals, SyncPolicy policy, CancellationToken cancellationToken) { var links = await _links.ListAsync(remote.Server, cancellationToken).ConfigureAwait(false); var linkedLocals = links.Where(l => l.LocalId is not null).Select(l => l.LocalId!.Value).ToHashSet(); var linkedRemotes = links.Where(l => l.LocalId is not null).Select(l => l.RemoteId).ToHashSet(); var candidates = listing.Where(d => !linkedRemotes.Contains(d.Id) && d.FrameCount > 0 && d.FirstCapture is not null) .ToLookup(d => Second(d.FirstCapture!.Value)); foreach (var local in locals.Values.Where(l => !linkedLocals.Contains(l.Id) && l.FrameCount > 0 && l.FirstCapture is not null).ToList()) { foreach (var dto in candidates[Second(local.FirstCapture!.Value)]) { if (linkedRemotes.Contains(dto.Id) || !string.Equals(dto.CameraModel, local.CameraModel, StringComparison.OrdinalIgnoreCase)) continue; var key = string.Create(CultureInfo.InvariantCulture, $"{local.Id:N}:{local.FramesVersion}:{dto.Id:N}:{dto.FramesVersion}"); if (_unmatched.ContainsKey(key)) continue; try { if (!await SameOriginalsAsync(remote, local, dto, cancellationToken).ConfigureAwait(false)) { _unmatched[key] = 0; continue; } } catch (Exception ex) when (IsIsolated(ex, cancellationToken)) { Log.Warning(LogCategory, $"{local.DisplayTitle} could not be matched with {dto.DisplayTitle}", ex); continue; } Log.Info(LogCategory, $"{local.DisplayTitle} holds the originals of {dto.DisplayTitle} on {remote.Server}: linked"); var link = links.FirstOrDefault(l => l.RemoteId == dto.Id); var linked = Unlinked(Observe(link, remote.Server, dto)) with { LocalId = local.Id }; await ReconcileSafelyAsync(remote, linked, dto, local, locals, policy, cancellationToken).ConfigureAwait(false); linkedRemotes.Add(dto.Id); break; } } } private async Task SameOriginalsAsync(ISyncRemote remote, Sequence local, SequenceDto dto, CancellationToken cancellationToken) { var hashes = (await SequenceFrames.ListAsync(_db, local.Id, cancellationToken).ConfigureAwait(false)) .Select(f => f.Sha256?.ToLowerInvariant()).OfType().ToHashSet(StringComparer.Ordinal); if (hashes.Count == 0) return false; var manifest = await remote.GetManifestAsync(dto.Id, cancellationToken).ConfigureAwait(false); var shared = manifest.Frames.Select(f => f.Sha256.ToLowerInvariant()).Distinct(StringComparer.Ordinal).Count(hashes.Contains); return shared > 0 && shared * 2 >= Math.Min(hashes.Count, manifest.Frames.Count); } private static DateTime Second(DateTime time) => new(time.Ticks - time.Ticks % TimeSpan.TicksPerSecond, DateTimeKind.Unspecified); /// The same instant as databases keep it (to the microsecond). internal static bool SameMoment(DateTimeOffset a, DateTimeOffset b) => a.UtcTicks / 10 == b.UtcTicks / 10; /// /// Writes the server's payload into the local index, project and sidecar in one step (settings version + 1) under the /// sequence's lease, which the editor's saves and the archive's frame commits take too, and only while the local copy /// is still at the settings version was read at. The settings' frame positions are moved to /// this copy's frames as they are under the lease (). Null when an edit was saved on this /// PC since, or the frames of either copy could not be read as one state: nothing is written then. A payload the local /// copy already holds writes nothing, so it needs neither a writable archive nor the lease. /// private async Task ApplyLocallyAsync(Sequence local, SyncPayload payload, SyncPositions positions, CancellationToken cancellationToken) { if (await positions.ToLocalAsync(payload.SettingsJson, local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } stored) return null; // Settled as it is: an archive that is offline or read-only does not keep the link from being agreed. if ((payload with { SettingsJson = stored.SettingsJson }).SameAs(SyncPayload.Of(stored.Local))) return stored.Local; var archive = await _db.Archives.GetAsync(local.ArchiveId, cancellationToken).ConfigureAwait(false) ?? throw new InvalidDataException($"Archive {local.ArchiveId} is not indexed."); var paths = new ArchivePaths(archive.RootPath); ArchiveVolume.EnsureWritable(paths); var folder = paths.Sequence(local.Folder); await using (await _writer.LockAsync(paths, local.Id, cancellationToken).ConfigureAwait(false)) { // The frames cannot change under the lease: the positions are moved to them as they are now. if (await positions.ToLocalAsync(payload.SettingsJson, local.SettingsVersion, cancellationToken).ConfigureAwait(false) is not { } current) return null; var row = current.Local; payload = payload with { SettingsJson = current.SettingsJson }; var metadata = !payload.SameMetadata(SyncPayload.Of(row)); var settings = !SyncRules.SameSettings(payload.SettingsJson, row.SettingsJson); if (!metadata && !settings) return row; string? project = null; Guid? hero = null; if (settings) { var edited = ProjectStore.Parse(SyncRules.NormalizeSettings(payload.SettingsJson)); project = edited.ToJson(); hero = await HeroFrame.EditAsync(_db, row.Id, ProjectStore.Read(row, folder).HeroFrame, edited.HeroFrame, cancellationToken).ConfigureAwait(false); } var edit = new SequenceEdit { Title = metadata ? payload.Title?.Trim() ?? string.Empty : null, Tags = metadata ? payload.Tags : null, Rating = metadata ? payload.Rating : null, Notes = metadata ? payload.Notes ?? string.Empty : null, SettingsJson = project, HeroFrameId = hero, }; // The index first, at the version read under the lease: a writer that skips the lease still cannot be overwritten. var result = await _db.Sequences.EditAsync(row.Id, edit, row.SettingsVersion, cancellationToken).ConfigureAwait(false); if (result is not { Outcome: SequenceEditOutcome.Updated, Sequence: { } updated }) return null; if (project is not null) AtomicFile.WriteAllText(folder.ProjectPath, project); if (metadata) { var manifest = SequenceWriter.ReadLocked(folder, row.Id); ManifestStore.WriteSequence(folder.ManifestPath, manifest with { Title = updated.Title, Tags = updated.Tags, Rating = updated.Rating, Notes = updated.Notes, Versions = manifest.Versions with { Settings = Math.Max(manifest.Versions.Settings, updated.SettingsVersion) }, UpdatedAt = DateTimeOffset.UtcNow, }); await Archives.Indexer.RecordStampsAsync(archive, folder, row.Id, cancellationToken).ConfigureAwait(false); } return updated; } } /// /// "Save both": this PC's project is kept next to project.json as project.this-pc-<local time>.json /// before the server's replaces it; returns that file. /// private async Task SaveCopyAsync(Sequence local, CancellationToken cancellationToken) { var archive = await _db.Archives.GetAsync(local.ArchiveId, cancellationToken).ConfigureAwait(false) ?? throw new InvalidDataException($"Archive {local.ArchiveId} is not indexed."); var folder = new ArchivePaths(archive.RootPath).Sequence(local.Folder); Directory.CreateDirectory(folder.MetaDirectory); var path = Path.Combine(folder.MetaDirectory, SavedCopyName(DateTime.Now)); AtomicFile.WriteAllText(path, SyncRules.NormalizeSettings(local.SettingsJson)); Log.Info(LogCategory, $"Settings of {local.DisplayTitle} kept as {path}"); return path; } /// The file name of a "Save both" copy kept at . public static string SavedCopyName(DateTime localTime) => string.Create(CultureInfo.InvariantCulture, $"{SavedCopyPrefix}{localTime.ToString(SavedCopyTimeFormat, CultureInfo.InvariantCulture)}.json"); /// File name pattern of "Save both" copies in a sequence's .titano folder. public const string SavedCopyPattern = SavedCopyPrefix + "*.json"; private const string SavedCopyPrefix = "project.this-pc-"; private const string SavedCopyTimeFormat = "yyyyMMdd-HHmmss"; /// When a "Save both" copy was kept (local time, from its name); null for a name of another shape. public static DateTime? SavedCopyTime(string fileName) { var name = Path.GetFileNameWithoutExtension(fileName); return name.StartsWith(SavedCopyPrefix, StringComparison.Ordinal) && DateTime.TryParseExact(name[SavedCopyPrefix.Length..], SavedCopyTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var time) ? time : null; } private async Task ForgetAsync(SyncLink link, CancellationToken cancellationToken) { await _links.DeleteAsync(link.Server, link.RemoteId, cancellationToken).ConfigureAwait(false); Proxies.Delete(link.Server, link.RemoteId); Log.Info(LogCategory, $"{link.Title} is no longer on {link.Server}"); } /// Writes each local sequence's state for the Library and returns the table. private async Task> StoreStatesAsync(string server, Dictionary? locals, CancellationToken cancellationToken) { var entries = await EntriesAsync(server, locals, cancellationToken).ConfigureAwait(false); foreach (var entry in entries) { if (entry.Local is { } local && SyncRules.ToSequenceState(entry.Status) is { } state && state != local.SyncState) await _links.SetLocalStateAsync(local.Id, state, cancellationToken).ConfigureAwait(false); } return entries; } /// The table from the stored links and local sequences (read from the index when null). private async Task> EntriesAsync(string server, Dictionary? known, CancellationToken cancellationToken) { var links = await _links.ListAsync(server, cancellationToken).ConfigureAwait(false); var locals = known is null ? await LocalSequencesAsync(cancellationToken).ConfigureAwait(false) : new Dictionary(known); var transfers = await _links.ActiveTransfersAsync(cancellationToken).ConfigureAwait(false); var entries = new List(links.Count + locals.Count); foreach (var link in links) { var local = await LocalOfAsync(link, locals, cancellationToken).ConfigureAwait(false); if (local is not null) locals.Remove(local.Id); var transfer = Pick(local is null ? null : transfers.GetValueOrDefault(local.Id), transfers.GetValueOrDefault(link.RemoteId)); entries.Add(new SyncEntry(local, link, transfer is null ? SyncRules.Status(local, link) : SyncStatus.Transferring, transfer)); } foreach (var local in locals.Values) { var transfer = Pick(transfers.GetValueOrDefault(local.Id), null); entries.Add(new SyncEntry(local, null, transfer is null ? SyncStatus.LocalOnly : SyncStatus.Transferring, transfer)); } return [.. entries.OrderBy(e => e.Status switch { SyncStatus.Conflict => 0, SyncStatus.Transferring => 1, _ => 2 }) .ThenBy(e => e.Title, StringComparer.CurrentCultureIgnoreCase)]; } /// The transfer a row shows: the user's before background ones, originals before renders before proxies, running before waiting. private static Job? Pick(IReadOnlyList? local, IReadOnlyList? remote) => (local ?? []).Concat(remote ?? []) .OrderByDescending(j => j.Priority) .ThenBy(j => TransferKind(j) switch { SyncTransferKind.DownloadProxies => 2, SyncTransferKind.SendExports or SyncTransferKind.GetExports => 1, _ => 0, }) .ThenBy(j => j.State == JobState.Running ? 0 : 1) .FirstOrDefault(); /// The local copy of a link: from , or the index when the paged listing missed it. private async Task LocalOfAsync(SyncLink? link, Dictionary? locals, CancellationToken cancellationToken) { if (link?.LocalId is not { } id) return null; if (locals is not null && locals.TryGetValue(id, out var known)) return known; return await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false); } private async Task<(SyncLink Link, Sequence Local)> LinkedAsync(string server, Guid remoteId, CancellationToken cancellationToken) { var link = await _links.GetAsync(server, remoteId, cancellationToken).ConfigureAwait(false); var local = link?.LocalId is { } id ? await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false) : null; if (link is null || local is null) throw new InvalidOperationException($"Sequence {remoteId} has no local copy."); return (link, local); } private async Task> LocalSequencesAsync(CancellationToken cancellationToken) { var all = new Dictionary(); string? cursor = null; do { var page = await _db.Sequences.ListAsync(new SequenceQuery { Limit = 1000, Cursor = cursor, Sort = SequenceSort.Oldest }, cancellationToken) .ConfigureAwait(false); foreach (var s in page.Items) all[s.Id] = s; cursor = page.NextCursor; } while (cursor is not null); return all; } private async Task EnqueueAsync(JobEngine engine, JobKind kind, Guid target, string label, SyncJobParameters parameters, JobPriority priority, CancellationToken cancellationToken) { var wanted = parameters.Kind(kind); var active = (await _links.ActiveTransfersAsync(cancellationToken).ConfigureAwait(false)).GetValueOrDefault(target) ?? []; foreach (var job in active) { var running = TransferKind(job); var outranked = running == SyncTransferKind.DownloadProxies && job.Priority < priority; if (running == wanted && !outranked) return job; if (outranked && wanted is SyncTransferKind.DownloadProxies or SyncTransferKind.DownloadOriginals) await engine.CancelAsync(job.Id, cancellationToken).ConfigureAwait(false); } return await engine.EnqueueAsync(kind, [target], label, JsonSerializer.Serialize(parameters, SyncJobJson.Default.SyncJobParameters), priority, cancellationToken: cancellationToken).ConfigureAwait(false); } /// The link with the server's side as lists it (a new link when there was none). internal static SyncLink Observe(SyncLink? link, string server, SequenceDto dto) => (link ?? new SyncLink { Server = server, RemoteId = dto.Id, Title = dto.DisplayTitle }) with { Title = dto.DisplayTitle, FrameCount = dto.FrameCount, TotalBytes = dto.TotalBytes, RenderCount = dto.RenderedCount, RemoteStatus = (SequenceStatus)dto.Status, RemoteSettingsVersion = dto.SettingsVersion, RemoteFramesVersion = dto.FramesVersion, RemoteUpdatedAt = dto.UpdatedAt, }; private static SyncLink Agreed(SyncLink link, int localVersion, int remoteVersion) => link with { BaseLocalSettings = localVersion, BaseRemoteSettings = remoteVersion, RemoteSettingsVersion = remoteVersion, ConflictLocalSettings = null, ConflictRemoteSettings = null, ConflictRemote = null, }; private static SyncLink Unlinked(SyncLink link) => link with { LocalId = null, BaseLocalSettings = null, BaseRemoteSettings = null, DiffLocalFrames = null, DiffRemoteFrames = null, DiffLocalRenders = null, DiffRemoteRenders = null, DiffRemoteUpdatedAt = null, MissingRemote = 0, MissingLocal = 0, ExportsToGet = 0, ExportsToGetBytes = 0, ExportsToSend = 0, ExportsToSendBytes = 0, ConflictLocalSettings = null, ConflictRemoteSettings = null, ConflictRemote = null, }; }