Files
Chronos/src/Titano.Data/Sync/SyncRemote.cs
T
Claude dcc9dcd213 feat(data): filter sequences by renders and sync state; read a remote sequence's first frame alone
SequenceQuery.Rendered and SyncStates back the Library's Rendered, Not
rendered and Synced status choices. ISyncRemote.FirstFrameAsync reads one row
of the frames page (default: the whole list), so a remote card without a hero
frame no longer downloads the server's manifest for its thumbnail.

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

132 lines
7.0 KiB
C#

using Titano.Protocol;
using Titano.Protocol.Client;
namespace Titano.Data.Sync;
/// <summary>
/// The server calls the sync engine makes (BRIEF §14.3). <see cref="TitanoSyncRemote"/> sends them with
/// <see cref="TitanoClient"/>; screenshots and tests substitute fakes. Failures are <see cref="TitanoApiException"/>s
/// (404 gone, 409 settings conflict) or network exceptions. Dispose a remote handed out by an <see cref="ISyncConnector"/>.
/// </summary>
public interface ISyncRemote : IDisposable
{
/// <summary>Normalised server address the links are stored under (<see cref="SyncService.ServerKey"/>).</summary>
string Server { get; }
/// <summary>Every sequence on the server.</summary>
Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken);
Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken);
Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken);
/// <summary>Every frame row of a sequence, in index order.</summary>
Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken);
/// <summary>The first frame row of a sequence (its thumbnail stands for a sequence without a hero frame); null when it has none yet.</summary>
async Task<FrameDto?> FirstFrameAsync(Guid sequenceId, CancellationToken cancellationToken) =>
(await ListFramesAsync(sequenceId, cancellationToken).ConfigureAwait(false)).FirstOrDefault();
/// <summary>A 409 <see cref="TitanoApiException"/> means the settings version moved on the server.</summary>
Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken);
/// <summary>
/// Chunked, resumable upload of one original into <paramref name="sequenceId"/>, or into the sequence the server
/// files <paramref name="sessionKey"/> under (created by the first one); the server verifies <paramref name="sha256"/>
/// before committing it (BRIEF §14.4). An original the server already holds is not sent again: the response then
/// names <paramref name="sequenceId"/> (<see cref="Guid.Empty"/> without one). See <see cref="TitanoClient.UploadFileAsync"/>.
/// </summary>
Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken);
/// <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);
}
/// <summary>Where sync jobs find their server connection.</summary>
public interface ISyncConnector
{
/// <summary>A signed-in remote of <paramref name="server"/> owned by the caller; null when this PC is not connected to that server.</summary>
ValueTask<ISyncRemote?> ConnectAsync(string server, CancellationToken cancellationToken);
}
/// <summary><see cref="ISyncRemote"/> over a signed-in <see cref="TitanoClient"/>, which it disposes when it owns it.</summary>
public sealed class TitanoSyncRemote(TitanoClient client, bool ownsClient) : ISyncRemote
{
private const int SequencePage = 500;
private const int FramePage = 1000;
public string Server { get; } = SyncService.ServerKey(client.BaseAddress);
public async Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken)
{
var all = new List<SequenceDto>();
string? cursor = null;
do
{
var page = await client.ListSequencesAsync(null, cursor, SequencePage, cancellationToken).ConfigureAwait(false);
all.AddRange(page.Items);
cursor = page.NextCursor;
}
while (cursor is not null);
return all;
}
public Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken) => client.GetSequenceAsync(id, cancellationToken);
public Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken) => client.GetManifestAsync(id, cancellationToken);
public async Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
{
var all = new List<FrameDto>();
string? cursor = null;
do
{
var page = await client.ListFramesAsync(sequenceId, cursor, FramePage, cancellationToken).ConfigureAwait(false);
all.AddRange(page.Items);
cursor = page.NextCursor;
}
while (cursor is not null);
return all;
}
/// <summary>One row of the first frames page instead of the whole list.</summary>
public async Task<FrameDto?> FirstFrameAsync(Guid sequenceId, CancellationToken cancellationToken) =>
(await client.ListFramesAsync(sequenceId, null, 1, cancellationToken).ConfigureAwait(false)).Items.FirstOrDefault();
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken) =>
client.UpdateSequenceAsync(id, request, cancellationToken);
public Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken) =>
client.UploadFileAsync(path, sequenceId, capturedAt, sessionKey, sha256, null, cancellationToken);
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()
{
if (ownsClient) client.Dispose();
}
}