using Titano.Core.Import; using Titano.Core.Model; using Titano.Data.Archiving; namespace Titano.App.Import; /// Plans an import into one destination (BRIEF §12.4); nothing is written. public interface IImportDestination { ImportDestinationKind Kind { get; } /// Photos read so far. Task PlanAsync(string sourceRoot, ImportOptions options, Guid? cardId, IProgress? progress, CancellationToken cancellationToken); } /// This PC's archive: the planner knows the card's history, the archive's duplicates and its sequences. public sealed class ArchiveDestination(ArchiveService archives, Archive archive) : IImportDestination { public Archive Archive { get; } = archive; public ImportDestinationKind Kind => ImportDestinationKind.Archive; public Task PlanAsync(string sourceRoot, ImportOptions options, Guid? cardId, IProgress? progress, CancellationToken cancellationToken) => archives.PlanImportAsync(Archive.Id, sourceRoot, options, cardId, progress, cancellationToken); } /// /// 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. /// public sealed class UnindexedDestination(ImportDestinationKind kind, string root) : IImportDestination { public ImportDestinationKind Kind { get; } = kind; /// The folder, or the local staging folder of server uploads. public string Root { get; } = root; public Task PlanAsync(string sourceRoot, ImportOptions options, Guid? cardId, IProgress? progress, CancellationToken cancellationToken) { var problems = new List(); 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); } /// No history and no index; folders are reused so a repeated import lands next to the first one. private sealed class NoIndex : IImportLookup { public static readonly NoIndex Instance = new(); public Task WasImportedAsync(CardFile file, CancellationToken cancellationToken) => Task.FromResult(false); public Task> FindCandidatesAsync(long size, DateTime capturedAt, CancellationToken cancellationToken) => Task.FromResult>([]); public Task FindSessionFolderAsync(string sessionKey, CancellationToken cancellationToken) => Task.FromResult(null); public bool IsFolderTaken(string folder) => false; } } /// A failed photo of an import: where it is on the card and why. public sealed record ImportFailure(string SourcePath, string Message); /// What an import did (BRIEF §12.4 verification result). /// Copied and committed (verified by read-back when ). /// Copied, but the destination already held the same content. /// Not copied: imported before, or already in the archive. public sealed record ImportOutcome(int Copied, int Duplicates, int Skipped, IReadOnlyList Failures, bool Verified) { public bool IsComplete => Failures.Count == 0; /// Photos the destination now holds a verified copy of. public int Imported => Copied + Duplicates; /// Totals of an executor run (folder or server destination). public static ImportOutcome From(IEnumerable results, bool verified) { int copied = 0, duplicates = 0, skipped = 0; var failures = new List(); 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); } /// /// 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. /// public static async Task CheckAsync(ImportPlan plan, bool verified, Func> isInArchive, CancellationToken cancellationToken) { int copied = 0, skipped = 0; var failures = new List(); 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); } } /// /// 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. /// public static class LocalImport { public static async Task RunAsync(ImportPlan plan, IImportSink sink, bool verify, IProgress? progress, CancellationToken cancellationToken) { var results = new List(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); } }