using System.IO; using System.Net; using Chronos.App.Shell; using Chronos.Core.Diagnostics; using Chronos.Core.Import; using Chronos.Protocol; using Chronos.Protocol.Client; namespace Chronos.App.Import; /// /// Import destination "Server" (BRIEF §12.4, §14.4): each verified copy staged on this PC is uploaded with the /// protocol's resumable chunks and the hash computed while copying; the server checks that hash before it commits the /// frame, and only then does the photo count as imported. Photos of one plan session share its session key, so the /// server files them in one sequence — the same one when an interrupted import is started again. A typed session /// title is set on that sequence once; a whole folder's own name only on a sequence that has no title yet, so a rename /// made on the server survives the next import of the folder. A file the server refuses (a hash mismatch, a format it /// rejects) is listed in and the import goes on (BRIEF §13: errors per file); a failed connection, /// sign-in or server stops it. A file the server already held counts as a duplicate: the server answers so only for a /// copy that is not flagged missing or damaged, and a server import never deletes anything from the card. /// public sealed class ServerImportSink(ChronosClient client, string stagingDirectory) : IImportSink { private readonly HashSet _titled = []; public string StagingDirectory { get; } = stagingDirectory; /// The copies wait in this PC's cache folder until they are uploaded: the server's space is its quota. public bool StagesOnThisPc => true; /// Every file committed or refused so far, by card path; pass it to . public ImportSinkReport Report { get; } = new(); /// /// Runs a confirmed plan into the server through this sink () and tells the server's /// other clients about it as (BRIEF §14.3 cardEvent): started, its progress, and /// finished, also when it stopped early. /// public async Task RunAsync(ImportPlan plan, string card, bool verify, IProgress? progress, CancellationToken cancellationToken) { var events = new CardImportReporter(client, plan.Id, card, plan.FileCount); events.Started(); ImportOutcome? outcome = null; try { outcome = await LocalImport.RunAsync(plan, this, card, verify, events.Forward(progress), cancellationToken, Report).ConfigureAwait(false); return outcome; } finally { await events.FinishedAsync(outcome).ConfigureAwait(false); } } public async Task> CommitAsync(PlannedSession session, IReadOnlyList files, CancellationToken cancellationToken) { var results = new List(files.Count); foreach (var file in files) { // The server records the uploaded file name as the original name: upload the card's name, not the staging name. var folder = Path.Combine(StagingDirectory, Path.GetFileNameWithoutExtension(file.StagedPath)); var named = Path.Combine(folder, Path.GetFileName(file.Item.SourcePath)); Directory.CreateDirectory(folder); File.Move(file.StagedPath, named); CompleteUploadResponse? committed = null; try { committed = await client.UploadFileAsync(named, null, file.Item.CapturedAt, session.SessionKey, file.Sha256, null, cancellationToken) .ConfigureAwait(false); // An upload the server already held reports no position in a sequence. var result = new SinkCommit(committed?.FrameId, file.Item.FileName, committed is null || committed.Index < 0); Report.Committed[file.Item.SourcePath] = result; results.Add(result); } catch (ChronosApiException ex) when (IsRefusal(ex)) { // The executor has no failed commit: the refusal travels in the report, which turns this result into a failure. The // server words its refusal in the client's language; one without words reads as the catalog's sentence. Report.Rejected[file.Item.SourcePath] = ErrorText.ForServer(ex); results.Add(new SinkCommit(null, file.Item.FileName, Duplicate: false)); } finally { // Our own staged copy; the original stays on the card. File.Delete(named); Directory.Delete(folder); } if (committed is { Index: >= 0 } && session.Title is { Length: > 0 } title && _titled.Add(committed.SequenceId)) await SetTitleAsync(committed.SequenceId, title, keepExisting: title == session.DefaultTitle, cancellationToken).ConfigureAwait(false); } return results; } /// /// Names the sequence; with (the title the user did not type) only one without a /// title. A refused title leaves the photos imported under the sequence's own name. /// private async Task SetTitleAsync(Guid sequenceId, string title, bool keepExisting, CancellationToken cancellationToken) { try { if (keepExisting && (await client.GetSequenceAsync(sequenceId, cancellationToken).ConfigureAwait(false)).Title is { Length: > 0 }) return; await client.UpdateSequenceAsync(sequenceId, new UpdateSequenceRequest(title, null, null, null, null, null), cancellationToken).ConfigureAwait(false); } catch (ChronosApiException ex) when (IsRefusal(ex)) { Log.Warning("import", $"The server refused the title of sequence {sequenceId}", ex); } } /// The server refused this file; other files may still go through (unlike a busy server or a lost sign-in). private static bool IsRefusal(ChronosApiException ex) => !ex.IsTransient && ex.Status is not (HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) && (int)ex.Status is >= 400 and < 500; } /// /// The reports of one card import into the server (BRIEF §14.3 cardEvent, §14.4), sent one at a time and in order: /// progress at most once a second, and a progress report still waiting is replaced by the next report, so a slow or /// unreachable server never piles them up. A report that fails is logged and the import goes on; a server without the /// endpoint (404) gets no more reports. Once the import is over its end gets two seconds to reach the server, then /// whatever is still on its way is given up. /// /// Photos of the plan, skipped ones included (like ). public sealed class CardImportReporter(ChronosClient client, Guid importId, string card, int total, TimeProvider? time = null) { private static readonly TimeSpan ProgressInterval = TimeSpan.FromSeconds(1); private static readonly TimeSpan SendTimeout = TimeSpan.FromSeconds(10); private static readonly TimeSpan FinishWait = TimeSpan.FromSeconds(2); private readonly TimeProvider _time = time ?? TimeProvider.System; private readonly CancellationTokenSource _stop = new(); private readonly Lock _gate = new(); private readonly List _queue = []; private Task _sending = Task.CompletedTask; private bool _draining; private bool _finished; private bool _unsupported; private long? _lastProgress; private ImportProgress? _latest; public void Started() => Post(CardImportStates.Started, 0, 0); /// Progress that reaches (the screen's own) and these reports. public IProgress Forward(IProgress? inner) => new Forwarding(this, inner); public void Report(ImportProgress progress) { lock (_gate) { _latest = progress; var now = _time.GetTimestamp(); if (_lastProgress is { } last && _time.GetElapsedTime(last, now) < ProgressInterval) return; _lastProgress = now; } Post(CardImportStates.Progress, progress.ItemsDone, progress.Failed); } /// /// Reports the end: every photo handled when the import gave an , else as far as its progress /// got. Waits at most two seconds for the reports still queued, then stops sending: an import that was cancelled or /// lost its server ends at once instead of waiting for a server that does not answer. /// public async Task FinishedAsync(ImportOutcome? outcome) { int done, failed; Task sending; lock (_gate) { done = outcome is null ? _latest?.ItemsDone ?? 0 : total; failed = outcome?.Failures.Count ?? _latest?.Failed ?? 0; } Post(CardImportStates.Finished, done, failed); lock (_gate) sending = _sending; try { await sending.WaitAsync(FinishWait, _time).ConfigureAwait(false); } catch (TimeoutException) { Log.Info("import", $"The last reports of the import from {card} were given up: the server did not take them in time"); } finally { // Nothing is sent once the import is over: its client goes next. await _stop.CancelAsync().ConfigureAwait(false); } } private void Post(string state, int done, int failed) { var report = new CardImportReport(importId, card, state, Math.Clamp(done, 0, total), total, Math.Clamp(failed, 0, total)); lock (_gate) { if (_finished || _unsupported) return; _finished = state == CardImportStates.Finished; if (_queue.Count > 0 && _queue[^1].State == CardImportStates.Progress) _queue[^1] = report; else _queue.Add(report); if (_draining) return; _draining = true; _sending = Task.Run(DrainAsync); } } private async Task DrainAsync() { while (true) { CardImportReport report; lock (_gate) { if (_queue.Count == 0 || _unsupported || _stop.IsCancellationRequested) { _queue.Clear(); _draining = false; return; } report = _queue[0]; _queue.RemoveAt(0); } await SendAsync(report).ConfigureAwait(false); } } private async Task SendAsync(CardImportReport report) { try { using var timeout = CancellationTokenSource.CreateLinkedTokenSource(_stop.Token); timeout.CancelAfter(SendTimeout); await client.ReportCardAsync(report, timeout.Token).ConfigureAwait(false); } catch (OperationCanceledException) when (_stop.IsCancellationRequested) { // The import is over and did not wait for this one. } catch (ChronosApiException ex) when (ex.Status is HttpStatusCode.NotFound or HttpStatusCode.MethodNotAllowed) { // A server from before card events. lock (_gate) _unsupported = true; Log.Info("import", $"The server takes no card reports ({ex.Error.Code})"); } catch (Exception ex) { // The reports only inform the other clients: nothing about them may stop or fail the import. Log.Warning("import", $"The {report.State} report of the import from {card} did not reach the server", ex); } } private sealed class Forwarding(CardImportReporter reporter, IProgress? inner) : IProgress { public void Report(ImportProgress value) { inner?.Report(value); reporter.Report(value); } } }