using System.IO; using Chronos.Core.Model; using Chronos.Data; using Chronos.Data.Jobs; using Chronos.Data.Processing; using Chronos.Data.Sync; namespace Chronos.App.Editor; /// Whether the Export "Sync" option can be used, or why not (its tooltip says which). public enum ExportSyncGate { Available, /// A server sequence: its renders are made on the server and stay there. Remote, /// No server session is open. SignedOut, /// The server does not have this sequence. NotLinked, /// The account may not send files to the server. NoUpload, } /// What became of the Export "Sync" of a finished render. /// Why the render was not sent, or . /// The queued send job; null when it was not queued. public sealed record ExportSyncResult(ExportSyncGate Gate, Job? Send); /// /// Export "Sync" (Export.html "register the export on the server when connected", BRIEF ยง14.5): a render made on this /// PC whose export asked for it is sent to the server the sequence is linked to once it is done /// (). The choice travels inside the render job (its export), so it /// holds after the editor closes; a render that ends while the session is down is queued for the last session's server /// and its send waits for the session (and fails visibly on the Jobs screen when it does not come back). /// public sealed class ExportSync(ChronosDatabase db, SyncService sync, JobEngine engine) { /// The option's state for a sequence. /// Key of the signed-in server; null while signed out. /// The server has the sequence (a sync link joins them). public static ExportSyncGate Gate(bool isRemote, string? server, bool linked, bool canUpload) => isRemote ? ExportSyncGate.Remote : server is null ? ExportSyncGate.SignedOut : !linked ? ExportSyncGate.NotLinked : !canUpload ? ExportSyncGate.NoUpload : ExportSyncGate.Available; /// Queues sending a finished render's output. /// Key of the server: the signed-in one, else the one of the saved session; null without either. /// The account may send files (assumed while the session is down: the server refuses the send otherwise). /// The send job or why there is none; null when the job is not a finished render of this PC that asked for it. public async Task SendAsync(Job job, string? server, bool canUpload, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(job); if (job is not { Kind: JobKind.Render, State: JobState.Done, TargetIds: [var target] } || !Wanted(job)) return null; if (await db.Sequences.GetAsync(target, cancellationToken) is not { } local) return null; var linked = server is not null && await sync.LinkOfAsync(server, local.Id, cancellationToken) is not null; var gate = Gate(false, server, linked, canUpload); return gate == ExportSyncGate.Available ? new ExportSyncResult(gate, await sync.EnqueueSendExportsAsync(engine, server!, local, cancellationToken)) : new ExportSyncResult(gate, null); } private static bool Wanted(Job job) { try { return ProcessingJobs.ReadRender(job.ParametersJson).Export.SyncToServer; } catch (InvalidDataException) { return false; } } }