using System.Text.Json; using System.Text.Json.Serialization; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Data.Jobs; namespace Chronos.Data.Sync; /// What an upload or download job moves. public enum SyncTransferKind { /// Originals and settings of a local sequence to the server. Upload, /// Renders of a local copy to the server. SendExports, /// Originals of a server sequence into the local archive. DownloadOriginals, /// Proxies of a server sequence into the proxy cache. DownloadProxies, /// Renders of a server sequence into the local copy's exports. GetExports, } /// /// Parameters of upload and download jobs. The job's target is the local sequence for an upload and the server /// sequence for a download. /// /// Archive that receives downloaded originals when the sequence has no local copy yet. /// /// The new photos of a pair integrated with automatic photos (owner review 6, design ยง7.3): they go only into the two /// sequences of that pair, never into one the job would create or retarget. /// internal sealed record SyncJobParameters(string Server, SyncDownloadMode? Mode = null, Guid? ArchiveId = null, SyncUploadMode? Upload = null, bool? Automatic = null) { [JsonIgnore] public bool IsAutomatic => Automatic == true; 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)] [JsonSerializable(typeof(SyncJobParameters))] internal sealed partial class SyncJobJson : JsonSerializerContext; /// Runs upload and download jobs with a connection to their server; the job fails with a reason when this PC is not connected to it. internal sealed class SyncJobHandler(JobKind kind, SyncTransfers transfers, ISyncConnector connector) : IJobHandler { public JobKind Kind => kind; public async Task RunAsync(JobContext context) { 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 (parameters.Kind(kind)) { case SyncTransferKind.Upload: await transfers.UploadAsync(context, remote, target, parameters.IsAutomatic).ConfigureAwait(false); break; 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, parameters.IsAutomatic).ConfigureAwait(false); break; } } }