using System.IO; using Titano.Core.Model; using Titano.Data; using Titano.Data.Jobs; using Titano.Data.Processing; using Titano.Data.Sync; namespace Titano.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, } /// /// Export "Sync" (Export.html, 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 (), while signed in with /// the Upload right. The choice travels inside the render job (its export), so it holds after the editor closes. /// public sealed class ExportSync(TitanoDatabase 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; returns the send job, or null when the job is not a finished render /// of this PC that asked for it, or the sequence cannot be sent now. /// 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; if (Gate(false, server, linked, canUpload) != ExportSyncGate.Available) return null; return await sync.EnqueueSendExportsAsync(engine, server!, local, cancellationToken); } private static bool Wanted(Job job) { try { return ProcessingJobs.ReadRender(job.ParametersJson).Export.SyncToServer; } catch (InvalidDataException) { return false; } } }