A whole folder imported again to the server sent its folder name as the
sequence title on every run, undoing a rename made on the server. The
sink now sends a typed title as before, but the folder's default name
only to a sequence that has no title yet (it reads the sequence first).
The archive already applied a title only when it created the sequence.
A session that ends on another day, as a whole folder often does,
showed only its first date ("28/09/2026 · 18:02 → 18:05"), which read
as a few minutes on one day. Its plan row and Show plan heading now
show both dates, from two new invariant resources (with the
right-to-left arrow).
The Gap and Minimum boxes now show their tooltip while disabled, so the
reason Whole folder turns them off reaches them, not only the Sessions
check box. Doc comments that said a merge needs the same camera now
mention the whole-folder rule.
Tests: a server whole-folder import run twice keeps the server's rename
and still applies a typed title; multi-day time text in both reading
directions; and the Import view model's Group rules (folder only, cards
planned by sessions, switching drops splits, tooltips say why).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
258 lines
12 KiB
C#
258 lines
12 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 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 <see cref="Report"/> 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.
|
|
/// </summary>
|
|
public sealed class ServerImportSink(ChronosClient client, string stagingDirectory) : IImportSink
|
|
{
|
|
private readonly HashSet<Guid> _titled = [];
|
|
|
|
public string StagingDirectory { get; } = stagingDirectory;
|
|
|
|
/// <summary>The copies wait in this PC's cache folder until they are uploaded: the server's space is its quota.</summary>
|
|
public bool StagesOnThisPc => true;
|
|
|
|
/// <summary>Every file committed or refused so far, by card path; pass it to <see cref="LocalImport.RunAsync"/>.</summary>
|
|
public ImportSinkReport Report { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Runs a confirmed plan into the server through this sink (<see cref="LocalImport.RunAsync"/>) and tells the server's
|
|
/// other clients about it as <paramref name="card"/> (BRIEF §14.3 <c>cardEvent</c>): started, its progress, and
|
|
/// finished, also when it stopped early.
|
|
/// </summary>
|
|
public async Task<ImportOutcome> RunAsync(ImportPlan plan, string card, bool verify, IProgress<ImportProgress>? 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<IReadOnlyList<SinkCommit>> CommitAsync(PlannedSession session, IReadOnlyList<StagedFile> files, CancellationToken cancellationToken)
|
|
{
|
|
var results = new List<SinkCommit>(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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Names the sequence; with <paramref name="keepExisting"/> (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.
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>The server refused this file; other files may still go through (unlike a busy server or a lost sign-in).</summary>
|
|
private static bool IsRefusal(ChronosApiException ex) =>
|
|
!ex.IsTransient && ex.Status is not (HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) && (int)ex.Status is >= 400 and < 500;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The reports of one card import into the server (BRIEF §14.3 <c>cardEvent</c>, §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.
|
|
/// </summary>
|
|
/// <param name="total">Photos of the plan, skipped ones included (like <see cref="ImportProgress.ItemsTotal"/>).</param>
|
|
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<CardImportReport> _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);
|
|
|
|
/// <summary>Progress that reaches <paramref name="inner"/> (the screen's own) and these reports.</summary>
|
|
public IProgress<ImportProgress> Forward(IProgress<ImportProgress>? 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reports the end: every photo handled when the import gave an <paramref name="outcome"/>, 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.
|
|
/// </summary>
|
|
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<ImportProgress>? inner) : IProgress<ImportProgress>
|
|
{
|
|
public void Report(ImportProgress value)
|
|
{
|
|
inner?.Report(value);
|
|
reporter.Report(value);
|
|
}
|
|
}
|
|
}
|