diff --git a/i18n/en/Import.resx b/i18n/en/Import.resx index 1b9a34e4..1070093f 100644 --- a/i18n/en/Import.resx +++ b/i18n/en/Import.resx @@ -14,6 +14,11 @@ Format card Keep card Available once the card is remembered.long + Available once a plan has read the card's camera.long + Only for a card imported into this PC's archive.long + The import is copying; its plan is fixed.long + Only imports into this PC's archive are analysed here.long + Only a remembered card has a profile.long Formatting needs a complete import with verified copies.long Create or adopt an archive first (Library or Settings).long Only for a remembered card that is inserted.long @@ -40,6 +45,8 @@ Format {0}?long {0:N0} photo was copied and verified. Formatting removes everything on the card.long {0:N0} photos were copied and verified. Formatting removes everything on the card.long + {0:N0} other file on the card was never imported and would be lost:long + {0:N0} other files on the card were never imported and would be lost:long Keep card Keep Type the card name to confirmlong @@ -57,11 +64,13 @@ {0:N0} file {0:N0} files Session {0} · {1}long + Split here Plan Cannot read {0}: {1}long {0} · not insertedlong {0} #{1}invariant {0} · {1} used + {0} · {1} used · {2} last import {0} no DCIM · {0} free serial {0} @@ -81,8 +90,11 @@ Checking {0:N0} photos Delete {0:N0} photo… Delete {0:N0} photos… + Delete photos… {0:N0} kept {0:N0} kept + {0:N0} other file not importedlong + {0:N0} other files not importedlong {0} · {1}invariant the archive copy differs the archive copy is missing @@ -114,6 +126,7 @@ Show plan {0} missing Plan + Camera profile Copy settings from Edit Forget card… @@ -157,6 +170,7 @@ Session Size {0} · {1}invariant + Merge New · {0}long new Server @@ -173,6 +187,7 @@ {0:N0} photos read Importing with its saved settingslong Cancel + {0} changed since it was checkedlong The card check stoppedlong Deleting from the card stoppedlong {0:N0} photo deleted from the cardlong @@ -186,9 +201,11 @@ {0} formattedlong Import {0} insertedlong + New card inserted · {0}long Resume The interrupted import can continuelong Importing {0}long + not yet known to Titanolong Aperture Camera name Capture date diff --git a/i18n/en/Tooltips.resx b/i18n/en/Tooltips.resx index 460744d5..0ab3d0ba 100644 --- a/i18n/en/Tooltips.resx +++ b/i18n/en/Tooltips.resx @@ -12,17 +12,26 @@ Open the public releases page in the browser Every language but English is machine-translated; propose a better wording on the public issue tracker Titano uses no third-party code: everything beyond .NET and Windows is written in-house - Index an existing folder tree in place; nothing is moved or renamed + Index an existing folder tree in place, or copy it into the archive layout + Copy a folder tree into the archive layout with the naming templates; the Import screen shows the plan first + Index an existing folder tree in place; nothing is moved or renamed The archive folder cannot be reached; the index stays as last seen The archive folder is reachable and writable Readable but not writable: browse only + Write a consistent copy of the index database to the backups folder; the last three are kept Photos per camera; the smallest are grouped + Stop the search; nothing has changed + Show every sequence of the selection again Close without changing anything · Esc Hide the sequences of this year Show the folder of the first copy in Explorer Open the sequence of the first copy in the editor Find identical photos across sequences by hash · reads the index only Show the sequences of this year + Keep only the sequences of one camera + Choose this value; choose it again to clear it + Keep only the sequences in one state + Keep only the sequences with one tag Queue the light analysis of these sequences Find the duplicate groups Find files and rows that do not match @@ -35,7 +44,7 @@ Open the job history Keep the index row Last incremental scan: when it finished and how long it took - Leases, temporary files, thumbnail packs and duplicate flags; relink a moved archive + Stale leases, temporary files, thumbnail packs and duplicate flags; database backup; relink a moved archive List what maintenance would change, without changing it Rebuild every proxy of the archive · runs in the background Clean stale leases and temporary files, compact thumbnail packs, mark duplicates · originals untouched @@ -56,7 +65,7 @@ Make exactly these changes to folders, sidecars and index · resumes after a crash Change the tags of these sequences in sidecars and index Incremental scan: new, changed and removed files by modification time and size - The selected years and sequences; nothing selected means all + The selected years and sequences (all when nothing is selected), narrowed by camera, status or tag Run the deep analysis on every sequence in the set Export the set as CSV: one row per sequence Move the set into a new folder layout · dry run first @@ -70,6 +79,7 @@ Tags to remove, separated by commas Title template; the title token is the current title · checked as you type Re-hash every original and report bit rot · reads the whole archive + {0} · Right arrow opens the year, Left arrow closes it Dismiss Open the folder with the log files Return to the advisor's value @@ -96,6 +106,7 @@ Copy into a folder of your choice; not indexed unless you adopt it later Folder used when the destination is Folder Folder template · checked as you type + Use the settings shared by every {0} card; changes made here apply to all of them Remove this card's nickname, settings and history; photos stay Open the Windows format dialog after a typed confirmation After the import, offer the Windows format dialog once every copy is verified @@ -107,9 +118,11 @@ Keep this card remembered This photo stays on the card Show the imported sequences in the Library + Join this session to session {0} of the same camera; they become one sequence Groups smaller than this join the previous session Plan this card again Name of this card in Titano · Enter saves + Files on the card that are not photos; they were never imported Open Import with this card selected Look again for cards and folders with a DCIM tree Change the nickname of this card @@ -122,6 +135,7 @@ Show every file, where it goes and what the plan resolved Leave photos already in the archive (same hash) on the card Free space at the destination after the import; on a server, what your quota leaves + Start a new session at this photo; it becomes its own sequence Copy with verification; the card is only touched after every hash matches · Ctrl+Enter Look inside every subfolder of the source Insert a token into the template used last diff --git a/i18n/it/Import.it.resx b/i18n/it/Import.it.resx index 02ead40b..ca5e1d47 100644 --- a/i18n/it/Import.it.resx +++ b/i18n/it/Import.it.resx @@ -14,6 +14,11 @@ Formatta scheda Mantieni scheda Disponibile quando la scheda è memorizzata. + Disponibile quando un piano ha letto la fotocamera della scheda. + Solo per una scheda importata nell'archivio di questo PC. + L'importazione sta copiando: il suo piano non cambia più. + Qui si analizzano solo le importazioni nell'archivio di questo PC. + Solo una scheda memorizzata ha un profilo. Per formattare serve un'importazione completa con copie verificate. Crea o adotta prima un archivio (Libreria o Impostazioni). Solo per una scheda nota e inserita. @@ -40,6 +45,8 @@ Formattare {0}? {0:N0} foto è stata copiata e verificata. La formattazione cancella tutto il contenuto della scheda. {0:N0} foto sono state copiate e verificate. La formattazione cancella tutto il contenuto della scheda. + {0:N0} altro file sulla scheda non è mai stato importato e andrebbe perso: + {0:N0} altri file sulla scheda non sono mai stati importati e andrebbero persi: Mantieni scheda Mantieni Scrivi il nome della scheda per confermare @@ -57,10 +64,12 @@ {0:N0} file {0:N0} file Sessione {0} · {1} + Dividi qui Piano Impossibile leggere {0}: {1} {0} · non inserita {0} · {1} occupati + {0} · {1} occupati · {2} ultima importazione {0} senza DCIM · {0} liberi seriale {0} @@ -80,8 +89,11 @@ Verifica di {0:N0} foto Elimina {0:N0} foto… Elimina {0:N0} foto… + Elimina foto… {0:N0} conservata {0:N0} conservate + {0:N0} altro file non importato + {0:N0} altri file non importati la copia in archivio è diversa manca la copia in archivio verificata @@ -103,7 +115,7 @@ circa {0} {0:N0} doppione {0:N0} doppioni - {0} liberi dopo l'import + {0} liberi dopo l'importazione {0:N0} già importata {0:N0} già importate {0:N0} sessione @@ -111,6 +123,7 @@ Mostra piano mancano {0} Piano + Profilo fotocamera Copia impostazioni da Modifica Dimentica scheda… @@ -146,12 +159,13 @@ Sottocartelle Verifica hash Si aggiunge a · {0} - aggiunge + esistente Fotocamera Fotogrammi · intervallo Sequenza Sessione Dimensione + Unisci Nuova · {0} nuova Server @@ -167,6 +181,7 @@ {0:N0} foto lette Importazione con le impostazioni salvate Annulla + {0} è cambiata dopo il controllo Controllo della scheda interrotto Eliminazione dalla scheda interrotta {0:N0} foto eliminata dalla scheda @@ -180,9 +195,11 @@ {0} formattata Importa {0} inserita + Nuova scheda inserita · {0} Riprendi L'importazione interrotta può continuare Importazione di {0} + ancora sconosciuta a Titano Diaframma Nome fotocamera Data di scatto diff --git a/i18n/it/Tooltips.it.resx b/i18n/it/Tooltips.it.resx index ea3bf870..854b4914 100644 --- a/i18n/it/Tooltips.it.resx +++ b/i18n/it/Tooltips.it.resx @@ -12,17 +12,26 @@ Apri la pagina pubblica delle versioni nel browser Tutte le lingue tranne l'inglese sono tradotte automaticamente; proponi una formulazione migliore nel tracker pubblico Titano non usa codice di terze parti: tutto ciò che va oltre .NET e Windows è scritto internamente - Indicizza sul posto una cartella esistente; nulla viene spostato o rinominato + Indicizza sul posto una cartella esistente o copiala nella struttura dell'archivio + Copia una cartella nella struttura dell'archivio con i modelli dei nomi; la schermata Importa mostra prima il piano + Indicizza sul posto una cartella esistente; nulla viene spostato o rinominato La cartella dell'archivio non è raggiungibile; l'indice resta com'era La cartella dell'archivio è raggiungibile e scrivibile Leggibile ma non scrivibile: solo consultazione + Scrive una copia coerente del database dell'indice nella cartella delle copie; si tengono le ultime tre Foto per fotocamera; le minori sono raggruppate + Interrompi la ricerca; nulla è stato modificato + Mostra di nuovo tutte le sequenze della selezione Chiudi senza modificare nulla · Esc Nascondi le sequenze di quest'anno Mostra in Esplora risorse la cartella della prima copia Apri nell'editor la sequenza della prima copia Trova foto identiche tra le sequenze tramite hash · legge solo l'indice Mostra le sequenze di quest'anno + Tieni solo le sequenze di una fotocamera + Scegli questo valore; sceglilo di nuovo per toglierlo + Tieni solo le sequenze in uno stato + Tieni solo le sequenze con un tag Metti in coda l'analisi rapida di queste sequenze Trova i gruppi di doppioni Trova file e righe che non corrispondono @@ -35,10 +44,10 @@ Apri la cronologia delle attività Mantieni la riga dell'indice Ultima scansione incrementale: quando è finita e quanto è durata - Lease, file temporanei, pacchetti di miniature e segni dei doppioni; ricollega un archivio spostato + Blocchi scaduti, file temporanei, pacchetti di miniature e segni dei doppioni; copia del database; ricollega un archivio spostato Elenca cosa cambierebbe la manutenzione, senza cambiarlo Ricrea tutti i proxy dell'archivio · in background - Pulisce lease scaduti e file temporanei, compatta le miniature, segna i doppioni · originali intatti + Pulisce i blocchi scaduti e i file temporanei, compatta le miniature, segna i doppioni · originali intatti Stato dell'indice, doppioni, orfani e manutenzione recente Segna ogni copia dopo la prima perché i render la saltino · esegue la manutenzione Foto al mese per data del primo scatto @@ -56,20 +65,21 @@ Applica esattamente queste modifiche a cartelle, sidecar e indice · riprende dopo un arresto Cambia i tag di queste sequenze in sidecar e indice Scansione incrementale: file nuovi, modificati e rimossi per data e dimensione - Anni e sequenze selezionati; senza selezione, tutte - Esegui l'analisi profonda su ogni sequenza dell'insieme - Esporta l'insieme in CSV: una riga per sequenza - Sposta l'insieme in una nuova struttura di cartelle · prima la prova + Anni e sequenze selezionati (tutti se nulla è selezionato), ristretti per fotocamera, stato o tag + Esegui l'analisi profonda su ogni sequenza della selezione + Esporta la selezione in CSV: una riga per sequenza + Sposta la selezione in una nuova struttura di cartelle · prima la simulazione Crea o aggiorna i proxy - Rinomina i titoli dell'insieme con un modello · prima la prova - Aggiungi o rimuovi tag su tutto l'insieme + Rinomina i titoli della selezione con un modello · prima la simulazione + Aggiungi o rimuovi tag su tutta la selezione Crea o aggiorna le miniature - Ricalcola l'hash degli originali dell'insieme + Ricalcola l'hash degli originali della selezione Spazio degli originali per anno di scatto Tag da aggiungere, separati da virgole Tag da rimuovere, separati da virgole Modello del titolo; il segnaposto title è il titolo attuale · controllato mentre scrivi Ricalcola l'hash di ogni originale e segnala i danni · legge tutto l'archivio + {0} · Freccia destra apre l'anno, freccia sinistra lo chiude Chiudi Apri la cartella dei file di log Torna al valore consigliato @@ -96,6 +106,7 @@ Copia in una cartella a scelta; non indicizzata finché non la adotti Cartella usata quando la destinazione è Cartella Modello della cartella · controllato mentre scrivi + Usa le impostazioni condivise da tutte le schede della {0}; le modifiche fatte qui valgono per tutte Rimuovi soprannome, impostazioni e cronologia della scheda; le foto restano Apri la formattazione di Windows dopo una conferma scritta Dopo l'importazione, proponi la formattazione di Windows quando ogni copia è verificata @@ -107,9 +118,11 @@ Mantieni la scheda in memoria Questa foto resta sulla scheda Mostra le sequenze importate nella Libreria + Unisci questa sessione alla sessione {0} della stessa fotocamera: diventano una sola sequenza I gruppi più piccoli si uniscono alla sessione precedente Pianifica di nuovo questa scheda Nome della scheda in Titano · Invio salva + File sulla scheda che non sono foto: non sono mai stati importati Apri Importa con questa scheda selezionata Cerca di nuovo schede e cartelle con DCIM Cambia il nome della scheda @@ -122,6 +135,7 @@ Mostra ogni file, dove andrà e cosa ha risolto il piano Lascia sulla scheda le foto già in archivio (stesso hash) Spazio libero nella destinazione dopo l'importazione; su un server, quanto resta della tua quota + Inizia una nuova sessione da questa foto: diventa una sequenza a sé Copia con verifica; la scheda si tocca solo dopo che ogni hash coincide · Ctrl+Invio Cerca in ogni sottocartella della sorgente Inserisci un segnaposto nell'ultimo modello usato diff --git a/src/Titano.App/Import/ArchiveImportCheck.cs b/src/Titano.App/Import/ArchiveImportCheck.cs new file mode 100644 index 00000000..a22406f5 --- /dev/null +++ b/src/Titano.App/Import/ArchiveImportCheck.cs @@ -0,0 +1,47 @@ +using Titano.Core.Import; +using Titano.Core.Model; +using Titano.Data; + +namespace Titano.App.Import; + +/// +/// The verification result of an archive import job (BRIEF §12.4): every planned copy matched to its own committed frame, +/// and the job's own error text for the copies that did not arrive. +/// +public static class ArchiveImportCheck +{ + /// The card the job recorded its history for; null for a folder source. + public static async Task CheckAsync(TitanoDatabase db, Guid archiveId, ImportPlan plan, Job job, Guid? cardId, bool verified, + CancellationToken cancellationToken) + { + var errors = ImportOutcome.JobErrors(job.LogTail); + if (cardId is { } card) + { + // A card import records each committed file with the job and its frame. + var records = (await db.MediaCards.ListImportsByJobAsync(card, job.Id, cancellationToken).ConfigureAwait(false)) + .Where(r => r.FrameId is not null) + .ToDictionary(r => (r.Key.Path, r.Key.Size)); + return await ImportOutcome.CheckAsync(plan, verified, (item, _) => Task.FromResult( + !records.TryGetValue((item.SourcePath, item.Size), out var record) ? CommitCheck.Missing + : record.FrameId == item.ExistingFrameId ? CommitCheck.AlreadyThere + : CommitCheck.Copied), errors, cancellationToken).ConfigureAwait(false); + } + + // A folder has no import history: the copy is a frame of its session's sequence with the photo's size, capture time + // and (when the plan hashed it) content, other than the frame it duplicates. + var sessionOf = plan.Sessions.SelectMany(s => s.Items.Select(i => (i.SourcePath, s.SessionKey))).ToDictionary(x => x.SourcePath, x => x.SessionKey); + var sequences = new Dictionary(StringComparer.Ordinal); + return await ImportOutcome.CheckAsync(plan, verified, async (item, ct) => + { + var key = sessionOf[item.SourcePath]; + if (!sequences.TryGetValue(key, out var sequence)) + sequences[key] = sequence = await db.Sequences.FindBySessionKeyAsync(archiveId, key, ct).ConfigureAwait(false); + var matches = (await db.Frames.FindBySizeAndCaptureAsync(archiveId, item.Size, item.CapturedAt, ct).ConfigureAwait(false)) + .Where(f => f.SequenceId == sequence && (item.Sha256 is null || string.Equals(f.Sha256, item.Sha256, StringComparison.OrdinalIgnoreCase))) + .ToList(); + return matches.Any(f => f.Id != item.ExistingFrameId) ? CommitCheck.Copied + : matches.Count > 0 ? CommitCheck.AlreadyThere + : CommitCheck.Missing; + }, errors, cancellationToken).ConfigureAwait(false); + } +} diff --git a/src/Titano.App/Import/CameraProfiles.cs b/src/Titano.App/Import/CameraProfiles.cs new file mode 100644 index 00000000..66801ec4 --- /dev/null +++ b/src/Titano.App/Import/CameraProfiles.cs @@ -0,0 +1,57 @@ +using System.Text.Json.Serialization; +using Titano.Core.Model; +using Titano.Data; +using Titano.Data.Archiving; +using Titano.Data.Repositories; + +namespace Titano.App.Import; + +/// +/// A camera profile (BRIEF §13): import settings shared by every card of one camera model. The cards in +/// follow it: they import with these settings, and an import started from any of them saves its +/// settings here for all of them. +/// +public sealed record CameraProfile(string? Make, string Model, CardProfile Profile, IReadOnlyList Cards) +{ + public bool Follows(Guid cardId) => Cards.Contains(cardId); + + public CameraProfile With(Guid cardId) => Follows(cardId) ? this : this with { Cards = [.. Cards, cardId] }; + + public CameraProfile Without(Guid cardId) => this with { Cards = [.. Cards.Where(c => c != cardId)] }; +} + +/// The settings a card imports with, and whether they are its camera's shared profile. +public sealed record ResolvedProfile(CardProfile Profile, bool FollowsCamera); + +public static class CameraProfiles +{ + /// + /// A card follows its camera's profile when it joined it, or when it has no settings of its own yet (a new card for a + /// known camera needs no setup, and joins); otherwise it keeps its own settings, and a card without any starts from + /// . + /// + public static ResolvedProfile Resolve(MediaCard card, CameraProfile? camera, CardProfile defaults) => + camera is not null && (camera.Follows(card.Id) || card.ProfileJson is not { Length: > 0 }) + ? new ResolvedProfile(camera.Profile, true) + : new ResolvedProfile(card.ProfileJson is { Length: > 0 } ? ImportService.ProfileOf(card) : defaults, false); +} + +/// Camera profiles in the index's application settings, one per camera model. +public sealed class CameraProfileStore(TitanoDatabase db) +{ + private const string KeyPrefix = "import.cameraProfile."; + + /// The profile of a camera; null when the camera is unknown or has none. + public async Task FindAsync(string? make, string? model, CancellationToken cancellationToken = default) => + model is null ? null : await db.Settings.GetAsync(SettingScope.App, null, Key(make, model), CameraProfileJson.Default.CameraProfile, cancellationToken) + .ConfigureAwait(false); + + public Task SaveAsync(CameraProfile profile, CancellationToken cancellationToken = default) => + db.Settings.SetAsync(SettingScope.App, null, Key(profile.Make, profile.Model), profile, CameraProfileJson.Default.CameraProfile, cancellationToken); + + private static string Key(string? make, string model) => KeyPrefix + (make ?? string.Empty).Trim().ToLowerInvariant() + "|" + model.Trim().ToLowerInvariant(); +} + +[JsonSourceGenerationOptions(UseStringEnumConverter = true)] +[JsonSerializable(typeof(CameraProfile))] +internal sealed partial class CameraProfileJson : JsonSerializerContext; diff --git a/src/Titano.App/Import/CardActions.cs b/src/Titano.App/Import/CardActions.cs index d4f9f8a5..2fecded4 100644 --- a/src/Titano.App/Import/CardActions.cs +++ b/src/Titano.App/Import/CardActions.cs @@ -1,4 +1,5 @@ using Titano.App.Infrastructure; +using Titano.Core.Archiving; using Titano.Core.Import; using Titano.Core.Localization; @@ -7,12 +8,13 @@ namespace Titano.App.Import; /// /// What may be done to the card after an import (BRIEF §0.2 rule 8, §12.4, §13). Deleting needs a recorded, /// verified import into this PC's archive and a dry run that re-hashed every card file and its copy; formatting -/// needs, on top, a complete verified import and a dry run in which every photo still on the card is safe to delete. -/// Ejecting only needs a removable card. Each refusal comes with the sentence the button's tooltip shows. +/// needs, on top, a complete verified import and a dry run in which every photo on the card is safe to delete; files +/// that are not photos (videos, camera folders) never block it but are named in its confirmation. Ejecting only needs +/// a removable card. Each refusal comes with the sentence the button's tooltip shows. /// -/// Photos on the card now (the scanner's count), for the format check. +/// Every file on the card when the dry run ran; null until then. public sealed record CardActions(ImportDestinationKind Destination, bool IsKnownCard, bool IsInserted, bool IsRemovable, ImportOutcome? Outcome, - ErasePlan? Erase, int CardPhotos) + ErasePlan? Erase, CardSnapshot? Card) { /// The dry run of "Delete imported" may start. public bool CanCheck => CheckBlocker is null; @@ -26,14 +28,14 @@ public sealed record CardActions(ImportDestinationKind Destination, bool IsKnown public bool CanDelete => DeleteBlocker is null; public string? DeleteBlocker => - CheckBlocker ?? (Erase is null ? Loc.L("Import.Blocked.NoDryRun") : DeleteCount == 0 ? Loc.L("Import.Blocked.NothingVerified") : null); + CheckBlocker ?? (Erase is null || Card is null ? Loc.L("Import.Blocked.NoDryRun") : DeleteCount == 0 ? Loc.L("Import.Blocked.NothingVerified") : null); public bool CanFormat => FormatBlocker is null; public string? FormatBlocker => DeleteBlocker ?? (Outcome is not { IsComplete: true, Verified: true } ? Loc.L("Import.Blocked.Incomplete") - : KeptCount > 0 || DeleteCount < CardPhotos ? Loc.L("Import.Blocked.NotAllVerified") + : KeptCount > 0 || DeleteCount < Card!.Photos ? Loc.L("Import.Blocked.NotAllVerified") : null); public bool CanEject => IsInserted && IsRemovable; @@ -44,6 +46,17 @@ public sealed record CardActions(ImportDestinationKind Destination, bool IsKnown public int KeptCount => Erase?.Items.Count(i => i.Verdict != EraseVerdict.Delete) ?? 0; + /// Files on the card that are not verified photos (videos, camera folders): formatting would lose them. + public IReadOnlyList NotImported + { + get + { + if (Card is null || Erase is null) return []; + var verified = Erase.ToDelete.Select(i => i.Candidate.SourcePath).ToHashSet(StringComparer.OrdinalIgnoreCase); + return [.. Card.Files.Keys.Where(p => !verified.Contains(p) && !ArchiveScanner.IsPhoto(p)).Order(StringComparer.OrdinalIgnoreCase)]; + } + } + /// "4,812 verified on the card · 96.2 GB · 2 kept": the dry run in one line; empty before it ran. public string EraseText => Erase is null ? string.Empty @@ -52,6 +65,7 @@ public sealed record CardActions(ImportDestinationKind Destination, bool IsKnown Loc.Plural("Import.Erase.Verified", DeleteCount), Formatting.Bytes(DeleteBytes), KeptCount > 0 ? Loc.Plural("Import.Erase.Kept", KeptCount) : string.Empty, + NotImported.Count > 0 ? Loc.Plural("Import.Erase.Other", NotImported.Count) : string.Empty, }.Where(t => t.Length > 0)); /// Why each kept photo stays on the card ("IMG_0042.CR2 · the archive copy no longer matches"). diff --git a/src/Titano.App/Import/CardPool.cs b/src/Titano.App/Import/CardPool.cs index 8882011f..75e4a611 100644 --- a/src/Titano.App/Import/CardPool.cs +++ b/src/Titano.App/Import/CardPool.cs @@ -1,6 +1,10 @@ using System.Globalization; +using System.IO; using Titano.App.Infrastructure; +using Titano.Core.Diagnostics; +using Titano.Core.Import; using Titano.Core.Localization; +using Titano.Core.Metadata; using Titano.Core.Model; using Titano.Core.Naming; @@ -18,11 +22,36 @@ public enum SourceKind Folder, } +/// What an inserted card holds: its photos and the camera of the first one (Devices list, insertion toast). +public sealed record CardContents(int Photos, string? Camera) +{ + /// Lists the card's photos (no metadata but the first photo's); blocks for a directory walk. + public static CardContents Read(string root, CancellationToken cancellationToken = default) + { + var photos = CardScanner.Find(root, includeSubfolders: true, cancellationToken); + string? camera = null; + if (photos.Count > 0) + { + try + { + var first = MetadataReader.Read(Path.Combine(root, photos[0].RelativePath), cancellationToken); + camera = PathTemplate.CameraName(first.Make, first.Model) is { Length: > 0 } name ? name : null; + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("import", $"Cannot read the first photo of {root}", ex); + } + } + return new CardContents(photos.Count, camera); + } +} + /// /// One entry of the Devices list (BRIEF §12.4, §13 card pool): an inserted volume joined with its remembered card, a /// remembered card that is not inserted, or a picked folder. is the card identity or the folder path. /// -public sealed record ImportSource(string Key, SourceKind Kind, CardDevice? Device, MediaCard? Card, string? FolderPath) +/// What an inserted card holds, once listed. +public sealed record ImportSource(string Key, SourceKind Kind, CardDevice? Device, MediaCard? Card, string? FolderPath, CardContents? Contents = null) { public bool IsInserted => Device is not null || FolderPath is not null; @@ -36,11 +65,13 @@ public sealed record ImportSource(string Key, SourceKind Kind, CardDevice? Devic : Device is { Label.Length: > 0 } labelled ? labelled.Label : Device?.Root ?? FolderPath ?? Card?.Label ?? string.Empty; - /// "128 GB · 96.2 GB used", "no DCIM · 1.8 TB free", "Canon EOS R5 · not inserted". + /// "128 GB · 96.2 GB used · 4,812 photos", "no DCIM · 1.8 TB free", "Canon EOS R5 · not inserted". public string Subtitle => Kind switch { SourceKind.Folder => FolderPath ?? string.Empty, SourceKind.Drive => Loc.L("Import.Device.NoDcim", Formatting.Bytes(Device!.FreeBytes)), + _ when Device is not null && Contents is { } contents => Loc.L("Import.Device.CardPhotos", Formatting.Bytes(Device.TotalBytes), + Formatting.Bytes(Device.UsedBytes), Loc.Plural("Shell.Status.Photos", contents.Photos)), _ when Device is not null => Loc.L("Import.Device.Card", Formatting.Bytes(Device.TotalBytes), Formatting.Bytes(Device.UsedBytes)), _ => Loc.L("Import.Device.Away", CameraText(Card) is { Length: > 0 } camera ? camera : Formatting.Bytes(Card!.Capacity)), }; @@ -55,6 +86,19 @@ public sealed record ImportSource(string Key, SourceKind Kind, CardDevice? Devic _ => "Import.Device.State.New", }); + /// "Canon EOS R5 · 4,812 photos": the insertion toast's line; "not yet known to Titano" for a new card. + public string ToastText + { + get + { + var parts = new List(); + if ((CameraText(Card) is { Length: > 0 } known ? known : Contents?.Camera) is { } camera) parts.Add(camera); + if (Contents is { } contents) parts.Add(Loc.Plural("Shell.Status.Photos", contents.Photos)); + if (!IsKnown) parts.Add(Loc.L("Import.Toast.Unknown")); + return string.Join(" · ", parts); + } + } + /// "128 GB · serial 3F2A-9C11 · Canon EOS R5 #0431": the identity line of the profile header. public string IdentityText { @@ -83,7 +127,9 @@ public sealed record ImportSource(string Key, SourceKind Kind, CardDevice? Devic /// Builds the Devices list: inserted volumes first (in drive order), then remembered cards, then a picked folder. public static class CardPool { - public static IReadOnlyList Build(IEnumerable devices, IEnumerable cards, string? folder) + /// What the inserted cards hold, by card identity, as far as listed. + public static IReadOnlyList Build(IEnumerable devices, IEnumerable cards, string? folder, + IReadOnlyDictionary? contents = null) { var known = cards.ToDictionary(c => c.Identity, StringComparer.Ordinal); var list = new List(); @@ -91,7 +137,7 @@ public static class CardPool { known.Remove(device.Identity, out var card); var kind = device.HasDcim || card is not null ? SourceKind.Card : SourceKind.Drive; - list.Add(new ImportSource(device.Identity, kind, device, card, null)); + list.Add(new ImportSource(device.Identity, kind, device, card, null, contents?.GetValueOrDefault(device.Identity))); } foreach (var card in known.Values.OrderByDescending(c => c.LastSeen)) list.Add(new ImportSource(card.Identity, SourceKind.Card, null, card, null)); diff --git a/src/Titano.App/Import/CardSnapshot.cs b/src/Titano.App/Import/CardSnapshot.cs new file mode 100644 index 00000000..ec2b70bc --- /dev/null +++ b/src/Titano.App/Import/CardSnapshot.cs @@ -0,0 +1,41 @@ +using System.IO; +using Titano.Core.Archiving; + +namespace Titano.App.Import; + +/// A card file's size and write time. +public readonly record struct CardFileStamp(long Size, DateTime ModifiedUtc); + +/// +/// Every file on a card when its dry run ran (BRIEF §0.2 rule 8), hidden and system files aside: the format gate +/// compares it with the verified photos, and formatting refuses a card that changed since — another card, a photo shot +/// meanwhile, a file touched. +/// +public sealed record CardSnapshot(string Identity, IReadOnlyDictionary Files) +{ + private static readonly EnumerationOptions Listing = new() + { + IgnoreInaccessible = true, + RecurseSubdirectories = true, + AttributesToSkip = FileAttributes.ReparsePoint | FileAttributes.System | FileAttributes.Hidden, + }; + + /// Lists the card below (forward-slash paths, as the import history keys them). + public static CardSnapshot Take(string root, string identity, CancellationToken cancellationToken = default) + { + var files = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var file in new DirectoryInfo(root).EnumerateFiles("*", Listing)) + { + cancellationToken.ThrowIfCancellationRequested(); + files[Path.GetRelativePath(root, file.FullName).Replace('\\', '/')] = new CardFileStamp(file.Length, file.LastWriteTimeUtc); + } + return new CardSnapshot(identity, files); + } + + /// Photos on the card, wherever they are. + public int Photos => Files.Keys.Count(p => ArchiveScanner.IsPhoto(p)); + + /// True when is the same card holding exactly the same files, unchanged. + public bool IsUnchanged(CardSnapshot now) => + Identity == now.Identity && Files.Count == now.Files.Count && Files.All(f => now.Files.TryGetValue(f.Key, out var stamp) && stamp == f.Value); +} diff --git a/src/Titano.App/Import/ImportChoices.cs b/src/Titano.App/Import/ImportChoices.cs index bcc62d93..c34c7345 100644 --- a/src/Titano.App/Import/ImportChoices.cs +++ b/src/Titano.App/Import/ImportChoices.cs @@ -63,19 +63,25 @@ public sealed record ImportChoices DeepAnalysis = DeepAnalysis, }; - /// Planner options; are the session titles typed in the plan, by session key. - public ImportOptions ToOptions(IReadOnlyDictionary? titles = null) => new() - { - FolderTemplate = FolderTemplate, - FileTemplate = FileTemplate, - // Without session splitting every camera body and format stays one session. - SessionGap = SplitSessions ? TimeSpan.FromMinutes(System.Math.Max(1, GapMinutes)) : TimeSpan.MaxValue, - MinimumSessionFrames = System.Math.Max(1, MinimumFrames), - IncludeSubfolders = IncludeSubfolders, - VerifyCopies = VerifyCopies, - Duplicates = Duplicates, - Titles = titles ?? new Dictionary(), - }; + /// + /// Planner options; are the session titles typed in the plan, by session key; + /// and the card paths where the user split or merged sessions. + /// + public ImportOptions ToOptions(IReadOnlyDictionary? titles = null, IReadOnlySet? splits = null, IReadOnlySet? merges = null) => + new() + { + FolderTemplate = FolderTemplate, + FileTemplate = FileTemplate, + // Without session splitting every camera body and format stays one session. + SessionGap = SplitSessions ? TimeSpan.FromMinutes(System.Math.Max(1, GapMinutes)) : TimeSpan.MaxValue, + MinimumSessionFrames = System.Math.Max(1, MinimumFrames), + IncludeSubfolders = IncludeSubfolders, + VerifyCopies = VerifyCopies, + Duplicates = Duplicates, + Titles = titles ?? new Dictionary(), + SplitBefore = splits ?? new HashSet(), + MergeAt = merges ?? new HashSet(), + }; /// True when a choice changes which photos form which session or where they go, so the card must be planned again. public bool NeedsNewPlan(ImportChoices previous) => @@ -93,19 +99,3 @@ public sealed record ImportChoices private static bool UsesTitle(string template) => template.Contains("{title", StringComparison.OrdinalIgnoreCase); } - -/// -/// Camera profiles (BRIEF §13): a card without saved settings imports with the settings saved last by another card -/// of the same camera model, so a new card for a known camera needs no setup. -/// -public static class CameraProfiles -{ - public static CardProfile? Find(string? make, string? model, IEnumerable cards) => - model is null - ? null - : cards.Where(c => c.ProfileJson is { Length: > 0 } && string.Equals(c.CameraModel, model, StringComparison.Ordinal) - && string.Equals(c.CameraMake, make, StringComparison.Ordinal)) - .MaxBy(c => c.LastSeen) is { } source - ? ImportService.ProfileOf(source) - : null; -} diff --git a/src/Titano.App/Import/ImportDestinations.cs b/src/Titano.App/Import/ImportDestinations.cs index a24f7a59..426541ef 100644 --- a/src/Titano.App/Import/ImportDestinations.cs +++ b/src/Titano.App/Import/ImportDestinations.cs @@ -1,6 +1,12 @@ +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; @@ -67,6 +73,29 @@ public sealed class UnindexedDestination(ImportDestinationKind kind, string root /// A failed photo of an import: where it is on the card and why. public sealed record ImportFailure(string SourcePath, string Message); +/// Whether a planned copy reached the archive after an archive import job. +public enum CommitCheck +{ + /// No frame of the archive is this photo's copy. + Missing, + + /// The job committed a new frame for the photo. + Copied, + + /// The job found the same content already at the destination and kept that frame. + AlreadyThere, +} + +/// +/// 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. +/// +public sealed class ImportSinkReport +{ + public Dictionary Committed { get; } = new(StringComparer.Ordinal); + public Dictionary Rejected { get; } = new(StringComparer.Ordinal); +} + /// 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. @@ -78,14 +107,30 @@ public sealed record ImportOutcome(int Copied, int Duplicates, int Skipped, IRea /// 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) + /// + /// 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 + /// () every copy not reached fails with that reason. + /// + public static ImportOutcome From(ImportPlan plan, IEnumerable results, bool verified, ImportSinkReport? report = null, + string? stopped = null) { + var byPath = new Dictionary(StringComparer.Ordinal); + foreach (var r in results) byPath[r.Item.SourcePath] = r; int copied = 0, duplicates = 0, skipped = 0; var failures = new List(); - foreach (var r in results) + foreach (var item in plan.Items) { - switch (r.Status) + 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++; @@ -97,7 +142,7 @@ public sealed record ImportOutcome(int Copied, int Duplicates, int Skipped, IRea skipped++; break; default: - failures.Add(new ImportFailure(r.Item.SourcePath, r.Error ?? string.Empty)); + failures.Add(new ImportFailure(item.SourcePath, result?.Error ?? stopped ?? Loc.L("Import.Result.NotCommitted"))); break; } } @@ -105,13 +150,14 @@ public sealed record ImportOutcome(int Copied, int Duplicates, int Skipped, IRea } /// - /// 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. + /// After an archive import job: each planned copy is matched to its own committed frame (); + /// a copy that did not arrive shows the job's own error for that file (, from its log) or + /// that it was not committed. The copies were hashed and read back while the job ran. /// - public static async Task CheckAsync(ImportPlan plan, bool verified, Func> isInArchive, - CancellationToken cancellationToken) + public static async Task CheckAsync(ImportPlan plan, bool verified, Func> check, + IReadOnlyDictionary errors, CancellationToken cancellationToken) { - int copied = 0, skipped = 0; + int copied = 0, duplicates = 0, skipped = 0; var failures = new List(); foreach (var item in plan.Items) { @@ -120,33 +166,80 @@ public sealed record ImportOutcome(int Copied, int Duplicates, int Skipped, IRea skipped++; continue; } - if (await isInArchive(item, cancellationToken).ConfigureAwait(false)) copied++; - else failures.Add(new ImportFailure(item.SourcePath, Core.Localization.Loc.L("Import.Result.NotCommitted"))); + 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, 0, skipped, failures, verified); + return new ImportOutcome(copied, duplicates, skipped, failures, verified); + } + + /// + /// 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". + /// + public static IReadOnlyDictionary JobErrors(string? logTail) + { + var errors = new Dictionary(StringComparer.Ordinal); + if (string.IsNullOrEmpty(logTail)) return errors; + var line = Regex.Escape(Loc.L("Archive.Log.ImportFailed", "\u0001", "\u0002")) + .Replace("\u0001", "(?.+?)", StringComparison.Ordinal) + .Replace("\u0002", "(?.*)", 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; } } /// /// 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. +/// 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. /// public static class LocalImport { public static async Task RunAsync(ImportPlan plan, IImportSink sink, bool verify, IProgress? progress, - CancellationToken cancellationToken) + CancellationToken cancellationToken, ImportSinkReport? report = null) { var results = new List(plan.FileCount); - await new ImportExecutor(plan, sink).RunAsync(0, verify, batch => + string? stopped = null; + try { - results.AddRange(batch.Results); - progress?.Report(batch.Progress); - return ValueTask.CompletedTask; - }, p => + 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)) { - progress?.Report(p); - return ValueTask.CompletedTask; - }, cancellationToken).ConfigureAwait(false); - return ImportOutcome.From(results, verify); + Log.Warning("import", $"Import into {plan.DestinationRoot} stopped", ex); + // The server's own sentence (in the user's language) rather than the status line. + stopped = ex is TitanoApiException api ? api.Error.Message : ex.Message; + } + return ImportOutcome.From(plan, results, verify, report, stopped); } + + /// The card left, or the destination as a whole failed: its disk, the connection, the sign-in or the server (a timeout too). + private static bool Stops(Exception ex) => + ex is IOException or UnauthorizedAccessException or HttpRequestException or TitanoApiException or UntrustedCertificateException + or InvalidOperationException or OperationCanceledException; } diff --git a/src/Titano.App/Import/ImportPlans.cs b/src/Titano.App/Import/ImportPlans.cs index 74f81946..06dc0eb8 100644 --- a/src/Titano.App/Import/ImportPlans.cs +++ b/src/Titano.App/Import/ImportPlans.cs @@ -40,8 +40,10 @@ public sealed record PlanTotals(int Sessions, int Photos, int CopyCount, long Co /// One row of the session table: what the planner grouped and where it goes. /// Title the sequence gets when the user types none (its folder name). /// The session starts a new sequence (false: it continues one imported before). +/// Card path of the session's first photo (where a merge joins it to the previous session). +/// Number of the earlier session of the same camera and format it can merge into; null for none. public sealed record PlanSessionRow(int Number, string SessionKey, string? Title, string DefaultTitle, string TimeText, string FramesText, - string SizeText, string CameraText, string TargetText, bool IsNew); + string SizeText, string CameraText, string TargetText, bool IsNew, string FirstPath, int? MergesInto); public enum PlanRowKind { @@ -50,7 +52,8 @@ public enum PlanRowKind } /// A line of "Show plan": a session heading or one file and its destination. -public sealed record PlanDetailRow(PlanRowKind Kind, string Source, string Destination, string ActionText, string SizeText, bool IsSkipped); +/// A file that is not the first of its session: a new session can start there. +public sealed record PlanDetailRow(PlanRowKind Kind, string Source, string Destination, string ActionText, string SizeText, bool IsSkipped, bool CanSplit); /// Everything "Show plan" lists: every file to its destination, the totals and the conflicts. public sealed record PlanDetails(IReadOnlyList Rows, string TotalsText, IReadOnlyList Conflicts); @@ -119,10 +122,20 @@ public static class ImportPlans _ => Loc.L(isNew ? "Import.Session.New" : "Import.Session.Adds", s.Folder), }; return new PlanSessionRow(s.Number, s.SessionKey, s.Title, leaf, TimeText(s), FramesText(s), Formatting.Bytes(s.Items.Sum(i => i.Size)), - PathTemplate.CameraName(s.CameraMake, s.CameraModel), target, isNew); + PathTemplate.CameraName(s.CameraMake, s.CameraModel), target, isNew, s.Items.Count > 0 ? s.Items[0].SourcePath : string.Empty, + MergeTarget(plan, s)?.Number); }), ]; + /// + /// The session joins when the user merges it (BRIEF §13): the one before it of the same + /// camera body and format, whose last photo precedes its first; sessions of other cameras or formats never merge. + /// + public static PlannedSession? MergeTarget(ImportPlan plan, PlannedSession session) => + plan.Sessions.TakeWhile(s => !ReferenceEquals(s, session)) + .LastOrDefault(s => s.CameraMake == session.CameraMake && s.CameraModel == session.CameraModel && s.CameraSerial == session.CameraSerial + && s.Format == session.Format); + /// "Show plan": every file and where it goes, with the conflicts the plan resolved or reports. public static PlanDetails Details(ImportPlan plan, ImportDestinationKind destination, bool verify) { @@ -131,11 +144,12 @@ public static class ImportPlans { var folder = DestinationFolder(s, destination); rows.Add(new PlanDetailRow(PlanRowKind.Session, Loc.L("Import.Details.Session", s.Number, TimeText(s)), folder, - Loc.Plural("Shell.Status.Photos", s.Items.Count), Formatting.Bytes(s.Items.Sum(i => i.Size)), false)); - foreach (var item in s.Items) + Loc.Plural("Shell.Status.Photos", s.Items.Count), Formatting.Bytes(s.Items.Sum(i => i.Size)), false, false)); + for (var i = 0; i < s.Items.Count; i++) { + var item = s.Items[i]; rows.Add(new PlanDetailRow(PlanRowKind.File, item.SourcePath, folder + "/" + item.FileName, ActionText(item), - Formatting.Bytes(item.Size), item.Action != ImportAction.Copy)); + Formatting.Bytes(item.Size), item.Action != ImportAction.Copy, i > 0)); } } var totals = Totals(plan, verify); @@ -161,17 +175,32 @@ public static class ImportPlans _ => session.Folder, }; - /// "→ 2026/2026-09-28 Canon EOS R5/2026-09-28_18-02-11_0001.CR2": the first file the plan copies. - public static string? FirstDestination(ImportPlan plan, ImportDestinationKind destination) + /// The first files the plan copies, as folder/name (the name alone for the server). + public static IReadOnlyList FirstDestinations(ImportPlan plan, ImportDestinationKind destination, int count = 3) => + [ + .. plan.Sessions.SelectMany(s => s.Items.Where(i => i.Action == ImportAction.Copy) + .Select(i => destination == ImportDestinationKind.Server ? i.FileName : s.Folder + "/" + i.FileName)) + .Take(count), + ]; + + /// + /// "→ 2026/2026-09-28 Canon EOS R5/2026-09-28_18-02-11_0001.CR2 · 2026-09-28_18-02-13_0002.CR2 · …" (BRIEF §7.1: the first + /// three names): a name in the same folder as the one before it is shown without the folder. Empty without names. + /// + public static string NamePreview(IReadOnlyList paths) { - foreach (var s in plan.Sessions) + if (paths.Count == 0) return string.Empty; + var shown = new List(paths.Count); + for (var i = 0; i < paths.Count; i++) { - if (s.Items.FirstOrDefault(i => i.Action == ImportAction.Copy) is { } item) - return destination == ImportDestinationKind.Server ? item.FileName : s.Folder + "/" + item.FileName; + var folder = FolderOf(paths[i]); + shown.Add(i > 0 && folder == FolderOf(paths[i - 1]) ? paths[i][(folder.Length == 0 ? 0 : folder.Length + 1)..] : paths[i]); } - return null; + return Loc.L("Import.Names.Preview", string.Join(" · ", shown)); } + private static string FolderOf(string path) => path.LastIndexOf('/') is var slash and >= 0 ? path[..slash] : string.Empty; + private static string ActionText(PlannedItem item) => item.Action switch { ImportAction.SkipImported => Loc.L("Import.Action.ImportedBefore"), diff --git a/src/Titano.App/Import/ImportResultViewModel.cs b/src/Titano.App/Import/ImportResultViewModel.cs index 42bde730..0b3e1743 100644 --- a/src/Titano.App/Import/ImportResultViewModel.cs +++ b/src/Titano.App/Import/ImportResultViewModel.cs @@ -17,11 +17,13 @@ namespace Titano.App.Import; /// The end of an import (BRIEF §12.4, §13): the verification result and the after-import actions — check the card /// (dry run: every card file and its archive copy hashed again), delete the verified photos or format the card /// (each after a confirmation, never by default), eject, open in the Library. decides -/// what is allowed and why not. +/// what is allowed and why not; a card that leaves needs a new check, and formatting refuses a card that changed +/// since its check. /// public sealed class ImportResultViewModel : LocalizedViewModel { private const int ListedFailures = 100; + private const int ListedOther = 8; private readonly ShellViewModel _shell; private readonly ImportPlan _plan; @@ -43,12 +45,11 @@ public sealed class ImportResultViewModel : LocalizedViewModel _card = card; _device = device; Outcome = outcome; - _actions = new CardActions(choices.Destination, card is not null, device is not null, device?.IsRemovable == true, outcome, null, - plan.FileCount + plan.Problems.Count); + _actions = new CardActions(choices.Destination, card is not null, device is not null, device?.IsRemovable == true, outcome, null, null); Failures = [.. outcome.Failures.Take(ListedFailures).Select(f => Loc.L("Import.Result.Failure", f.SourcePath, f.Message))]; CheckCommand = new AsyncRelayCommand(CheckAsync, () => _actions.CanCheck && !_isBusy); DeleteCommand = new AsyncRelayCommand(DeleteAsync, () => _actions.CanDelete && !_isBusy); - FormatCommand = new RelayCommand(Format, () => _actions.CanFormat && !_isBusy); + FormatCommand = new AsyncRelayCommand(FormatAsync, () => _actions.CanFormat && !_isBusy); EjectCommand = new AsyncRelayCommand(EjectAsync, () => _actions.CanEject && !_isBusy); OpenLibraryCommand = new AsyncRelayCommand(async () => { @@ -122,8 +123,8 @@ public sealed class ImportResultViewModel : LocalizedViewModel public string CheckToolTip => _actions.CheckBlocker ?? Loc.L("Tooltips.Import.Check"); - /// "Delete 4,812 photos…". - public string DeleteText => Loc.Plural("Import.Erase.Delete", _actions.DeleteCount); + /// "Delete 4,812 photos…" once the card was checked; "Delete photos…" before. + public string DeleteText => _actions.Erase is null ? Loc.L("Import.Erase.DeletePlain") : Loc.Plural("Import.Erase.Delete", _actions.DeleteCount); public string DeleteToolTip => _actions.DeleteBlocker ?? Loc.L("Tooltips.Import.Delete"); @@ -131,15 +132,25 @@ public sealed class ImportResultViewModel : LocalizedViewModel public string EjectToolTip => _actions.CanEject ? Loc.L("Tooltips.Import.Eject") : Loc.L("Import.Blocked.NotRemovable"); - /// The card left or came back (Devices list); actions follow. + /// + /// The card is listed again, left, or came back (Devices list); actions follow. A card that left may come back + /// changed (photos shot meanwhile), so its dry run no longer counts: a new check is needed. + /// public void SetDevice(CardDevice? device) { + var same = device is not null && _device is not null && device.Identity == _device.Identity && device.Root == _device.Root; _device = device; - Actions = _actions with { IsInserted = device is not null, IsRemovable = device?.IsRemovable == true }; + Actions = _actions with + { + IsInserted = device is not null, + IsRemovable = device?.IsRemovable == true, + Erase = same ? _actions.Erase : null, + Card = same ? _actions.Card : null, + }; } /// Shows a dry run computed elsewhere (capture, tests). - public void ShowErase(ErasePlan erase, int cardPhotos) => Actions = _actions with { Erase = erase, CardPhotos = cardPhotos }; + public void ShowErase(ErasePlan erase, CardSnapshot card) => Actions = _actions with { Erase = erase, Card = card }; /// Ejects when the profile asks for it and nothing more is to be done on the card. public async Task AutoEjectAsync() @@ -150,17 +161,18 @@ public sealed class ImportResultViewModel : LocalizedViewModel private async Task CheckAsync() { if (_shell.Services.Archives is not { } archives || _archive is null || _card is null || _device is null) return; - var root = _device.Root; + var device = _device; IsBusy = true; try { var progress = new Progress(n => BusyText = Loc.Plural("Import.Erase.Checking", n)); - var (erase, photos) = await Task.Run(async () => + var (erase, snapshot) = await Task.Run(async () => { - var plan = await archives.PlanCardEraseAsync(_archive.Id, _card.Id, root, progress).ConfigureAwait(false); - return (plan, CardScanner.Find(root, includeSubfolders: true).Count); + // Listed before hashing: formatting later compares the card with this list. + var card = CardSnapshot.Take(device.Root, device.Identity); + return (await archives.PlanCardEraseAsync(_archive.Id, _card.Id, device.Root, progress).ConfigureAwait(false), card); }); - ShowErase(erase, photos); + if (_device == device) ShowErase(erase, snapshot); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or DatabaseException) { @@ -195,7 +207,7 @@ public sealed class ImportResultViewModel : LocalizedViewModel _shell.Notify(Loc.Plural("Import.Toast.Deleted", result.Deleted), result.Kept.Count > 0 ? Loc.Plural("Import.Erase.Kept", result.Kept.Count) : null); // The card changed: a new dry run is needed before anything else is deleted. - Actions = _actions with { Erase = null }; + Actions = _actions with { Erase = null, Card = null }; } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { @@ -209,14 +221,19 @@ public sealed class ImportResultViewModel : LocalizedViewModel if (_choices.Eject && _actions.CanEject) await EjectAsync(); } - private void Format() + private async Task FormatAsync() { - if (_device is not { } device) return; + if (_device is not { } device || _actions.Card is not { } checkedCard) return; var name = _card?.Nickname ?? device.Label; + var notImported = _actions.NotImported; var confirmed = _shell.Services.Dialogs.Confirm(new ConfirmRequest { Heading = Loc.L("Import.Confirm.FormatHeading", name), - Message = Loc.Plural("Import.Confirm.FormatMessage", _actions.DeleteCount), + Message = notImported.Count == 0 + ? Loc.Plural("Import.Confirm.FormatMessage", _actions.DeleteCount) + : Loc.Plural("Import.Confirm.FormatMessage", _actions.DeleteCount) + " " + Loc.Plural("Import.Confirm.FormatOther", notImported.Count), + Detail = notImported.Count == 0 ? null : OtherFilesDetail(notImported), + DetailToolTip = Loc.L("Tooltips.Import.NotImported"), SafeText = Loc.L("Import.Confirm.Keep"), SafeToolTip = Loc.L("Tooltips.Import.KeepCard"), ActionText = Loc.L("Import.Confirm.Format"), @@ -226,11 +243,42 @@ public sealed class ImportResultViewModel : LocalizedViewModel TypedPrompt = Loc.L("Import.Confirm.TypeName"), }); if (!confirmed) return; + // Right before formatting: the same card, holding exactly what was checked (BRIEF §0.2 rule 8). + IsBusy = true; + bool unchanged; + try + { + var present = _shell.Services.Devices.Devices.FirstOrDefault(d => d.Identity == device.Identity && d.Root == device.Root); + unchanged = present is not null && checkedCard.IsUnchanged(await Task.Run(() => CardSnapshot.Take(present.Root, present.Identity))); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + Log.Warning("import", "The card could not be listed before formatting", ex); + unchanged = false; + } + finally + { + IsBusy = false; + } + if (!unchanged) + { + Actions = _actions with { Erase = null, Card = null }; + _shell.Notify(Loc.L("Import.Toast.CardChanged", name), Loc.L("Import.Blocked.NoDryRun")); + return; + } var outcome = _shell.Services.Devices.Format(device); - if (outcome == FormatOutcome.Formatted) Actions = _actions with { Erase = null }; + if (outcome == FormatOutcome.Formatted) Actions = _actions with { Erase = null, Card = null }; if (outcome != FormatOutcome.Cancelled) _shell.Notify(Loc.L("Import.Toast.Format." + outcome, name)); } + /// The first files that were never imported, one per line. + private static string OtherFilesDetail(IReadOnlyList files) + { + var shown = files.Take(ListedOther).ToList(); + if (files.Count > ListedOther) shown.Add("…"); + return string.Join(Environment.NewLine, shown); + } + private async Task EjectAsync() { if (_device is not { } device) return; diff --git a/src/Titano.App/Import/ImportScenes.cs b/src/Titano.App/Import/ImportScenes.cs index c4bd9741..ece6b806 100644 --- a/src/Titano.App/Import/ImportScenes.cs +++ b/src/Titano.App/Import/ImportScenes.cs @@ -1,49 +1,92 @@ +using Titano.App.Sections; using Titano.App.Shell; +using Titano.Core.Import; +using Titano.Core.Localization; +using Titano.Data; namespace Titano.App.Import; /// /// The Import states that --capture renders (import-<state>-<theme>.png) and the screen smoke -/// tests check, on the card of : the card just inserted with its plan and sessions (and the -/// insertion toast), "Show plan", and the verification result with the after-import actions. +/// tests check, on the cards of : the card just inserted (with its toast) and its plan and +/// sessions, "Show plan", the import copying, an import that stopped, the verification result with the after-import +/// actions, and a card seen for the first time (its nickname field and toast). The caller dismisses toasts before each +/// state, and after it unless . /// public static class ImportScenes { - public static readonly IReadOnlyList States = ["plan", "showplan", "done"]; + public static readonly IReadOnlyList States = ["inserted", "plan", "showplan", "running", "failed", "done", "newcard"]; + + /// States that show a toast on purpose (the insertion toasts); the caller dismisses the others' toasts. + public static bool ShowsToast(string state) => state is "inserted" or "newcard"; public static async Task ShowAsync(ShellViewModel shell, string state) { var import = shell.Import; + var data = shell.Services.Environment.DataDirectory; shell.Navigate(SectionKey.Import); switch (state) { + case "inserted": + await ShowPlanAsync(import, SampleCards.Card(data)); + await import.CardInsertedAsync(SampleCards.Card(data)); + break; case "plan": - var card = SampleCards.Card(shell.Services.Environment.DataDirectory); - if (import.Result is not null) import.NewImportCommand.Execute(null); - import.CloseDetailsCommand.Execute(null); - await import.RefreshSourcesAsync(); - import.SelectSource(card.Identity); - import.KeepCard = true; - await import.WaitForPlanAsync(); - if (import.Sessions.Count > 0) - { - var sunset = import.Sessions[^1].Title; - sunset.Text = "Harbour sunset"; - sunset.Commit(); - } - await import.CardInsertedAsync(card); + await ShowPlanAsync(import, SampleCards.Card(data)); break; case "showplan": import.ShowPlanCommand.Execute(null); break; - case "done": + case "running": import.CloseDetailsCommand.Execute(null); + var plan = Plan(import); + import.ShowRunning(new ImportProgress(31, plan.FileCount, plan.CopyBytes * 31 / plan.FileCount, plan.CopyBytes, 31, 0, 0, null)); + break; + case "failed": + import.ShowFailed(Loc.L("Archive.Error.SourceGone", SampleCards.Card(data).Root)); + break; + case "done": + await ShowPlanAsync(import, SampleCards.Card(data)); import.DeleteImported = true; - var plan = import.Plan ?? throw new InvalidOperationException("The sample card has no plan."); - import.ShowResult(SampleCards.Outcome(plan)).ShowErase(SampleCards.Erase(plan), plan.FileCount); + var done = Plan(import); + var card = SampleCards.Card(data); + import.ShowResult(SampleCards.Outcome(done)).ShowErase(SampleCards.Erase(done), CardSnapshot.Take(card.Root, card.Identity)); + break; + case "newcard": + // Inserted first (its toast says it is new), then chosen: its first plan registers it and asks for a nickname. + var fresh = SampleCards.NewCard(data); + await ForgetAsync(shell, fresh.Identity); + await import.CardInsertedAsync(fresh); + await ShowPlanAsync(import, fresh); break; default: throw new ArgumentOutOfRangeException(nameof(state), state, "Unknown Import state."); } } + + /// The card selected, its plan ready, the sunset session titled. + private static async Task ShowPlanAsync(ImportViewModel import, CardDevice card) + { + if (import.Result is not null) import.NewImportCommand.Execute(null); + import.CloseDetailsCommand.Execute(null); + await import.RefreshSourcesAsync(); + import.SelectSource(card.Identity); + import.KeepCard = true; + await import.WaitForPlanAsync(); + if (import.Sessions.Count == 3) + { + var sunset = import.Sessions[^1].Title; + sunset.Text = "Harbour sunset"; + sunset.Commit(); + } + } + + private static ImportPlan Plan(ImportViewModel import) => import.Plan ?? throw new InvalidOperationException("The sample card has no plan."); + + /// The new card stays new in every theme: an earlier state registered it on its first plan. + private static async Task ForgetAsync(ShellViewModel shell, string identity) + { + if (shell.Services.Database is not { } db || await db.MediaCards.FindByIdentityAsync(identity) is not { } known) return; + await db.MediaCards.DeleteAsync(known.Id); + } } diff --git a/src/Titano.App/Import/ImportSessionRowViewModel.cs b/src/Titano.App/Import/ImportSessionRowViewModel.cs index cb533025..15c6dd08 100644 --- a/src/Titano.App/Import/ImportSessionRowViewModel.cs +++ b/src/Titano.App/Import/ImportSessionRowViewModel.cs @@ -1,16 +1,23 @@ +using System.Windows.Input; using Titano.App.Settings; using Titano.Core.Localization; using Titano.Ui.Mvvm; namespace Titano.App.Import; -/// A session of the plan with its editable title (the sequence title; empty keeps the folder name). +/// +/// A session of the plan with its editable title (the sequence title; empty keeps the folder name) and its merge into +/// the earlier session of the same camera (BRIEF §13). Both can change only while the plan is not running. +/// public sealed class ImportSessionRowViewModel : ObservableObject { - public ImportSessionRowViewModel(PlanSessionRow row, Action titleChanged) + private bool _isEditable = true; + + public ImportSessionRowViewModel(PlanSessionRow row, Action titleChanged, Action merge) { Row = row; Title = new EditableField(row.Title ?? string.Empty, _ => null, text => titleChanged(row.SessionKey, text), () => Loc.L("Tooltips.Import.SessionTitle")); + MergeCommand = new RelayCommand(() => merge(row), () => CanMerge); } public PlanSessionRow Row { get; } @@ -18,4 +25,30 @@ public sealed class ImportSessionRowViewModel : ObservableObject public EditableField Title { get; } public string Placeholder => Row.DefaultTitle; + + public ICommand MergeCommand { get; } + + /// False while the import copies: the plan it runs is fixed. + public bool IsEditable + { + get => _isEditable; + set + { + if (!Set(ref _isEditable, value)) return; + OnPropertyChanged(nameof(TitleToolTip)); + OnPropertyChanged(nameof(MergeToolTip)); + OnPropertyChanged(nameof(CanMerge)); + } + } + + public bool CanMerge => IsEditable && Row.MergesInto is not null; + + /// The merge button is shown only where a merge is possible. + public bool HasMerge => Row.MergesInto is not null; + + public string TitleToolTip => IsEditable ? Title.ToolTip : Loc.L("Import.Blocked.Copying"); + + public string MergeToolTip => !IsEditable ? Loc.L("Import.Blocked.Copying") + : Row.MergesInto is { } target ? Loc.L("Tooltips.Import.Merge", target) + : string.Empty; } diff --git a/src/Titano.App/Import/SampleCards.cs b/src/Titano.App/Import/SampleCards.cs index d514eb96..72df396c 100644 --- a/src/Titano.App/Import/SampleCards.cs +++ b/src/Titano.App/Import/SampleCards.cs @@ -13,7 +13,8 @@ namespace Titano.App.Import; /// /// Capture and smoke-test data ONLY (--capture, never the user's data): a simulated Canon EOS R5 card in the /// capture folder — a night, a dawn and a sunset session of small JPEGs with Exif — remembered as "EOS R5 · card A", -/// a second remembered card that is not inserted, a USB drive without DCIM, and the import result shown afterwards. +/// a Sony card seen for the first time, a second remembered card that is not inserted, a USB drive without DCIM, and +/// the import result shown afterwards. /// public static class SampleCards { @@ -28,16 +29,26 @@ public static class SampleCards (new DateTime(2026, 9, 28, 18, 2, 11), 24, 2), ]; + /// The new card's one session: a morning harbour series. + private static readonly (DateTime Start, int Photos, int Interval)[] NewSessions = [(new DateTime(2026, 9, 30, 7, 15, 0), 12, 5)]; + /// The inserted card: its root is a folder of the capture environment. public static CardDevice Card(string dataDirectory) => new(Path.Combine(dataDirectory, "card") + Path.DirectorySeparatorChar, "EOS_DIGITAL", Serial, 127_865_454_592, 31_665_454_592, IsRemovable: true, HasDcim: true); - /// Writes the card's photos, remembers both cards (the inserted one with its last import) and inserts the card and a USB drive. + /// A Sony card Titano has never seen (the nickname field and the "new card" toast). + public static CardDevice NewCard(string dataDirectory) => + new(Path.Combine(dataDirectory, "newcard") + Path.DirectorySeparatorChar, "SONY", 0x2B7E1A09, 63_864_569_856, 60_120_000_000, IsRemovable: true, + HasDcim: true); + + /// Writes the cards' photos, remembers two cards (the inserted one with its last import) and inserts both cards and a USB drive. public static async Task InsertAsync(TitanoDatabase db, SampleDeviceMonitor devices, string dataDirectory) { var card = Card(dataDirectory); - WritePhotos(card.Root); + WritePhotos(Path.Combine(card.Root, "DCIM", "100CANON"), "IMG_", Sessions, "Canon", "Canon EOS R5", "0431"); + var newCard = NewCard(dataDirectory); + WritePhotos(Path.Combine(newCard.Root, "DCIM", "100MSDCF"), "DSC0", NewSessions, "SONY", "ILCE-7M4", "4410287"); var known = new MediaCard { Id = Guid.CreateVersion7(), @@ -69,6 +80,7 @@ public static class SampleCards LastSeen = LastImport.AddDays(-6), }); devices.Insert(card); + devices.Insert(newCard); devices.Insert(new CardDevice(@"F:\", "USB DRIVE", 0x51E02B44, 2_000_398_934_016, 1_800_000_000_000, IsRemovable: true, HasDcim: false)); } @@ -88,26 +100,25 @@ public static class SampleCards ]); } - private static void WritePhotos(string root) + private static void WritePhotos(string folder, string prefix, (DateTime Start, int Photos, int Interval)[] sessions, string make, string model, string body) { - var folder = Path.Combine(root, "DCIM", "100CANON"); if (Directory.Exists(folder)) return; Directory.CreateDirectory(folder); var n = 1; - foreach (var (start, photos, interval) in Sessions) + foreach (var (start, photos, interval) in sessions) { for (var i = 0; i < photos; i++, n++) { var captured = start.AddSeconds(i * interval); - var path = Path.Combine(folder, $"IMG_{n:0000}.JPG"); - File.WriteAllBytes(path, Photo(captured, n)); + var path = Path.Combine(folder, $"{prefix}{n:0000}.JPG"); + File.WriteAllBytes(path, Photo(captured, n, make, model, body)); File.SetLastWriteTime(path, captured); } } } /// A small sky gradient with grain (distinct bytes per photo) and the camera's Exif. - private static byte[] Photo(DateTime captured, int seed) + private static byte[] Photo(DateTime captured, int seed, string make, string model, string body) { const int Width = 96, Height = 64; using var image = new ImageBuffer(Width, Height, 3, clear: false); @@ -126,9 +137,9 @@ public static class SampleCards } var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { - Make = "Canon", - Model = "Canon EOS R5", - BodySerial = "0431", + Make = make, + Model = model, + BodySerial = body, CapturedAt = captured, Iso = 100, ExposureTime = 1.0 / 250, diff --git a/src/Titano.App/Import/ServerImportSink.cs b/src/Titano.App/Import/ServerImportSink.cs index 5100fb6d..d65b352e 100644 --- a/src/Titano.App/Import/ServerImportSink.cs +++ b/src/Titano.App/Import/ServerImportSink.cs @@ -1,4 +1,6 @@ using System.IO; +using System.Net; +using Titano.Core.Diagnostics; using Titano.Core.Import; using Titano.Protocol; using Titano.Protocol.Client; @@ -10,7 +12,9 @@ namespace Titano.App.Import; /// 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. +/// title is set on that sequence once. 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. /// public sealed class ServerImportSink(TitanoClient client, string stagingDirectory) : IImportSink { @@ -18,6 +22,9 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector public string StagingDirectory { get; } = stagingDirectory; + /// Every file committed or refused so far, by card path; pass it to . + public ImportSinkReport Report { get; } = new(); + public async Task> CommitAsync(PlannedSession session, IReadOnlyList files, CancellationToken cancellationToken) { var results = new List(files.Count); @@ -28,16 +35,21 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector var named = Path.Combine(folder, Path.GetFileName(file.Item.SourcePath)); Directory.CreateDirectory(folder); File.Move(file.StagedPath, named); + CompleteUploadResponse? committed = null; try { - var committed = await client.UploadFileAsync(named, null, file.Item.CapturedAt, session.SessionKey, file.Sha256, null, cancellationToken) + 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 duplicate = committed is null || committed.Index < 0; - results.Add(new SinkCommit(committed?.FrameId, file.Item.FileName, duplicate)); - if (committed is { Index: >= 0 } && session.Title is { Length: > 0 } title && _titled.Add(committed.SequenceId)) - await client.UpdateSequenceAsync(committed.SequenceId, new UpdateSequenceRequest(title, null, null, null, null, null), cancellationToken) - .ConfigureAwait(false); + 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 (TitanoApiException ex) when (IsRefusal(ex)) + { + // The executor has no failed commit: the refusal travels in the report, which turns this result into a failure. + Report.Rejected[file.Item.SourcePath] = ex.Error.Message; + results.Add(new SinkCommit(null, file.Item.FileName, Duplicate: false)); } finally { @@ -45,7 +57,26 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector 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, cancellationToken).ConfigureAwait(false); } return results; } + + /// A refused title leaves the photos imported under the sequence's own name. + private async Task SetTitleAsync(Guid sequenceId, string title, CancellationToken cancellationToken) + { + try + { + await client.UpdateSequenceAsync(sequenceId, new UpdateSequenceRequest(title, null, null, null, null, null), cancellationToken).ConfigureAwait(false); + } + catch (TitanoApiException 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(TitanoApiException ex) => + !ex.IsTransient && ex.Status is not (HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) && (int)ex.Status is >= 400 and < 500; } diff --git a/src/Titano.App/Sections/ImportView.xaml b/src/Titano.App/Sections/ImportView.xaml index 064a7f44..8634835a 100644 --- a/src/Titano.App/Sections/ImportView.xaml +++ b/src/Titano.App/Sections/ImportView.xaml @@ -16,6 +16,7 @@ + @@ -37,7 +38,7 @@ - + + + + + + + + @@ -97,7 +135,7 @@ VerticalContentAlignment="Top"> - + @@ -126,6 +164,7 @@ + @@ -142,7 +181,7 @@ -