fix(sync): resolve conflicts against the versions shown, isolate failures, renders on request

- Conflicts are settled against the versions the user was shown; a later change on
  either side refuses the choice and compares again. Title, tags, rating and notes
  changed differently on both sides are a conflict too (no silent tag union).
- Links are written under the engine's gate and forgotten only after a 404; linked
  local copies are read by id; unlinked copies with the same originals are linked
  by content.
- A failure on one sequence is recorded on its link; the others are still compared.
- Downloads hash unverified local copies first and restore originals missing here.
- Background proxy downloads give way to the user's requests; "Keep proxies offline"
  refreshes the caches of sequences opened with proxies, checks free space and backs
  off after a failure.
- Renders travel on request: Send exports (chunked, hash-verified upload keeping the
  render id; new POST /sequences/{id}/renders endpoints) and Get exports.
- Server screen: stable row buttons, mockup column grid, Tab through rows, conflict
  rows first on the card colour, localised conflict detail, Save both confirmation,
  Pause metered uploads, culture switch, Library one-word sync glyphs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-03 10:26:56 +00:00
1 parent 3601e0a47f
commit 2d40a613b9
48 files changed
+2498 -465

No files matched your search

+112 -45
View File
@@ -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;
}
/// <summary>An upload in progress for the sync table (Comet over the ridge, 46 %).</summary>
/// <summary>
/// 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).
/// </summary>
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<Sequence> 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));
}
/// <summary>
/// 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.
/// </summary>
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);
}
/// <summary>
/// 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.
/// </summary>
private async Task FillServerAsync(TitanoDatabase db, IReadOnlyList<Sequence> 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
/// <summary>
/// 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.
/// </summary>
private sealed class SampleSyncRemote : ISyncRemote
{
private readonly Dictionary<Guid, (SequenceDto Dto, string? Settings, int Frames)> _sequences = [];
private readonly Dictionary<Guid, (SequenceDto Dto, string? Settings, Guid[] Frames)> _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<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken) =>
Task.FromResult<IReadOnlyList<SequenceDto>>([.. _sequences.Values.Select(s => s.Dto)]);
public Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken)
{
lock (_sequences) return Task.FromResult<IReadOnlyList<SequenceDto>>([.. _sequences.Values.Select(s => s.Dto)]);
}
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken) => Task.FromResult(_sequences[id].Dto);
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken)
{
lock (_sequences) return Task.FromResult(_sequences[id].Dto);
}
public Task<SequenceManifest> 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<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => Task.FromResult<IReadOnlyList<FrameDto>>([]);
public Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
{
lock (_sequences)
{
return Task.FromResult<IReadOnlyList<FrameDto>>([.. _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<SequenceDto> 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<CompleteUploadResponse?> 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.");
/// <summary>Proxies and thumbnails: a dawn gradient whose sun rises with the frame.</summary>
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<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
throw new NotSupportedException("The sample server serves no renders.");
public Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
throw new NotSupportedException("The sample server takes no renders.");
public async IAsyncEnumerable<ServerEvent> EventsAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
{
+4 -1
View File
@@ -111,12 +111,15 @@ public sealed class AppSettings
/// <summary>Let the server send render jobs to this PC (BRIEF §14.7).</summary>
public bool RenderNodeOptIn { get; set; }
/// <summary>Keep the proxies of every server sequence without local originals in the cache (BRIEF §12.7).</summary>
/// <summary>Keep the proxy caches of the server sequences this PC opened with proxies up to date (BRIEF §12.7).</summary>
public bool KeepProxiesOffline { get; set; }
/// <summary>Send and take project settings changed on one side without asking (Server.html).</summary>
public bool SyncSettingsAutomatically { get; set; } = true;
/// <summary>Pause upload jobs while Windows reports the connection as metered (Server.html).</summary>
public bool PauseUploadsOnMetered { get; set; }
// ---- Jobs ----
/// <summary>"Clear done": finished jobs older than this are hidden from the Jobs list (they stay in the history).</summary>
public DateTimeOffset? JobsClearedAt { get; set; }
+8 -5
View File
@@ -147,17 +147,20 @@ public sealed class SequenceItem : ObservableObject
OnPropertyChanged(nameof(PrimaryStatus));
}
/// <summary>Where a local copy stands against the server (the sync engine writes the state); null while it is only here.</summary>
/// <summary>
/// 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.
/// </summary>
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,
};
/// <summary>Card status row: analysed, proxies, renders, sync, warnings (Main.html).</summary>
/// <summary>Card status row: analysed, proxies, renders, errors or warnings, sync (Main.html); the sync glyph is last so a narrow card cuts it first.</summary>
public static IReadOnlyList<CardStatus> BuildStatuses(Sequence s, bool analysing)
{
var list = new List<CardStatus>(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;
}
}
+4 -1
View File
@@ -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)
{
+70 -42
View File
@@ -117,6 +117,7 @@
<CheckBox Content="{l:Loc Server.RenderNode}" IsChecked="{Binding RenderNodeOptIn}" ToolTip="{l:Loc Tooltips.Server.RenderNode}" Margin="0,0,22,0" />
<CheckBox Content="{l:Loc Server.KeepProxies}" IsChecked="{Binding KeepProxiesOffline}" ToolTip="{l:Loc Tooltips.Server.KeepProxies}" Margin="0,0,22,0" />
<CheckBox Content="{l:Loc Server.AutoSync}" IsChecked="{Binding SyncSettingsAutomatically}" ToolTip="{l:Loc Tooltips.Server.AutoSync}" Margin="0,0,22,0" />
<CheckBox Content="{l:Loc Server.PauseMetered}" IsChecked="{Binding PauseUploadsOnMetered}" ToolTip="{l:Loc Tooltips.Server.PauseMetered}" Margin="0,0,22,0" />
</WrapPanel>
</Border>
</Grid>
@@ -132,37 +133,51 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DockPanel Margin="14,10" LastChildFill="False">
<!-- Toolbar in visual (and Tab) order: title, chips, search, Upload all local. -->
<Grid Margin="14,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{l:Loc Server.Sync.Title}" FontWeight="SemiBold" VerticalAlignment="Center" Margin="0,0,10,0" />
<Button DockPanel.Dock="Right" Content="{l:Loc Server.Sync.UploadAll}" Command="{Binding UploadAllCommand}" ToolTip="{Binding UploadAllToolTip}"
Height="30" Margin="10,0,0,0" />
<TextBox DockPanel.Dock="Right" Width="200" Height="30" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" ui:Field.Glyph="{StaticResource Icon.Search}"
ui:Field.Placeholder="{l:Loc Server.Sync.Search}" ToolTip="{l:Loc Tooltips.Server.Sync.Search}" AutomationProperties.Name="{l:Loc Server.Sync.Search}" />
<ItemsControl ItemsSource="{Binding Chips}" ItemsPanel="{StaticResource Panel.Row}" Focusable="False" VerticalAlignment="Center">
<ItemsControl Grid.Column="1" ItemsSource="{Binding Chips}" ItemsPanel="{StaticResource Panel.Row}" Focusable="False" VerticalAlignment="Center">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type srv:SyncFilterChip}">
<Button x:Name="Chip" Style="{StaticResource Button.Chip}" Content="{Binding Text}" Tag="{Binding IsSelected}" Command="{Binding SelectCommand}"
ToolTip="{Binding ToolTip}" Height="28" FontSize="12" Margin="0,0,10,0" Typography.NumeralAlignment="Tabular" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding NeedsAttention}" Value="True">
<!-- Conflicts waiting are drawn in warning colours, unless the chip is the chosen one (then Accent, DESIGN §4). -->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding NeedsAttention}" Value="True" />
<Condition Binding="{Binding IsSelected}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter TargetName="Chip" Property="BorderBrush" Value="{DynamicResource Brush.Warning}" />
<Setter TargetName="Chip" Property="Foreground" Value="{DynamicResource Brush.Warning}" />
</DataTrigger>
</MultiDataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
<TextBox Grid.Column="3" Width="200" Height="30" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" ui:Field.Glyph="{StaticResource Icon.Search}"
ui:Field.Placeholder="{l:Loc Server.Sync.Search}" ToolTip="{l:Loc Tooltips.Server.Sync.Search}" AutomationProperties.Name="{l:Loc Server.Sync.Search}" />
<Button Grid.Column="4" Content="{l:Loc Server.Sync.UploadAll}" Command="{Binding UploadAllCommand}" ToolTip="{Binding UploadAllToolTip}"
ToolTipService.ShowOnDisabled="True" Height="30" Margin="10,0,0,0" />
</Grid>
<!-- Header; the rows repeat its columns (Server.html: 1.6fr 150px 150px 150px 1.4fr, here stars with minimums). -->
<Border Grid.Row="1" BorderBrush="{DynamicResource Brush.Stroke}" BorderThickness="0,1,0,1" Padding="14,8">
<!-- Header; the rows repeat its columns (Server.html: 1.6fr 150px 150px 150px 1.4fr with 10 px gaps). The right padding
keeps the space of the rows' scroll bar, so the columns line up. -->
<Border Grid.Row="1" BorderBrush="{DynamicResource Brush.Stroke}" BorderThickness="0,1,0,1" Padding="14,8,24,8">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.4*" MinWidth="140" />
<ColumnDefinition Width="*" MinWidth="110" />
<ColumnDefinition Width="*" MinWidth="110" />
<ColumnDefinition Width="*" MinWidth="120" />
<ColumnDefinition Width="1.6*" MinWidth="330" />
<ColumnDefinition Width="1.6*" MinWidth="140" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="1.4*" MinWidth="280" />
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource Text.Header}" Text="{l:Loc Server.Sync.Column.Sequence}" />
<TextBlock Grid.Column="1" Style="{StaticResource Text.Header}" Text="{l:Loc Server.Sync.Column.Local}" Margin="10,0,0,0" />
@@ -171,35 +186,48 @@
</Grid>
</Border>
<!-- Rows (virtualised): this PC, the server, the state and the actions that apply. -->
<ListBox Grid.Row="2" ItemsSource="{Binding Rows}" ItemContainerStyle="{StaticResource ListBoxItem.PlainRow}"
<!-- Rows (virtualised): this PC, the server, the state and the actions that apply; conflicts on the card colour.
Tab walks every row button (the rows themselves take no focus). Actions wrap when longer labels need it. -->
<ListBox x:Name="SyncList" Grid.Row="2" ItemsSource="{Binding Rows}" ItemContainerStyle="{StaticResource ListBoxItem.PlainRow}"
VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.ScrollUnit="Pixel"
ScrollViewer.HorizontalScrollBarVisibility="Disabled" AutomationProperties.Name="{l:Loc Server.Sync.Title}"
Visibility="{Binding HasRows, Converter={StaticResource BoolToVisibility}}">
ScrollViewer.HorizontalScrollBarVisibility="Disabled" KeyboardNavigation.TabNavigation="Continue"
AutomationProperties.Name="{l:Loc Server.Sync.Title}" Visibility="{Binding HasRows, Converter={StaticResource BoolToVisibility}}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type srv:SyncRow}">
<Grid Height="40" Background="Transparent" Margin="14,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.4*" MinWidth="140" />
<ColumnDefinition Width="*" MinWidth="110" />
<ColumnDefinition Width="*" MinWidth="110" />
<ColumnDefinition Width="*" MinWidth="120" />
<ColumnDefinition Width="1.6*" MinWidth="330" />
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource Text.Cell}" Text="{Binding Title}" ToolTip="{Binding Title}" />
<TextBlock Grid.Column="1" Style="{StaticResource Text.Cell}" Text="{Binding LocalText}" Margin="10,0,0,0" />
<TextBlock Grid.Column="2" Style="{StaticResource Text.Cell}" Text="{Binding ServerText}" Margin="10,0,0,0" />
<ui:StatusGlyph Grid.Column="3" Kind="{Binding StateKind}" Text="{Binding StateText}" ToolTip="{Binding StateToolTip}"
FontSize="13" VerticalAlignment="Center" Margin="10,0,0,0" />
<ItemsControl Grid.Column="4" ItemsSource="{Binding Actions}" ItemsPanel="{StaticResource Panel.Row}" Focusable="False"
VerticalAlignment="Center" Margin="10,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type srv:SyncRowAction}">
<Button Style="{StaticResource Button.Row}" Content="{Binding Text}" Command="{Binding Command}" ToolTip="{Binding ToolTip}" FontSize="12" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
<Border x:Name="RowBorder" Background="Transparent" Padding="14,0">
<Grid MinHeight="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1.6*" MinWidth="140" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="160" />
<ColumnDefinition Width="1.4*" MinWidth="280" />
</Grid.ColumnDefinitions>
<TextBlock Style="{StaticResource Text.Cell}" Text="{Binding Title}" ToolTip="{Binding Title}" />
<TextBlock Grid.Column="1" Style="{StaticResource Text.Cell}" Text="{Binding LocalText}" ToolTip="{Binding LocalText}" Margin="10,0,0,0" />
<TextBlock Grid.Column="2" Style="{StaticResource Text.Cell}" Text="{Binding ServerText}" ToolTip="{Binding ServerText}" Margin="10,0,0,0" />
<ui:StatusGlyph Grid.Column="3" Kind="{Binding StateKind}" Text="{Binding StateText}" ToolTip="{Binding StateToolTip}"
FontSize="13" VerticalAlignment="Center" Margin="10,0,0,0" />
<ItemsControl Grid.Column="4" ItemsSource="{Binding Actions}" Focusable="False" VerticalAlignment="Center" Margin="10,4,0,4">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type srv:SyncRowAction}">
<Button Style="{StaticResource Button.Row}" Content="{Binding Text}" Command="{Binding Command}" ToolTip="{Binding ToolTip}"
FontSize="12" Margin="0,2,6,2" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsConflict}" Value="True">
<Setter TargetName="RowBorder" Property="Background" Value="{DynamicResource Brush.Card}" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
+16 -2
View File
@@ -12,7 +12,7 @@ namespace Titano.App.Sections;
/// <summary>
/// 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 (<see cref="Sync"/>).
/// proxies kept offline, automatic project sync, uploads paused on metered networks) and the sync table (<see cref="Sync"/>).
/// </summary>
public sealed class ServerViewModel : SectionViewModel
{
@@ -138,7 +138,7 @@ public sealed class ServerViewModel : SectionViewModel
}
}
/// <summary>Keep the proxies of every server sequence without local originals on this PC.</summary>
/// <summary>Keep the proxy caches of server sequences opened with proxies up to date on this PC.</summary>
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);
}
/// <summary>Pause uploads while the connection is metered.</summary>
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();
}
}
/// <summary>Re-reads values another screen changed (Settings › Server).</summary>
public void Refresh() => OnPropertyChanged(nameof(RenderNodeOptIn));
+12 -4
View File
@@ -13,9 +13,10 @@ public delegate ISyncRemote SyncRemoteFactory(Uri address, string token, IReadOn
/// <summary>
/// This PC's sync with the signed-in server (BRIEF §4.4, §14.5): the <see cref="SyncService"/> of the index, the
/// connection of upload and download jobs (<see cref="ISyncConnector"/>) and a <see cref="SyncAgent"/> that keeps the
/// table current while a session is open, started and stopped in order as the session changes. Options come from
/// <see cref="AppSettings.SyncSettingsAutomatically"/> and <see cref="AppSettings.KeepProxiesOffline"/>.
/// connection of upload and download jobs (<see cref="ISyncConnector"/>), a <see cref="SyncAgent"/> that keeps the
/// table current while a session is open, started and stopped in order as the session changes, and the
/// <see cref="UploadHold"/> for metered connections. Options come from <see cref="AppSettings.SyncSettingsAutomatically"/>,
/// <see cref="AppSettings.KeepProxiesOffline"/> and <see cref="AppSettings.PauseUploadsOnMetered"/>.
/// </summary>
public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDisposable
{
@@ -42,7 +43,12 @@ public sealed class SyncHost(AppServices services) : ISyncConnector, IAsyncDispo
/// <summary>Key of the signed-in server (<see cref="SyncService.ServerKey"/>); null while signed out.</summary>
public string? Server => Volatile.Read(ref _credentials) is { } c ? SyncService.ServerKey(c.Address) : null;
public SyncPolicy Policy => new(services.Settings.SyncSettingsAutomatically, services.Settings.KeepProxiesOffline);
/// <summary>The options, and whether the account may change server sequences (without it this PC's edits are not sent).</summary>
public SyncPolicy Policy => new(services.Settings.SyncSettingsAutomatically, services.Settings.KeepProxiesOffline,
services.Server.User is { } user && (user.Permissions & (long)Permissions.Edit) != 0);
/// <summary>Pauses uploads while the connection is metered and the option is on.</summary>
public UploadHold Uploads { get; } = new(services);
/// <summary>The agent started, stopped or compared, or the table of the signed-in server changed (any thread).</summary>
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);
}
/// <summary>Compares now, once the session switch in progress settled; null while signed out.</summary>
@@ -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();
}
+72 -21
View File
@@ -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;
/// <summary>A row button: "Upload", "Keep mine", …</summary>
public sealed record SyncRowAction(SyncAction Action, string Text, string ToolTip, ICommand Command);
/// <summary>A row button: "Upload", "Keep mine", …; kept while the row offers it, so focus, tooltips and clicks survive updates.</summary>
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);
}
/// <summary>The label in the current language.</summary>
public void Relabel() => OnPropertyChanged(nameof(Text));
}
/// <summary>One row of the sync table (Server.html): sequence, this PC, server, state and the actions that apply.</summary>
public sealed class SyncRow : ObservableObject
{
private readonly Func<SyncRow, SyncAction, Task> _act;
private SyncEntry _entry;
private SyncRights _rights;
private IReadOnlyList<SyncRowAction> _actions = [];
public SyncRow(SyncEntry entry, SyncRights rights, Func<SyncRow, SyncAction, Task> 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<SyncRowAction> Actions { get; private set; }
/// <summary>Takes a fresher comparison of the same sequence.</summary>
/// <summary>Settings differ on both sides: the row is drawn on the card colour, as in Server.html.</summary>
public bool IsConflict => _entry.Status == SyncStatus.Conflict && _entry.Transfer is null;
public IReadOnlyList<SyncRowAction> Actions
{
get => _actions;
private set => Set(ref _actions, value);
}
/// <summary>Takes a fresher comparison of the same sequence; buttons the row still offers are kept.</summary>
public void Update(SyncEntry entry, SyncRights rights)
{
_entry = entry;
_rights = rights;
Actions = BuildActions();
OnAllPropertiesChanged();
UpdateActions(rights);
RaiseTexts();
}
private List<SyncRowAction> 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
/// <summary>Takes the progress of the row's transfer ("46 % received") without touching its buttons.</summary>
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));
}
/// <summary>Texts in the current language.</summary>
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));
}
}
+115 -56
View File
@@ -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<SyncFilter> select)
public string ToolTip => Loc.L("Tooltips.Server.Sync.Filter." + Filter);
public ICommand SelectCommand { get; } = new RelayCommand(() => select(filter));
/// <summary>Conflicts waiting for a choice: the chip is drawn in the warning colour.</summary>
/// <summary>Conflicts waiting for a choice: the chip is drawn in the warning colour unless it is the selected one.</summary>
public bool NeedsAttention => Filter == SyncFilter.Conflicts && _count > 0;
public bool IsSelected
@@ -45,22 +44,33 @@ public sealed class SyncFilterChip(SyncFilter filter, Action<SyncFilter> select)
OnPropertyChanged(nameof(NeedsAttention));
}
}
/// <summary>Texts in the current language.</summary>
public void Relabel()
{
OnPropertyChanged(nameof(Text));
OnPropertyChanged(nameof(ToolTip));
}
}
/// <summary>
/// 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.
/// </summary>
public sealed class SyncTableViewModel : ObservableObject, IDisposable
{
private readonly ShellViewModel _shell;
private readonly IReadOnlyList<SyncFilterChip> _chips;
private readonly HashSet<Guid> _openWhenCached = [];
/// <summary>"Open with proxies" waiting for its proxy download: server id → the job that fills the cache.</summary>
private readonly Dictionary<Guid, Guid> _openWhenCached = [];
private readonly ConcurrentDictionary<Guid, JobProgressInfo> _progress = new();
private readonly SemaphoreSlim _reloading = new(1, 1);
private readonly AsyncRelayCommand _uploadAll;
private IReadOnlyList<SyncEntry> _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<SyncFilter>().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<SyncRow> Rows { get; } = [];
public IReadOnlyList<SyncFilterChip> 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)));
/// <summary>What "Upload all local" sends, or why it is disabled.</summary>
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)));
/// <summary>"Last sync 10:15 · manifest diff 0.4 s"; null before the first comparison.</summary>
public string? LastSyncText => Host.Agent?.Snapshot is { } s
@@ -128,12 +143,13 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
private IEnumerable<SyncEntry> LocalOnly => _entries.Where(e => e.Status == SyncStatus.LocalOnly);
/// <summary>Starts following the sync engine and the transfer jobs.</summary>
/// <summary>Starts following the sync engine, the transfer jobs and the language.</summary>
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);
}
/// <summary>Counts, rows, footer and the Library's sync glyphs after a reload.</summary>
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();
}
/// <summary>Updates the rows in place (scroll position and recycled containers survive), adding and removing as needed.</summary>
private void ApplyFilter()
/// <summary>
/// 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.
/// </summary>
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));
}
/// <summary>The existing row of an entry, updated, or a new one.</summary>
private SyncRow Row(SyncEntry entry, SyncRights rights, Dictionary<Guid, SyncRow> existing)
{
if (!existing.TryGetValue(entry.Id, out var row)) return new SyncRow(entry, rights, ActAsync);
row.Update(entry, rights);
return row;
}
/// <summary>The Library shows each local copy's sync state: rows it has loaded take the fresh state.</summary>
private void UpdateLibrary()
{
@@ -235,15 +270,6 @@ public sealed class SyncTableViewModel : ObservableObject, IDisposable
}
}
/// <summary>"Open with proxies" waits for the proxy download; the editor opens once it is in the cache.</summary>
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();
}
/// <summary>Settles a conflict; overwriting one side is confirmed first with the settings that differ (the dry run).</summary>
/// <summary>Settles a conflict once the user confirmed the plan: the settings that differ and what each choice overwrites.</summary>
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);
}
}
/// <summary>"Keep this PC's settings?" with the settings that differ: one side's edits are about to be overwritten.</summary>
private static ConfirmRequest OverwriteRequest(SyncEntry entry, SyncAction action) => new()
/// <summary>The dry run of a resolution: what is replaced, by which side's values, and where "Save both" keeps this PC's.</summary>
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();
});
}
/// <summary>
/// "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.
/// </summary>
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);
}
/// <summary>Transfer progress ("46 % received") without reading the table again, at most a few times a second.</summary>
@@ -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);
}
});
}
+152 -13
View File
@@ -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,
}
/// <summary>What the signed-in account may do (server permission bits).</summary>
/// <param name="CanUpload">Send originals and settings.</param>
/// <param name="CanUpload">Send originals, settings and renders (upload and edit rights).</param>
/// <param name="CanFetchOriginals">Download originals (editors and render nodes).</param>
public readonly record struct SyncRights(bool CanUpload, bool CanFetchOriginals)
/// <param name="CanEdit">Change the server's copy of a sequence's settings, title, tags, rating and notes.</param>
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);
}
/// <summary>The words, glyphs and actions of the sync table (BRIEF §12.7): plain functions of a <see cref="SyncEntry"/>.</summary>
public static class SyncText
{
/// <summary>Resource keys of the settings and sequence fields a conflict can show, by JSON path (sections, then members).</summary>
private static readonly Dictionary<string, string> 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",
};
/// <summary>"originals · proxies", "proxies" (cached server proxies) or "—".</summary>
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)),
};
}
/// <summary>"exposure.deflickerWindow.manual: 41 here, 33 on the server" for each setting that differs (at most three).</summary>
public static string Conflict(SyncEntry entry)
/// <summary>One line for the State tooltip: "Deflicker window 41 here, 33 on the server · +2".</summary>
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)),
};
}
/// <summary>
/// What differs, one line per setting or sequence field in project order ("Deflicker window 41 here, 33 on the
/// server", "Tags differ"), at most <paramref name="max"/>.
/// </summary>
public static IReadOnlyList<string> Conflict(SyncEntry entry, int max)
{
if (entry is not { Local: { } local, Link.ConflictRemote: { } theirs }) return [];
var lines = new List<string>();
var shown = new HashSet<string>(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;
}
/// <summary>The row's buttons, in the order the mockup shows them.</summary>
@@ -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;
}
/// <summary>The bytes an upload or download of the row moves (for the tooltips).</summary>
/// <summary>The bytes an action moves (for the tooltips).</summary>
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,
};
/// <summary>The tooltip of a row button: what it does and, for transfers, what it moves.</summary>
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));
/// <summary>
/// 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.
/// </summary>
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]);
}
/// <summary>A title, tags, rating or notes value as shown ("—" when empty).</summary>
private static string Text(string? value) => string.IsNullOrWhiteSpace(value) ? Loc.L("Server.Sync.None") : Shorten(value);
/// <summary>A side's value as shown ("41", "on", "auto"); null when it is not a single value worth printing.</summary>
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<bool>(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<bool>(out var flag) => Loc.L(flag ? "Server.Setting.On" : "Server.Setting.Off"),
JsonValue value when value.TryGetValue<double>(out var number) => number.ToString("0.###", Loc.Culture),
JsonValue value when value.TryGetValue<string>(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);
}
+147
View File
@@ -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;
/// <summary>
/// "Pause uploads on metered networks" (Server.html): while <see cref="AppSettings.PauseUploadsOnMetered"/> 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.
/// </summary>
public sealed class UploadHold : IDisposable
{
private static readonly TimeSpan Interval = TimeSpan.FromMinutes(1);
private readonly AppServices _services;
private readonly HashSet<Guid> _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;
}
/// <summary>Follows the jobs of <paramref name="engine"/> (once the index is open).</summary>
public void Attach(JobEngine engine)
{
if (ReferenceEquals(_engine, engine)) return;
if (_engine is not null) _engine.JobChanged -= OnJobChanged;
_engine = engine;
engine.JobChanged += OnJobChanged;
Request();
}
/// <summary>Applies the option and the connection's cost now (the option changed).</summary>
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();
}
}
}
/// <summary>The connection cost Windows reports (Settings › Network › Metered connection) through <c>INetworkCostManager</c>.</summary>
internal static class MeteredNetwork
{
private const uint Fixed = 0x2;
private const uint Variable = 0x4;
private const uint OverDataLimit = 0x10000;
private const uint Roaming = 0x40000;
/// <summary>True when the machine's connection is metered (fixed or variable cost, over its data limit, or roaming); false when unknown.</summary>
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);
}
}
+25 -2
View File
@@ -14,7 +14,7 @@ namespace Titano.App.Shell;
/// <summary>
/// <c>Titano.exe --capture &lt;dir&gt;</c>: renders every client screen at 1440×900 in the dark and light themes to
/// <c>&lt;screen&gt;-&lt;theme&gt;.png</c> from <see cref="SampleData"/> (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.
/// </summary>
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);
/// <summary>A dialog shown off screen, saved, then closed.</summary>
private static async Task<int> 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<int> SaveAsync(Window window, string directory, string screen, AppTheme theme, ShellViewModel? waitForThumbnails = null)
{
await Settle(window);
@@ -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)
);
@@ -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)
);
+33 -7
View File
@@ -27,8 +27,8 @@ public sealed record SyncAgentOptions
/// and every <see cref="SyncAgentOptions.RefreshInterval"/>; the sequences named by the server's <c>sequenceChanged</c>
/// 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
/// <see cref="SyncPolicy.KeepProxiesOffline"/>, proxy downloads queued for every server sequence without local
/// originals whose cache is stale. Dispose to stop.
/// <see cref="SyncPolicy.KeepProxiesOffline"/>, 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.
/// </summary>
public sealed class SyncAgent : IAsyncDisposable
{
@@ -43,6 +43,11 @@ public sealed class SyncAgent : IAsyncDisposable
private readonly Channel<bool> _refreshWake = Channel.CreateBounded<bool>(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropWrite });
private readonly Channel<bool> _eventWake = Channel.CreateBounded<bool>(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropWrite });
private readonly ConcurrentDictionary<Guid, byte> _changed = new();
/// <summary>Proxy downloads this agent queued, by server id, and the failures of each sequence's last ones.</summary>
private readonly Dictionary<Guid, Guid> _proxyJobs = [];
private readonly Dictionary<Guid, (int Failures, DateTime NotBefore)> _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
}
}
/// <summary>Queues (background priority) the proxies of server sequences without local originals whose cache is stale.</summary>
/// <summary>
/// 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.
/// </summary>
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;
}
}
+53 -6
View File
@@ -6,12 +6,54 @@ using Titano.Data.Jobs;
namespace Titano.Data.Sync;
/// <summary>What an upload or download job moves.</summary>
public enum SyncTransferKind
{
/// <summary>Originals and settings of a local sequence to the server.</summary>
Upload,
/// <summary>Renders of a local copy to the server.</summary>
SendExports,
/// <summary>Originals of a server sequence into the local archive.</summary>
DownloadOriginals,
/// <summary>Proxies of a server sequence into the proxy cache.</summary>
DownloadProxies,
/// <summary>Renders of a server sequence into the local copy's exports.</summary>
GetExports,
}
/// <summary>
/// Parameters of upload and download jobs. The job's target is the local sequence for an upload and the server
/// sequence for a download.
/// </summary>
/// <param name="ArchiveId">Archive that receives downloaded originals when the sequence has no local copy yet.</param>
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;
+26 -19
View File
@@ -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);
/// <summary>Upload and download jobs that are waiting, running or paused, by target id (local id for uploads, server id for downloads).</summary>
public async Task<IReadOnlyDictionary<Guid, Job>> ActiveTransfersAsync(CancellationToken cancellationToken)
public async Task<IReadOnlyDictionary<Guid, IReadOnlyList<Job>>> ActiveTransfersAsync(CancellationToken cancellationToken)
{
var transfers = new Dictionary<Guid, Job>();
var jobs = new List<Job>();
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<Job>)[.. 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),
};
}
+45 -10
View File
@@ -20,20 +20,20 @@ public enum SyncStatus
/// <summary>The server holds originals or settings this PC lacks.</summary>
ServerAhead,
/// <summary>Both sides changed the settings since they last agreed: the user picks.</summary>
/// <summary>Both sides changed the settings, title, tags, rating or notes since they last agreed: the user picks.</summary>
Conflict,
/// <summary>An upload or download job is queued, running or paused.</summary>
Transferring,
}
/// <summary>How the user settles a settings conflict (BRIEF §12.7).</summary>
/// <summary>How the user settles a conflict (BRIEF §12.7).</summary>
public enum SyncResolution
{
KeepMine,
TakeServer,
/// <summary>The server's settings win; this PC's are kept next to the project as a dated copy.</summary>
/// <summary>The server's version wins; this PC's settings are kept next to the project as a dated copy.</summary>
SaveBoth,
}
@@ -45,12 +45,25 @@ public enum SyncDownloadMode
/// <summary>Proxies (thumbnails where the server has none) into the proxy cache.</summary>
Proxies,
/// <summary>The server's renders the local copy lacks, into its exports folder.</summary>
Exports,
}
/// <summary>What an upload job sends.</summary>
public enum SyncUploadMode
{
/// <summary>Originals and settings (creating the server sequence when needed).</summary>
Originals,
/// <summary>This PC's renders the server lacks (BRIEF §14.5: renders are uploaded on request).</summary>
Exports,
}
/// <summary>
/// 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.
/// </summary>
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; }
/// <summary>Local originals the server does not have.</summary>
public int MissingRemote { get; init; }
@@ -82,13 +97,23 @@ public sealed record SyncLink
/// <summary>Server originals this PC does not have.</summary>
public int MissingLocal { get; init; }
/// <summary>Server renders the local copy does not have, and their size.</summary>
public int ExportsToGet { get; init; }
public long ExportsToGetBytes { get; init; }
/// <summary>Renders of the local copy the server does not have (whose file is still on this PC), and their size.</summary>
public int ExportsToSend { get; init; }
public long ExportsToSendBytes { get; init; }
/// <summary>Settings versions at which the content was found to conflict; null when it does not.</summary>
public int? ConflictLocalSettings { get; init; }
public int? ConflictRemoteSettings { get; init; }
/// <summary>The server's settings when the conflict was found, so it can be shown without asking the server again.</summary>
public string? ConflictSettingsJson { get; init; }
/// <summary>The server's side when the conflict was found: what the user is shown and what "Take server" applies.</summary>
public SyncPayload? ConflictRemote { get; init; }
/// <summary>Frames whose proxy (or thumbnail) is in this PC's proxy cache.</summary>
public int ProxiesCached { get; init; }
@@ -96,6 +121,9 @@ public sealed record SyncLink
/// <summary>Server frames version the proxy cache was filled at.</summary>
public int? ProxiesFramesVersion { get; init; }
/// <summary>Why the last comparison of this sequence failed (it is compared again next time); null when it worked.</summary>
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
}
/// <summary>One row of the sync table: the local sequence, the server sequence, or both, and where they stand.</summary>
/// <param name="Transfer">The upload or download job working on it, if any.</param>
/// <param name="Transfer">The upload or download job working on it, if any (the user's before background ones).</param>
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<SyncEntry> Entrie
/// <summary>Options a refresh applies.</summary>
/// <param name="AutoSyncSettings">Send and take settings changed on one side only (Server.html "Sync projects automatically").</param>
/// <param name="KeepProxiesOffline">Keep the proxies of every server sequence without local originals in the cache.</param>
public sealed record SyncPolicy(bool AutoSyncSettings = true, bool KeepProxiesOffline = false);
/// <param name="KeepProxiesOffline">Keep the proxy caches of server sequences this PC opened with proxies up to date.</param>
/// <param name="CanEdit">The account may change server sequences; without it this PC's edits are never sent.</param>
public sealed record SyncPolicy(bool AutoSyncSettings = true, bool KeepProxiesOffline = false, bool CanEdit = true);
/// <summary>
/// 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.
/// </summary>
public sealed class SyncConflictChangedException(string message) : InvalidOperationException(message);
+15
View File
@@ -38,6 +38,15 @@ public interface ISyncRemote : IDisposable
/// <summary>Streams a frame's original, proxy or thumb, resuming with Range from the destination's length.</summary>
Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken);
/// <summary>Streams a render's file, resuming with Range from the destination's length; returns the file name the server gives it.</summary>
Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken);
/// <summary>
/// Chunked, resumable upload of a render of <paramref name="sequenceId"/> made on this PC; the server verifies the
/// SHA-256 and keeps the render's id. Null when the server already has it. See <see cref="TitanoClient.UploadRenderAsync"/>.
/// </summary>
Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken);
/// <summary>Server events until cancelled or the socket closes.</summary>
IAsyncEnumerable<ServerEvent> 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<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
client.DownloadRenderAsync(renderId, destination, cancellationToken);
public Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
client.UploadRenderAsync(sequenceId, path, request, null, cancellationToken);
public IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken) => client.EventsAsync(cancellationToken);
public void Dispose()
+30 -12
View File
@@ -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<string> Tags, int
/// <summary>Title, tags, rating and notes match (blank equals absent, tags ignore case and order).</summary>
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<string> Tags, int
/// <summary>The PATCH that makes the server's copy equal to this one; settings are sent in their normalised form.</summary>
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;
}
/// <summary>One setting that differs: its dotted path ("exposure.deflicker.window") and the JSON value on each side (null when absent).</summary>
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(SyncPayload))]
internal sealed partial class SyncPayloadJson : JsonSerializerContext;
/// <summary>
/// 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).
/// </summary>
public sealed record SettingsDifference(string Path, string? Mine, string? Theirs);
/// <summary>Decisions of the sync engine that need no I/O: the status of a row, settings equality and differences, merges.</summary>
public static class SyncRules
{
/// <summary>Path prefix of the title, tags, rating and notes in <see cref="Differences(SyncPayload, SyncPayload, int)"/>.</summary>
public const string MetadataPath = "sequence.";
/// <summary>
/// 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.
/// </summary>
public static SyncStatus Status(Sequence? local, SyncLink? link)
{
@@ -91,13 +100,20 @@ public static class SyncRules
}
/// <summary>
/// Metadata-only divergence (settings equal): tags are united, title, rating and notes come from the side edited last.
/// Up to <paramref name="max"/> differences between two payloads: title, tags, rating and notes first (paths
/// <c>sequence.title</c>, <c>sequence.tags</c>, …, tags as one comma-separated value), then the settings.
/// </summary>
public static SyncPayload Merge(SyncPayload local, SyncPayload remote, bool localIsNewer)
public static IReadOnlyList<SettingsDifference> 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<SettingsDifference>();
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;
}
/// <summary>
@@ -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
+366 -113
View File
@@ -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
/// <summary>
/// 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 <see cref="ResolveAsync"/>. Uploads and downloads run as jobs (<see cref="CreateJobHandlers"/>): 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 <see cref="RemoteSequence"/>. Proxies and
/// thumbnails never travel upward. The state of each local sequence is written to its <c>sync_state</c> 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 <see cref="ResolveAsync"/>, against the versions the user was shown. Uploads and
/// downloads run as jobs (<see cref="CreateJobHandlers"/>): 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 <see cref="RemoteSequence"/>; 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 <c>sync_state</c> for the Library. A failure while
/// comparing one sequence is recorded on its link and does not stop the others.
/// </summary>
public sealed class SyncService
{
private const string LogCategory = "sync";
private const string SessionKeyPrefix = "titano-sync:";
/// <summary>Recomputes states without sending or taking settings on the user's behalf.</summary>
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);
/// <summary>Unlinked pairs whose manifests were compared at these frame versions without a match (not fetched again).</summary>
private readonly ConcurrentDictionary<string, byte> _unmatched = new(StringComparer.Ordinal);
public SyncService(TitanoDatabase db, ArchiveService archives, SyncOptions options)
{
_db = db;
@@ -79,6 +90,9 @@ public sealed class SyncService
/// <summary>"https://nas.local:8443": the key links are stored under (scheme, lower-case host, port, path).</summary>
public static string ServerKey(Uri address) => address.GetLeftPart(UriPartial.Path).TrimEnd('/').ToLowerInvariant();
/// <summary>What an upload or download job moves; null for any other job.</summary>
public static SyncTransferKind? TransferKind(Job job) => SyncJobParameters.Read(job)?.Kind(job.Kind);
/// <summary>Session key the local archive groups the downloaded originals of one server sequence by.</summary>
internal static string SessionKey(Guid remoteId) => SessionKeyPrefix + remoteId.ToString("N");
@@ -86,30 +100,34 @@ public sealed class SyncService
/// <summary>
/// Lists the server, compares every sequence with its local copy, sends or takes settings changed on one side when
/// <see cref="SyncPolicy.AutoSyncSettings"/> is on, forgets sequences deleted on the server, and stores the states.
/// <see cref="SyncPolicy.AutoSyncSettings"/> is on, forgets sequences the server confirms deleted, links unlinked
/// copies holding the same originals, and stores the states.
/// </summary>
public async Task<SyncSnapshot> RefreshAsync(ISyncRemote remote, SyncPolicy policy, CancellationToken cancellationToken)
{
var clock = Stopwatch.StartNew();
var listing = await remote.ListSequencesAsync(cancellationToken).ConfigureAwait(false);
Dictionary<Guid, Sequence> 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);
}
/// <summary>The sync table of <paramref name="server"/> as last compared: every server sequence and every local one, with active transfers.</summary>
public async Task<IReadOnlyList<SyncEntry>> 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<SyncEntry>(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;
}
/// <summary>
/// The sync table of <paramref name="server"/> as last compared: every server sequence and every local one, with
/// active transfers; conflicts first, then transfers, then by title.
/// </summary>
public Task<IReadOnlyList<SyncEntry>> ListAsync(string server, CancellationToken cancellationToken) => EntriesAsync(server, null, cancellationToken);
/// <summary>
/// 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 ----------------
/// <summary>Settles a settings conflict as the user chose (BRIEF §12.7); a 409 means the server changed again meanwhile.</summary>
public async Task ResolveAsync(ISyncRemote remote, Guid remoteId, SyncResolution resolution, CancellationToken cancellationToken)
/// <summary>
/// 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 <see cref="SyncConflictChangedException"/>
/// is thrown. Returns the file this PC's settings were saved to for <see cref="SyncResolution.SaveBoth"/>, else null.
/// </summary>
public async Task<string?> 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;
}
/// <summary>Sends or takes settings changed on one side only (the row action while automatic sync is off).</summary>
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<Job> EnqueueUploadAsync(JobEngine engine, string server, Sequence local, CancellationToken cancellationToken = default) =>
EnqueueAsync(engine, JobKind.UploadCommit, local.Id, local.DisplayTitle, new SyncJobParameters(server), JobPriority.User, cancellationToken);
/// <summary>Queues sending the renders of a local copy the server lacks (BRIEF §14.5), unless that is already waiting or running.</summary>
public Task<Job> 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);
/// <summary>
/// Queues a download of a server sequence, unless one is already waiting or running: originals into
/// <paramref name="archiveId"/> (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
/// <paramref name="archiveId"/> (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.
/// </summary>
public Task<Job> 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
/// <summary>Creates or updates the link of a local sequence to a server sequence (an upload or download made it).</summary>
internal async Task<SyncLink> 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();
}
}
/// <summary>Forgets a link whose server sequence an upload found deleted (its originals go into a new one).</summary>
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();
}
}
/// <summary>
@@ -312,6 +365,25 @@ public sealed class SyncService
}
}
/// <summary>
/// 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.
/// </summary>
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);
}
/// <summary>Records what the proxy cache holds for a server sequence.</summary>
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)
/// <summary>The file of a local render: archive-relative for outputs inside the archive, absolute otherwise.</summary>
internal static string RenderFile(Archive archive, Render render) =>
Path.IsPathRooted(render.OutputPath) ? render.OutputPath : new ArchivePaths(archive.RootPath).FromRelative(render.OutputPath);
/// <summary>
/// Compares one server sequence (under the gate): fetches it, forgets its link on a 404, records a failure on its
/// link. <paramref name="locals"/> (when given) receives the local copy as it stands after the comparison.
/// </summary>
private async Task CompareOneAsync(ISyncRemote remote, Guid remoteId, SyncLink? link, Dictionary<Guid, Sequence>? 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<Guid, Sequence>? 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);
}
/// <summary>Compares originals when frame counters moved, then settles the settings; returns the updated link.</summary>
private async Task<SyncLink> ReconcileAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence? local, SyncPolicy policy,
/// <summary>A failure that concerns one sequence; a lost session (401) or the caller's cancellation stops the whole comparison.</summary>
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,
};
/// <summary>Compares originals and renders when either side's moved, then settles the settings; returns the updated link and local copy.</summary>
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<SyncLink> SettleSettingsAsync(ISyncRemote remote, SyncLink link, SequenceDto dto, Sequence local, SequenceManifest? manifest,
SyncPolicy policy, CancellationToken cancellationToken)
/// <summary>Renders each side lacks, by id (a render keeps its id on both sides); local renders whose file is gone cannot be sent.</summary>
private async Task<SyncLink> CompareExportsAsync(SyncLink link, Sequence local, IReadOnlyList<RenderDto> 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);
}
/// <summary>Sends this PC's payload unless the server moved past <paramref name="expected"/>, which shows up as a conflict next.</summary>
@@ -425,6 +561,62 @@ public sealed class SyncService
}
}
/// <summary>
/// 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.
/// </summary>
private async Task MatchByContentAsync(ISyncRemote remote, IReadOnlyList<SequenceDto> listing, Dictionary<Guid, Sequence> 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<bool> 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<string>().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);
/// <summary>Writes a payload into the local sidecar, project and index (each write bumps the local settings version).</summary>
private async Task<Sequence> 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;
}
/// <summary>"Save both": this PC's project is kept as <c>.titano/project.local-&lt;UTC time&gt;.json</c> before the server's replaces it.</summary>
private async Task SaveCopyAsync(Sequence local, CancellationToken cancellationToken)
/// <summary>"Save both": this PC's project is kept next to <c>project.json</c> as <c>project.this-pc-&lt;local time&gt;.json</c> before the server's replaces it.</summary>
private async Task<string> 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
}
/// <summary>Writes each local sequence's state for the Library and returns the table.</summary>
private async Task<IReadOnlyList<SyncEntry>> StoreStatesAsync(string server, CancellationToken cancellationToken)
private async Task<IReadOnlyList<SyncEntry>> StoreStatesAsync(string server, Dictionary<Guid, Sequence>? 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;
}
/// <summary>The table from the stored links and <paramref name="known"/> local sequences (read from the index when null).</summary>
private async Task<IReadOnlyList<SyncEntry>> EntriesAsync(string server, Dictionary<Guid, Sequence>? known, CancellationToken cancellationToken)
{
var links = await _links.ListAsync(server, cancellationToken).ConfigureAwait(false);
var locals = known is null ? await LocalSequencesAsync(cancellationToken).ConfigureAwait(false) : new Dictionary<Guid, Sequence>(known);
var transfers = await _links.ActiveTransfersAsync(cancellationToken).ConfigureAwait(false);
var entries = new List<SyncEntry>(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)];
}
/// <summary>The transfer a row shows: the user's before background ones, originals before renders before proxies, running before waiting.</summary>
private static Job? Pick(IReadOnlyList<Job>? local, IReadOnlyList<Job>? 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();
/// <summary>The local copy of a link: from <paramref name="locals"/>, or the index when the paged listing missed it.</summary>
private async Task<Sequence?> LocalOfAsync(SyncLink? link, Dictionary<Guid, Sequence>? 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<Job> 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,
};
}
+181 -13
View File
@@ -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.
/// </summary>
internal sealed class SyncTransfers(SyncService sync)
{
private const long SpaceReserve = 512L << 20;
/// <summary>Recomputes a sequence's state after a transfer without sending or taking settings on the user's behalf.</summary>
private static readonly SyncPolicy CompareOnly = new(AutoSyncSettings: false);
/// <summary>Space a cached proxy is assumed to take before it is downloaded (1920 px JPEG, BRIEF §8.5).</summary>
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<string> have = local is null
? new HashSet<string>()
: (await SequenceFrames.ListAsync(db, local.Id, ct).ConfigureAwait(false)).Select(f => f.Sha256).OfType<string>().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)
}
}
/// <summary>
/// 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.
/// </summary>
private async Task<(HashSet<string> Have, Dictionary<string, string> 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<string, string>(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);
/// <summary>Puts a verified copy of an original flagged missing back where its record says; a file that reappeared meanwhile is kept.</summary>
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);
}
/// <summary>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.</summary>
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;
}
/// <summary>"name.mp4", else "name (2).mp4", … — an existing export is never replaced.</summary>
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);
}
@@ -175,6 +175,46 @@ public sealed class TitanoClient : IDisposable
.ConfigureAwait(false);
}
/// <summary>
/// Sends a render of <paramref name="sequenceId"/> made on this PC in resumable chunks; the server verifies the
/// SHA-256 in <paramref name="request"/> and files it under the sequence's exports with the same id. Null when the
/// server already has that render.
/// </summary>
public async Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, IProgress<UploadProgress>? 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<RenderDto>(HttpMethod.Post, $"api/v1/sequences/{sequenceId}/renders/{uploadId}/complete",
ProtocolJson.Default.RenderDto, ct), ct).ConfigureAwait(false);
}
/// <summary>
/// Streams a render's file to <paramref name="destination"/>, resuming with Range from its current length; returns
/// the file name the server gives it.
/// </summary>
public async Task<string?> 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('"');
}
/// <summary>Sends every chunk the server does not have yet; each PUT is idempotent and retried on transient errors.</summary>
private async Task SendChunksAsync(string path, long length, Guid uploadId, int chunkSize, IReadOnlyList<int> alreadyReceived,
IProgress<UploadProgress>? progress, CancellationToken ct)
+1
View File
@@ -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))]
+20
View File
@@ -16,3 +16,23 @@ public sealed record CompleteUploadResponse(Guid FrameId, Guid SequenceId, int I
/// <summary><c>GET /api/v1/uploads/{id}</c>: resume state.</summary>
public sealed record UploadState(Guid UploadId, string FileName, long Size, string Sha256, int ChunkSize, IReadOnlyList<int> Received, string State);
/// <summary>
/// <c>POST /api/v1/sequences/{id}/renders</c>: 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 <see cref="StartUploadResponse.AlreadyExists"/>. Chunks go to
/// <c>/uploads/{uploadId}/chunks/{n}</c>; <c>POST /sequences/{id}/renders/{uploadId}/complete</c> verifies the SHA-256
/// and files the render under the sequence's exports.
/// </summary>
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);
@@ -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<IResult> ListAsync(HttpContext context, TitanoDatabase db, ServerArchive archive, int? year, string? camera, string? status,
+1
View File
@@ -121,6 +121,7 @@ public static class ServerHost
services.AddSingleton<UploadStore>();
services.AddSingleton<UploadService>();
services.AddSingleton<RenderStore>();
services.AddSingleton<RenderUploads>();
services.AddSingleton<SequenceJobs>();
services.AddSingleton<RenderNodeService>();
services.AddSingleton<JobEngineHost>();
+7 -2
View File
@@ -12,7 +12,12 @@ public sealed class RenderStore(TitanoDatabase db, ServerArchive archive, TimePr
/// Moves <paramref name="stagedFile"/> (on the archive volume) to <c>&lt;sequence folder&gt;/exports/</c>, never
/// replacing an existing export, and inserts the render row. Null when the sequence has no folder in the archive yet.
/// </summary>
public async Task<Render?> CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, CancellationToken cancellationToken)
public Task<Render?> CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, CancellationToken cancellationToken) =>
CommitAsync(sequence, stagedFile, output, producedBy, Guid.CreateVersion7(), cancellationToken);
/// <summary>As above, keeping <paramref name="id"/>: a render a client sends keeps the id it has on that client.</summary>
public async Task<Render?> 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),
+118
View File
@@ -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;
/// <summary>
/// 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 <see cref="RenderStore"/> under the
/// sequence's exports with the client's render id, so both sides recognise the same render.
/// </summary>
public sealed class RenderUploads(
TitanoDatabase db,
ServerOptions options,
UploadService uploads,
UploadStore store,
RenderStore renders,
EventHub events,
TimeProvider time)
{
public async Task<Outcome<StartUploadResponse>> StartAsync(Caller caller, Guid sequenceId, RenderUploadRequest request, CancellationToken cancellationToken)
{
if (await db.Sequences.GetAsync(sequenceId, cancellationToken) is null) return Outcome<StartUploadResponse>.Fail(404, "not_found", "sequence");
if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Outcome<StartUploadResponse>.Fail(400, "invalid_file_name");
if (!UploadRules.IsSha256(request.Sha256)) return Outcome<StartUploadResponse>.Fail(400, "invalid_hash");
if (request.Size <= 0) return Outcome<StartUploadResponse>.Fail(400, "bad_request", "size");
if (request.Size > options.MaxUploadBytes)
return Outcome<StartUploadResponse>.Fail(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture));
if (string.IsNullOrWhiteSpace(request.PresetName) || request.PresetName.Length > 200) return Outcome<StartUploadResponse>.Fail(400, "bad_request", "presetName");
if (request.RenderId == Guid.Empty) return Outcome<StartUploadResponse>.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<StartUploadResponse>.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<StartUploadResponse>.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);
}
/// <summary>Verifies a fully received render and files it; completing a committed one answers it again (a lost response).</summary>
public async Task<Outcome<RenderDto>> 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<RenderDto>.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<RenderDto>.Fail(409, "upload_state");
if (!upload.IsComplete)
return Outcome<RenderDto>.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<RenderDto>.Fail(409, "upload_state");
if (await uploads.StageVerifiedAsync(upload, cancellationToken) is not { } staged) return Outcome<RenderDto>.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<RenderDto>.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);
}
+1 -1
View File
@@ -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<CompleteUploadResponse>(404, "not_found", "upload");
if (upload.JobId is not null) return Fail<CompleteUploadResponse>(409, "wrong_upload_kind");
if (upload.JobId is not null || upload.RenderJson is not null) return Fail<CompleteUploadResponse>(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<CompleteUploadResponse>(409, "upload_state");
+4 -3
View File
@@ -1248,10 +1248,11 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ui:StatusGlyph">
<StackPanel Orientation="Horizontal" Background="Transparent">
<!-- A dock panel, so a glyph in a fixed-width cell ends in an ellipsis instead of being cut. -->
<DockPanel Background="Transparent">
<ui:Icon x:Name="Glyph" Glyph="{StaticResource Icon.Dot}" Size="11" Margin="0,0,4,0" VerticalAlignment="Center" />
<TextBlock Text="{TemplateBinding Text}" VerticalAlignment="Center" />
</StackPanel>
<TextBlock Text="{TemplateBinding Text}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="Kind" Value="Analysed"><Setter Property="Foreground" Value="{DynamicResource Brush.Success}" /></Trigger>
<Trigger Property="Kind" Value="Done"><Setter Property="Foreground" Value="{DynamicResource Brush.Success}" /></Trigger>