diff --git a/i18n/en/Errors.resx b/i18n/en/Errors.resx
index 7391b68d..77cce968 100644
--- a/i18n/en/Errors.resx
+++ b/i18n/en/Errors.resx
@@ -18,11 +18,15 @@
The sequence to render is no longer in the archive.Enter a server address such as nas.local:8443 or https://titano.example.com.This file does not contain Titano settings.
- The archive disk needs {0} GB more for these originals.{0} gigabytes missing
+ This PC or the server changed the sequence again; compare and choose again.
+ The disk needs {0} GB more for this download.{0} gigabytes missing
+ {0} render is no longer on this PC; the others were sent.{0} count
+ {0} renders are no longer on this PC; the others were sent.{0} count{0} did not arrive intact; download it again.{0} file nameThe sequence is no longer in this PC's archive index.Open or create an archive on this PC to receive the originals.Not connected to this server; connect and retry the job.
+ This sequence is not on the server yet; upload it first.No original reached the server: they are missing here or already in another server sequence.{0} original is missing or changed since it was archived; verify the archive.{0} count{0} originals are missing or changed since they were archived; verify the archive.{0} count
diff --git a/i18n/en/Library.resx b/i18n/en/Library.resx
index b4bac4f4..cc92b057 100644
--- a/i18n/en/Library.resx
+++ b/i18n/en/Library.resx
@@ -62,10 +62,8 @@
AnalysedAnalysingConflict
- Newer hereProxiesRemote
- Newer on serverSyncedViewGrid
diff --git a/i18n/en/Server.resx b/i18n/en/Server.resx
index 9e2ada8b..3d9acadf 100644
--- a/i18n/en/Server.resx
+++ b/i18n/en/Server.resx
@@ -13,9 +13,13 @@
Server certificateTLS {0} trusted {1}Cancel
- {0}: {1} here, {2} on the serverlong; {0} setting path, {1} value on this PC, {2} value on the server
+ {0} differs{0} setting name
+ {0} {1} here, {2} on the serverlong; {0} setting name, {1} value on this PC, {2} value on the serverThe server copy of {0} will be replaced by this PC's settings.long; {0} sequence titleKeep this PC's settings?
+ {0} · +{1}invariant; {0} first difference, {1} count of the others
+ This PC takes the server's version of {0} and keeps its own settings as a dated copy next to the project.long; {0} sequence title
+ Save both versions?This PC's settings of {0} will be replaced by the server copy.long; {0} sequence titleTake the server's settings?Connect
@@ -88,6 +92,7 @@
Sign inSign inUser
+ Pause metered uploads{0} of {1}{0} usedOffer this PC as render node
@@ -96,23 +101,64 @@
UploaderViewerTitano time-lapse archive, uploads, jobs and rendering server.long
+ Aspect
+ auto
+ Batch list
+ Camera offset
+ Crop
+ Deflicker
+ Deflicker strength
+ Deflicker window
+ Excluded frames
+ Export folder
+ Export preset
+ Export values
+ Exposure offset
+ Fit to duration
+ Flow quality
+ Frame rate
+ Hero frame
+ Highlight knee
+ Holy grail
+ Camera keyframes
+ manual
+ Motion blur
+ Notes
+ off
+ on
+ Other setting
+ Output name
+ Rating
+ Regions
+ Shutter angle
+ Stabilisation
+ Stabilisation smoothing
+ Stacking
+ Tags
+ Time remap
+ Timing mode
+ Title
+ Trail length
+ Transitions
+ WB smoothingSign outConnectedConnecting…DisconnectedNot connectedDownload originals
+ Get exportsKeep mineOpen with proxiesPauseResumeSave both
+ Send exportsSync settingsTake serverUploadServer archive {1} · {0} sequencelong; {0} sequence count, {1} archive sizeServer archive {1} · {0} sequenceslong; {0} sequence count, {1} archive size
- The server copy changed again; compare and choose again.longThis PCSequenceServer
@@ -134,9 +180,12 @@
{0} uploads queued{0} count{0} received{0} percentNot compared · retrying
+ This PC's settings savedlongSearch
+ Show filesettings differdownloading
+ not comparedin syncnewer herelocal only
diff --git a/i18n/en/Tooltips.resx b/i18n/en/Tooltips.resx
index 8995adcf..8158a6d1 100644
--- a/i18n/en/Tooltips.resx
+++ b/i18n/en/Tooltips.resx
@@ -111,11 +111,12 @@
SHA-256 fingerprint of the server certificateForget this certificate; the next connection asks againServers found on this network and used before
- Keep JPEG proxies of every server sequence on this PC to edit without the originals; uses space in the cache folder
+ Keep the proxies of the server sequences you opened with proxies up to date on this PC, to edit without the originals; uses space in the cache folderlongKeep trusting this certificateClose without signing in · EscSent only to this server; Titano keeps a session token, never the passwordYour user name on the server
+ Pause uploads while Windows reports this connection as metered; they resume by themselves when it is notlongSpace you use on the server out of your quotaLet the server send render jobs to this PC when it is idle; uses your GPU encodersAdmins manage users, the archive and the server
@@ -131,6 +132,8 @@
Settings differ between this PC and the serverOnly on this PCOnly on the server
+ Copy the server's render to this PC ({1}){0} count, {1} size
+ Copy the {0} server renders to this PC ({1}){0} count, {1} sizeOverwrite the server copy with this PC's settingsWhen this PC last compared every sequence with the server, and how long it tookOpen the web admin in your browser (admins only)
@@ -140,8 +143,12 @@
The last comparison failed: {0}. It is retried automatically.{0} reasonTake the server copy and keep this PC's settings as a dated copy of the projectFilter the table by sequence title
+ Send this PC's render to the server, verified by hash ({1}){0} count, {1} size
+ Send this PC's {0} renders to the server, verified by hash ({1}){0} count, {1} size
+ Show the saved copy of this PC's settings in File ExplorerSettings changed both here and on the server; choose which to keepOriginals or proxies are arriving from the server
+ This sequence could not be compared: {0}. It is compared again automatically.long; {0} reasonSame originals and settings on this PC and on the serverThis PC has originals or settings the server does not have yetOnly on this PC; upload it to keep it on the server too
@@ -155,6 +162,9 @@
Replace this PC's settings with the server copyCopy originals and settings to the server ({0}){0} sizeSend every local-only sequence to the server ({0} sequences, {1}){0} count, {1} size
+ Your account cannot upload to this server
+ Connect to a server to upload
+ No sequence is only on this PCTrust this certificate for this server and continue to sign inUse this addressGraphics adapter and Media Foundation encoders found
diff --git a/i18n/it/Errors.it.resx b/i18n/it/Errors.it.resx
index 7bc74c12..be3d5591 100644
--- a/i18n/it/Errors.it.resx
+++ b/i18n/it/Errors.it.resx
@@ -18,11 +18,15 @@
La sequenza da renderizzare non è più nell'archivio.Inserisci l'indirizzo di un server, ad esempio nas.local:8443 o https://titano.example.com.Questo file non contiene impostazioni di Titano.
- Al disco dell'archivio servono altri {0} GB per questi originali.
+ La sequenza è cambiata di nuovo su questo PC o sul server; confronta e scegli di nuovo.
+ Al disco servono altri {0} GB per questo scaricamento.
+ {0} render non è più su questo PC; gli altri sono stati inviati.
+ {0} render non sono più su questo PC; gli altri sono stati inviati.{0} non è arrivato integro; scaricalo di nuovo.La sequenza non è più nell'indice dell'archivio di questo PC.Apri o crea un archivio su questo PC per ricevere gli originali.Non connesso a questo server; collegati e riprova l'attività.
+ Questa sequenza non è ancora sul server; caricala prima.Nessun originale è arrivato al server: mancano qui o sono già in un'altra sequenza del server.{0} originale manca o è cambiato dopo l'archiviazione; verifica l'archivio.{0} originali mancano o sono cambiati dopo l'archiviazione; verifica l'archivio.
diff --git a/i18n/it/Library.it.resx b/i18n/it/Library.it.resx
index 29240386..5d75b9af 100644
--- a/i18n/it/Library.it.resx
+++ b/i18n/it/Library.it.resx
@@ -62,10 +62,8 @@
AnalizzataIn analisiConflitto
- Più recente quiProxyRemota
- Più recente sul serverSincronizzataVistaGriglia
diff --git a/i18n/it/Server.it.resx b/i18n/it/Server.it.resx
index b5bd92ab..ab68efee 100644
--- a/i18n/it/Server.it.resx
+++ b/i18n/it/Server.it.resx
@@ -4,7 +4,7 @@
2.0System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
- Sincronia automatica progetti
+ Sincronizza progetti automaticamenteCertificatoDimenticaMantieni
@@ -12,9 +12,12 @@
Certificato del serverTLS {0} attendibile dal {1}Annulla
- {0}: {1} qui, {2} sul server
+ {0}: valori diversi
+ {0} {1} qui, {2} sul serverLa copia sul server di {0} sarà sostituita dalle impostazioni di questo PC.Tenere le impostazioni di questo PC?
+ Questo PC prende la versione del server di {0} e conserva le proprie impostazioni come copia datata accanto al progetto.
+ Salvare entrambe le versioni?Le impostazioni di {0} su questo PC saranno sostituite dalla copia del server.Usare le impostazioni del server?Connetti
@@ -87,6 +90,7 @@
AccediAccediUtente
+ Pausa su reti a consumo{0} di {1}{0} usatiOffri questo PC per il rendering
@@ -95,23 +99,64 @@
CaricatoreVisualizzatoreServer Titano per archivio time-lapse, caricamenti, job e rendering.
+ Proporzioni
+ auto
+ Elenco batch
+ Spostamento camera
+ Ritaglio
+ Deflicker
+ Intensità deflicker
+ Finestra deflicker
+ Fotogrammi esclusi
+ Cartella di esportazione
+ Preset di esportazione
+ Valori di esportazione
+ Correzione esposizione
+ Adatta alla durata
+ Qualità del flusso
+ Frequenza fotogrammi
+ Fotogramma copertina
+ Compressione alte luci
+ Holy grail
+ Keyframe camera
+ manuale
+ Sfocatura di movimento
+ Note
+ disattivo
+ attivo
+ Altra impostazione
+ Nome del file
+ Valutazione
+ Regioni
+ Angolo otturatore
+ Stabilizzazione
+ Morbidezza stabilizzazione
+ Stacking
+ Tag
+ Rimappatura del tempo
+ Modalità dei tempi
+ Titolo
+ Lunghezza scie
+ Transizioni
+ Livellamento WBEsciConnessoConnessione…DisconnessoNon connessoScarica originali
+ Ricevi esportazioniTieni le mieApri con i proxyPausaRiprendiSalva entrambe
+ Invia esportazioniSincronizzaUsa il serverCaricaArchivio del server {1} · {0} sequenzaArchivio del server {1} · {0} sequenze
- La copia sul server è cambiata di nuovo; confronta e scegli di nuovo.Questo PCSequenzaServer
@@ -123,7 +168,7 @@
Conflitti · {0}Solo locali · {0}Sul server · {0}
- Ultima sincronia {0} · confronto {1} s
+ Ultima sincronizzazione {0} · confronto {1} sNessuna sequenza corrispondeApri pannello adminoriginali
@@ -131,21 +176,24 @@
{0} caricamento in coda{0} caricamenti in coda{0} ricevuto
- Confronto non riuscito · riprovo
+ Confronto fallito · riprovo
+ Impostazioni di questo PC salvateCerca
+ Mostra fileimpostazioni diversein scaricamento
+ non confrontatasincronizzatapiù recente quisolo localein pausasolo proxysul server
- più recente sul server
+ server più recentein caricamentoin attesaSequenze
- Carica tutte le locali
+ Carica le localiConsidera attendibileAnnulla{0} usa un certificato che Titano non conosce ancora. Confronta l'impronta con quella del pannello di amministrazione del server prima di considerarlo attendibile.
diff --git a/i18n/it/Tooltips.it.resx b/i18n/it/Tooltips.it.resx
index cd171b20..8b59f72e 100644
--- a/i18n/it/Tooltips.it.resx
+++ b/i18n/it/Tooltips.it.resx
@@ -111,11 +111,12 @@
Impronta SHA-256 del certificato del serverDimentica questo certificato; alla prossima connessione verrà chiesto di nuovoServer trovati in questa rete e usati in precedenza
- Tieni su questo PC i proxy JPEG di ogni sequenza del server per lavorare senza gli originali; usa spazio nella cartella cache
+ Mantieni aggiornati su questo PC i proxy delle sequenze del server aperte con i proxy, per lavorare senza gli originali; usa spazio nella cartella cacheContinua a considerare attendibile questo certificatoChiude senza accedere · EscInviata solo a questo server; Titano conserva un token di sessione, mai la passwordIl tuo nome utente sul server
+ Mette in pausa i caricamenti mentre Windows segnala questa connessione come a consumo; riprendono da soli quando non lo èSpazio che usi sul server rispetto alla tua quotaPermette al server di inviare rendering a questo PC quando è inattivo; usa gli encoder della tua GPUGli amministratori gestiscono utenti, archivio e server
@@ -131,6 +132,8 @@
Le impostazioni differiscono tra questo PC e il serverSolo su questo PCSolo sul server
+ Copia su questo PC il render del server ({1})
+ Copia su questo PC i {0} render del server ({1})Sovrascrivi la copia del server con le impostazioni di questo PCQuando questo PC ha confrontato per l'ultima volta tutte le sequenze con il server, e quanto ha impiegatoApri l'amministrazione web nel browser (solo amministratori)
@@ -140,8 +143,12 @@
L'ultimo confronto non è riuscito: {0}. Viene ripetuto automaticamente.Usa la copia del server e conserva le impostazioni di questo PC come copia datata del progettoFiltra la tabella per titolo della sequenza
+ Invia al server il render di questo PC, verificato con l'hash ({1})
+ Invia al server i {0} render di questo PC, verificati con l'hash ({1})
+ Mostra in Esplora file la copia salvata delle impostazioni di questo PCLe impostazioni sono cambiate sia qui sia sul server; scegli quali tenereOriginali o proxy in arrivo dal server
+ Questa sequenza non è stata confrontata: {0}. Il confronto viene ripetuto automaticamente.Stessi originali e impostazioni su questo PC e sul serverQuesto PC ha originali o impostazioni che il server non ha ancoraSolo su questo PC; caricala per averla anche sul server
@@ -155,6 +162,9 @@
Sostituisci le impostazioni di questo PC con la copia del serverCopia originali e impostazioni sul server ({0})Invia al server ogni sequenza solo locale ({0} sequenze, {1})
+ Il tuo account non può caricare su questo server
+ Collegati a un server per caricare
+ Nessuna sequenza è solo su questo PCConsidera attendibile questo certificato per questo server e prosegui con l'accessoUsa questo indirizzoScheda grafica ed encoder Media Foundation trovati
diff --git a/src/Titano.App/DesignTime/SampleData.cs b/src/Titano.App/DesignTime/SampleData.cs
index 5bcf98c6..348785f7 100644
--- a/src/Titano.App/DesignTime/SampleData.cs
+++ b/src/Titano.App/DesignTime/SampleData.cs
@@ -9,6 +9,7 @@ using Titano.Core.Imaging;
using Titano.Core.Model;
using Titano.Core.Video;
using Titano.Data;
+using Titano.Data.Jobs;
using Titano.Data.Sync;
using Titano.Protocol;
using Titano.Protocol.Client;
@@ -31,6 +32,8 @@ public sealed class SampleData
private readonly AppServices _services;
private readonly SampleSyncRemote _remote = new();
private TitanoDatabase? _db;
+ private Guid _sunrise;
+ private Guid _lakeComo;
private SampleData(AppServices services) => _services = services;
@@ -87,55 +90,84 @@ public sealed class SampleData
settings.FirstRunCompleted = true;
}
- /// An upload in progress for the sync table (Comet over the ridge, 46 %).
+ ///
+ /// Transfers for the sync table: an upload at 46 % (Comet over the ridge), an upload waiting (Fog lifting, Asti), a
+ /// download of originals running (Sunrise over Superga) and one paused (Lake Como storm).
+ ///
public async Task FillSyncAsync()
{
var db = _db ?? throw new InvalidOperationException("FillAsync first.");
- var comet = (await db.Sequences.ListAsync(new Data.Repositories.SequenceQuery { Text = "Comet", Limit = 1 })).Items[0];
+ async Task LocalAsync(string text) => (await db.Sequences.ListAsync(new Data.Repositories.SequenceQuery { Text = text, Limit = 1 })).Items[0];
var now = DateTimeOffset.UtcNow;
- await db.Jobs.EnqueueAsync(new Job
+ Job Transfer(JobKind kind, Guid target, string label, string parameters, JobState state, long done, long total) => new()
{
Id = Guid.CreateVersion7(),
- Kind = JobKind.UploadCommit,
- TargetIds = [comet.Id],
- TargetLabel = comet.DisplayTitle,
- ParametersJson = $$"""{"server":"{{ServerAddress}}"}""",
- State = JobState.Running,
+ Kind = kind,
+ TargetIds = [target],
+ TargetLabel = label,
+ ParametersJson = parameters,
+ State = state,
Priority = JobPriority.User,
- ProgressDone = 462,
- ProgressTotal = 1_005,
- ItemsPerSecond = 1.6,
- Eta = TimeSpan.FromMinutes(4),
- NodeId = _services.WorkerId,
- Attempts = 1,
+ ProgressDone = done,
+ ProgressTotal = total,
+ ItemsPerSecond = state == JobState.Running ? 1.6 : null,
+ Eta = state == JobState.Running ? TimeSpan.FromMinutes(4) : null,
+ NodeId = state == JobState.Queued ? null : _services.WorkerId,
+ Attempts = state == JobState.Queued ? 0 : 1,
Created = now.AddMinutes(-5),
- Started = now.AddMinutes(-5),
- });
+ Started = state == JobState.Queued ? null : now.AddMinutes(-5),
+ };
+ var upload = $$"""{"server":"{{ServerAddress}}"}""";
+ var originals = $$"""{"server":"{{ServerAddress}}","mode":"Originals"}""";
+ var comet = await LocalAsync("Comet");
+ var fog = await LocalAsync("Fog lifting");
+ await db.Jobs.EnqueueAsync(Transfer(JobKind.UploadCommit, comet.Id, comet.DisplayTitle, upload, JobState.Running, 462, 1_005));
+ await db.Jobs.EnqueueAsync(Transfer(JobKind.UploadCommit, fog.Id, fog.DisplayTitle, upload, JobState.Queued, 0, 0));
+ await db.Jobs.EnqueueAsync(Transfer(JobKind.Download, _sunrise, "Sunrise over Superga", originals, JobState.Running, 1_210, 2_880));
+ await db.Jobs.EnqueueAsync(Transfer(JobKind.Download, _lakeComo, "Lake Como storm", originals, JobState.Paused, 300, 1_450));
}
///
- /// The server's side: four sequences this PC also holds (two the same, one with settings changed on both sides, one
- /// with more originals on the server) and two only on the server; the others are only here.
+ /// Fetches the proxies of Sunrise over Superga into the proxy cache through a download job on a one-off engine (the
+ /// capture's engine does not run jobs), so the sync table shows a sequence kept with proxies only.
+ ///
+ public async Task CacheProxiesAsync()
+ {
+ var db = _db ?? throw new InvalidOperationException("FillAsync first.");
+ var service = _services.Sync.Service ?? throw new InvalidOperationException("The index is not open.");
+ await using var engine = new JobEngine(db, service.CreateJobHandlers(_services.Sync),
+ new JobEngineOptions { Workers = 1, WorkerId = "sample-proxies", Kinds = [JobKind.Download], PollInterval = TimeSpan.FromMilliseconds(50) });
+ await engine.StartAsync();
+ var job = await service.EnqueueDownloadAsync(engine, ServerAddress, _sunrise, "Sunrise over Superga", SyncDownloadMode.Proxies, null);
+ for (var i = 0; i < 200 && await db.Jobs.GetAsync(job.Id) is { State: JobState.Queued or JobState.Running }; i++) await Task.Delay(50);
+ }
+
+ ///
+ /// The server's side: six sequences this PC also holds (two the same, one with settings changed on both sides, one
+ /// with more originals on the server, one with more here, one with renders this PC lacks) and two only on the server;
+ /// the others are only here.
///
private async Task FillServerAsync(TitanoDatabase db, IReadOnlyList local)
{
Guid Id(string title) => local.First(s => s.Title == title).Id;
await db.Sequences.EditAsync(Id("Holy grail · Monviso"), new Data.Repositories.SequenceEdit { SettingsJson = DeflickerWindow(41) }, null);
- var linked = new (string Title, int Frames, string? Settings)[]
+ var linked = new (string Title, int Frames, string? Settings, int Renders)[]
{
- ("Harbour sunset", 3, null),
- ("Aurora, Tromsø", 3, null),
- ("Holy grail · Monviso", 3, DeflickerWindow(33)),
- ("Construction cam · week 38", 8, null),
+ ("Harbour sunset", 3, null, 0),
+ ("Aurora, Tromsø", 3, null, 0),
+ ("Holy grail · Monviso", 3, DeflickerWindow(33), 0),
+ ("Construction cam · week 38", 8, null, 0),
+ ("Milan skyline dusk", 1, null, 0),
+ ("Star trails, Colle del Nivolet", 3, null, 3),
};
- foreach (var (title, frames, settings) in linked)
+ foreach (var (title, frames, settings, renders) in linked)
{
// The server's copy carries the same title, tags and rating as this PC's.
var sequence = (await db.Sequences.GetAsync(Id(title)))!;
- var remote = _remote.Add(SampleSyncRemote.Dto(sequence), settings, frames);
+ var remote = _remote.Add(SampleSyncRemote.Dto(sequence) with { RenderedCount = renders }, settings, frames);
await _services.Sync.Service!.LinkAsync(ServerAddress, remote, sequence.Id);
}
- _remote.Add(SampleSyncRemote.Dto(new Sequence
+ _sunrise = _remote.Add(SampleSyncRemote.Dto(new Sequence
{
Id = Guid.CreateVersion7(),
ArchiveId = Guid.Empty,
@@ -147,7 +179,7 @@ public sealed class SampleData
RenderedCount = 1,
UpdatedAt = DateTimeOffset.UtcNow,
}), null, 3);
- _remote.Add(SampleSyncRemote.Dto(new Sequence
+ _lakeComo = _remote.Add(SampleSyncRemote.Dto(new Sequence
{
Id = Guid.CreateVersion7(),
ArchiveId = Guid.Empty,
@@ -395,11 +427,12 @@ public sealed class SampleData
///
/// The sample server's sequences for the sync table: listings, manifests (frames hashed from their index, so
- /// unhashed sample frames match them by count) and settings edits; transfers are not part of the screenshots.
+ /// unhashed sample frames match them by count; renders as many as the listing says), settings edits and proxies
+ /// (generated JPEGs); uploads and originals are not part of the screenshots.
///
private sealed class SampleSyncRemote : ISyncRemote
{
- private readonly Dictionary _sequences = [];
+ private readonly Dictionary _sequences = [];
public string Server => ServerAddress;
@@ -409,39 +442,73 @@ public sealed class SampleData
public Guid Add(SequenceDto dto, string? settings, int frames)
{
- _sequences[dto.Id] = (dto, settings, frames);
+ lock (_sequences) _sequences[dto.Id] = (dto, settings, [.. Enumerable.Range(0, frames).Select(_ => Guid.CreateVersion7())]);
return dto.Id;
}
- public Task> ListSequencesAsync(CancellationToken cancellationToken) =>
- Task.FromResult>([.. _sequences.Values.Select(s => s.Dto)]);
+ public Task> ListSequencesAsync(CancellationToken cancellationToken)
+ {
+ lock (_sequences) return Task.FromResult>([.. _sequences.Values.Select(s => s.Dto)]);
+ }
- public Task GetSequenceAsync(Guid id, CancellationToken cancellationToken) => Task.FromResult(_sequences[id].Dto);
+ public Task GetSequenceAsync(Guid id, CancellationToken cancellationToken)
+ {
+ lock (_sequences) return Task.FromResult(_sequences[id].Dto);
+ }
public Task GetManifestAsync(Guid id, CancellationToken cancellationToken)
{
- var (dto, settings, frames) = _sequences[id];
- var hasProxy = (dto.Status & (long)SequenceStatus.ProxiesBuilt) != 0;
- return Task.FromResult(new SequenceManifest(id, dto.FramesVersion, dto.SettingsVersion, settings, dto.Tags,
- [.. Enumerable.Range(0, frames).Select(i => new ManifestFrame(Guid.CreateVersion7(), i, $"IMG_{i + 1:0000}.JPG", 24_000_000,
- Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(BitConverter.GetBytes(i))), hasProxy, true))], []));
+ lock (_sequences)
+ {
+ var (dto, settings, frames) = _sequences[id];
+ var hasProxy = (dto.Status & (long)SequenceStatus.ProxiesBuilt) != 0;
+ var created = new DateTimeOffset(2026, 9, 20, 21, 0, 0, TimeSpan.Zero);
+ return Task.FromResult(new SequenceManifest(id, dto.FramesVersion, dto.SettingsVersion, settings, dto.Tags,
+ [.. frames.Select((frame, i) => new ManifestFrame(frame, i, $"IMG_{i + 1:0000}.JPG", 24_000_000,
+ Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(BitConverter.GetBytes(i))), hasProxy, true))],
+ [.. Enumerable.Range(0, dto.RenderedCount).Select(i => new RenderDto(Guid.CreateVersion7(), i == 0 ? "4K HEVC" : "1080p H.264", "hevc",
+ 3840, 2160, 30, 21.3, 640_000_000, created.AddHours(i), "Studio NAS"))]));
+ }
}
- public Task> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => Task.FromResult>([]);
+ public Task> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
+ {
+ lock (_sequences)
+ {
+ return Task.FromResult>([.. _sequences[sequenceId].Frames.Select((frame, i) => new FrameDto(frame, sequenceId, i,
+ $"IMG_{i + 1:0000}.JPG", 24_000_000, null, new DateTime(2026, 7, 2, 5, 40, 0).AddSeconds(30 * i), null, 1.0 / 250, 8, 100, 24, 1, 320, 180,
+ "jpeg", 0))]);
+ }
+ }
public Task UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken)
{
- var (dto, _, frames) = _sequences[id];
- var updated = dto with { SettingsVersion = dto.SettingsVersion + 1, UpdatedAt = DateTimeOffset.UtcNow };
- _sequences[id] = (updated, request.SettingsJson, frames);
- return Task.FromResult(updated);
+ lock (_sequences)
+ {
+ var (dto, _, frames) = _sequences[id];
+ var updated = dto with { SettingsVersion = dto.SettingsVersion + 1, UpdatedAt = DateTimeOffset.UtcNow };
+ _sequences[id] = (updated, request.SettingsJson, frames);
+ return Task.FromResult(updated);
+ }
}
public Task UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken) => throw new NotSupportedException("The sample server takes no uploads.");
- public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
- throw new NotSupportedException("The sample server serves no files.");
+ /// Proxies and thumbnails: a dawn gradient whose sun rises with the frame.
+ public async Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken)
+ {
+ if (kind == "original") throw new NotSupportedException("The sample server serves no originals.");
+ int index;
+ lock (_sequences) index = _sequences.Values.Select(s => Array.IndexOf(s.Frames, frameId)).Max();
+ await destination.WriteAsync(Gradient(["#1B2A4A", "#E8925A", "#F6D28B"], 320, 180, index / 3.0), cancellationToken);
+ }
+
+ public Task DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
+ throw new NotSupportedException("The sample server serves no renders.");
+
+ public Task UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
+ throw new NotSupportedException("The sample server takes no renders.");
public async IAsyncEnumerable EventsAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
{
diff --git a/src/Titano.App/Infrastructure/AppSettings.cs b/src/Titano.App/Infrastructure/AppSettings.cs
index dcf5bb47..31514da3 100644
--- a/src/Titano.App/Infrastructure/AppSettings.cs
+++ b/src/Titano.App/Infrastructure/AppSettings.cs
@@ -111,12 +111,15 @@ public sealed class AppSettings
/// Let the server send render jobs to this PC (BRIEF §14.7).
public bool RenderNodeOptIn { get; set; }
- /// Keep the proxies of every server sequence without local originals in the cache (BRIEF §12.7).
+ /// Keep the proxy caches of the server sequences this PC opened with proxies up to date (BRIEF §12.7).
public bool KeepProxiesOffline { get; set; }
/// Send and take project settings changed on one side without asking (Server.html).
public bool SyncSettingsAutomatically { get; set; } = true;
+ /// Pause upload jobs while Windows reports the connection as metered (Server.html).
+ public bool PauseUploadsOnMetered { get; set; }
+
// ---- Jobs ----
/// "Clear done": finished jobs older than this are hidden from the Jobs list (they stay in the history).
public DateTimeOffset? JobsClearedAt { get; set; }
diff --git a/src/Titano.App/Library/SequenceItem.cs b/src/Titano.App/Library/SequenceItem.cs
index ef467181..28771425 100644
--- a/src/Titano.App/Library/SequenceItem.cs
+++ b/src/Titano.App/Library/SequenceItem.cs
@@ -147,17 +147,20 @@ public sealed class SequenceItem : ObservableObject
OnPropertyChanged(nameof(PrimaryStatus));
}
- /// Where a local copy stands against the server (the sync engine writes the state); null while it is only here.
+ ///
+ /// Where a local copy stands against the server (the sync engine writes the state), in one word as DESIGN §4 draws
+ /// it ("● Synced", "● Remote", "▲ Conflict") with the detail in the tooltip; null while it is only here.
+ ///
public static CardStatus? SyncStatus(SyncState state) => state switch
{
SyncState.InSync => new CardStatus(StatusKind.Synced, Loc.L("Library.Status.Synced"), Loc.L("Tooltips.Library.Status.Synced")),
- SyncState.LocalAhead => new CardStatus(StatusKind.Synced, Loc.L("Library.Status.LocalAhead"), Loc.L("Tooltips.Library.Status.LocalAhead")),
- SyncState.ServerAhead => new CardStatus(StatusKind.Remote, Loc.L("Library.Status.ServerAhead"), Loc.L("Tooltips.Library.Status.ServerAhead")),
+ SyncState.LocalAhead => new CardStatus(StatusKind.Synced, Loc.L("Library.Status.Synced"), Loc.L("Tooltips.Library.Status.LocalAhead")),
+ SyncState.ServerAhead => new CardStatus(StatusKind.Remote, Loc.L("Library.Status.Remote"), Loc.L("Tooltips.Library.Status.ServerAhead")),
SyncState.Conflict => new CardStatus(StatusKind.Warning, Loc.L("Library.Status.Conflict"), Loc.L("Tooltips.Library.Status.Conflict")),
_ => null,
};
- /// Card status row: analysed, proxies, renders, sync, warnings (Main.html).
+ /// Card status row: analysed, proxies, renders, errors or warnings, sync (Main.html); the sync glyph is last so a narrow card cuts it first.
public static IReadOnlyList BuildStatuses(Sequence s, bool analysing)
{
var list = new List(5);
@@ -178,11 +181,11 @@ public sealed class SequenceItem : ObservableObject
if (s.RenderedCount > 0)
list.Add(new CardStatus(StatusKind.Rendered, Formatting.Count(s.RenderedCount), Loc.Plural("Tooltips.Library.Status.Rendered", s.RenderedCount)));
- if (SyncStatus(s.SyncState) is { } sync) list.Add(sync);
if ((s.Status & SequenceStatus.HasErrors) != 0)
list.Add(new CardStatus(StatusKind.Error, Formatting.Count(Math.Max(1, s.WarningCount)), Loc.L("Tooltips.Library.Status.Errors")));
else if (s.WarningCount > 0 || (s.Status & SequenceStatus.HasWarnings) != 0)
list.Add(new CardStatus(StatusKind.Warning, Formatting.Count(Math.Max(1, s.WarningCount)), Loc.Plural("Tooltips.Library.Status.Warnings", Math.Max(1, s.WarningCount))));
+ if (SyncStatus(s.SyncState) is { } sync) list.Add(sync);
return list;
}
}
diff --git a/src/Titano.App/Sections/LibraryViewModel.cs b/src/Titano.App/Sections/LibraryViewModel.cs
index a70ed8d7..b4a185a1 100644
--- a/src/Titano.App/Sections/LibraryViewModel.cs
+++ b/src/Titano.App/Sections/LibraryViewModel.cs
@@ -497,7 +497,10 @@ public sealed class LibraryViewModel : SectionViewModel
{
if (await _db.Sequences.GetAsync(item.Id) is { } fresh) item.Update(fresh);
}
- if (job.Kind is JobKind.Scan or JobKind.Rebuild or JobKind.Import or JobKind.Adopt or JobKind.Move or JobKind.UploadCommit or JobKind.Download) await RefreshAsync();
+ // Downloads of proxies only fill the proxy cache, so the Library is read again for originals and exports only.
+ if (job.Kind is JobKind.Scan or JobKind.Rebuild or JobKind.Import or JobKind.Adopt or JobKind.Move or JobKind.UploadCommit
+ || Data.Sync.SyncService.TransferKind(job) is Data.Sync.SyncTransferKind.DownloadOriginals or Data.Sync.SyncTransferKind.GetExports)
+ await RefreshAsync();
}
catch (DatabaseException ex)
{
diff --git a/src/Titano.App/Sections/ServerView.xaml b/src/Titano.App/Sections/ServerView.xaml
index c4d1d067..a6bdfcd5 100644
--- a/src/Titano.App/Sections/ServerView.xaml
+++ b/src/Titano.App/Sections/ServerView.xaml
@@ -117,6 +117,7 @@
+
@@ -132,37 +133,51 @@
-
+
+
+
+
+
+
+
+
+
-
-
-
+
-
+
+
+
+
+
+
-
+
-
+
+
+
-
-
+
+
-
-
-
-
-
+
+
+
+
+
@@ -171,35 +186,48 @@
-
-
+
+ ScrollViewer.HorizontalScrollBarVisibility="Disabled" KeyboardNavigation.TabNavigation="Continue"
+ AutomationProperties.Name="{l:Loc Server.Sync.Title}" Visibility="{Binding HasRows, Converter={StaticResource BoolToVisibility}}">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Titano.App/Sections/ServerViewModel.cs b/src/Titano.App/Sections/ServerViewModel.cs
index d6daacc2..b3f01f5b 100644
--- a/src/Titano.App/Sections/ServerViewModel.cs
+++ b/src/Titano.App/Sections/ServerViewModel.cs
@@ -12,7 +12,7 @@ namespace Titano.App.Sections;
///
/// Server (BRIEF §12.7, Server.html): address with LAN-discovered servers, connect with trust-on-first-use and login,
/// status (name, version, latency, certificate), account and quota, sign out, the options of this PC (render node,
-/// proxies kept offline, automatic project sync) and the sync table ().
+/// proxies kept offline, automatic project sync, uploads paused on metered networks) and the sync table ().
///
public sealed class ServerViewModel : SectionViewModel
{
@@ -138,7 +138,7 @@ public sealed class ServerViewModel : SectionViewModel
}
}
- /// Keep the proxies of every server sequence without local originals on this PC.
+ /// Keep the proxy caches of server sequences opened with proxies up to date on this PC.
public bool KeepProxiesOffline
{
get => Shell.Services.Settings.KeepProxiesOffline;
@@ -152,6 +152,20 @@ public sealed class ServerViewModel : SectionViewModel
set => SetSyncOption(value, Shell.Services.Settings.SyncSettingsAutomatically, v => Shell.Services.Settings.SyncSettingsAutomatically = v);
}
+ /// Pause uploads while the connection is metered.
+ public bool PauseUploadsOnMetered
+ {
+ get => Shell.Services.Settings.PauseUploadsOnMetered;
+ set
+ {
+ if (Shell.Services.Settings.PauseUploadsOnMetered == value) return;
+ Shell.Services.Settings.PauseUploadsOnMetered = value;
+ Shell.Services.SaveSettings();
+ Shell.Services.Sync.Uploads.Request();
+ OnPropertyChanged();
+ }
+ }
+
/// Re-reads values another screen changed (Settings › Server).
public void Refresh() => OnPropertyChanged(nameof(RenderNodeOptIn));
diff --git a/src/Titano.App/Server/SyncHost.cs b/src/Titano.App/Server/SyncHost.cs
index 69bb216e..445cb56a 100644
--- a/src/Titano.App/Server/SyncHost.cs
+++ b/src/Titano.App/Server/SyncHost.cs
@@ -13,9 +13,10 @@ public delegate ISyncRemote SyncRemoteFactory(Uri address, string token, IReadOn
///
/// This PC's sync with the signed-in server (BRIEF §4.4, §14.5): the of the index, the
-/// connection of upload and download jobs () and a that keeps the
-/// table current while a session is open, started and stopped in order as the session changes. Options come from
-/// and .
+/// connection of upload and download jobs (), a that keeps the
+/// table current while a session is open, started and stopped in order as the session changes, and the
+/// for metered connections. Options come from ,
+/// and .
///
public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDisposable
{
@@ -42,7 +43,12 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
/// Key of the signed-in server (); null while signed out.
public string? Server => Volatile.Read(ref _credentials) is { } c ? SyncService.ServerKey(c.Address) : null;
- public SyncPolicy Policy => new(services.Settings.SyncSettingsAutomatically, services.Settings.KeepProxiesOffline);
+ /// The options, and whether the account may change server sequences (without it this PC's edits are not sent).
+ public SyncPolicy Policy => new(services.Settings.SyncSettingsAutomatically, services.Settings.KeepProxiesOffline,
+ services.Server.User is { } user && (user.Permissions & (long)Permissions.Edit) != 0);
+
+ /// Pauses uploads while the connection is metered and the option is on.
+ public UploadHold Uploads { get; } = new(services);
/// The agent started, stopped or compared, or the table of the signed-in server changed (any thread).
public event Action? Changed;
@@ -68,6 +74,7 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
: null;
Volatile.Write(ref _credentials, wanted);
_switch = SwitchAsync(_switch, wanted);
+ if (services.Engine is { } engine) Uploads.Attach(engine);
}
/// Compares now, once the session switch in progress settled; null while signed out.
@@ -108,6 +115,7 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
public async ValueTask DisposeAsync()
{
Volatile.Write(ref _credentials, null);
+ Uploads.Dispose();
await _switch;
await StopAsync();
}
diff --git a/src/Titano.App/Server/SyncRow.cs b/src/Titano.App/Server/SyncRow.cs
index 91c86041..a5647aa7 100644
--- a/src/Titano.App/Server/SyncRow.cs
+++ b/src/Titano.App/Server/SyncRow.cs
@@ -1,28 +1,43 @@
using System.Windows.Input;
-using Titano.App.Infrastructure;
using Titano.Core.Localization;
+using Titano.Core.Model;
using Titano.Data.Sync;
using Titano.Ui.Controls;
using Titano.Ui.Mvvm;
namespace Titano.App.Server;
-/// A row button: "Upload", "Keep mine", …
-public sealed record SyncRowAction(SyncAction Action, string Text, string ToolTip, ICommand Command);
+/// A row button: "Upload", "Keep mine", …; kept while the row offers it, so focus, tooltips and clicks survive updates.
+public sealed class SyncRowAction(SyncAction action, ICommand command) : ObservableObject
+{
+ private string _toolTip = string.Empty;
+
+ public SyncAction Action { get; } = action;
+ public string Text => Loc.L("Server.Sync.Action." + Action);
+ public ICommand Command { get; } = command;
+
+ public string ToolTip
+ {
+ get => _toolTip;
+ set => Set(ref _toolTip, value);
+ }
+
+ /// The label in the current language.
+ public void Relabel() => OnPropertyChanged(nameof(Text));
+}
/// One row of the sync table (Server.html): sequence, this PC, server, state and the actions that apply.
public sealed class SyncRow : ObservableObject
{
private readonly Func _act;
private SyncEntry _entry;
- private SyncRights _rights;
+ private IReadOnlyList _actions = [];
public SyncRow(SyncEntry entry, SyncRights rights, Func act)
{
_entry = entry;
- _rights = rights;
_act = act;
- Actions = BuildActions();
+ UpdateActions(rights);
}
public SyncEntry Entry => _entry;
@@ -33,26 +48,62 @@ public sealed class SyncRow : ObservableObject
public StatusKind StateKind => SyncText.State(_entry).Kind;
public string StateText => SyncText.State(_entry).Text;
public string StateToolTip => SyncText.State(_entry).ToolTip;
- public IReadOnlyList Actions { get; private set; }
- /// Takes a fresher comparison of the same sequence.
+ /// Settings differ on both sides: the row is drawn on the card colour, as in Server.html.
+ public bool IsConflict => _entry.Status == SyncStatus.Conflict && _entry.Transfer is null;
+
+ public IReadOnlyList Actions
+ {
+ get => _actions;
+ private set => Set(ref _actions, value);
+ }
+
+ /// Takes a fresher comparison of the same sequence; buttons the row still offers are kept.
public void Update(SyncEntry entry, SyncRights rights)
{
_entry = entry;
- _rights = rights;
- Actions = BuildActions();
- OnAllPropertiesChanged();
+ UpdateActions(rights);
+ RaiseTexts();
}
- private List BuildActions() =>
- [
- .. SyncText.Actions(_entry, _rights).Select(action => new SyncRowAction(action, Loc.L("Server.Sync.Action." + action), ToolTip(action),
- new AsyncRelayCommand(() => _act(this, action)))),
- ];
-
- private string ToolTip(SyncAction action) => action switch
+ /// Takes the progress of the row's transfer ("46 % received") without touching its buttons.
+ public void UpdateProgress(long done, long total)
{
- SyncAction.Upload or SyncAction.DownloadOriginals => Loc.L("Tooltips.Server.Sync." + action, Formatting.Bytes(SyncText.Size(_entry, action))),
- _ => Loc.L("Tooltips.Server.Sync." + action),
- };
+ if (_entry.Transfer is not { } job) return;
+ _entry = _entry with { Transfer = job with { ProgressDone = done, ProgressTotal = total } };
+ OnPropertyChanged(nameof(ServerText));
+ }
+
+ /// Texts in the current language.
+ public void Relabel()
+ {
+ foreach (var action in _actions)
+ {
+ action.Relabel();
+ action.ToolTip = SyncText.ToolTip(_entry, action.Action);
+ }
+ RaiseTexts();
+ }
+
+ private void UpdateActions(SyncRights rights)
+ {
+ var wanted = SyncText.Actions(_entry, rights);
+ if (!wanted.SequenceEqual(_actions.Select(a => a.Action)))
+ {
+ var kept = _actions.ToDictionary(a => a.Action);
+ Actions = [.. wanted.Select(action => kept.GetValueOrDefault(action) ?? new SyncRowAction(action, new AsyncRelayCommand(() => _act(this, action))))];
+ }
+ foreach (var action in _actions) action.ToolTip = SyncText.ToolTip(_entry, action.Action);
+ }
+
+ private void RaiseTexts()
+ {
+ OnPropertyChanged(nameof(Title));
+ OnPropertyChanged(nameof(LocalText));
+ OnPropertyChanged(nameof(ServerText));
+ OnPropertyChanged(nameof(StateKind));
+ OnPropertyChanged(nameof(StateText));
+ OnPropertyChanged(nameof(StateToolTip));
+ OnPropertyChanged(nameof(IsConflict));
+ }
}
diff --git a/src/Titano.App/Server/SyncTableViewModel.cs b/src/Titano.App/Server/SyncTableViewModel.cs
index 7b659ee1..88fb2f8f 100644
--- a/src/Titano.App/Server/SyncTableViewModel.cs
+++ b/src/Titano.App/Server/SyncTableViewModel.cs
@@ -1,6 +1,6 @@
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
-using System.Net;
+using System.IO;
using System.Windows.Input;
using System.Windows.Threading;
using Titano.App.Infrastructure;
@@ -10,7 +10,6 @@ using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Jobs;
using Titano.Data.Sync;
-using Titano.Protocol.Client;
using Titano.Ui.Mvvm;
namespace Titano.App.Server;
@@ -26,7 +25,7 @@ public sealed class SyncFilterChip(SyncFilter filter, Action select)
public string ToolTip => Loc.L("Tooltips.Server.Sync.Filter." + Filter);
public ICommand SelectCommand { get; } = new RelayCommand(() => select(filter));
- /// Conflicts waiting for a choice: the chip is drawn in the warning colour.
+ /// Conflicts waiting for a choice: the chip is drawn in the warning colour unless it is the selected one.
public bool NeedsAttention => Filter == SyncFilter.Conflicts && _count > 0;
public bool IsSelected
@@ -45,22 +44,33 @@ public sealed class SyncFilterChip(SyncFilter filter, Action select)
OnPropertyChanged(nameof(NeedsAttention));
}
}
+
+ /// Texts in the current language.
+ public void Relabel()
+ {
+ OnPropertyChanged(nameof(Text));
+ OnPropertyChanged(nameof(ToolTip));
+ }
}
///
/// The sync table of the Server screen (BRIEF §12.7, Server.html): every sequence of this PC and of the signed-in
/// server with what each side holds, where it stands and what can be done (upload, download originals, open with
-/// proxies, settle a settings conflict, pause or resume a transfer), filter chips, search, "Upload all local" and the
-/// footer (last comparison, server archive, admin panel). Reloads when the sync engine reports a change; transfer
-/// progress is applied at most a few times a second. Runs on the UI thread.
+/// proxies, get or send exports, settle a conflict, pause or resume a transfer), filter chips, search, "Upload all
+/// local" and the footer (last comparison, server archive, admin panel). Reloads when the sync engine reports a change;
+/// transfer progress is applied at most a few times a second. Runs on the UI thread.
///
public sealed class SyncTableViewModel : ObservableObject, IDisposable
{
private readonly ShellViewModel _shell;
private readonly IReadOnlyList _chips;
- private readonly HashSet _openWhenCached = [];
+
+ /// "Open with proxies" waiting for its proxy download: server id → the job that fills the cache.
+ private readonly Dictionary _openWhenCached = [];
+
private readonly ConcurrentDictionary _progress = new();
private readonly SemaphoreSlim _reloading = new(1, 1);
+ private readonly AsyncRelayCommand _uploadAll;
private IReadOnlyList _entries = [];
private SyncFilter _filter = SyncFilter.All;
private string _search = string.Empty;
@@ -72,7 +82,7 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
{
_shell = shell;
_chips = [.. Enum.GetValues().Select(f => new SyncFilterChip(f, Select) { IsSelected = f == SyncFilter.All })];
- UploadAllCommand = new AsyncRelayCommand(UploadAllAsync, () => Rights.CanUpload && LocalOnly.Any());
+ _uploadAll = new AsyncRelayCommand(UploadAllAsync, () => IsActive && Rights.CanUpload && LocalOnly.Any());
OpenAdminCommand = new RelayCommand(() =>
{
if (Host.Server is { } server) ShellActions.OpenUrl(server + "/admin");
@@ -81,7 +91,7 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
public ObservableCollection Rows { get; } = [];
public IReadOnlyList Chips => _chips;
- public ICommand UploadAllCommand { get; }
+ public ICommand UploadAllCommand => _uploadAll;
public ICommand OpenAdminCommand { get; }
public string SearchText
@@ -89,7 +99,7 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
get => _search;
set
{
- if (Set(ref _search, value ?? string.Empty)) ApplyFilter();
+ if (Set(ref _search, value ?? string.Empty)) ApplyFilter(rebuild: true);
}
}
@@ -104,7 +114,12 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
: _entries.Count == 0 ? Loc.L("Server.Sync.Empty")
: Loc.L("Server.Sync.NoMatch");
- public string UploadAllToolTip => Loc.L("Tooltips.Server.Sync.UploadAll", Formatting.Count(LocalOnly.Count()), Formatting.Bytes(LocalOnly.Sum(e => e.Local!.TotalBytes)));
+ /// What "Upload all local" sends, or why it is disabled.
+ public string UploadAllToolTip =>
+ !IsActive ? Loc.L("Tooltips.Server.Sync.UploadAll.NotConnected")
+ : !Rights.CanUpload ? Loc.L("Tooltips.Server.Sync.UploadAll.NoRight")
+ : !LocalOnly.Any() ? Loc.L("Tooltips.Server.Sync.UploadAll.Nothing")
+ : Loc.L("Tooltips.Server.Sync.UploadAll", Formatting.Count(LocalOnly.Count()), Formatting.Bytes(LocalOnly.Sum(e => e.Local!.TotalBytes)));
/// "Last sync 10:15 · manifest diff 0.4 s"; null before the first comparison.
public string? LastSyncText => Host.Agent?.Snapshot is { } s
@@ -128,12 +143,13 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
private IEnumerable LocalOnly => _entries.Where(e => e.Status == SyncStatus.LocalOnly);
- /// Starts following the sync engine and the transfer jobs.
+ /// Starts following the sync engine, the transfer jobs and the language.
public void Attach()
{
if (_attached) return;
_attached = true;
Host.Changed += OnChanged;
+ Loc.CultureChanged += OnCultureChanged;
if (_shell.Services.Jobs is { } jobs)
{
jobs.JobChanged += OnJobChanged;
@@ -168,7 +184,9 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
public void Dispose()
{
if (!_attached) return;
+ _attached = false;
Host.Changed -= OnChanged;
+ Loc.CultureChanged -= OnCultureChanged;
if (_shell.Services.Jobs is { } jobs)
{
jobs.JobChanged -= OnJobChanged;
@@ -180,16 +198,15 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
{
_filter = filter;
foreach (var chip in _chips) chip.IsSelected = chip.Filter == filter;
- ApplyFilter();
+ ApplyFilter(rebuild: true);
}
/// Counts, rows, footer and the Library's sync glyphs after a reload.
private void Refresh()
{
foreach (var chip in _chips) chip.Count = _entries.Count(e => SyncText.Matches(e, chip.Filter, string.Empty));
- ApplyFilter();
+ ApplyFilter(rebuild: false);
UpdateLibrary();
- OpenCachedProxies();
OnPropertyChanged(nameof(IsActive));
OnPropertyChanged(nameof(UploadAllToolTip));
OnPropertyChanged(nameof(LastSyncText));
@@ -197,34 +214,52 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
OnPropertyChanged(nameof(ErrorText));
OnPropertyChanged(nameof(ErrorToolTip));
OnPropertyChanged(nameof(CanOpenAdmin));
- CommandManager.InvalidateRequerySuggested();
+ _uploadAll.RaiseCanExecuteChanged();
}
- /// Updates the rows in place (scroll position and recycled containers survive), adding and removing as needed.
- private void ApplyFilter()
+ ///
+ /// Shows the entries the filter and search keep. A reload updates the rows in place (scroll position, focus and
+ /// recycled containers survive); a new filter or search lays the rows out again.
+ ///
+ private void ApplyFilter(bool rebuild)
{
var rights = Rights;
var wanted = _entries.Where(e => SyncText.Matches(e, _filter, _search.Trim())).ToList();
var byId = Rows.ToDictionary(r => r.Id);
- for (var i = 0; i < wanted.Count; i++)
+ if (rebuild)
{
- var entry = wanted[i];
- if (byId.Remove(entry.Id, out var row))
+ Rows.Clear();
+ foreach (var entry in wanted) Rows.Add(Row(entry, rights, byId));
+ }
+ else
+ {
+ for (var i = 0; i < wanted.Count; i++)
{
- row.Update(entry, rights);
- var at = Rows.IndexOf(row);
- if (at != i) Rows.Move(at, i);
+ var entry = wanted[i];
+ if (i < Rows.Count && Rows[i].Id == entry.Id)
+ {
+ Rows[i].Update(entry, rights);
+ continue;
+ }
+ var row = Row(entry, rights, byId);
+ var at = byId.ContainsKey(entry.Id) ? Rows.IndexOf(row) : -1;
+ if (at >= 0) Rows.Move(at, i);
+ else Rows.Insert(i, row);
}
- else
- {
- Rows.Insert(i, new SyncRow(entry, rights, ActAsync));
- }
+ while (Rows.Count > wanted.Count) Rows.RemoveAt(Rows.Count - 1);
}
- while (Rows.Count > wanted.Count) Rows.RemoveAt(Rows.Count - 1);
OnPropertyChanged(nameof(HasRows));
OnPropertyChanged(nameof(EmptyText));
}
+ /// The existing row of an entry, updated, or a new one.
+ private SyncRow Row(SyncEntry entry, SyncRights rights, Dictionary existing)
+ {
+ if (!existing.TryGetValue(entry.Id, out var row)) return new SyncRow(entry, rights, ActAsync);
+ row.Update(entry, rights);
+ return row;
+ }
+
/// The Library shows each local copy's sync state: rows it has loaded take the fresh state.
private void UpdateLibrary()
{
@@ -235,15 +270,6 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
}
}
- /// "Open with proxies" waits for the proxy download; the editor opens once it is in the cache.
- private void OpenCachedProxies()
- {
- foreach (var entry in _entries)
- {
- if (entry.Status == SyncStatus.ProxiesOnly && _openWhenCached.Remove(entry.Id)) _shell.OpenSequence(entry.Id);
- }
- }
-
private async Task ActAsync(SyncRow row, SyncAction action)
{
if (Host is not { Service: { } service, Server: { } server } || _shell.Services.Engine is not { } engine) return;
@@ -253,6 +279,9 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
case SyncAction.Upload:
await service.EnqueueUploadAsync(engine, server, entry.Local!);
break;
+ case SyncAction.SendExports:
+ await service.EnqueueSendExportsAsync(engine, server, entry.Local!);
+ break;
case SyncAction.DownloadOriginals:
if ((entry.Local?.ArchiveId ?? (await Host.CurrentArchiveAsync())?.Id) is not { } archive)
{
@@ -261,12 +290,18 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
}
await service.EnqueueDownloadAsync(engine, server, entry.Id, entry.Title, SyncDownloadMode.Originals, archive);
break;
+ case SyncAction.GetExports:
+ await service.EnqueueDownloadAsync(engine, server, entry.Id, entry.Title, SyncDownloadMode.Exports, null);
+ break;
case SyncAction.OpenWithProxies when entry.Status == SyncStatus.ProxiesOnly && service.OpenRemote(server, entry.Id) is not null:
_shell.OpenSequence(entry.Id);
return;
case SyncAction.OpenWithProxies:
- _openWhenCached.Add(entry.Id);
- await service.EnqueueDownloadAsync(engine, server, entry.Id, entry.Title, SyncDownloadMode.Proxies, null);
+ var job = await service.EnqueueDownloadAsync(engine, server, entry.Id, entry.Title, SyncDownloadMode.Proxies, null);
+ _openWhenCached[entry.Id] = job.Id;
+ // A download that finished before it was recorded here opens at once.
+ if (await _shell.Services.Database!.Jobs.GetAsync(job.Id) is { State: JobState.Done or JobState.Failed or JobState.Cancelled } ended)
+ OpenWhenCached(ended);
break;
case SyncAction.Pause:
await _shell.Services.Jobs!.PauseAsync(entry.Transfer!.Id);
@@ -275,7 +310,7 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
await _shell.Services.Jobs!.ResumeAsync(entry.Transfer!.Id);
break;
case SyncAction.SyncSettings when Host.Remote is { } remote:
- await service.SyncSettingsAsync(remote, entry.Id, CancellationToken.None);
+ await service.SyncSettingsAsync(remote, entry.Id, Host.Policy, CancellationToken.None);
break;
case SyncAction.KeepMine or SyncAction.TakeServer or SyncAction.SaveBoth when Host.Remote is { } remote:
await ResolveAsync(service, remote, entry, action);
@@ -284,40 +319,43 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
await ReloadAsync();
}
- /// Settles a conflict; overwriting one side is confirmed first with the settings that differ (the dry run).
+ /// Settles a conflict once the user confirmed the plan: the settings that differ and what each choice overwrites.
private async Task ResolveAsync(SyncService service, ISyncRemote remote, SyncEntry entry, SyncAction action)
{
+ if (!_shell.Services.Dialogs.Confirm(ConflictRequest(entry, action))) return;
var resolution = action switch
{
SyncAction.KeepMine => SyncResolution.KeepMine,
SyncAction.TakeServer => SyncResolution.TakeServer,
_ => SyncResolution.SaveBoth,
};
- if (resolution != SyncResolution.SaveBoth && !_shell.Services.Dialogs.Confirm(OverwriteRequest(entry, action))) return;
try
{
- await service.ResolveAsync(remote, entry.Id, resolution, CancellationToken.None);
+ if (await service.ResolveAsync(remote, entry.Id, resolution, CancellationToken.None) is { } saved)
+ {
+ _shell.Notify(Loc.L("Server.Sync.SavedBoth"), Path.GetFileName(saved), Loc.L("Server.Sync.ShowCopy"), Loc.L("Tooltips.Server.Sync.ShowCopy"),
+ () => ShellActions.Reveal(saved));
+ }
}
- catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.Conflict)
+ catch (SyncConflictChangedException ex)
{
- // The server copy changed again meanwhile: compare again and let the user choose on fresh content.
- _shell.Notify(Loc.L("Server.Sync.ChangedAgain"));
- await service.RefreshSequencesAsync(remote, [entry.Id], Host.Policy, CancellationToken.None);
+ // One side changed after the choice was shown: the row now shows the fresh content.
+ _shell.Notify(ex.Message);
}
}
- /// "Keep this PC's settings?" with the settings that differ: one side's edits are about to be overwritten.
- private static ConfirmRequest OverwriteRequest(SyncEntry entry, SyncAction action) => new()
+ /// The dry run of a resolution: what is replaced, by which side's values, and where "Save both" keeps this PC's.
+ internal static ConfirmRequest ConflictRequest(SyncEntry entry, SyncAction action) => new()
{
Heading = Loc.L("Server.Conflict." + action + ".Title"),
Message = Loc.L("Server.Conflict." + action + ".Message", entry.Title),
- Detail = SyncText.Conflict(entry),
+ Detail = string.Join(Environment.NewLine, SyncText.Conflict(entry, 10)),
DetailToolTip = Loc.L("Tooltips.Server.Conflict.Detail"),
SafeText = Loc.L("Server.Conflict.Cancel"),
SafeToolTip = Loc.L("Tooltips.Server.Conflict.Cancel"),
ActionText = Loc.L("Server.Sync.Action." + action),
ActionToolTip = Loc.L("Tooltips.Server.Sync." + action),
- IsDestructive = true,
+ IsDestructive = action != SyncAction.SaveBoth,
};
private async Task UploadAllAsync()
@@ -335,11 +373,34 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
private void OnChanged() => Dispatch(() => _ = ReloadAsync());
+ private void OnCultureChanged(object? sender, EventArgs e) => Dispatch(() =>
+ {
+ foreach (var chip in _chips) chip.Relabel();
+ foreach (var row in Rows) row.Relabel();
+ OnAllPropertiesChanged();
+ });
+
private void OnJobChanged(Job job)
{
if (job.Kind is not (JobKind.UploadCommit or JobKind.Download)) return;
_progress.TryRemove(job.Id, out _);
- OnChanged();
+ Dispatch(() =>
+ {
+ OpenWhenCached(job);
+ _ = ReloadAsync();
+ });
+ }
+
+ ///
+ /// "Open with proxies" opens the editor when the download it queued is done, if the Server screen is still the one
+ /// in front; a failed or cancelled download drops the request.
+ ///
+ private void OpenWhenCached(Job job)
+ {
+ if (job.State is not (JobState.Done or JobState.Failed or JobState.Cancelled)) return;
+ var remoteId = _openWhenCached.FirstOrDefault(p => p.Value == job.Id).Key;
+ if (!_openWhenCached.Remove(remoteId)) return;
+ if (job.State == JobState.Done && ReferenceEquals(_shell.Current, _shell.Server)) _shell.OpenSequence(remoteId);
}
/// Transfer progress ("46 % received") without reading the table again, at most a few times a second.
@@ -352,11 +413,9 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
{
await Task.Delay(250);
Interlocked.Exchange(ref _progressQueued, 0);
- var rights = Rights;
foreach (var row in Rows)
{
- if (row.Entry.Transfer is { } job && _progress.TryGetValue(job.Id, out var p))
- row.Update(row.Entry with { Transfer = job with { ProgressDone = p.Done, ProgressTotal = p.Total } }, rights);
+ if (row.Entry.Transfer is { } job && _progress.TryGetValue(job.Id, out var p)) row.UpdateProgress(p.Done, p.Total);
}
});
}
diff --git a/src/Titano.App/Server/SyncText.cs b/src/Titano.App/Server/SyncText.cs
index 07ded298..c3d66d66 100644
--- a/src/Titano.App/Server/SyncText.cs
+++ b/src/Titano.App/Server/SyncText.cs
@@ -1,3 +1,6 @@
+using System.Globalization;
+using System.Text.Json;
+using System.Text.Json.Nodes;
using Titano.App.Infrastructure;
using Titano.Core.Localization;
using Titano.Core.Model;
@@ -25,23 +28,68 @@ public enum SyncAction
KeepMine,
TakeServer,
SaveBoth,
+ GetExports,
+ SendExports,
Pause,
Resume,
}
/// What the signed-in account may do (server permission bits).
-/// Send originals and settings.
+/// Send originals, settings and renders (upload and edit rights).
/// Download originals (editors and render nodes).
-public readonly record struct SyncRights(bool CanUpload, bool CanFetchOriginals)
+/// Change the server's copy of a sequence's settings, title, tags, rating and notes.
+public readonly record struct SyncRights(bool CanUpload, bool CanFetchOriginals, bool CanEdit)
{
public static SyncRights Of(long permissions) => new(
(permissions & (long)Permissions.Upload) != 0 && (permissions & (long)Permissions.Edit) != 0,
- (permissions & (long)(Permissions.Edit | Permissions.RenderNode)) != 0);
+ (permissions & (long)(Permissions.Edit | Permissions.RenderNode)) != 0,
+ (permissions & (long)Permissions.Edit) != 0);
}
/// The words, glyphs and actions of the sync table (BRIEF §12.7): plain functions of a .
public static class SyncText
{
+ /// Resource keys of the settings and sequence fields a conflict can show, by JSON path (sections, then members).
+ private static readonly Dictionary SettingKeys = new(StringComparer.OrdinalIgnoreCase)
+ {
+ ["sequence.title"] = "Server.Setting.Title",
+ ["sequence.tags"] = "Server.Setting.Tags",
+ ["sequence.rating"] = "Server.Setting.Rating",
+ ["sequence.notes"] = "Server.Setting.Notes",
+ ["timing.mode"] = "Server.Setting.TimingMode",
+ ["timing.frameRate"] = "Server.Setting.FrameRate",
+ ["timing.fitToSeconds"] = "Server.Setting.FitToDuration",
+ ["timing.remapCurve"] = "Server.Setting.TimeRemap",
+ ["exposure.deflicker"] = "Server.Setting.Deflicker",
+ ["exposure.deflickerWindow"] = "Server.Setting.DeflickerWindow",
+ ["exposure.deflickerStrength"] = "Server.Setting.DeflickerStrength",
+ ["exposure.regions"] = "Server.Setting.Regions",
+ ["exposure.holyGrail"] = "Server.Setting.HolyGrail",
+ ["exposure.transitionLength"] = "Server.Setting.Transitions",
+ ["exposure.whiteBalanceSmoothing"] = "Server.Setting.WhiteBalance",
+ ["exposure.exposureOffsetEv"] = "Server.Setting.ExposureOffset",
+ ["exposure.highlightKnee"] = "Server.Setting.HighlightKnee",
+ ["motion.motionBlur"] = "Server.Setting.MotionBlur",
+ ["motion.targetShutterAngle"] = "Server.Setting.ShutterAngle",
+ ["motion.flowQuality"] = "Server.Setting.FlowQuality",
+ ["motion.stabilize"] = "Server.Setting.Stabilize",
+ ["motion.stabilizeSmoothing"] = "Server.Setting.StabilizeSmoothing",
+ ["camera.crop"] = "Server.Setting.Crop",
+ ["camera.aspect"] = "Server.Setting.Aspect",
+ ["camera.offsetX"] = "Server.Setting.CameraOffset",
+ ["camera.offsetY"] = "Server.Setting.CameraOffset",
+ ["camera.keyframes"] = "Server.Setting.Keyframes",
+ ["stack.mode"] = "Server.Setting.Stacking",
+ ["stack.length"] = "Server.Setting.TrailLength",
+ ["export.preset"] = "Server.Setting.ExportPreset",
+ ["export.overrides"] = "Server.Setting.ExportValues",
+ ["export.nameTemplate"] = "Server.Setting.OutputName",
+ ["export.folder"] = "Server.Setting.ExportFolder",
+ ["export.batch"] = "Server.Setting.Batch",
+ ["excludedFrames"] = "Server.Setting.ExcludedFrames",
+ ["heroFrame"] = "Server.Setting.HeroFrame",
+ };
+
/// "originals · proxies", "proxies" (cached server proxies) or "—".
public static string Local(SyncEntry entry)
{
@@ -76,6 +124,7 @@ public static class SyncText
Loc.L(upload ? "Tooltips.Server.Sync.State.Uploading" : "Tooltips.Server.Sync.State.Downloading")),
};
}
+ if (entry.Link?.Error is { } error) return (StatusKind.Warning, Loc.L("Server.Sync.State.Failed"), Loc.L("Tooltips.Server.Sync.State.Failed", error));
return entry.Status switch
{
SyncStatus.InSync => (StatusKind.Done, Loc.L("Server.Sync.State.InSync"), Loc.L("Tooltips.Server.Sync.State.InSync")),
@@ -84,16 +133,44 @@ public static class SyncText
SyncStatus.ProxiesOnly => (StatusKind.Remote, Loc.L("Server.Sync.State.ProxiesOnly"), Loc.L("Tooltips.Server.Sync.State.ProxiesOnly")),
SyncStatus.LocalAhead => (StatusKind.Synced, Loc.L("Server.Sync.State.LocalAhead"), Loc.L("Tooltips.Server.Sync.State.LocalAhead")),
SyncStatus.ServerAhead => (StatusKind.Remote, Loc.L("Server.Sync.State.ServerAhead"), Loc.L("Tooltips.Server.Sync.State.ServerAhead")),
- _ => (StatusKind.Warning, Loc.L("Server.Sync.State.Conflict"), Conflict(entry)),
+ _ => (StatusKind.Warning, Loc.L("Server.Sync.State.Conflict"), ConflictSummary(entry)),
};
}
- /// "exposure.deflickerWindow.manual: 41 here, 33 on the server" for each setting that differs (at most three).
- public static string Conflict(SyncEntry entry)
+ /// One line for the State tooltip: "Deflicker window 41 here, 33 on the server · +2".
+ public static string ConflictSummary(SyncEntry entry)
{
- var differences = entry.Link?.ConflictSettingsJson is { } theirs ? SyncRules.Differences(entry.Local?.SettingsJson, theirs) : [];
- if (differences.Count == 0) return Loc.L("Tooltips.Server.Sync.State.Conflict");
- return string.Join("\n", differences.Select(d => Loc.L("Server.Conflict.Difference", d.Path, d.Mine ?? Loc.L("Server.Sync.None"), d.Theirs ?? Loc.L("Server.Sync.None"))));
+ var lines = Conflict(entry, int.MaxValue);
+ return lines.Count switch
+ {
+ 0 => Loc.L("Tooltips.Server.Sync.State.Conflict"),
+ 1 => lines[0],
+ _ => Loc.L("Server.Conflict.More", lines[0], Formatting.Count(lines.Count - 1)),
+ };
+ }
+
+ ///
+ /// What differs, one line per setting or sequence field in project order ("Deflicker window 41 here, 33 on the
+ /// server", "Tags differ"), at most .
+ ///
+ public static IReadOnlyList Conflict(SyncEntry entry, int max)
+ {
+ if (entry is not { Local: { } local, Link.ConflictRemote: { } theirs }) return [];
+ var lines = new List();
+ var shown = new HashSet(StringComparer.Ordinal);
+ foreach (var difference in SyncRules.Differences(SyncPayload.Of(local), theirs, 64))
+ {
+ var (key, field) = Setting(difference.Path);
+ if (!shown.Add(key)) continue;
+ var label = Loc.L(key);
+ var metadata = difference.Path.StartsWith(SyncRules.MetadataPath, StringComparison.Ordinal);
+ lines.Add((metadata ? Text(difference.Mine) : Value(difference.Mine, field)) is { } mine
+ && (metadata ? Text(difference.Theirs) : Value(difference.Theirs, field)) is { } server
+ ? Loc.L("Server.Conflict.Difference", label, mine, server)
+ : Loc.L("Server.Conflict.Changed", label));
+ if (lines.Count == max) break;
+ }
+ return lines;
}
/// The row's buttons, in the order the mockup shows them.
@@ -116,24 +193,42 @@ public static class SyncText
case SyncStatus.ServerAhead when entry.Link is { MissingLocal: > 0 }:
if (rights.CanFetchOriginals) actions.Add(SyncAction.DownloadOriginals);
break;
- case SyncStatus.LocalAhead or SyncStatus.ServerAhead:
+ case SyncStatus.LocalAhead when rights.CanEdit:
+ case SyncStatus.ServerAhead:
actions.Add(SyncAction.SyncSettings);
break;
case SyncStatus.Conflict:
- actions.AddRange([SyncAction.KeepMine, SyncAction.TakeServer, SyncAction.SaveBoth]);
- break;
+ if (rights.CanEdit) actions.Add(SyncAction.KeepMine);
+ actions.AddRange([SyncAction.TakeServer, SyncAction.SaveBoth]);
+ return actions;
+ }
+ if (entry is { Local: not null, Link: { } link })
+ {
+ if (link.ExportsToGet > 0) actions.Add(SyncAction.GetExports);
+ if (link.ExportsToSend > 0 && rights.CanUpload) actions.Add(SyncAction.SendExports);
}
return actions;
}
- /// The bytes an upload or download of the row moves (for the tooltips).
+ /// The bytes an action moves (for the tooltips).
public static long Size(SyncEntry entry, SyncAction action) => action switch
{
SyncAction.Upload => entry.Local?.TotalBytes ?? 0,
SyncAction.DownloadOriginals => entry.Link?.TotalBytes ?? 0,
+ SyncAction.GetExports => entry.Link?.ExportsToGetBytes ?? 0,
+ SyncAction.SendExports => entry.Link?.ExportsToSendBytes ?? 0,
_ => 0,
};
+ /// The tooltip of a row button: what it does and, for transfers, what it moves.
+ public static string ToolTip(SyncEntry entry, SyncAction action) => action switch
+ {
+ SyncAction.Upload or SyncAction.DownloadOriginals => Loc.L("Tooltips.Server.Sync." + action, Formatting.Bytes(Size(entry, action))),
+ SyncAction.GetExports => Loc.Plural("Tooltips.Server.Sync.GetExports", entry.Link!.ExportsToGet, Formatting.Bytes(Size(entry, action))),
+ SyncAction.SendExports => Loc.Plural("Tooltips.Server.Sync.SendExports", entry.Link!.ExportsToSend, Formatting.Bytes(Size(entry, action))),
+ _ => Loc.L("Tooltips.Server.Sync." + action),
+ };
+
public static bool Matches(SyncEntry entry, SyncFilter filter, string search) =>
(filter switch
{
@@ -143,5 +238,49 @@ public static class SyncText
_ => true,
}) && (search.Length == 0 || entry.Title.Contains(search, StringComparison.CurrentCultureIgnoreCase));
+ ///
+ /// The label key of a difference path and the member below the setting it reaches ("isAuto", "manual", … of an
+ /// Auto parameter; null for the setting itself): "exposure.deflickerWindow.manual" is the deflicker window's manual value.
+ ///
+ private static (string Key, string? Field) Setting(string path)
+ {
+ var segments = path.Split('.');
+ for (var length = segments.Length; length > 0; length--)
+ {
+ if (SettingKeys.TryGetValue(string.Join('.', segments[..length]), out var key)) return (key, length < segments.Length ? segments[length] : null);
+ }
+ return ("Server.Setting.Other", segments[^1]);
+ }
+
+ /// A title, tags, rating or notes value as shown ("—" when empty).
+ private static string Text(string? value) => string.IsNullOrWhiteSpace(value) ? Loc.L("Server.Sync.None") : Shorten(value);
+
+ /// A side's value as shown ("41", "on", "auto"); null when it is not a single value worth printing.
+ private static string? Value(string? json, string? field)
+ {
+ if (json is null) return field is null or "manual" ? Loc.L("Server.Sync.None") : null;
+ if (field is not (null or "manual" or "autoValue" or "isAuto")) return null;
+ JsonNode? node;
+ try
+ {
+ node = JsonNode.Parse(json);
+ }
+ catch (JsonException)
+ {
+ return null;
+ }
+ if (field == "isAuto") return node is JsonValue v && v.TryGetValue(out var auto) ? Loc.L(auto ? "Server.Setting.Auto" : "Server.Setting.Manual") : null;
+ return node switch
+ {
+ null => Loc.L("Server.Sync.None"),
+ JsonValue value when value.TryGetValue(out var flag) => Loc.L(flag ? "Server.Setting.On" : "Server.Setting.Off"),
+ JsonValue value when value.TryGetValue(out var number) => number.ToString("0.###", Loc.Culture),
+ JsonValue value when value.TryGetValue(out var text) => Shorten(text),
+ _ => null,
+ };
+ }
+
+ private static string Shorten(string text) => text.Length <= 40 ? text : string.Concat(text.AsSpan(0, 39), "…");
+
private static string Join(params string[] parts) => string.Join(" · ", parts);
}
diff --git a/src/Titano.App/Server/UploadHold.cs b/src/Titano.App/Server/UploadHold.cs
new file mode 100644
index 00000000..df5735b9
--- /dev/null
+++ b/src/Titano.App/Server/UploadHold.cs
@@ -0,0 +1,147 @@
+using System.Net.NetworkInformation;
+using System.Runtime.InteropServices;
+using Titano.App.Infrastructure;
+using Titano.Core.Diagnostics;
+using Titano.Core.Model;
+using Titano.Data.Jobs;
+
+namespace Titano.App.Server;
+
+///
+/// "Pause uploads on metered networks" (Server.html): while is on and
+/// Windows reports the connection as metered, upload jobs are paused (a running one stops after its current chunk and
+/// keeps its checkpoint); they are resumed when the connection is no longer metered or the option goes off. Only the
+/// uploads it paused are resumed. Checked when the network or an upload job changes, and every minute.
+///
+public sealed class UploadHold : IDisposable
+{
+ private static readonly TimeSpan Interval = TimeSpan.FromMinutes(1);
+
+ private readonly AppServices _services;
+ private readonly HashSet _held = [];
+ private readonly SemaphoreSlim _gate = new(1, 1);
+ private readonly Timer _timer;
+ private JobEngine? _engine;
+
+ public UploadHold(AppServices services)
+ {
+ _services = services;
+ _timer = new Timer(_ => Request(), null, Interval, Interval);
+ NetworkChange.NetworkAddressChanged += OnNetworkChanged;
+ NetworkChange.NetworkAvailabilityChanged += OnNetworkChanged;
+ }
+
+ /// Follows the jobs of (once the index is open).
+ public void Attach(JobEngine engine)
+ {
+ if (ReferenceEquals(_engine, engine)) return;
+ if (_engine is not null) _engine.JobChanged -= OnJobChanged;
+ _engine = engine;
+ engine.JobChanged += OnJobChanged;
+ Request();
+ }
+
+ /// Applies the option and the connection's cost now (the option changed).
+ public void Request() => _ = ApplyAsync();
+
+ public void Dispose()
+ {
+ _timer.Dispose();
+ NetworkChange.NetworkAddressChanged -= OnNetworkChanged;
+ NetworkChange.NetworkAvailabilityChanged -= OnNetworkChanged;
+ if (_engine is not null) _engine.JobChanged -= OnJobChanged;
+ }
+
+ private bool Holding => _services.Settings.PauseUploadsOnMetered && MeteredNetwork.IsMetered();
+
+ private void OnNetworkChanged(object? sender, EventArgs e) => Request();
+
+ private void OnJobChanged(Job job)
+ {
+ if (job is { Kind: JobKind.UploadCommit, State: JobState.Queued or JobState.Running }) Request();
+ }
+
+ private async Task ApplyAsync()
+ {
+ if (_engine is not { } engine || _services.Database is not { } db) return;
+ await _gate.WaitAsync();
+ try
+ {
+ if (Holding)
+ {
+ foreach (var state in new[] { JobState.Queued, JobState.Running })
+ {
+ foreach (var job in (await db.Jobs.ListAsync(state, JobKind.UploadCommit, 500)).Items)
+ {
+ await engine.PauseAsync(job.Id);
+ _held.Add(job.Id);
+ }
+ }
+ return;
+ }
+ foreach (var id in _held)
+ {
+ if (await db.Jobs.GetAsync(id) is { State: JobState.Paused }) await engine.ResumeAsync(id);
+ }
+ _held.Clear();
+ }
+ catch (Data.DatabaseException ex)
+ {
+ Log.Warning("sync", "Uploads could not be paused or resumed for the metered connection", ex);
+ }
+ finally
+ {
+ _gate.Release();
+ }
+ }
+}
+
+/// The connection cost Windows reports (Settings › Network › Metered connection) through INetworkCostManager.
+internal static class MeteredNetwork
+{
+ private const uint Fixed = 0x2;
+ private const uint Variable = 0x4;
+ private const uint OverDataLimit = 0x10000;
+ private const uint Roaming = 0x40000;
+
+ /// True when the machine's connection is metered (fixed or variable cost, over its data limit, or roaming); false when unknown.
+ public static bool IsMetered()
+ {
+ try
+ {
+ var manager = (INetworkCostManager)new NetworkListManagerClass();
+ try
+ {
+ return manager.GetCost(out var cost, IntPtr.Zero) == 0 && (cost & (Fixed | Variable | OverDataLimit | Roaming)) != 0;
+ }
+ finally
+ {
+ Marshal.ReleaseComObject(manager);
+ }
+ }
+ catch (COMException ex)
+ {
+ Log.Warning("sync", "The connection cost could not be read", ex);
+ return false;
+ }
+ }
+
+ [ComImport]
+ [Guid("DCB00C01-570F-4A9B-8D69-199FDBA5723B")]
+ private class NetworkListManagerClass;
+
+ [ComImport]
+ [Guid("DCB00008-570F-4A9B-8D69-199FDBA5723B")]
+ [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+ private interface INetworkCostManager
+ {
+ [PreserveSig]
+ int GetCost(out uint cost, IntPtr destination);
+
+ [PreserveSig]
+ int GetDataPlanStatus(IntPtr status, IntPtr destination);
+
+ [PreserveSig]
+ int SetDestinationAddresses(uint length, IntPtr addresses, short append);
+ }
+}
diff --git a/src/Titano.App/Shell/ScreenCapture.cs b/src/Titano.App/Shell/ScreenCapture.cs
index 22291041..af7efa5e 100644
--- a/src/Titano.App/Shell/ScreenCapture.cs
+++ b/src/Titano.App/Shell/ScreenCapture.cs
@@ -14,7 +14,7 @@ namespace Titano.App.Shell;
///
/// Titano.exe --capture <dir>: renders every client screen at 1440×900 in the dark and light themes to
/// <screen>-<theme>.png from (first run on an empty index, then a populated
-/// one, then the sync table with an upload and a settings conflict), for visual review and phase reports (BRIEF §20.2), then exits.
+/// one, then the sync table with transfers, a conflict and its confirmation), for visual review and phase reports (BRIEF §20.2), then exits.
///
public static class ScreenCapture
{
@@ -57,6 +57,7 @@ public static class ScreenCapture
await shell.Jobs.ReloadAsync();
await shell.Services.Server.RestoreAsync();
await shell.Services.Sync.RefreshAsync();
+ await sample.CacheProxiesAsync();
await shell.Server.Sync.ReloadAsync();
shell.About.SetResult(null);
await shell.Settings.DetectEncodersAsync();
@@ -100,7 +101,7 @@ public static class ScreenCapture
written += await SaveAsync(window, directory, key.ToString().ToLowerInvariant(), theme);
}
}
- // The sync table with an upload in progress, then filtered to the settings conflict.
+ // The sync table with transfers in every state, then filtered to the conflict, then the conflict's confirmation.
await sample.FillSyncAsync();
await shell.Server.Sync.ReloadAsync();
foreach (var theme in Themes)
@@ -111,6 +112,9 @@ public static class ScreenCapture
written += await SaveAsync(window, directory, "server-sync", theme);
SelectSyncFilter(shell, SyncFilter.Conflicts);
written += await SaveAsync(window, directory, "server-conflict", theme);
+ if (shell.Server.Sync.Rows.FirstOrDefault() is { } conflict)
+ written += await SaveDialogAsync(new ConfirmDialog(SyncTableViewModel.ConflictRequest(conflict.Entry, SyncAction.KeepMine)), directory,
+ "server-conflict-confirm", theme);
SelectSyncFilter(shell, SyncFilter.All);
}
Log.Info("capture", $"{written} screenshot(s) written to {Path.GetFullPath(directory)}");
@@ -130,6 +134,25 @@ public static class ScreenCapture
private static void SelectSyncFilter(ShellViewModel shell, SyncFilter filter) =>
shell.Server.Sync.Chips.First(c => c.Filter == filter).SelectCommand.Execute(null);
+ /// A dialog shown off screen, saved, then closed.
+ private static async Task SaveDialogAsync(Window dialog, string directory, string screen, AppTheme theme)
+ {
+ dialog.WindowStartupLocation = WindowStartupLocation.Manual;
+ dialog.Left = -20000;
+ dialog.Top = -20000;
+ dialog.ShowActivated = false;
+ dialog.ShowInTaskbar = false;
+ dialog.Show();
+ try
+ {
+ return await SaveAsync(dialog, directory, screen, theme);
+ }
+ finally
+ {
+ dialog.Close();
+ }
+ }
+
private static async Task SaveAsync(Window window, string directory, string screen, AppTheme theme, ShellViewModel? waitForThumbnails = null)
{
await Settle(window);
diff --git a/src/Titano.Data/Migrations/postgres/0004_sync.sql b/src/Titano.Data/Migrations/postgres/0004_sync.sql
index 2acff6e6..88aff90a 100644
--- a/src/Titano.Data/Migrations/postgres/0004_sync.sql
+++ b/src/Titano.Data/Migrations/postgres/0004_sync.sql
@@ -1,5 +1,6 @@
-- Client sync state (BRIEF §4.4, §14.5): one row per sequence of a server this client has listed, linked to the local
--- copy when there is one. Versions are the counters both sides agreed on at the last sync. Mirrors sqlite/0004_sync.sql.
+-- copy when there is one. Versions are the counters both sides agreed on at the last sync; conflict_remote is the
+-- server's side (title, tags, rating, notes, settings) of an unresolved conflict as JSON. Mirrors sqlite/0004_sync.sql.
CREATE TABLE sync_links (
server text COLLATE "C" NOT NULL,
@@ -17,13 +18,20 @@ CREATE TABLE sync_links (
base_remote_settings integer,
diff_local_frames integer,
diff_remote_frames integer,
+ diff_local_renders integer,
+ diff_remote_renders integer,
missing_remote integer NOT NULL DEFAULT 0,
missing_local integer NOT NULL DEFAULT 0,
+ exports_to_get integer NOT NULL DEFAULT 0,
+ exports_to_get_bytes bigint NOT NULL DEFAULT 0,
+ exports_to_send integer NOT NULL DEFAULT 0,
+ exports_to_send_bytes bigint NOT NULL DEFAULT 0,
conflict_local_settings integer,
conflict_remote_settings integer,
- conflict_settings text,
+ conflict_remote text,
proxies_cached integer NOT NULL DEFAULT 0,
proxies_frames_version integer,
+ error text,
synced_at timestamptz,
PRIMARY KEY (server, remote_id)
);
diff --git a/src/Titano.Data/Migrations/sqlite/0004_sync.sql b/src/Titano.Data/Migrations/sqlite/0004_sync.sql
index 9e846d0d..ddc67059 100644
--- a/src/Titano.Data/Migrations/sqlite/0004_sync.sql
+++ b/src/Titano.Data/Migrations/sqlite/0004_sync.sql
@@ -1,5 +1,6 @@
-- Client sync state (BRIEF §4.4, §14.5): one row per sequence of a server this client has listed, linked to the local
--- copy when there is one. Versions are the counters both sides agreed on at the last sync. Mirrors postgres/0004_sync.sql.
+-- copy when there is one. Versions are the counters both sides agreed on at the last sync; conflict_remote is the
+-- server's side (title, tags, rating, notes, settings) of an unresolved conflict as JSON. Mirrors postgres/0004_sync.sql.
CREATE TABLE sync_links (
server TEXT NOT NULL,
@@ -17,13 +18,20 @@ CREATE TABLE sync_links (
base_remote_settings INTEGER,
diff_local_frames INTEGER,
diff_remote_frames INTEGER,
+ diff_local_renders INTEGER,
+ diff_remote_renders INTEGER,
missing_remote INTEGER NOT NULL DEFAULT 0,
missing_local INTEGER NOT NULL DEFAULT 0,
+ exports_to_get INTEGER NOT NULL DEFAULT 0,
+ exports_to_get_bytes INTEGER NOT NULL DEFAULT 0,
+ exports_to_send INTEGER NOT NULL DEFAULT 0,
+ exports_to_send_bytes INTEGER NOT NULL DEFAULT 0,
conflict_local_settings INTEGER,
conflict_remote_settings INTEGER,
- conflict_settings TEXT,
+ conflict_remote TEXT,
proxies_cached INTEGER NOT NULL DEFAULT 0,
proxies_frames_version INTEGER,
+ error TEXT,
synced_at TEXT,
PRIMARY KEY (server, remote_id)
);
diff --git a/src/Titano.Data/Sync/SyncAgent.cs b/src/Titano.Data/Sync/SyncAgent.cs
index 1bb57426..1d47748d 100644
--- a/src/Titano.Data/Sync/SyncAgent.cs
+++ b/src/Titano.Data/Sync/SyncAgent.cs
@@ -27,8 +27,8 @@ public sealed record SyncAgentOptions
/// and every ; the sequences named by the server's sequenceChanged
/// events (WebSocket) compared again within a second, except those an upload or download is working on (it compares
/// them when it ends); failures retried with exponential backoff; and, with
-/// , proxy downloads queued for every server sequence without local
-/// originals whose cache is stale. Dispose to stop.
+/// , background downloads that bring stale proxy caches of the sequences
+/// this PC opened with proxies up to date (a failed one waits with the same backoff). Dispose to stop.
///
public sealed class SyncAgent : IAsyncDisposable
{
@@ -43,6 +43,11 @@ public sealed class SyncAgent : IAsyncDisposable
private readonly Channel _refreshWake = Channel.CreateBounded(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropWrite });
private readonly Channel _eventWake = Channel.CreateBounded(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropWrite });
private readonly ConcurrentDictionary _changed = new();
+
+ /// Proxy downloads this agent queued, by server id, and the failures of each sequence's last ones.
+ private readonly Dictionary _proxyJobs = [];
+
+ private readonly Dictionary _proxyFailures = [];
private readonly SemaphoreSlim _refreshing = new(1, 1);
private Task _running = Task.CompletedTask;
private SyncSnapshot? _snapshot;
@@ -200,8 +205,9 @@ public sealed class SyncAgent : IAsyncDisposable
var due = ids.Where(id => !busy.Contains(id)).ToList();
if (due.Count > 0) await _sync.RefreshSequencesAsync(_remote, due, _policy(), cancellationToken).ConfigureAwait(false);
}
- catch (Exception ex) when (ex is not OperationCanceledException)
+ catch (Exception ex) when (!cancellationToken.IsCancellationRequested)
{
+ // Includes timeouts reported as cancellation: the loop goes on and a full comparison catches up.
Log.Warning(LogCategory, $"Comparing changed sequences of {Server} failed", ex);
RequestRefresh();
}
@@ -212,14 +218,34 @@ public sealed class SyncAgent : IAsyncDisposable
}
}
- /// Queues (background priority) the proxies of server sequences without local originals whose cache is stale.
+ ///
+ /// Queues (background priority) the proxies of server sequences this PC keeps proxies of, without local originals,
+ /// whose cache is stale; a sequence whose last download failed waits for its backoff.
+ ///
private async Task QueueProxiesAsync(SyncSnapshot snapshot, CancellationToken cancellationToken)
{
+ foreach (var (remoteId, jobId) in _proxyJobs.ToList())
+ {
+ var job = await _sync.Database.Jobs.GetAsync(jobId, cancellationToken).ConfigureAwait(false);
+ if (job?.State is JobState.Queued or JobState.Running or JobState.Paused) continue;
+ _proxyJobs.Remove(remoteId);
+ if (job?.State == JobState.Failed)
+ {
+ var failures = _proxyFailures.GetValueOrDefault(remoteId).Failures + 1;
+ _proxyFailures[remoteId] = (failures, DateTime.UtcNow + Backoff(failures));
+ }
+ else
+ {
+ _proxyFailures.Remove(remoteId);
+ }
+ }
foreach (var entry in snapshot.Entries)
{
- if (entry is not { Local: null, Transfer: null, Link: { ServerHasProxies: true, ProxiesStale: true } link }) continue;
- await _sync.EnqueueDownloadAsync(_engine!, Server, link.RemoteId, link.Title, SyncDownloadMode.Proxies, null, JobPriority.Background, cancellationToken)
- .ConfigureAwait(false);
+ if (entry is not { Local: null, Transfer: null, Link: { ProxiesCached: > 0, ServerHasProxies: true, ProxiesStale: true } link }) continue;
+ if (_proxyFailures.TryGetValue(link.RemoteId, out var failed) && DateTime.UtcNow < failed.NotBefore) continue;
+ var job = await _sync.EnqueueDownloadAsync(_engine!, Server, link.RemoteId, link.Title, SyncDownloadMode.Proxies, null, JobPriority.Background,
+ cancellationToken).ConfigureAwait(false);
+ _proxyJobs[link.RemoteId] = job.Id;
}
}
diff --git a/src/Titano.Data/Sync/SyncJobHandler.cs b/src/Titano.Data/Sync/SyncJobHandler.cs
index ab826246..be602d4b 100644
--- a/src/Titano.Data/Sync/SyncJobHandler.cs
+++ b/src/Titano.Data/Sync/SyncJobHandler.cs
@@ -6,12 +6,54 @@ using Titano.Data.Jobs;
namespace Titano.Data.Sync;
+/// What an upload or download job moves.
+public enum SyncTransferKind
+{
+ /// Originals and settings of a local sequence to the server.
+ Upload,
+
+ /// Renders of a local copy to the server.
+ SendExports,
+
+ /// Originals of a server sequence into the local archive.
+ DownloadOriginals,
+
+ /// Proxies of a server sequence into the proxy cache.
+ DownloadProxies,
+
+ /// Renders of a server sequence into the local copy's exports.
+ GetExports,
+}
+
///
/// Parameters of upload and download jobs. The job's target is the local sequence for an upload and the server
/// sequence for a download.
///
/// Archive that receives downloaded originals when the sequence has no local copy yet.
-internal sealed record SyncJobParameters(string Server, SyncDownloadMode? Mode = null, Guid? ArchiveId = null);
+internal sealed record SyncJobParameters(string Server, SyncDownloadMode? Mode = null, Guid? ArchiveId = null, SyncUploadMode? Upload = null)
+{
+ public static SyncJobParameters? Read(Job job)
+ {
+ if (job.Kind is not (JobKind.UploadCommit or JobKind.Download) || job.ParametersJson is not { } json) return null;
+ try
+ {
+ return JsonSerializer.Deserialize(json, SyncJobJson.Default.SyncJobParameters);
+ }
+ catch (JsonException)
+ {
+ return null;
+ }
+ }
+
+ public SyncTransferKind Kind(JobKind kind) => (kind, Mode, Upload) switch
+ {
+ (JobKind.UploadCommit, _, SyncUploadMode.Exports) => SyncTransferKind.SendExports,
+ (JobKind.UploadCommit, _, _) => SyncTransferKind.Upload,
+ (_, SyncDownloadMode.Proxies, _) => SyncTransferKind.DownloadProxies,
+ (_, SyncDownloadMode.Exports, _) => SyncTransferKind.GetExports,
+ _ => SyncTransferKind.DownloadOriginals,
+ };
+}
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
@@ -25,19 +67,24 @@ internal sealed class SyncJobHandler(JobKind kind, SyncTransfers transfers, ISyn
public async Task RunAsync(JobContext context)
{
- var parameters = (context.Job.ParametersJson is { } json ? JsonSerializer.Deserialize(json, SyncJobJson.Default.SyncJobParameters) : null)
- ?? throw new InvalidDataException("The job has no sync parameters.");
+ var parameters = SyncJobParameters.Read(context.Job) ?? throw new InvalidDataException("The job has no sync parameters.");
if (context.Job.TargetIds is not [var target, ..]) throw new InvalidDataException("The job has no target sequence.");
using var remote = await connector.ConnectAsync(parameters.Server, context.Cancellation).ConfigureAwait(false)
?? throw new InvalidOperationException(Loc.L("Errors.Sync.NotConnected"));
- switch (kind, parameters.Mode)
+ switch (parameters.Kind(kind))
{
- case (JobKind.UploadCommit, _):
+ case SyncTransferKind.Upload:
await transfers.UploadAsync(context, remote, target).ConfigureAwait(false);
break;
- case (JobKind.Download, SyncDownloadMode.Proxies):
+ case SyncTransferKind.SendExports:
+ await transfers.SendExportsAsync(context, remote, target).ConfigureAwait(false);
+ break;
+ case SyncTransferKind.DownloadProxies:
await transfers.DownloadProxiesAsync(context, remote, target).ConfigureAwait(false);
break;
+ case SyncTransferKind.GetExports:
+ await transfers.GetExportsAsync(context, remote, target).ConfigureAwait(false);
+ break;
default:
await transfers.DownloadOriginalsAsync(context, remote, target, parameters.ArchiveId).ConfigureAwait(false);
break;
diff --git a/src/Titano.Data/Sync/SyncLinkStore.cs b/src/Titano.Data/Sync/SyncLinkStore.cs
index 33f1496e..1d28e3f4 100644
--- a/src/Titano.Data/Sync/SyncLinkStore.cs
+++ b/src/Titano.Data/Sync/SyncLinkStore.cs
@@ -1,3 +1,4 @@
+using System.Text.Json;
using Titano.Core.Model;
using Titano.Data.Repositories;
@@ -9,8 +10,8 @@ internal sealed class SyncLinkStore(TitanoDatabase db)
private const string Columns =
"server, remote_id, local_id, title, frame_count, total_bytes, render_count, remote_status, remote_settings_version, " +
"remote_frames_version, remote_updated_at, base_local_settings, base_remote_settings, diff_local_frames, diff_remote_frames, " +
- "missing_remote, missing_local, conflict_local_settings, conflict_remote_settings, conflict_settings, proxies_cached, " +
- "proxies_frames_version, synced_at";
+ "diff_local_renders, diff_remote_renders, missing_remote, missing_local, exports_to_get, exports_to_get_bytes, exports_to_send, " +
+ "exports_to_send_bytes, conflict_local_settings, conflict_remote_settings, conflict_remote, proxies_cached, proxies_frames_version, error, synced_at";
private static readonly string UpsertSql = SqlDialect.Upsert("sync_links", Columns.Split(", "), 2);
@@ -32,8 +33,10 @@ internal sealed class SyncLinkStore(TitanoDatabase db)
[
l.Server, l.RemoteId, l.LocalId, l.Title, l.FrameCount, l.TotalBytes, l.RenderCount, (int)l.RemoteStatus, l.RemoteSettingsVersion,
l.RemoteFramesVersion, l.RemoteUpdatedAt, l.BaseLocalSettings, l.BaseRemoteSettings, l.DiffLocalFrames, l.DiffRemoteFrames,
- l.MissingRemote, l.MissingLocal, l.ConflictLocalSettings, l.ConflictRemoteSettings, l.ConflictSettingsJson, l.ProxiesCached,
- l.ProxiesFramesVersion, l.SyncedAt,
+ l.DiffLocalRenders, l.DiffRemoteRenders, l.MissingRemote, l.MissingLocal, l.ExportsToGet, l.ExportsToGetBytes, l.ExportsToSend,
+ l.ExportsToSendBytes, l.ConflictLocalSettings, l.ConflictRemoteSettings,
+ l.ConflictRemote is { } remote ? JsonSerializer.Serialize(remote, SyncPayloadJson.Default.SyncPayload) : null,
+ l.ProxiesCached, l.ProxiesFramesVersion, l.Error, l.SyncedAt,
], cancellationToken);
public Task DeleteAsync(string server, Guid remoteId, CancellationToken cancellationToken) =>
@@ -48,9 +51,9 @@ internal sealed class SyncLinkStore(TitanoDatabase db)
_db.ExecuteAsync("UPDATE sequences SET origin = $2 WHERE id = $1", [localId, (int)origin], cancellationToken);
/// Upload and download jobs that are waiting, running or paused, by target id (local id for uploads, server id for downloads).
- public async Task> ActiveTransfersAsync(CancellationToken cancellationToken)
+ public async Task>> ActiveTransfersAsync(CancellationToken cancellationToken)
{
- var transfers = new Dictionary();
+ var jobs = new List();
foreach (var kind in new[] { JobKind.UploadCommit, JobKind.Download })
{
foreach (var state in Active)
@@ -59,16 +62,13 @@ internal sealed class SyncLinkStore(TitanoDatabase db)
do
{
var page = await db.Jobs.ListAsync(state, kind, 500, cursor, cancellationToken).ConfigureAwait(false);
- foreach (var job in page.Items)
- {
- if (job.TargetIds.Count > 0) transfers.TryAdd(job.TargetIds[0], job);
- }
+ jobs.AddRange(page.Items.Where(j => j.TargetIds.Count > 0));
cursor = page.NextCursor;
}
while (cursor is not null);
}
}
- return transfers;
+ return jobs.GroupBy(j => j.TargetIds[0]).ToDictionary(g => g.Key, g => (IReadOnlyList)[.. g]);
}
private static SyncLink Map(DbRow r) => new()
@@ -88,13 +88,20 @@ internal sealed class SyncLinkStore(TitanoDatabase db)
BaseRemoteSettings = r.GetInt32OrNull(12),
DiffLocalFrames = r.GetInt32OrNull(13),
DiffRemoteFrames = r.GetInt32OrNull(14),
- MissingRemote = r.GetInt32(15),
- MissingLocal = r.GetInt32(16),
- ConflictLocalSettings = r.GetInt32OrNull(17),
- ConflictRemoteSettings = r.GetInt32OrNull(18),
- ConflictSettingsJson = r.GetStringOrNull(19),
- ProxiesCached = r.GetInt32(20),
- ProxiesFramesVersion = r.GetInt32OrNull(21),
- SyncedAt = r.GetDateTimeOffsetOrNull(22),
+ DiffLocalRenders = r.GetInt32OrNull(15),
+ DiffRemoteRenders = r.GetInt32OrNull(16),
+ MissingRemote = r.GetInt32(17),
+ MissingLocal = r.GetInt32(18),
+ ExportsToGet = r.GetInt32(19),
+ ExportsToGetBytes = r.GetInt64(20),
+ ExportsToSend = r.GetInt32(21),
+ ExportsToSendBytes = r.GetInt64(22),
+ ConflictLocalSettings = r.GetInt32OrNull(23),
+ ConflictRemoteSettings = r.GetInt32OrNull(24),
+ ConflictRemote = r.GetStringOrNull(25) is { } json ? JsonSerializer.Deserialize(json, SyncPayloadJson.Default.SyncPayload) : null,
+ ProxiesCached = r.GetInt32(26),
+ ProxiesFramesVersion = r.GetInt32OrNull(27),
+ Error = r.GetStringOrNull(28),
+ SyncedAt = r.GetDateTimeOffsetOrNull(29),
};
}
diff --git a/src/Titano.Data/Sync/SyncModel.cs b/src/Titano.Data/Sync/SyncModel.cs
index 7d2e730f..ca465d6c 100644
--- a/src/Titano.Data/Sync/SyncModel.cs
+++ b/src/Titano.Data/Sync/SyncModel.cs
@@ -20,20 +20,20 @@ public enum SyncStatus
/// The server holds originals or settings this PC lacks.
ServerAhead,
- /// Both sides changed the settings since they last agreed: the user picks.
+ /// Both sides changed the settings, title, tags, rating or notes since they last agreed: the user picks.
Conflict,
/// An upload or download job is queued, running or paused.
Transferring,
}
-/// How the user settles a settings conflict (BRIEF §12.7).
+/// How the user settles a conflict (BRIEF §12.7).
public enum SyncResolution
{
KeepMine,
TakeServer,
- /// The server's settings win; this PC's are kept next to the project as a dated copy.
+ /// The server's version wins; this PC's settings are kept next to the project as a dated copy.
SaveBoth,
}
@@ -45,12 +45,25 @@ public enum SyncDownloadMode
/// Proxies (thumbnails where the server has none) into the proxy cache.
Proxies,
+
+ /// The server's renders the local copy lacks, into its exports folder.
+ Exports,
+}
+
+/// What an upload job sends.
+public enum SyncUploadMode
+{
+ /// Originals and settings (creating the server sequence when needed).
+ Originals,
+
+ /// This PC's renders the server lacks (BRIEF §14.5: renders are uploaded on request).
+ Exports,
}
///
/// One server sequence as this client last saw it, linked to its local copy when there is one. The base versions are
-/// the settings counters both sides had when they last agreed; the diff versions are the frame counters the missing
-/// counts were computed at, so originals are compared again only when either side's frames change.
+/// the settings counters both sides had when they last agreed; the diff versions are the frame counters and render
+/// counts the missing counts were computed at, so originals and renders are compared again only when either side's change.
///
public sealed record SyncLink
{
@@ -75,6 +88,8 @@ public sealed record SyncLink
public int? BaseRemoteSettings { get; init; }
public int? DiffLocalFrames { get; init; }
public int? DiffRemoteFrames { get; init; }
+ public int? DiffLocalRenders { get; init; }
+ public int? DiffRemoteRenders { get; init; }
/// Local originals the server does not have.
public int MissingRemote { get; init; }
@@ -82,13 +97,23 @@ public sealed record SyncLink
/// Server originals this PC does not have.
public int MissingLocal { get; init; }
+ /// Server renders the local copy does not have, and their size.
+ public int ExportsToGet { get; init; }
+
+ public long ExportsToGetBytes { get; init; }
+
+ /// Renders of the local copy the server does not have (whose file is still on this PC), and their size.
+ public int ExportsToSend { get; init; }
+
+ public long ExportsToSendBytes { get; init; }
+
/// Settings versions at which the content was found to conflict; null when it does not.
public int? ConflictLocalSettings { get; init; }
public int? ConflictRemoteSettings { get; init; }
- /// The server's settings when the conflict was found, so it can be shown without asking the server again.
- public string? ConflictSettingsJson { get; init; }
+ /// The server's side when the conflict was found: what the user is shown and what "Take server" applies.
+ public SyncPayload? ConflictRemote { get; init; }
/// Frames whose proxy (or thumbnail) is in this PC's proxy cache.
public int ProxiesCached { get; init; }
@@ -96,6 +121,9 @@ public sealed record SyncLink
/// Server frames version the proxy cache was filled at.
public int? ProxiesFramesVersion { get; init; }
+ /// Why the last comparison of this sequence failed (it is compared again next time); null when it worked.
+ public string? Error { get; init; }
+
public DateTimeOffset? SyncedAt { get; init; }
public bool ServerHasProxies => (RemoteStatus & SequenceStatus.ProxiesBuilt) != 0;
@@ -108,7 +136,7 @@ public sealed record SyncLink
}
/// One row of the sync table: the local sequence, the server sequence, or both, and where they stand.
-/// The upload or download job working on it, if any.
+/// The upload or download job working on it, if any (the user's before background ones).
public sealed record SyncEntry(Sequence? Local, SyncLink? Link, SyncStatus Status, Job? Transfer = null)
{
public string Title => Local?.DisplayTitle ?? Link?.Title ?? string.Empty;
@@ -131,5 +159,12 @@ public sealed record SyncSnapshot(string Server, IReadOnlyList Entrie
/// Options a refresh applies.
/// Send and take settings changed on one side only (Server.html "Sync projects automatically").
-/// Keep the proxies of every server sequence without local originals in the cache.
-public sealed record SyncPolicy(bool AutoSyncSettings = true, bool KeepProxiesOffline = false);
+/// Keep the proxy caches of server sequences this PC opened with proxies up to date.
+/// The account may change server sequences; without it this PC's edits are never sent.
+public sealed record SyncPolicy(bool AutoSyncSettings = true, bool KeepProxiesOffline = false, bool CanEdit = true);
+
+///
+/// A conflict could not be settled as the user chose because one side changed again after it was shown; the
+/// sequence was compared again, so the user can choose on the fresh content.
+///
+public sealed class SyncConflictChangedException(string message) : InvalidOperationException(message);
diff --git a/src/Titano.Data/Sync/SyncRemote.cs b/src/Titano.Data/Sync/SyncRemote.cs
index 70194202..3cf2dc88 100644
--- a/src/Titano.Data/Sync/SyncRemote.cs
+++ b/src/Titano.Data/Sync/SyncRemote.cs
@@ -38,6 +38,15 @@ public interface ISyncRemote : IDisposable
/// Streams a frame's original, proxy or thumb, resuming with Range from the destination's length.
Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken);
+ /// Streams a render's file, resuming with Range from the destination's length; returns the file name the server gives it.
+ Task DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken);
+
+ ///
+ /// Chunked, resumable upload of a render of made on this PC; the server verifies the
+ /// SHA-256 and keeps the render's id. Null when the server already has it. See .
+ ///
+ Task UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken);
+
/// Server events until cancelled or the socket closes.
IAsyncEnumerable EventsAsync(CancellationToken cancellationToken);
}
@@ -99,6 +108,12 @@ public sealed class TitanoSyncRemote(TitanoClient client, bool ownsClient) : ISy
public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
client.DownloadFrameAsync(frameId, kind, destination, null, cancellationToken);
+ public Task DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
+ client.DownloadRenderAsync(renderId, destination, cancellationToken);
+
+ public Task UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
+ client.UploadRenderAsync(sequenceId, path, request, null, cancellationToken);
+
public IAsyncEnumerable EventsAsync(CancellationToken cancellationToken) => client.EventsAsync(cancellationToken);
public void Dispose()
diff --git a/src/Titano.Data/Sync/SyncRules.cs b/src/Titano.Data/Sync/SyncRules.cs
index c9fe5ebe..a8aa0e12 100644
--- a/src/Titano.Data/Sync/SyncRules.cs
+++ b/src/Titano.Data/Sync/SyncRules.cs
@@ -1,5 +1,7 @@
+using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
+using System.Text.Json.Serialization;
using Titano.Core.Model;
using Titano.Core.Project;
using Titano.Protocol;
@@ -15,8 +17,8 @@ public sealed record SyncPayload(string? Title, IReadOnlyList Tags, int
/// Title, tags, rating and notes match (blank equals absent, tags ignore case and order).
public bool SameMetadata(SyncPayload other) =>
- Blank(Title) == Blank(other.Title)
- && Blank(Notes) == Blank(other.Notes)
+ SyncRules.Blank(Title) == SyncRules.Blank(other.Title)
+ && SyncRules.Blank(Notes) == SyncRules.Blank(other.Notes)
&& Rating == other.Rating
&& Tags.ToHashSet(StringComparer.OrdinalIgnoreCase).SetEquals(other.Tags);
@@ -25,20 +27,27 @@ public sealed record SyncPayload(string? Title, IReadOnlyList Tags, int
/// The PATCH that makes the server's copy equal to this one; settings are sent in their normalised form.
public UpdateSequenceRequest ToRequest(int? expectedSettingsVersion) =>
new(Title ?? string.Empty, Tags, Rating, Notes ?? string.Empty, SyncRules.NormalizeSettings(SettingsJson), expectedSettingsVersion);
-
- private static string Blank(string? text) => text?.Trim() ?? string.Empty;
}
-/// One setting that differs: its dotted path ("exposure.deflicker.window") and the JSON value on each side (null when absent).
+[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
+[JsonSerializable(typeof(SyncPayload))]
+internal sealed partial class SyncPayloadJson : JsonSerializerContext;
+
+///
+/// One field that differs: its dotted path ("exposure.deflickerWindow.manual", or "sequence.tags" for the metadata)
+/// and the value on each side (JSON for settings, plain text for metadata; null when absent).
+///
public sealed record SettingsDifference(string Path, string? Mine, string? Theirs);
/// Decisions of the sync engine that need no I/O: the status of a row, settings equality and differences, merges.
public static class SyncRules
{
+ /// Path prefix of the title, tags, rating and notes in .
+ public const string MetadataPath = "sequence.";
+
///
/// Where a linked pair stands. Settings changed on both sides since they last agreed are a conflict (the engine
- /// settles equal content and metadata-only differences before a row is shown); missing originals count before
- /// settings changed on one side only.
+ /// settles equal content before a row is shown); missing originals count before settings changed on one side only.
///
public static SyncStatus Status(Sequence? local, SyncLink? link)
{
@@ -91,13 +100,20 @@ public static class SyncRules
}
///
- /// Metadata-only divergence (settings equal): tags are united, title, rating and notes come from the side edited last.
+ /// Up to differences between two payloads: title, tags, rating and notes first (paths
+ /// sequence.title, sequence.tags, …, tags as one comma-separated value), then the settings.
///
- public static SyncPayload Merge(SyncPayload local, SyncPayload remote, bool localIsNewer)
+ public static IReadOnlyList Differences(SyncPayload mine, SyncPayload theirs, int max = 3)
{
- var newer = localIsNewer ? local : remote;
- var tags = local.Tags.Concat(remote.Tags).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
- return newer with { Tags = tags, SettingsJson = local.SettingsJson };
+ var differences = new List();
+ if (Blank(mine.Title) != Blank(theirs.Title)) differences.Add(new(MetadataPath + "title", Blank(mine.Title), Blank(theirs.Title)));
+ if (!mine.Tags.ToHashSet(StringComparer.OrdinalIgnoreCase).SetEquals(theirs.Tags))
+ differences.Add(new(MetadataPath + "tags", string.Join(", ", mine.Tags), string.Join(", ", theirs.Tags)));
+ if (mine.Rating != theirs.Rating)
+ differences.Add(new(MetadataPath + "rating", mine.Rating.ToString(CultureInfo.InvariantCulture), theirs.Rating.ToString(CultureInfo.InvariantCulture)));
+ if (Blank(mine.Notes) != Blank(theirs.Notes)) differences.Add(new(MetadataPath + "notes", Blank(mine.Notes), Blank(theirs.Notes)));
+ if (differences.Count < max) differences.AddRange(Differences(mine.SettingsJson, theirs.SettingsJson, max - differences.Count));
+ return differences.Count > max ? differences.GetRange(0, max) : differences;
}
///
@@ -114,6 +130,8 @@ public static class SyncRules
return (hashed.Count - matched + Math.Max(0, unhashed - unmatchedRemote), Math.Max(0, unmatchedRemote - unhashed));
}
+ internal static string Blank(string? text) => text?.Trim() ?? string.Empty;
+
private static JsonNode? Parse(string json)
{
try
diff --git a/src/Titano.Data/Sync/SyncService.cs b/src/Titano.Data/Sync/SyncService.cs
index dce35e76..4b46f848 100644
--- a/src/Titano.Data/Sync/SyncService.cs
+++ b/src/Titano.Data/Sync/SyncService.cs
@@ -1,3 +1,4 @@
+using System.Collections.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Net;
@@ -5,6 +6,7 @@ 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.Data.Archiving;
using Titano.Data.Jobs;
@@ -35,24 +37,33 @@ public sealed record SyncOptions
///
/// 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 moved, the manifest: originals are matched by SHA-256 and never conflict; 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 settings is a conflict the user settles
-/// with . 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 . Proxies and
-/// thumbnails never travel upward. The state of each local sequence is written to its sync_state for the Library.
+/// 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. 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. Proxies and thumbnails never travel
+/// upward. 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 ProjectStore _projects;
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;
@@ -79,6 +90,9 @@ public sealed class SyncService
/// "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");
@@ -86,30 +100,34 @@ public sealed class SyncService
///
/// Lists the server, compares every sequence with its local copy, sends or takes settings changed on one side when
- /// is on, forgets sequences deleted on the server, and stores the states.
+ /// 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);
- var locals = await LocalSequencesAsync(cancellationToken).ConfigureAwait(false);
+ locals = await LocalSequencesAsync(cancellationToken).ConfigureAwait(false);
foreach (var dto in listing)
{
links.Remove(dto.Id, out var link);
- var local = link?.LocalId is { } id ? locals.GetValueOrDefault(id) : null;
- await ReconcileSafelyAsync(remote, Observe(link, remote.Server, dto), dto, local, policy, cancellationToken).ConfigureAwait(false);
+ var local = await LocalOfAsync(link, locals, cancellationToken).ConfigureAwait(false);
+ await ReconcileSafelyAsync(remote, Observe(link, remote.Server, dto), dto, local, locals, policy, cancellationToken).ConfigureAwait(false);
}
- foreach (var gone in links.Values) await ForgetAsync(gone, 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, cancellationToken).ConfigureAwait(false);
+ var entries = await StoreStatesAsync(remote.Server, locals, cancellationToken).ConfigureAwait(false);
Changed?.Invoke(remote.Server);
return new SyncSnapshot(remote.Server, entries, DateTimeOffset.UtcNow, clock.Elapsed);
}
@@ -123,49 +141,22 @@ public sealed class SyncService
foreach (var id in remoteIds)
{
var link = await _links.GetAsync(remote.Server, id, cancellationToken).ConfigureAwait(false);
- SequenceDto dto;
- try
- {
- dto = await remote.GetSequenceAsync(id, cancellationToken).ConfigureAwait(false);
- }
- catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.NotFound)
- {
- if (link is not null) await ForgetAsync(link, cancellationToken).ConfigureAwait(false);
- continue;
- }
- var local = link?.LocalId is { } localId ? await _db.Sequences.GetAsync(localId, cancellationToken).ConfigureAwait(false) : null;
- await ReconcileSafelyAsync(remote, Observe(link, remote.Server, dto), dto, local, policy, cancellationToken).ConfigureAwait(false);
+ await CompareOneAsync(remote, id, link, null, policy, cancellationToken).ConfigureAwait(false);
}
}
finally
{
_gate.Release();
}
- await StoreStatesAsync(remote.Server, cancellationToken).ConfigureAwait(false);
+ 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.
- public async Task> ListAsync(string server, CancellationToken cancellationToken)
- {
- var links = await _links.ListAsync(server, cancellationToken).ConfigureAwait(false);
- var locals = await LocalSequencesAsync(cancellationToken).ConfigureAwait(false);
- var transfers = await _links.ActiveTransfersAsync(cancellationToken).ConfigureAwait(false);
- var entries = new List(links.Count + locals.Count);
- foreach (var link in links)
- {
- var local = link.LocalId is { } id && locals.Remove(id, out var found) ? found : null;
- var transfer = (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 = transfers.GetValueOrDefault(local.Id);
- entries.Add(new SyncEntry(local, null, transfer is null ? SyncStatus.LocalOnly : SyncStatus.Transferring, transfer));
- }
- entries.Sort((a, b) => string.Compare(a.Title, b.Title, StringComparison.CurrentCultureIgnoreCase));
- return entries;
- }
+ ///
+ /// 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
@@ -180,41 +171,73 @@ public sealed class SyncService
// ---------------- Settings ----------------
- /// Settles a settings conflict as the user chose (BRIEF §12.7); a 409 means the server changed again meanwhile.
- public async Task ResolveAsync(ISyncRemote remote, Guid remoteId, SyncResolution resolution, CancellationToken cancellationToken)
+ ///
+ /// 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);
- var dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false);
- link = Observe(link, remote.Server, dto);
- if (resolution == SyncResolution.KeepMine)
+ if (link is not { ConflictRemote: { } theirs, ConflictLocalSettings: { } seenLocal, ConflictRemoteSettings: { } seenRemote }
+ || local.SettingsVersion != seenLocal)
{
- var updated = await remote.UpdateSequenceAsync(remoteId, SyncPayload.Of(local).ToRequest(dto.SettingsVersion), cancellationToken).ConfigureAwait(false);
- link = Agreed(Observe(link, remote.Server, updated), local.SettingsVersion, updated.SettingsVersion);
+ changed = true;
+ }
+ else if (resolution == SyncResolution.KeepMine)
+ {
+ try
+ {
+ var updated = await remote.UpdateSequenceAsync(remoteId, SyncPayload.Of(local).ToRequest(seenRemote), 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
{
- if (resolution == SyncResolution.SaveBoth) await SaveCopyAsync(local, cancellationToken).ConfigureAwait(false);
- var manifest = await remote.GetManifestAsync(remoteId, cancellationToken).ConfigureAwait(false);
- var applied = await ApplyLocallyAsync(local, SyncPayload.Of(dto, manifest.SettingsJson), cancellationToken).ConfigureAwait(false);
- link = Agreed(link, applied.SettingsVersion, manifest.SettingsVersion);
+ var dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false);
+ if (dto.SettingsVersion != seenRemote)
+ {
+ changed = true;
+ }
+ else
+ {
+ if (resolution == SyncResolution.SaveBoth) saved = await SaveCopyAsync(local, cancellationToken).ConfigureAwait(false);
+ var applied = await ApplyLocallyAsync(local, theirs, cancellationToken).ConfigureAwait(false);
+ link = Agreed(Observe(link, remote.Server, dto), applied.SettingsVersion, seenRemote);
+ }
+ }
+ if (!changed)
+ {
+ await _links.UpsertAsync(link with { SyncedAt = DateTimeOffset.UtcNow }, cancellationToken).ConfigureAwait(false);
+ Log.Info(LogCategory, $"Conflict on {local.DisplayTitle} settled: {resolution}");
}
- await _links.UpsertAsync(link with { SyncedAt = DateTimeOffset.UtcNow }, cancellationToken).ConfigureAwait(false);
- Log.Info(LogCategory, $"Conflict on {local.DisplayTitle} settled: {resolution}");
}
finally
{
_gate.Release();
}
- await StoreStatesAsync(remote.Server, cancellationToken).ConfigureAwait(false);
+ 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, CancellationToken cancellationToken) =>
- RefreshSequencesAsync(remote, [remoteId], new SyncPolicy(AutoSyncSettings: true), cancellationToken);
+ public Task SyncSettingsAsync(ISyncRemote remote, Guid remoteId, SyncPolicy policy, CancellationToken cancellationToken) =>
+ RefreshSequencesAsync(remote, [remoteId], policy with { AutoSyncSettings = true }, cancellationToken);
// ---------------- Transfers ----------------
@@ -222,9 +245,16 @@ public sealed class SyncService
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 one is already waiting or running: originals into
- /// (ignored when the sequence already has a local copy), or proxies into the cache.
+ /// 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) =>
@@ -253,11 +283,34 @@ public sealed class SyncService
/// 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)
{
- var link = await _links.GetAsync(server, remoteId, cancellationToken).ConfigureAwait(false)
- ?? new SyncLink { Server = server, RemoteId = remoteId, Title = local.DisplayTitle, RemoteUpdatedAt = DateTimeOffset.UtcNow };
- link = link with { LocalId = local.Id };
- await _links.UpsertAsync(link, cancellationToken).ConfigureAwait(false);
- return link;
+ 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();
+ }
}
///
@@ -312,6 +365,25 @@ public sealed class SyncService
}
}
+ ///
+ /// 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.
internal async Task SetProxiesAsync(string server, SequenceDto dto, int cached, int framesVersion, CancellationToken cancellationToken)
{
@@ -328,87 +400,151 @@ public sealed class SyncService
Changed?.Invoke(server);
}
- private async Task ReconcileSafelyAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence? local, SyncPolicy policy, CancellationToken cancellationToken)
+ /// 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
{
- link = await ReconcileAsync(remote, link, dto, local, policy, cancellationToken).ConfigureAwait(false);
+ dto = await remote.GetSequenceAsync(remoteId, cancellationToken).ConfigureAwait(false);
}
catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.NotFound)
{
- // Deleted between the listing and the manifest: the next refresh forgets it.
+ 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 (ex is IOException or UnauthorizedAccessException or InvalidDataException or LeaseUnavailableException)
+ catch (Exception ex) when (IsIsolated(ex, cancellationToken))
{
- // The local archive is offline or busy: compare again next time; the other sequences go on.
+ // 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);
}
- /// Compares originals when frame counters moved, then settles the settings; returns the updated link.
- private async Task ReconcileAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence? local, SyncPolicy policy,
+ /// 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);
+ return (link.LocalId is null ? link : Unlinked(link), null);
}
SequenceManifest? manifest = null;
- if (link.DiffLocalFrames != local.FramesVersion || link.DiffRemoteFrames != link.RemoteFramesVersion)
+ if (link.DiffLocalFrames != local.FramesVersion || link.DiffRemoteFrames != link.RemoteFramesVersion
+ || link.DiffLocalRenders != local.RenderedCount || link.DiffRemoteRenders != link.RenderCount)
{
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 (missingRemote, missingLocal) = SyncRules.CompareOriginals(
[.. frames.Where(f => (f.Flags & FrameFlags.Missing) == 0).Select(f => f.Sha256)], [.. manifest.Frames.Select(f => f.Sha256)]);
- link = link with
+ 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,
MissingRemote = missingRemote,
MissingLocal = missingLocal,
};
}
- link = await SettleSettingsAsync(remote, link, dto, local, manifest, policy, cancellationToken).ConfigureAwait(false);
- return link with { SyncedAt = DateTimeOffset.UtcNow };
+ (link, local) = await SettleSettingsAsync(remote, link, dto, local, manifest, policy, cancellationToken).ConfigureAwait(false);
+ return (link with { SyncedAt = DateTimeOffset.UtcNow }, local);
}
- private async Task SettleSettingsAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence local, SequenceManifest? manifest,
- SyncPolicy policy, CancellationToken cancellationToken)
+ /// 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;
+ if (!localChanged && !remoteChanged) return (link, local);
if (localChanged && remoteChanged)
{
- if (link.ConflictLocalSettings == local.SettingsVersion && link.ConflictRemoteSettings == link.RemoteSettingsVersion) return link;
+ 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);
var mine = SyncPayload.Of(local);
var theirs = SyncPayload.Of(dto, manifest.SettingsJson);
- if (mine.SameAs(theirs)) return Agreed(link, local.SettingsVersion, manifest.SettingsVersion);
- if (!SyncRules.SameSettings(mine.SettingsJson, theirs.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
{
- return link with
- {
- ConflictLocalSettings = local.SettingsVersion,
- ConflictRemoteSettings = link.RemoteSettingsVersion,
- ConflictSettingsJson = SyncRules.NormalizeSettings(theirs.SettingsJson),
- };
- }
- // Only title, tags, rating or notes differ: merge them and give both sides the result.
- var merged = SyncRules.Merge(mine, theirs, local.UpdatedAt >= dto.UpdatedAt);
- var applied = merged.SameMetadata(mine) ? local : await ApplyLocallyAsync(local, merged, cancellationToken).ConfigureAwait(false);
- return await PushAsync(remote, link, applied, manifest.SettingsVersion, cancellationToken).ConfigureAwait(false);
+ ConflictLocalSettings = local.SettingsVersion,
+ ConflictRemoteSettings = manifest.SettingsVersion,
+ ConflictRemote = theirs with { SettingsJson = SyncRules.NormalizeSettings(theirs.SettingsJson) },
+ }, local);
}
- if (!policy.AutoSyncSettings) return link;
- if (localChanged) return await PushAsync(remote, link, local, link.BaseRemoteSettings, cancellationToken).ConfigureAwait(false);
+ if (!policy.AutoSyncSettings) return (link, local);
+ if (localChanged)
+ return (policy.CanEdit ? await PushAsync(remote, link, local, link.BaseRemoteSettings, cancellationToken).ConfigureAwait(false) : link, local);
manifest ??= await remote.GetManifestAsync(link.RemoteId, cancellationToken).ConfigureAwait(false);
+ if (manifest.SettingsVersion != dto.SettingsVersion) return (link, local);
var pulled = await ApplyLocallyAsync(local, SyncPayload.Of(dto, manifest.SettingsJson), cancellationToken).ConfigureAwait(false);
- return Agreed(link, pulled.SettingsVersion, manifest.SettingsVersion);
+ return (Agreed(link, pulled.SettingsVersion, manifest.SettingsVersion), pulled);
}
/// Sends this PC's payload unless the server moved past , which shows up as a conflict next.
@@ -425,6 +561,62 @@ public sealed class SyncService
}
}
+ ///
+ /// 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);
+
/// Writes a payload into the local sidecar, project and index (each write bumps the local settings version).
private async Task ApplyLocallyAsync(Sequence local, SyncPayload payload, CancellationToken cancellationToken)
{
@@ -441,18 +633,21 @@ public sealed class SyncService
return await _db.Sequences.GetAsync(local.Id, cancellationToken).ConfigureAwait(false) ?? current;
}
- /// "Save both": this PC's project is kept as .titano/project.local-<UTC time>.json before the server's replaces it.
- private async Task SaveCopyAsync(Sequence local, CancellationToken cancellationToken)
+ /// "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.
+ 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 name = string.Create(CultureInfo.InvariantCulture, $"project.local-{DateTime.UtcNow:yyyyMMdd-HHmmss}.json");
- AtomicFile.WriteAllText(Path.Combine(folder.MetaDirectory, name), SyncRules.NormalizeSettings(local.SettingsJson));
- Log.Info(LogCategory, $"Settings of {local.DisplayTitle} kept as {name}");
+ 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;
}
+ internal static string SavedCopyName(DateTime localTime) => string.Create(CultureInfo.InvariantCulture, $"project.this-pc-{localTime:yyyyMMdd-HHmmss}.json");
+
private async Task ForgetAsync(SyncLink link, CancellationToken cancellationToken)
{
await _links.DeleteAsync(link.Server, link.RemoteId, cancellationToken).ConfigureAwait(false);
@@ -461,9 +656,9 @@ public sealed class SyncService
}
/// Writes each local sequence's state for the Library and returns the table.
- private async Task> StoreStatesAsync(string server, CancellationToken cancellationToken)
+ private async Task> StoreStatesAsync(string server, Dictionary? locals, CancellationToken cancellationToken)
{
- var entries = await ListAsync(server, cancellationToken).ConfigureAwait(false);
+ 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)
@@ -472,6 +667,50 @@ public sealed class SyncService
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);
@@ -498,8 +737,16 @@ public sealed class SyncService
private async Task EnqueueAsync(JobEngine engine, JobKind kind, Guid target, string label, SyncJobParameters parameters, JobPriority priority,
CancellationToken cancellationToken)
{
- var active = await _links.ActiveTransfersAsync(cancellationToken).ConfigureAwait(false);
- if (active.TryGetValue(target, out var running) && running.Kind == kind) return running;
+ 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);
}
@@ -525,7 +772,7 @@ public sealed class SyncService
RemoteSettingsVersion = remoteVersion,
ConflictLocalSettings = null,
ConflictRemoteSettings = null,
- ConflictSettingsJson = null,
+ ConflictRemote = null,
};
private static SyncLink Unlinked(SyncLink link) => link with
@@ -535,10 +782,16 @@ public sealed class SyncService
BaseRemoteSettings = null,
DiffLocalFrames = null,
DiffRemoteFrames = null,
+ DiffLocalRenders = null,
+ DiffRemoteRenders = null,
MissingRemote = 0,
MissingLocal = 0,
+ ExportsToGet = 0,
+ ExportsToGetBytes = 0,
+ ExportsToSend = 0,
+ ExportsToSendBytes = 0,
ConflictLocalSettings = null,
ConflictRemoteSettings = null,
- ConflictSettingsJson = null,
+ ConflictRemote = null,
};
}
diff --git a/src/Titano.Data/Sync/SyncTransfers.cs b/src/Titano.Data/Sync/SyncTransfers.cs
index 9f993d92..3fb1fa6b 100644
--- a/src/Titano.Data/Sync/SyncTransfers.cs
+++ b/src/Titano.Data/Sync/SyncTransfers.cs
@@ -19,15 +19,18 @@ namespace Titano.Data.Sync;
/// creating the server sequence first and sending its settings when the upload made it. Downloads fetch originals the
/// local copy lacks into the archive's staging folder (resuming with Range), require the manifest's SHA-256 and commit
/// them through the archive ingest, which names them and writes the sidecar; the first original creates the local
-/// sequence, which then takes the server's settings. Proxy downloads fill the proxy cache. A resumed job skips what
-/// is already on the other side, so pausing or a crash loses at most the files in flight.
+/// sequence, which then takes the server's settings. Frames of the local copy not hashed yet are hashed first (so the
+/// server's copies of them are recognised), and originals flagged missing here are restored to their recorded place.
+/// Proxy downloads fill the proxy cache. Renders travel on request: this PC's to the server (hash-verified chunked
+/// upload) and the server's into the local copy's exports. A resumed job skips what is already on the other side, so
+/// pausing or a crash loses at most the files in flight.
///
internal sealed class SyncTransfers(SyncService sync)
{
private const long SpaceReserve = 512L << 20;
- /// Recomputes a sequence's state after a transfer without sending or taking settings on the user's behalf.
- private static readonly SyncPolicy CompareOnly = new(AutoSyncSettings: false);
+ /// Space a cached proxy is assumed to take before it is downloaded (1920 px JPEG, BRIEF §8.5).
+ private const long ProxyEstimate = 3L << 19;
public async Task UploadAsync(JobContext context, ISyncRemote remote, Guid localId)
{
@@ -72,7 +75,7 @@ internal sealed class SyncTransfers(SyncService sync)
}).ConfigureAwait(false);
if (created) await sync.PushAllAsync(remote, target, ct).ConfigureAwait(false);
- await sync.RefreshSequencesAsync(remote, [target], CompareOnly, ct).ConfigureAwait(false);
+ await sync.RecompareAsync(remote, target, ct).ConfigureAwait(false);
if (skipped > 0) throw new InvalidDataException(Loc.Plural("Errors.Sync.Unreadable", skipped));
}
@@ -89,10 +92,8 @@ internal sealed class SyncTransfers(SyncService sync)
var manifest = await remote.GetManifestAsync(remoteId, ct).ConfigureAwait(false);
var captured = (await remote.ListFramesAsync(remoteId, ct).ConfigureAwait(false)).ToDictionary(f => f.Id, f => f.CapturedAt);
- IReadOnlySet have = local is null
- ? new HashSet()
- : (await SequenceFrames.ListAsync(db, local.Id, ct).ConfigureAwait(false)).Select(f => f.Sha256).OfType().ToHashSet(StringComparer.OrdinalIgnoreCase);
- var wanted = manifest.Frames.Where(f => !have.Contains(f.Sha256)).DistinctBy(f => f.Sha256.ToLowerInvariant()).ToList();
+ var (have, restore) = local is null ? ([], []) : await LocalContentAsync(archive, local, ct).ConfigureAwait(false);
+ var wanted = manifest.Frames.Where(f => !have.Contains(f.Sha256.ToLowerInvariant())).DistinctBy(f => f.Sha256.ToLowerInvariant()).ToList();
EnsureSpace(paths.Root, wanted.Sum(f => f.Size));
var staging = Path.Combine(paths.StagingDirectory, "sync-" + remoteId.ToString("N"));
@@ -114,16 +115,27 @@ internal sealed class SyncTransfers(SyncService sync)
await progress.StepAsync().ConfigureAwait(false);
}
var sequenceId = local?.Id;
+ var restored = 0;
await Parallel.ForEachAsync(rest, Workers(sync.Options.Downloads, ct), async (frame, token) =>
{
- await CommitAsync(remote, remoteId, archive.Id, sequenceId, frame, staging, captured, token).ConfigureAwait(false);
+ if (restore.TryGetValue(frame.Sha256.ToLowerInvariant(), out var place))
+ {
+ await RestoreAsync(remote, frame, staging, place, token).ConfigureAwait(false);
+ Interlocked.Increment(ref restored);
+ }
+ else
+ {
+ await CommitAsync(remote, remoteId, archive.Id, sequenceId, frame, staging, captured, token).ConfigureAwait(false);
+ }
await progress.StepAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
TryDeleteDirectory(staging);
+ // Restored originals are checked against their records, which clears their missing flag.
+ if (restored > 0) await VerifyAsync(archive, local!.Id, ct).ConfigureAwait(false);
if (created) await sync.TakeAllAsync(remote, remoteId, ct).ConfigureAwait(false);
- await sync.RefreshSequencesAsync(remote, [remoteId], CompareOnly, ct).ConfigureAwait(false);
- if (sequenceId is { } downloaded && wanted.Count > 0)
+ await sync.RecompareAsync(remote, remoteId, ct).ConfigureAwait(false);
+ if (sequenceId is { } downloaded && wanted.Count > restored)
{
var label = local!.DisplayTitle;
await EnqueueFollowUpAsync(ArchiveJobs.Thumbnails([downloaded], label), ct).ConfigureAwait(false);
@@ -131,6 +143,161 @@ internal sealed class SyncTransfers(SyncService sync)
}
}
+ ///
+ /// The originals a local copy holds (lower-case SHA-256) and where each original flagged missing belongs. Frames
+ /// not hashed yet (an adopted folder never verified) are hashed first through the archive's verify, so the
+ /// server's copies of them are recognised instead of committed again as duplicates.
+ ///
+ private async Task<(HashSet Have, Dictionary Restore)> LocalContentAsync(Archive archive, Sequence local, CancellationToken ct)
+ {
+ var frames = await SequenceFrames.ListAsync(sync.Database, local.Id, ct).ConfigureAwait(false);
+ if (frames.Any(f => f.Sha256 is null && (f.Flags & FrameFlags.Missing) == 0))
+ {
+ await VerifyAsync(archive, local.Id, ct).ConfigureAwait(false);
+ frames = await SequenceFrames.ListAsync(sync.Database, local.Id, ct).ConfigureAwait(false);
+ }
+ var have = frames.Where(f => (f.Flags & FrameFlags.Missing) == 0 && f.Sha256 is not null).Select(f => f.Sha256!.ToLowerInvariant())
+ .ToHashSet(StringComparer.Ordinal);
+ var paths = new ArchivePaths(archive.RootPath);
+ var restore = new Dictionary(StringComparer.Ordinal);
+ foreach (var frame in frames.Where(f => (f.Flags & FrameFlags.Missing) != 0 && f.Sha256 is not null))
+ {
+ var sha = frame.Sha256!.ToLowerInvariant();
+ if (!have.Contains(sha)) restore.TryAdd(sha, paths.FromRelative(frame.RelativePath));
+ }
+ return (have, restore);
+ }
+
+ private Task VerifyAsync(Archive archive, Guid sequenceId, CancellationToken ct) =>
+ sync.Archives.Verifier.VerifyAsync(archive, [sequenceId], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: ct);
+
+ /// Puts a verified copy of an original flagged missing back where its record says; a file that reappeared meanwhile is kept.
+ private static async Task RestoreAsync(ISyncRemote remote, ManifestFrame frame, string staging, string place, CancellationToken cancellationToken)
+ {
+ var staged = await FetchOriginalAsync(remote, frame, staging, cancellationToken).ConfigureAwait(false);
+ if (File.Exists(place))
+ {
+ File.Delete(staged);
+ return;
+ }
+ Directory.CreateDirectory(Path.GetDirectoryName(place)!);
+ File.Move(staged, place, overwrite: false);
+ }
+
+ public async Task SendExportsAsync(JobContext context, ISyncRemote remote, Guid localId)
+ {
+ var ct = context.Cancellation;
+ var db = sync.Database;
+ var local = await db.Sequences.GetAsync(localId, ct).ConfigureAwait(false) ?? throw new InvalidOperationException(Loc.L("Errors.Sync.LocalMissing"));
+ var archive = await db.Archives.GetAsync(local.ArchiveId, ct).ConfigureAwait(false) ?? throw new InvalidOperationException(Loc.L("Errors.Sync.NoArchive"));
+ var link = await sync.Links.FindByLocalAsync(remote.Server, local.Id, ct).ConfigureAwait(false)
+ ?? throw new InvalidOperationException(Loc.L("Errors.Sync.NotLinked"));
+ var there = (await remote.GetManifestAsync(link.RemoteId, ct).ConfigureAwait(false)).Renders.Select(r => r.Id).ToHashSet();
+ var renders = (await db.Renders.ListBySequenceAsync(local.Id, ct).ConfigureAwait(false)).Where(r => !there.Contains(r.Id)).ToList();
+ var progress = new TransferProgress(context, renders.Count, 0);
+ await progress.ReportAsync().ConfigureAwait(false);
+ var gone = 0;
+ foreach (var render in renders)
+ {
+ var file = SyncService.RenderFile(archive, render);
+ if (File.Exists(file))
+ {
+ var sha = await TitanoClient.HashFileAsync(file, ct).ConfigureAwait(false);
+ await remote.UploadRenderAsync(link.RemoteId, file, new RenderUploadRequest(render.Id, Path.GetFileName(file), new FileInfo(file).Length, sha,
+ render.PresetName, render.Codec, render.Width, render.Height, render.Fps, render.DurationSeconds, render.ProducedBy), ct).ConfigureAwait(false);
+ }
+ else
+ {
+ gone++;
+ }
+ await progress.StepAsync().ConfigureAwait(false);
+ }
+ await sync.RecompareAsync(remote, link.RemoteId, ct).ConfigureAwait(false);
+ if (gone > 0) throw new FileNotFoundException(Loc.Plural("Errors.Sync.ExportsGone", gone));
+ }
+
+ public async Task GetExportsAsync(JobContext context, ISyncRemote remote, Guid remoteId)
+ {
+ var ct = context.Cancellation;
+ var db = sync.Database;
+ var link = await sync.Links.GetAsync(remote.Server, remoteId, ct).ConfigureAwait(false);
+ var local = (link?.LocalId is { } id ? await db.Sequences.GetAsync(id, ct).ConfigureAwait(false) : null)
+ ?? throw new InvalidOperationException(Loc.L("Errors.Sync.LocalMissing"));
+ var archive = await db.Archives.GetAsync(local.ArchiveId, ct).ConfigureAwait(false) ?? throw new InvalidOperationException(Loc.L("Errors.Sync.NoArchive"));
+ var paths = new ArchivePaths(archive.RootPath);
+ ArchiveVolume.EnsureWritable(paths);
+ var here = (await db.Renders.ListBySequenceAsync(local.Id, ct).ConfigureAwait(false)).Select(r => r.Id).ToHashSet();
+ var wanted = (await remote.GetManifestAsync(remoteId, ct).ConfigureAwait(false)).Renders.Where(r => !here.Contains(r.Id)).ToList();
+ EnsureSpace(paths.Root, wanted.Sum(r => r.Bytes));
+
+ var staging = Path.Combine(paths.StagingDirectory, "exports-" + remoteId.ToString("N"));
+ var exports = paths.Sequence(local.Folder).Exports;
+ Directory.CreateDirectory(staging);
+ Directory.CreateDirectory(exports);
+ var progress = new TransferProgress(context, wanted.Count, 0);
+ await progress.ReportAsync().ConfigureAwait(false);
+ foreach (var render in wanted)
+ {
+ var (part, name) = await FetchRenderAsync(remote, render, staging, ct).ConfigureAwait(false);
+ var target = Unique(exports, name);
+ File.Move(part, target);
+ await db.Renders.InsertAsync(new Render
+ {
+ Id = render.Id,
+ SequenceId = local.Id,
+ PresetName = render.PresetName,
+ OutputPath = paths.ToRelative(target),
+ Codec = render.Codec,
+ Width = render.Width,
+ Height = render.Height,
+ Fps = render.Fps,
+ DurationSeconds = render.DurationSeconds,
+ Bytes = new FileInfo(target).Length,
+ Created = render.Created,
+ ProducedBy = render.ProducedBy,
+ }, ct).ConfigureAwait(false);
+ await progress.StepAsync().ConfigureAwait(false);
+ }
+ TryDeleteDirectory(staging);
+ await sync.RecompareAsync(remote, remoteId, ct).ConfigureAwait(false);
+ }
+
+ /// A render's file in staging (resumed with Range; started over once if the partial copy no longer fits) and the name the server gives it.
+ private static async Task<(string Part, string Name)> FetchRenderAsync(ISyncRemote remote, RenderDto render, string staging, CancellationToken cancellationToken)
+ {
+ var part = Path.Combine(staging, render.Id.ToString("N") + ".part");
+ for (var attempt = 1; ; attempt++)
+ {
+ try
+ {
+ string? name;
+ await using (var file = new FileStream(part, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
+ name = await remote.DownloadRenderAsync(render.Id, file, cancellationToken).ConfigureAwait(false);
+ return (part, SafeFileName(name) ?? render.Id.ToString("N"));
+ }
+ catch (TitanoApiException ex) when (ex.Status == HttpStatusCode.RequestedRangeNotSatisfiable && attempt == 1)
+ {
+ File.Delete(part);
+ }
+ }
+ }
+
+ private static string? SafeFileName(string? name)
+ {
+ var file = Path.GetFileName(name?.Trim());
+ return string.IsNullOrEmpty(file) || file.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || file is "." or ".." ? null : file;
+ }
+
+ /// "name.mp4", else "name (2).mp4", … — an existing export is never replaced.
+ private static string Unique(string folder, string fileName)
+ {
+ var candidate = Path.Combine(folder, fileName);
+ var stem = Path.GetFileNameWithoutExtension(fileName);
+ var extension = Path.GetExtension(fileName);
+ for (var n = 2; File.Exists(candidate); n++) candidate = Path.Combine(folder, string.Create(CultureInfo.InvariantCulture, $"{stem} ({n}){extension}"));
+ return candidate;
+ }
+
public async Task DownloadProxiesAsync(JobContext context, ISyncRemote remote, Guid remoteId)
{
var ct = context.Cancellation;
@@ -141,6 +308,7 @@ internal sealed class SyncTransfers(SyncService sync)
var folder = sync.Proxies.Folder(remote.Server, remoteId);
Directory.CreateDirectory(folder);
ProxyCache.Prune(folder, frames.Select(f => ProxyCache.FileName(f.Id)).ToHashSet(StringComparer.Ordinal));
+ EnsureSpace(folder, frames.Count(f => !File.Exists(Path.Combine(folder, ProxyCache.FileName(f.Id)))) * ProxyEstimate);
var files = new string?[frames.Count];
var progress = new TransferProgress(context, frames.Count, 0);
@@ -176,7 +344,7 @@ internal sealed class SyncTransfers(SyncService sync)
return (id, id == started);
}
// Deleted on the server meanwhile: the originals go into a new sequence.
- if (id == link?.RemoteId) await sync.Links.DeleteAsync(remote.Server, id, ct).ConfigureAwait(false);
+ if (id == link?.RemoteId) await sync.ForgetGoneAsync(remote.Server, id, ct).ConfigureAwait(false);
}
return (null, true);
}
diff --git a/src/Titano.Protocol/Client/TitanoClient.cs b/src/Titano.Protocol/Client/TitanoClient.cs
index d843ca82..96fe88a8 100644
--- a/src/Titano.Protocol/Client/TitanoClient.cs
+++ b/src/Titano.Protocol/Client/TitanoClient.cs
@@ -175,6 +175,46 @@ public sealed class TitanoClient : IDisposable
.ConfigureAwait(false);
}
+ ///
+ /// Sends a render of made on this PC in resumable chunks; the server verifies the
+ /// SHA-256 in and files it under the sequence's exports with the same id. Null when the
+ /// server already has that render.
+ ///
+ public async Task UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, IProgress? progress = null,
+ CancellationToken ct = default)
+ {
+ var length = new FileInfo(path).Length;
+ if (length != request.Size) throw new ArgumentException("The render size does not match the file.", nameof(request));
+ var start = await RetryAsync(() => SendAsync(HttpMethod.Post, $"api/v1/sequences/{sequenceId}/renders", request,
+ ProtocolJson.Default.RenderUploadRequest, ProtocolJson.Default.StartUploadResponse, ct), ct).ConfigureAwait(false);
+ if (start.AlreadyExists) return null;
+ var uploadId = start.UploadId ?? throw new InvalidOperationException("Server returned no upload id.");
+ await SendChunksAsync(path, length, uploadId, start.ChunkSize, start.Received, progress, ct).ConfigureAwait(false);
+ return await RetryAsync(() => SendAsync(HttpMethod.Post, $"api/v1/sequences/{sequenceId}/renders/{uploadId}/complete",
+ ProtocolJson.Default.RenderDto, ct), ct).ConfigureAwait(false);
+ }
+
+ ///
+ /// Streams a render's file to , resuming with Range from its current length; returns
+ /// the file name the server gives it.
+ ///
+ public async Task DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken ct = default)
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, $"api/v1/renders/{renderId}/file");
+ if (destination.CanSeek && destination.Length > 0)
+ {
+ destination.Seek(0, SeekOrigin.End);
+ request.Headers.Range = new RangeHeaderValue(destination.Length, null);
+ }
+ using var response = await _http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
+ await EnsureSuccessAsync(response, ct).ConfigureAwait(false);
+ if (response.StatusCode == HttpStatusCode.OK && destination.CanSeek) destination.SetLength(0);
+ await using var body = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false);
+ await body.CopyToAsync(destination, ct).ConfigureAwait(false);
+ var disposition = response.Content.Headers.ContentDisposition;
+ return (disposition?.FileNameStar ?? disposition?.FileName)?.Trim('"');
+ }
+
/// Sends every chunk the server does not have yet; each PUT is idempotent and retried on transient errors.
private async Task SendChunksAsync(string path, long length, Guid uploadId, int chunkSize, IReadOnlyList alreadyReceived,
IProgress? progress, CancellationToken ct)
diff --git a/src/Titano.Protocol/ProtocolJson.cs b/src/Titano.Protocol/ProtocolJson.cs
index 2db29cac..7af9a1b2 100644
--- a/src/Titano.Protocol/ProtocolJson.cs
+++ b/src/Titano.Protocol/ProtocolJson.cs
@@ -56,6 +56,7 @@ namespace Titano.Protocol;
[JsonSerializable(typeof(JobPriorityRequest))]
[JsonSerializable(typeof(RenderJobParameters))]
[JsonSerializable(typeof(RenderResultRequest))]
+[JsonSerializable(typeof(RenderUploadRequest))]
[JsonSerializable(typeof(NodeJobFailure))]
[JsonSerializable(typeof(ServerSettingsDto))]
[JsonSerializable(typeof(LogFileDto))]
diff --git a/src/Titano.Protocol/Uploads.cs b/src/Titano.Protocol/Uploads.cs
index b2ff863c..3be2f2e7 100644
--- a/src/Titano.Protocol/Uploads.cs
+++ b/src/Titano.Protocol/Uploads.cs
@@ -16,3 +16,23 @@ public sealed record CompleteUploadResponse(Guid FrameId, Guid SequenceId, int I
/// GET /api/v1/uploads/{id}: resume state.
public sealed record UploadState(Guid UploadId, string FileName, long Size, string Sha256, int ChunkSize, IReadOnlyList Received, string State);
+
+///
+/// POST /api/v1/sequences/{id}/renders: a client offers one of its renders of the sequence (BRIEF §14.5,
+/// renders are uploaded on request). The render keeps its id on the server, so both sides recognise it; when the
+/// server already has that id it answers 200 with . Chunks go to
+/// /uploads/{uploadId}/chunks/{n}; POST /sequences/{id}/renders/{uploadId}/complete verifies the SHA-256
+/// and files the render under the sequence's exports.
+///
+public sealed record RenderUploadRequest(
+ Guid RenderId,
+ string FileName,
+ long Size,
+ string Sha256,
+ string PresetName,
+ string? Codec,
+ int Width,
+ int Height,
+ double Fps,
+ double DurationSeconds,
+ string? ProducedBy);
diff --git a/src/Titano.Server/Api/SequenceEndpoints.cs b/src/Titano.Server/Api/SequenceEndpoints.cs
index dae6adf2..af64e3cc 100644
--- a/src/Titano.Server/Api/SequenceEndpoints.cs
+++ b/src/Titano.Server/Api/SequenceEndpoints.cs
@@ -37,6 +37,15 @@ public static class SequenceEndpoints
sequences.MapGet("/{id:guid}/analysis", AnalysisAsync).Requires(Permissions.Read);
sequences.MapGet("/{id:guid}/stats", StatsAsync).Requires(Permissions.Read);
sequences.MapGet("/{id:guid}/renders", RendersAsync).Requires(Permissions.Read);
+ // Renders made on a client, sent on request (BRIEF §14.5); chunks go through /uploads.
+ sequences.MapPost("/{id:guid}/renders", async (HttpContext c, Caller caller, Guid id, RenderUploadRequest request, RenderUploads uploads) =>
+ (await uploads.StartAsync(caller, id, request, c.RequestAborted)).ToResult(c, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse,
+ statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created))).Requires(Permissions.Upload | Permissions.Edit);
+ sequences.MapPost("/{id:guid}/renders/{uploadId:guid}/complete", async (HttpContext c, Caller caller, Guid id, Guid uploadId, RenderUploads uploads,
+ IHostApplicationLifetime lifetime) =>
+ // The commit runs to the end even if the client disconnects meanwhile.
+ (await uploads.CompleteAsync(caller, id, uploadId, lifetime.ApplicationStopping)).ToResult(c, r => Results.Json(r, ProtocolJson.Default.RenderDto)))
+ .Requires(Permissions.Upload | Permissions.Edit);
}
private static async Task ListAsync(HttpContext context, TitanoDatabase db, ServerArchive archive, int? year, string? camera, string? status,
diff --git a/src/Titano.Server/Hosting/ServerHost.cs b/src/Titano.Server/Hosting/ServerHost.cs
index a06cf32f..7ae2b5b8 100644
--- a/src/Titano.Server/Hosting/ServerHost.cs
+++ b/src/Titano.Server/Hosting/ServerHost.cs
@@ -121,6 +121,7 @@ public static class ServerHost
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
+ services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
diff --git a/src/Titano.Server/Storage/RenderStore.cs b/src/Titano.Server/Storage/RenderStore.cs
index 841e3f95..c23784d8 100644
--- a/src/Titano.Server/Storage/RenderStore.cs
+++ b/src/Titano.Server/Storage/RenderStore.cs
@@ -12,7 +12,12 @@ public sealed class RenderStore(TitanoDatabase db, ServerArchive archive, TimePr
/// Moves (on the archive volume) to <sequence folder>/exports/, never
/// replacing an existing export, and inserts the render row. Null when the sequence has no folder in the archive yet.
///
- public async Task CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, CancellationToken cancellationToken)
+ public Task CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, CancellationToken cancellationToken) =>
+ CommitAsync(sequence, stagedFile, output, producedBy, Guid.CreateVersion7(), cancellationToken);
+
+ /// As above, keeping : a render a client sends keeps the id it has on that client.
+ public async Task CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, Guid id,
+ CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(sequence.Folder) || ArchivePathGuard.Resolve(archive.Root, sequence.Folder.TrimEnd('/') + "/exports") is not { } exports)
return null;
@@ -21,7 +26,7 @@ public sealed class RenderStore(TitanoDatabase db, ServerArchive archive, TimePr
File.Move(stagedFile, target);
var render = new Render
{
- Id = Guid.CreateVersion7(),
+ Id = id,
SequenceId = sequence.Id,
PresetName = output.PresetName,
OutputPath = LongPath.ToPortableRelative(archive.Root, target),
diff --git a/src/Titano.Server/Storage/RenderUploads.cs b/src/Titano.Server/Storage/RenderUploads.cs
new file mode 100644
index 00000000..f0345a41
--- /dev/null
+++ b/src/Titano.Server/Storage/RenderUploads.cs
@@ -0,0 +1,118 @@
+using System.Globalization;
+using System.Text.Json;
+using Titano.Data;
+using Titano.Data.Repositories;
+using Titano.Protocol;
+using Titano.Server.Api;
+using Titano.Server.Configuration;
+using Titano.Server.Events;
+using Titano.Server.Security;
+using UploadState = Titano.Data.Repositories.UploadState;
+
+namespace Titano.Server.Storage;
+
+///
+/// Renders a client sends on request (BRIEF §14.5): chunked like frame uploads (same store, chunk endpoint and
+/// retention), verified against their SHA-256 at completion and filed by under the
+/// sequence's exports with the client's render id, so both sides recognise the same render.
+///
+public sealed class RenderUploads(
+ TitanoDatabase db,
+ ServerOptions options,
+ UploadService uploads,
+ UploadStore store,
+ RenderStore renders,
+ EventHub events,
+ TimeProvider time)
+{
+ public async Task> StartAsync(Caller caller, Guid sequenceId, RenderUploadRequest request, CancellationToken cancellationToken)
+ {
+ if (await db.Sequences.GetAsync(sequenceId, cancellationToken) is null) return Outcome.Fail(404, "not_found", "sequence");
+ if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Outcome.Fail(400, "invalid_file_name");
+ if (!UploadRules.IsSha256(request.Sha256)) return Outcome.Fail(400, "invalid_hash");
+ if (request.Size <= 0) return Outcome.Fail(400, "bad_request", "size");
+ if (request.Size > options.MaxUploadBytes)
+ return Outcome.Fail(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture));
+ if (string.IsNullOrWhiteSpace(request.PresetName) || request.PresetName.Length > 200) return Outcome.Fail(400, "bad_request", "presetName");
+ if (request.RenderId == Guid.Empty) return Outcome.Fail(400, "bad_request", "renderId");
+
+ if (await db.Renders.GetAsync(request.RenderId, cancellationToken) is { } known)
+ {
+ return known.SequenceId == sequenceId
+ ? new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, null)
+ : Outcome.Fail(409, "render_exists");
+ }
+ var sha = request.Sha256.ToLowerInvariant();
+ if (await db.Uploads.FindReceivingAsync(caller.Id, sha, cancellationToken) is { } open && open.SequenceId == sequenceId && open.Size == request.Size
+ && Read(open)?.RenderId == request.RenderId && store.Exists(open.Id))
+ return Started(open);
+ if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size) return Outcome.Fail(507, "insufficient_storage");
+
+ var now = time.GetUtcNow();
+ var upload = await db.Uploads.CreateAsync(new Upload
+ {
+ Id = Guid.CreateVersion7(),
+ UserId = caller.Id,
+ SequenceId = sequenceId,
+ FileName = fileName,
+ Size = request.Size,
+ Sha256 = sha,
+ ChunkSize = options.ChunkSizeBytes,
+ State = UploadState.Receiving,
+ Created = now,
+ UpdatedAt = now,
+ RenderJson = JsonSerializer.Serialize(request with { FileName = fileName, Sha256 = sha }, ProtocolJson.Default.RenderUploadRequest),
+ }, cancellationToken);
+ store.Create(upload.Id, upload.Size);
+ return Started(upload);
+ }
+
+ /// Verifies a fully received render and files it; completing a committed one answers it again (a lost response).
+ public async Task> CompleteAsync(Caller caller, Guid sequenceId, Guid uploadId, CancellationToken cancellationToken)
+ {
+ var upload = await db.Uploads.GetAsync(uploadId, cancellationToken);
+ if (upload is null || upload.UserId != caller.Id || upload.SequenceId != sequenceId || upload.JobId is not null || Read(upload) is not { } output)
+ return Outcome.Fail(404, "not_found", "upload");
+ if (upload.State == UploadState.Committed && await db.Renders.GetAsync(output.RenderId, cancellationToken) is { } done) return Dto.Render(done);
+ if (upload.State != UploadState.Receiving) return Outcome.Fail(409, "upload_state");
+ if (!upload.IsComplete)
+ return Outcome.Fail(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture));
+ if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Committing, cancellationToken: cancellationToken))
+ return Outcome.Fail(409, "upload_state");
+
+ if (await uploads.StageVerifiedAsync(upload, cancellationToken) is not { } staged) return Outcome.Fail(422, "hash_mismatch");
+ var sequence = await db.Sequences.GetAsync(sequenceId, cancellationToken);
+ var result = new RenderResultRequest(output.FileName, output.Size, output.Sha256, output.PresetName, output.Codec, output.Width, output.Height, output.Fps,
+ output.DurationSeconds);
+ var render = sequence is null ? null : await renders.CommitAsync(sequence, staged, result, output.ProducedBy ?? caller.ClientName ?? caller.User.Username,
+ output.RenderId, cancellationToken);
+ if (render is null)
+ {
+ UploadStore.TryDelete(staged);
+ await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Failed, cancellationToken: CancellationToken.None);
+ store.Delete(upload.Id);
+ return Outcome.Fail(409, "upload_state", "sequence");
+ }
+ await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Committed, cancellationToken: CancellationToken.None);
+ store.Delete(upload.Id);
+ if (await db.Sequences.GetAsync(sequenceId, CancellationToken.None) is { } updated)
+ events.Publish(new SequenceChangedEvent(updated.Id, "renders", updated.FramesVersion, updated.SettingsVersion));
+ return Dto.Render(render);
+ }
+
+ private static RenderUploadRequest? Read(Upload upload)
+ {
+ if (upload.RenderJson is not { } json) return null;
+ try
+ {
+ return JsonSerializer.Deserialize(json, ProtocolJson.Default.RenderUploadRequest) is { RenderId: var id } request && id != Guid.Empty ? request : null;
+ }
+ catch (JsonException)
+ {
+ return null;
+ }
+ }
+
+ private static StartUploadResponse Started(Upload upload) =>
+ new(upload.Id, upload.ChunkSize, UploadRules.ReceivedChunks(upload), AlreadyExists: false, FrameId: null);
+}
diff --git a/src/Titano.Server/Storage/UploadService.cs b/src/Titano.Server/Storage/UploadService.cs
index cef9f2ff..327bd02d 100644
--- a/src/Titano.Server/Storage/UploadService.cs
+++ b/src/Titano.Server/Storage/UploadService.cs
@@ -144,7 +144,7 @@ public sealed class UploadService(
var stopping = lifetime.ApplicationStopping;
var upload = await db.Uploads.GetAsync(id, stopping);
if (upload is null || upload.UserId != caller.Id) return Fail(404, "not_found", "upload");
- if (upload.JobId is not null) return Fail(409, "wrong_upload_kind");
+ if (upload.JobId is not null || upload.RenderJson is not null) return Fail(409, "wrong_upload_kind");
if (upload is { State: UploadState.Committed, FrameId: { } committed } && await db.Frames.GetAsync(committed, stopping) is { } done)
return new CompleteUploadResponse(done.Id, done.SequenceId, done.Index);
if (upload.State != UploadState.Receiving) return Fail(409, "upload_state");
diff --git a/src/Titano.Ui/Themes/Controls.xaml b/src/Titano.Ui/Themes/Controls.xaml
index 2a689464..a22debfb 100644
--- a/src/Titano.Ui/Themes/Controls.xaml
+++ b/src/Titano.Ui/Themes/Controls.xaml
@@ -1248,10 +1248,11 @@
-
+
+
-
-
+
+
diff --git a/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs b/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs
index 5b8b3f97..8c1ee512 100644
--- a/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs
+++ b/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs
@@ -1,7 +1,11 @@
+using System.Windows.Controls;
+using System.Windows.Input;
+using Titano.App.Infrastructure;
using Titano.App.Sections;
using Titano.App.Server;
using Titano.App.Shell;
using Titano.App.Tests.Windows;
+using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Sync;
using Titano.Testing;
@@ -9,7 +13,7 @@ using Titano.Ui.Controls;
namespace Titano.App.Tests.Server;
-/// The Server screen's sync table on the capture sample data: comparison, transfer, conflict and the Library's glyphs.
+/// The Server screen's sync table on the capture sample data: comparison, transfers, conflict and the Library's glyphs.
[WindowsOnly]
[Serial]
[Timeout(300_000)]
@@ -24,45 +28,77 @@ public sealed class ServerScreenSmokeTests
await shell.Library.RefreshAsync();
await shell.Services.Server.RestoreAsync();
Assert.NotNull(await shell.Services.Sync.RefreshAsync());
+ await app.Sample.CacheProxiesAsync();
var table = shell.Server.Sync;
await table.ReloadAsync();
- // 36 sequences here (4 also on the server) and 2 only on the server.
+ // 36 sequences here (6 also on the server) and 2 only on the server; the conflict is listed first.
Assert.True(table.IsActive);
Assert.Equal(38, table.Rows.Count);
+ Assert.Equal("Holy grail · Monviso", table.Rows[0].Title);
+ Assert.True(table.Rows[0].IsConflict);
Assert.Equal(1, Chip(table, SyncFilter.Conflicts).Count);
Assert.Equal(2, Chip(table, SyncFilter.Remote).Count);
- Assert.Equal(32, Chip(table, SyncFilter.LocalOnly).Count);
+ Assert.Equal(30, Chip(table, SyncFilter.LocalOnly).Count);
Assert.Equal(SyncStatus.InSync, Row(table, "Harbour sunset").Entry.Status);
Assert.Equal(SyncStatus.ServerAhead, Row(table, "Construction cam · week 38").Entry.Status);
- Assert.SequenceEqual([SyncAction.DownloadOriginals], Row(table, "Construction cam · week 38").Actions.Select(a => a.Action));
- Assert.SequenceEqual([SyncAction.OpenWithProxies, SyncAction.DownloadOriginals], Row(table, "Sunrise over Superga").Actions.Select(a => a.Action));
- Assert.SequenceEqual([SyncAction.Upload], Row(table, "Clouds over the Langhe").Actions.Select(a => a.Action));
+ Assert.SequenceEqual([SyncAction.DownloadOriginals], Actions(Row(table, "Construction cam · week 38")));
+ Assert.SequenceEqual([SyncAction.Upload], Actions(Row(table, "Milan skyline dusk")));
+ Assert.SequenceEqual([SyncAction.GetExports], Actions(Row(table, "Star trails, Colle del Nivolet")));
+ var sunrise = Row(table, "Sunrise over Superga");
+ Assert.Equal(SyncStatus.ProxiesOnly, sunrise.Entry.Status);
+ Assert.Equal(Loc.L("Server.Sync.Proxies"), sunrise.LocalText);
+ Assert.SequenceEqual([SyncAction.OpenWithProxies, SyncAction.DownloadOriginals], Actions(sunrise));
+ Assert.SequenceEqual([SyncAction.Upload], Actions(Row(table, "Clouds over the Langhe")));
var conflict = Row(table, "Holy grail · Monviso");
Assert.Equal(StatusKind.Warning, conflict.StateKind);
- Assert.SequenceEqual([SyncAction.KeepMine, SyncAction.TakeServer, SyncAction.SaveBoth], conflict.Actions.Select(a => a.Action));
- Assert.Contains("41", conflict.StateToolTip);
+ Assert.SequenceEqual([SyncAction.KeepMine, SyncAction.TakeServer, SyncAction.SaveBoth], Actions(conflict));
+ Assert.Equal(Loc.L("Server.Conflict.Difference", Loc.L("Server.Setting.DeflickerWindow"), "41", "33"), conflict.StateToolTip);
Assert.NotNull(table.LastSyncText);
Assert.NotNull(table.ArchiveText);
+ Assert.True(table.UploadAllCommand.CanExecute(null), "the table filled after the view bound");
- // The Library shows the real sync state of each local copy.
+ // Resolving shows the plan first: what differs and which side is overwritten.
+ var keepMine = SyncTableViewModel.ConflictRequest(conflict.Entry, SyncAction.KeepMine);
+ Assert.True(keepMine.IsDestructive);
+ Assert.Contains(Loc.L("Server.Setting.DeflickerWindow"), keepMine.Detail);
+ Assert.False(SyncTableViewModel.ConflictRequest(conflict.Entry, SyncAction.SaveBoth).IsDestructive);
+
+ // The Library shows the real sync state of each local copy, in one word.
var holyGrail = shell.Library.Items.Single(i => i.Title == "Holy grail · Monviso");
Assert.Equal(SyncState.Conflict, holyGrail.Sequence.SyncState);
- Assert.Contains(holyGrail.Statuses, s => s.Kind == StatusKind.Warning);
+ Assert.Equal(StatusKind.Warning, holyGrail.Statuses[^1].Kind);
Assert.Contains(shell.Library.Items.Single(i => i.Title == "Harbour sunset").Statuses, s => s.Kind == StatusKind.Synced);
+ var milan = shell.Library.Items.Single(i => i.Title == "Milan skyline dusk").Statuses[^1];
+ Assert.Equal((StatusKind.Synced, Loc.L("Library.Status.Synced")), (milan.Kind, milan.Text));
shell.Navigate(SectionKey.Server);
await app.ExpectAsync("server");
- // An upload in progress: the row shows what the server received and offers Pause.
+ // Tab walks the row buttons of every row: the rows take no focus, the list passes Tab on to their content.
+ var list = app.FindAll().Single(l => l.Name == "SyncList");
+ Assert.Equal(KeyboardNavigationMode.Continue, KeyboardNavigation.GetTabNavigation(list));
+ var rowButtons = app.FindAll