Plain classes behind the Import and Archive screens: plan totals, session rows and the Show plan listing; duplicate and title choices without a new scan; folder and server destinations with the protocol client's chunked, hash-verified uploads; the after-import gate that allows deleting or formatting a card only after a verified archive import and a dry run; the archive tree, chart aggregation, mass rename/retag and maintenance reports. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
148 lines
6.6 KiB
C#
148 lines
6.6 KiB
C#
using Titano.Core.Import;
|
|
using Titano.Core.Model;
|
|
using Titano.Data.Archiving;
|
|
|
|
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;
|
|
|
|
public 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);
|
|
return ImportPlanner.PlanAsync(sourceRoot, Root, photos, problems, options, NoIndex.Instance, cancellationToken);
|
|
}
|
|
|
|
/// <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>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).</summary>
|
|
public static ImportOutcome From(IEnumerable<ImportItemResult> results, bool verified)
|
|
{
|
|
int copied = 0, duplicates = 0, skipped = 0;
|
|
var failures = new List<ImportFailure>();
|
|
foreach (var r in results)
|
|
{
|
|
switch (r.Status)
|
|
{
|
|
case ImportItemStatus.Copied:
|
|
copied++;
|
|
break;
|
|
case ImportItemStatus.Duplicate:
|
|
duplicates++;
|
|
break;
|
|
case ImportItemStatus.Skipped:
|
|
skipped++;
|
|
break;
|
|
default:
|
|
failures.Add(new ImportFailure(r.Item.SourcePath, r.Error ?? string.Empty));
|
|
break;
|
|
}
|
|
}
|
|
return new ImportOutcome(copied, duplicates, skipped, failures, verified);
|
|
}
|
|
|
|
/// <summary>
|
|
/// After an archive import job: every planned copy must now be a frame of the archive (found by size and capture
|
|
/// time, the planner's own duplicate key); the copy was hashed and read back while the job ran.
|
|
/// </summary>
|
|
public static async Task<ImportOutcome> CheckAsync(ImportPlan plan, bool verified, Func<PlannedItem, CancellationToken, Task<bool>> isInArchive,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
int copied = 0, skipped = 0;
|
|
var failures = new List<ImportFailure>();
|
|
foreach (var item in plan.Items)
|
|
{
|
|
if (item.Action != ImportAction.Copy)
|
|
{
|
|
skipped++;
|
|
continue;
|
|
}
|
|
if (await isInArchive(item, cancellationToken).ConfigureAwait(false)) copied++;
|
|
else failures.Add(new ImportFailure(item.SourcePath, Core.Localization.Loc.L("Import.Result.NotCommitted")));
|
|
}
|
|
return new ImportOutcome(copied, 0, skipped, failures, verified);
|
|
}
|
|
}
|
|
|
|
/// <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.
|
|
/// </summary>
|
|
public static class LocalImport
|
|
{
|
|
public static async Task<ImportOutcome> RunAsync(ImportPlan plan, IImportSink sink, bool verify, IProgress<ImportProgress>? progress,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var results = new List<ImportItemResult>(plan.FileCount);
|
|
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);
|
|
return ImportOutcome.From(results, verify);
|
|
}
|
|
}
|