Files
Chronos/src/Titano.App/Import/ImportDestinations.cs
T
Claude e97b0e4f84 fix(import): staging failures of a server import name this PC, failed jobs show catalog sentences
A server import stages its copies in this PC's cache folder, yet a full disk
there read 'The destination is full.' beside every photo, and a refused
write 'Titano may not write to the destination.', which sent the user to
the server. IImportSink.StagesOnThisPc (true for ServerImportSink) selects
Import.Failed.CacheFull and Import.Failed.CacheAccess instead.

A failed archive import job showed the job engine's raw text (SQLite, file
system or framework English) as the Import screen's line. ImportFailures.Job
keeps the archive engine's own localized sentences and shows 'The import
stopped before the end.' for anything else; the raw text stays in the
tooltip and the logs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 19:00:51 +00:00

246 lines
12 KiB
C#

using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using Titano.Core.Diagnostics;
using Titano.Core.Import;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Protocol.Client;
namespace Titano.App.Import;
/// <summary>Plans an import into one destination (BRIEF §12.4); nothing is written.</summary>
public interface IImportDestination
{
ImportDestinationKind Kind { get; }
/// <param name="progress">Photos read so far.</param>
Task<ImportPlan> PlanAsync(string sourceRoot, ImportOptions options, Guid? cardId, IProgress<int>? progress, CancellationToken cancellationToken);
}
/// <summary>This PC's archive: the planner knows the card's history, the archive's duplicates and its sequences.</summary>
public sealed class ArchiveDestination(ArchiveService archives, Archive archive) : IImportDestination
{
public Archive Archive { get; } = archive;
public ImportDestinationKind Kind => ImportDestinationKind.Archive;
public Task<ImportPlan> PlanAsync(string sourceRoot, ImportOptions options, Guid? cardId, IProgress<int>? progress, CancellationToken cancellationToken) =>
archives.PlanImportAsync(Archive.Id, sourceRoot, options, cardId, progress, cancellationToken);
}
/// <summary>
/// A plain folder or the server: nothing is indexed on this PC, so every photo is copied; a folder destination finds
/// identical files by name and hash while copying, the server by hash before any byte is sent.
/// </summary>
public sealed class UnindexedDestination(ImportDestinationKind kind, string root) : IImportDestination
{
public ImportDestinationKind Kind { get; } = kind;
/// <summary>The folder, or the local staging folder of server uploads.</summary>
public string Root { get; } = root;
/// <summary>Room left on the server (the user's quota); null when unlimited or unknown. A folder reports its volume's space.</summary>
public long? ServerSpace { get; init; }
public async Task<ImportPlan> PlanAsync(string sourceRoot, ImportOptions options, Guid? cardId, IProgress<int>? progress, CancellationToken cancellationToken)
{
var problems = new List<ImportProblem>();
var files = CardScanner.Find(sourceRoot, options.IncludeSubfolders, cancellationToken);
var photos = CardScanner.ReadMetadata(sourceRoot, files, problems, progress, cancellationToken);
var plan = await ImportPlanner.PlanAsync(sourceRoot, Root, photos, problems, options, NoIndex.Instance, cancellationToken).ConfigureAwait(false);
// Staging only ever holds one batch; the space that matters for a server import is the quota.
return Kind == ImportDestinationKind.Server ? plan with { AvailableBytes = ServerSpace } : plan;
}
/// <summary>No history and no index; folders are reused so a repeated import lands next to the first one.</summary>
private sealed class NoIndex : IImportLookup
{
public static readonly NoIndex Instance = new();
public Task<bool> WasImportedAsync(CardFile file, CancellationToken cancellationToken) => Task.FromResult(false);
public Task<IReadOnlyList<ArchiveMatch>> FindCandidatesAsync(long size, DateTime capturedAt, CancellationToken cancellationToken) =>
Task.FromResult<IReadOnlyList<ArchiveMatch>>([]);
public Task<string?> FindSessionFolderAsync(string sessionKey, CancellationToken cancellationToken) => Task.FromResult<string?>(null);
public bool IsFolderTaken(string folder) => false;
}
}
/// <summary>A failed photo of an import: where it is on the card and why.</summary>
public sealed record ImportFailure(string SourcePath, string Message);
/// <summary>Whether a planned copy reached the archive after an archive import job.</summary>
public enum CommitCheck
{
/// <summary>No frame of the archive is this photo's copy.</summary>
Missing,
/// <summary>The job committed a new frame for the photo.</summary>
Copied,
/// <summary>The job found the same content already at the destination and kept that frame.</summary>
AlreadyThere,
}
/// <summary>
/// What a destination did per card file beyond the executor's commit results (server imports): the files it committed,
/// also those of a batch an abort interrupted, and the files it refused with the reason.
/// </summary>
public sealed class ImportSinkReport
{
public Dictionary<string, SinkCommit> Committed { get; } = new(StringComparer.Ordinal);
public Dictionary<string, string> Rejected { get; } = new(StringComparer.Ordinal);
}
/// <summary>What an import did (BRIEF §12.4 verification result).</summary>
/// <param name="Copied">Copied and committed (verified by read-back when <paramref name="Verified"/>).</param>
/// <param name="Duplicates">Copied, but the destination already held the same content.</param>
/// <param name="Skipped">Not copied: imported before, or already in the archive.</param>
public sealed record ImportOutcome(int Copied, int Duplicates, int Skipped, IReadOnlyList<ImportFailure> Failures, bool Verified)
{
public bool IsComplete => Failures.Count == 0;
/// <summary>Photos the destination now holds a verified copy of.</summary>
public int Imported => Copied + Duplicates;
/// <summary>
/// Totals of an executor run (folder or server destination): the executor's per-file results, corrected by what the
/// sink reported (refused files fail, files committed in a batch an abort interrupted count); after an abort
/// (<paramref name="stopped"/>) every copy not reached fails with that reason.
/// </summary>
public static ImportOutcome From(ImportPlan plan, IEnumerable<ImportItemResult> results, bool verified, ImportSinkReport? report = null,
string? stopped = null)
{
var byPath = new Dictionary<string, ImportItemResult>(StringComparer.Ordinal);
foreach (var r in results) byPath[r.Item.SourcePath] = r;
int copied = 0, duplicates = 0, skipped = 0;
var failures = new List<ImportFailure>();
foreach (var item in plan.Items)
{
if (report?.Rejected.TryGetValue(item.SourcePath, out var refused) == true)
{
failures.Add(new ImportFailure(item.SourcePath, refused));
continue;
}
var status = byPath.TryGetValue(item.SourcePath, out var result) ? result.Status
: report?.Committed.TryGetValue(item.SourcePath, out var commit) == true ? commit.Duplicate ? ImportItemStatus.Duplicate : ImportItemStatus.Copied
: item.Action != ImportAction.Copy ? ImportItemStatus.Skipped
: ImportItemStatus.Failed;
switch (status)
{
case ImportItemStatus.Copied:
copied++;
break;
case ImportItemStatus.Duplicate:
duplicates++;
break;
case ImportItemStatus.Skipped:
skipped++;
break;
default:
failures.Add(new ImportFailure(item.SourcePath, result?.Error ?? stopped ?? Loc.L("Import.Result.NotCommitted")));
break;
}
}
return new ImportOutcome(copied, duplicates, skipped, failures, verified);
}
/// <summary>
/// After an archive import job: each planned copy is matched to its own committed frame (<paramref name="check"/>);
/// a copy that did not arrive shows the job's own error for that file (<paramref name="errors"/>, from its log) or
/// that it was not committed. The copies were hashed and read back while the job ran.
/// </summary>
public static async Task<ImportOutcome> CheckAsync(ImportPlan plan, bool verified, Func<PlannedItem, CancellationToken, Task<CommitCheck>> check,
IReadOnlyDictionary<string, string> errors, CancellationToken cancellationToken)
{
int copied = 0, duplicates = 0, skipped = 0;
var failures = new List<ImportFailure>();
foreach (var item in plan.Items)
{
if (item.Action != ImportAction.Copy)
{
skipped++;
continue;
}
switch (await check(item, cancellationToken).ConfigureAwait(false))
{
case CommitCheck.Copied:
copied++;
break;
case CommitCheck.AlreadyThere:
duplicates++;
break;
default:
failures.Add(new ImportFailure(item.SourcePath,
errors.TryGetValue(item.SourcePath, out var error) ? error : Loc.L("Import.Result.NotCommitted")));
break;
}
}
return new ImportOutcome(copied, duplicates, skipped, failures, verified);
}
/// <summary>
/// The per-file errors an import job wrote to its log ("Not copied: path (error)", in the language it ran in), by card
/// path. The log keeps its last lines only, so older errors fall back to "not committed".
/// </summary>
public static IReadOnlyDictionary<string, string> JobErrors(string? logTail)
{
var errors = new Dictionary<string, string>(StringComparer.Ordinal);
if (string.IsNullOrEmpty(logTail)) return errors;
var line = Regex.Escape(Loc.L("Archive.Log.ImportFailed", "\u0001", "\u0002"))
.Replace("\u0001", "(?<path>.+?)", StringComparison.Ordinal)
.Replace("\u0002", "(?<error>.*)", StringComparison.Ordinal);
var pattern = new Regex("^(?:\\S+ )?" + line + "$", RegexOptions.CultureInvariant);
foreach (var entry in logTail.Split('\n'))
{
if (pattern.Match(entry.TrimEnd('\r')) is { Success: true } match) errors[match.Groups["path"].Value] = match.Groups["error"].Value;
}
return errors;
}
}
/// <summary>
/// Runs a confirmed plan into a folder or the server on this PC (no job handler exists for those destinations): copies
/// with hashing and read-back, commits each batch to the sink and collects the per-file results. A card that leaves or
/// a destination that fails as a whole (disk, connection, sign-in) stops the run; what was done until then is kept in
/// the outcome and every copy not reached fails with the reason.
/// </summary>
public static class LocalImport
{
/// <param name="source">The card or folder as the Devices list names it (a card that leaves is named in each failure).</param>
public static async Task<ImportOutcome> RunAsync(ImportPlan plan, IImportSink sink, string source, bool verify, IProgress<ImportProgress>? progress,
CancellationToken cancellationToken, ImportSinkReport? report = null)
{
var results = new List<ImportItemResult>(plan.FileCount);
string? stopped = null;
try
{
await new ImportExecutor(plan, sink).RunAsync(0, verify, batch =>
{
results.AddRange(batch.Results);
progress?.Report(batch.Progress);
return ValueTask.CompletedTask;
}, p =>
{
progress?.Report(p);
return ValueTask.CompletedTask;
}, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (!cancellationToken.IsCancellationRequested && Stops(ex))
{
Log.Warning("import", $"Import into {plan.DestinationRoot} stopped", ex);
stopped = ImportFailures.Stop(ex, source, sink.StagesOnThisPc);
}
return ImportOutcome.From(plan, results, verify, report, stopped);
}
/// <summary>The card left, or the destination as a whole failed: its disk, the connection, the sign-in or the server (a timeout too).</summary>
private static bool Stops(Exception ex) =>
ex is IOException or UnauthorizedAccessException or HttpRequestException or TitanoApiException or UntrustedCertificateException
or InvalidOperationException or OperationCanceledException;
}