using Titano.Protocol;
using Titano.Protocol.Client;
namespace Titano.Data.Sync;
///
/// The server calls the sync engine makes (BRIEF §14.3). sends them with
/// ; screenshots and tests substitute fakes. Failures are s
/// (404 gone, 409 settings conflict) or network exceptions. Dispose a remote handed out by an .
///
public interface ISyncRemote : IDisposable
{
/// Normalised server address the links are stored under ().
string Server { get; }
/// Every sequence on the server.
Task> ListSequencesAsync(CancellationToken cancellationToken);
Task GetSequenceAsync(Guid id, CancellationToken cancellationToken);
Task GetManifestAsync(Guid id, CancellationToken cancellationToken);
/// Every frame row of a sequence, in index order.
Task> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken);
/// The first frame row of a sequence (its thumbnail stands for a sequence without a hero frame); null when it has none yet.
async Task FirstFrameAsync(Guid sequenceId, CancellationToken cancellationToken) =>
(await ListFramesAsync(sequenceId, cancellationToken).ConfigureAwait(false)).FirstOrDefault();
/// A 409 means the settings version moved on the server.
Task UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken);
///
/// Chunked, resumable upload of one original into , or into the sequence the server
/// files under (created by the first one); the server verifies
/// before committing it (BRIEF §14.4). An original the server already holds is not sent again: the response then
/// names ( without one). See .
///
Task UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
CancellationToken cancellationToken);
/// Streams a frame's original, proxy or thumb, resuming with Range from the destination's length.
Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken);
/// Streams a render's file, resuming with Range from the destination's length; returns the file name the server gives it.
Task DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken);
///
/// Chunked, resumable upload of a render of made on this PC; the server verifies the
/// SHA-256 and keeps the render's id. Null when the server already has it. See .
///
Task UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken);
/// Server events until cancelled or the socket closes.
IAsyncEnumerable EventsAsync(CancellationToken cancellationToken);
}
/// Where sync jobs find their server connection.
public interface ISyncConnector
{
/// A signed-in remote of owned by the caller; null when this PC is not connected to that server.
ValueTask ConnectAsync(string server, CancellationToken cancellationToken);
}
/// over a signed-in , which it disposes when it owns it.
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> ListSequencesAsync(CancellationToken cancellationToken)
{
var all = new List();
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 GetSequenceAsync(Guid id, CancellationToken cancellationToken) => client.GetSequenceAsync(id, cancellationToken);
public Task GetManifestAsync(Guid id, CancellationToken cancellationToken) => client.GetManifestAsync(id, cancellationToken);
public async Task> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken)
{
var all = new List();
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;
}
/// One row of the first frames page instead of the whole list.
public async Task FirstFrameAsync(Guid sequenceId, CancellationToken cancellationToken) =>
(await client.ListFramesAsync(sequenceId, null, 1, cancellationToken).ConfigureAwait(false)).Items.FirstOrDefault();
public Task UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken) =>
client.UpdateSequenceAsync(id, request, cancellationToken);
public Task 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 DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
client.DownloadRenderAsync(renderId, destination, cancellationToken);
public Task UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
client.UploadRenderAsync(sequenceId, path, request, null, cancellationToken);
public IAsyncEnumerable EventsAsync(CancellationToken cancellationToken) => client.EventsAsync(cancellationToken);
public void Dispose()
{
if (ownsClient) client.Dispose();
}
}