feat(app): import and archive manager logic (plans, card pool, card gate, server sink, tree, charts, reports)

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
This commit is contained in:
Claude committed 2026-10-02 17:39:03 +00:00
1 parent 7d95536096
commit f7affe7840
22 files changed
+2258

No files matched your search

+46
View File
@@ -4,6 +4,16 @@
<resheader name="version"><value>2.0</value></resheader>
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<data name="Archive.Chart.MonthTip" xml:space="preserve"><value>{0} · {1} · {2}</value><comment>invariant</comment></data>
<data name="Archive.Chart.OtherCameras.one" xml:space="preserve"><value>{0:N0} other</value></data>
<data name="Archive.Chart.OtherCameras.other" xml:space="preserve"><value>{0:N0} others</value></data>
<data name="Archive.Chart.OtherFormats" xml:space="preserve"><value>Other</value></data>
<data name="Archive.Chart.ShareTip" xml:space="preserve"><value>{0} · {1} · {2}</value><comment>invariant</comment></data>
<data name="Archive.Chart.UnknownCamera" xml:space="preserve"><value>Unknown camera</value></data>
<data name="Archive.Chart.YearTip" xml:space="preserve"><value>{0} · {1} · {2}</value><comment>invariant</comment></data>
<data name="Archive.Duplicates.Copies.one" xml:space="preserve"><value>{0:N0} copy</value></data>
<data name="Archive.Duplicates.Copies.other" xml:space="preserve"><value>{0:N0} copies</value></data>
<data name="Archive.Duplicates.Place" xml:space="preserve"><value>{0} · {1}</value><comment>invariant</comment></data>
<data name="Archive.Empty" xml:space="preserve"><value>No archive yet</value></data>
<data name="Archive.Error.Busy" xml:space="preserve"><value>Another Titano ({0}) is writing here. Try again shortly.</value><comment>long: error sentence</comment></data>
<data name="Archive.Error.CopyMismatch" xml:space="preserve"><value>The copy of {0} does not match the original.</value><comment>long: error sentence</comment></data>
@@ -14,6 +24,20 @@
<data name="Archive.Error.SourceChanged" xml:space="preserve"><value>{0} changed after the plan was made.</value><comment>long: error sentence</comment></data>
<data name="Archive.Error.SourceGone" xml:space="preserve"><value>The card or folder {0} is no longer available.</value><comment>long: error sentence</comment></data>
<data name="Archive.Error.StalePlan" xml:space="preserve"><value>The archive changed after this plan was made.</value><comment>long: error sentence</comment></data>
<data name="Archive.Health.AllVerified" xml:space="preserve"><value>Every sequence verified</value></data>
<data name="Archive.Health.AnalysedOk" xml:space="preserve"><value>Every sequence analysed</value></data>
<data name="Archive.Health.Damaged" xml:space="preserve"><value>{0} missing · {1} damaged</value></data>
<data name="Archive.Health.Duplicates.one" xml:space="preserve"><value>{0:N0} duplicate frame</value></data>
<data name="Archive.Health.Duplicates.other" xml:space="preserve"><value>{0:N0} duplicate frames</value></data>
<data name="Archive.Health.FramesOk" xml:space="preserve"><value>No missing frames</value></data>
<data name="Archive.Health.NoDuplicates" xml:space="preserve"><value>No duplicate frames</value></data>
<data name="Archive.Health.NoProxies.one" xml:space="preserve"><value>{0:N0} sequence lacks proxies</value></data>
<data name="Archive.Health.NoProxies.other" xml:space="preserve"><value>{0:N0} sequences lack proxies</value></data>
<data name="Archive.Health.NotAnalysed.one" xml:space="preserve"><value>{0:N0} sequence not analysed</value></data>
<data name="Archive.Health.NotAnalysed.other" xml:space="preserve"><value>{0:N0} sequences not analysed</value></data>
<data name="Archive.Health.ProxiesOk" xml:space="preserve"><value>Every sequence has proxies</value></data>
<data name="Archive.Health.Unverified.one" xml:space="preserve"><value>{0:N0} sequence unverified</value></data>
<data name="Archive.Health.Unverified.other" xml:space="preserve"><value>{0:N0} sequences unverified</value></data>
<data name="Archive.Log.DryRun" xml:space="preserve"><value>Dry run: nothing was changed.</value><comment>long: job log line</comment></data>
<data name="Archive.Log.FolderFailed" xml:space="preserve"><value>Not indexed: {0} ({1})</value><comment>long: job log line</comment></data>
<data name="Archive.Log.FrameChanged" xml:space="preserve"><value>Changed: {0}</value><comment>long: job log line</comment></data>
@@ -27,4 +51,26 @@
<data name="Archive.Log.ScanDone" xml:space="preserve"><value>{0} folders, {1} indexed, {2} adopted, {3} removed</value><comment>long: job log line</comment></data>
<data name="Archive.Log.SequenceMissing" xml:space="preserve"><value>Sequence not found: {0}</value><comment>long: job log line</comment></data>
<data name="Archive.Log.VerifyDone" xml:space="preserve"><value>{0} photos checked, {1} intact, {2} hashed, {3} with problems</value><comment>long: job log line</comment></data>
<data name="Archive.Orphans.Missing.one" xml:space="preserve"><value>{0:N0} file missing</value></data>
<data name="Archive.Orphans.Missing.other" xml:space="preserve"><value>{0:N0} files missing</value></data>
<data name="Archive.Orphans.NewFolder" xml:space="preserve"><value>Not indexed yet</value></data>
<data name="Archive.Orphans.NoSidecar" xml:space="preserve"><value>No sidecar in {0}</value><comment>long</comment></data>
<data name="Archive.Orphans.Unlisted.one" xml:space="preserve"><value>{0:N0} photo not in sidecar</value></data>
<data name="Archive.Orphans.Unlisted.other" xml:space="preserve"><value>{0:N0} photos not in sidecar</value></data>
<data name="Archive.Recent.Line" xml:space="preserve"><value>{0} · {1}</value><comment>invariant</comment></data>
<data name="Archive.Rename.Summary.one" xml:space="preserve"><value>{0:N0} of {1:N0} changes</value></data>
<data name="Archive.Rename.Summary.other" xml:space="preserve"><value>{0:N0} of {1:N0} change</value></data>
<data name="Archive.Retag.Invalid" xml:space="preserve"><value>Each tag has 1 to {0} characters.</value><comment>long</comment></data>
<data name="Archive.Tile.Cameras.one" xml:space="preserve"><value>camera</value></data>
<data name="Archive.Tile.Cameras.other" xml:space="preserve"><value>cameras</value></data>
<data name="Archive.Tile.Formats.one" xml:space="preserve"><value>{0:N0} format</value></data>
<data name="Archive.Tile.Formats.other" xml:space="preserve"><value>{0:N0} formats</value></data>
<data name="Archive.Tile.Originals" xml:space="preserve"><value>originals</value></data>
<data name="Archive.Tile.Pending" xml:space="preserve"><value>{0} to analyse · {1} to verify</value><comment>long</comment></data>
<data name="Archive.Tile.Photos.one" xml:space="preserve"><value>photo</value></data>
<data name="Archive.Tile.Photos.other" xml:space="preserve"><value>photos</value></data>
<data name="Archive.Tile.Sequences.one" xml:space="preserve"><value>sequence</value></data>
<data name="Archive.Tile.Sequences.other" xml:space="preserve"><value>sequences</value></data>
<data name="Archive.Tile.YearRange" xml:space="preserve"><value>{0} – {1}</value><comment>invariant</comment></data>
<data name="Archive.Tree.Undated" xml:space="preserve"><value>Undated</value></data>
</root>
+53
View File
@@ -4,5 +4,58 @@
<resheader name="version"><value>2.0</value></resheader>
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<data name="Import.Action.Again" xml:space="preserve"><value>Import again</value></data>
<data name="Import.Action.Copy" xml:space="preserve"><value>Copy</value></data>
<data name="Import.Action.Duplicate" xml:space="preserve"><value>Already in archive</value></data>
<data name="Import.Action.ImportedBefore" xml:space="preserve"><value>Imported before</value></data>
<data name="Import.Blocked.Incomplete" xml:space="preserve"><value>Formatting needs a complete import with verified copies.</value><comment>long</comment></data>
<data name="Import.Blocked.NoCard" xml:space="preserve"><value>Only for a remembered card that is inserted.</value><comment>long</comment></data>
<data name="Import.Blocked.NoDryRun" xml:space="preserve"><value>Check the card first: every file and its copy are hashed again.</value><comment>long</comment></data>
<data name="Import.Blocked.NotAllVerified" xml:space="preserve"><value>Some photos on the card have no verified copy; formatting would lose them.</value><comment>long</comment></data>
<data name="Import.Blocked.NotArchive" xml:space="preserve"><value>Only imports into this PC's archive can be checked against the card.</value><comment>long</comment></data>
<data name="Import.Blocked.NothingImported" xml:space="preserve"><value>Nothing from this card has been imported yet.</value><comment>long</comment></data>
<data name="Import.Blocked.NothingVerified" xml:space="preserve"><value>No photo on the card matches a verified copy.</value><comment>long</comment></data>
<data name="Import.Details.Copy" xml:space="preserve"><value>copy {0} ({1})</value></data>
<data name="Import.Details.Files.one" xml:space="preserve"><value>{0:N0} file</value></data>
<data name="Import.Details.Files.other" xml:space="preserve"><value>{0:N0} files</value></data>
<data name="Import.Details.Session" xml:space="preserve"><value>Session {0} · {1}</value><comment>long</comment></data>
<data name="Import.Details.Unreadable" xml:space="preserve"><value>Cannot read {0}: {1}</value><comment>long</comment></data>
<data name="Import.Device.Away" xml:space="preserve"><value>{0} · not inserted</value><comment>long</comment></data>
<data name="Import.Device.Body" xml:space="preserve"><value>{0} #{1}</value><comment>invariant</comment></data>
<data name="Import.Device.Card" xml:space="preserve"><value>{0} · {1} · {2} used</value></data>
<data name="Import.Device.NoDcim" xml:space="preserve"><value>{0} · no DCIM · {1} free</value></data>
<data name="Import.Device.Serial" xml:space="preserve"><value>serial {0}</value></data>
<data name="Import.Device.State.Away" xml:space="preserve"><value>Known card, not inserted</value></data>
<data name="Import.Device.State.Drive" xml:space="preserve"><value>Drive without DCIM</value></data>
<data name="Import.Device.State.Folder" xml:space="preserve"><value>Folder</value></data>
<data name="Import.Device.State.Known" xml:space="preserve"><value>Known card</value></data>
<data name="Import.Device.State.New" xml:space="preserve"><value>New card</value></data>
<data name="Import.Empty" xml:space="preserve"><value>Insert a card or pick a folder</value></data>
<data name="Import.Erase.Kept.one" xml:space="preserve"><value>{0:N0} kept</value></data>
<data name="Import.Erase.Kept.other" xml:space="preserve"><value>{0:N0} kept</value></data>
<data name="Import.Erase.Reason" xml:space="preserve"><value>{0} · {1}</value><comment>invariant</comment></data>
<data name="Import.Erase.Verdict.CopyMismatch" xml:space="preserve"><value>the archive copy differs</value></data>
<data name="Import.Erase.Verdict.CopyMissing" xml:space="preserve"><value>the archive copy is missing</value></data>
<data name="Import.Erase.Verdict.Delete" xml:space="preserve"><value>verified</value></data>
<data name="Import.Erase.Verdict.SourceChanged" xml:space="preserve"><value>changed on the card</value></data>
<data name="Import.Erase.Verdict.SourceMissing" xml:space="preserve"><value>no longer on the card</value></data>
<data name="Import.Erase.Verified.one" xml:space="preserve"><value>{0:N0} verified on the card</value></data>
<data name="Import.Erase.Verified.other" xml:space="preserve"><value>{0:N0} verified on the card</value></data>
<data name="Import.Plan.About" xml:space="preserve"><value>about {0}</value></data>
<data name="Import.Plan.Duplicates.one" xml:space="preserve"><value>{0:N0} duplicate</value></data>
<data name="Import.Plan.Duplicates.other" xml:space="preserve"><value>{0:N0} duplicates</value></data>
<data name="Import.Plan.FreeAfter" xml:space="preserve"><value>{0} free after import</value></data>
<data name="Import.Plan.ImportedBefore.one" xml:space="preserve"><value>{0:N0} imported before</value></data>
<data name="Import.Plan.ImportedBefore.other" xml:space="preserve"><value>{0:N0} imported before</value></data>
<data name="Import.Plan.Sessions.one" xml:space="preserve"><value>{0:N0} session</value></data>
<data name="Import.Plan.Sessions.other" xml:space="preserve"><value>{0:N0} sessions</value></data>
<data name="Import.Plan.SpaceMissing" xml:space="preserve"><value>{0} missing</value></data>
<data name="Import.Result.NotCommitted" xml:space="preserve"><value>No archive copy after the import; see the job log.</value><comment>long</comment></data>
<data name="Import.Session.Adds" xml:space="preserve"><value>Adds to · {0}</value><comment>long</comment></data>
<data name="Import.Session.Frames" xml:space="preserve"><value>{0} · {1}</value><comment>invariant</comment></data>
<data name="Import.Session.New" xml:space="preserve"><value>New · {0}</value><comment>long</comment></data>
<data name="Import.Session.Server" xml:space="preserve"><value>Server</value></data>
<data name="Import.Session.Time" xml:space="preserve"><value>{0} · {1} → {2}</value><comment>invariant</comment></data>
<data name="Import.Start.one" xml:space="preserve"><value>Import {0:N0} photo</value></data>
<data name="Import.Start.other" xml:space="preserve"><value>Import {0:N0} photos</value></data>
</root>
+40
View File
@@ -4,6 +4,12 @@
<resheader name="version"><value>2.0</value></resheader>
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<data name="Archive.Chart.OtherCameras.one" xml:space="preserve"><value>{0:N0} altra</value></data>
<data name="Archive.Chart.OtherCameras.other" xml:space="preserve"><value>{0:N0} altre</value></data>
<data name="Archive.Chart.OtherFormats" xml:space="preserve"><value>Altro</value></data>
<data name="Archive.Chart.UnknownCamera" xml:space="preserve"><value>Fotocamera ignota</value></data>
<data name="Archive.Duplicates.Copies.one" xml:space="preserve"><value>{0:N0} copia</value></data>
<data name="Archive.Duplicates.Copies.other" xml:space="preserve"><value>{0:N0} copie</value></data>
<data name="Archive.Empty" xml:space="preserve"><value>Nessun archivio</value></data>
<data name="Archive.Error.Busy" xml:space="preserve"><value>Un altro Titano ({0}) sta scrivendo qui. Riprova tra poco.</value></data>
<data name="Archive.Error.CopyMismatch" xml:space="preserve"><value>La copia di {0} non corrisponde all'originale.</value></data>
@@ -14,6 +20,20 @@
<data name="Archive.Error.SourceChanged" xml:space="preserve"><value>{0} è cambiato dopo la creazione del piano.</value></data>
<data name="Archive.Error.SourceGone" xml:space="preserve"><value>La scheda o cartella {0} non è più disponibile.</value></data>
<data name="Archive.Error.StalePlan" xml:space="preserve"><value>L'archivio è cambiato dopo la creazione del piano.</value></data>
<data name="Archive.Health.AllVerified" xml:space="preserve"><value>Tutte le sequenze verificate</value></data>
<data name="Archive.Health.AnalysedOk" xml:space="preserve"><value>Tutte le sequenze analizzate</value></data>
<data name="Archive.Health.Damaged" xml:space="preserve"><value>{0} mancanti · {1} danneggiati</value></data>
<data name="Archive.Health.Duplicates.one" xml:space="preserve"><value>{0:N0} fotogramma doppio</value></data>
<data name="Archive.Health.Duplicates.other" xml:space="preserve"><value>{0:N0} fotogrammi doppi</value></data>
<data name="Archive.Health.FramesOk" xml:space="preserve"><value>Nessun fotogramma mancante</value></data>
<data name="Archive.Health.NoDuplicates" xml:space="preserve"><value>Nessun fotogramma doppio</value></data>
<data name="Archive.Health.NoProxies.one" xml:space="preserve"><value>{0:N0} sequenza senza proxy</value></data>
<data name="Archive.Health.NoProxies.other" xml:space="preserve"><value>{0:N0} sequenze senza proxy</value></data>
<data name="Archive.Health.NotAnalysed.one" xml:space="preserve"><value>{0:N0} sequenza non analizzata</value></data>
<data name="Archive.Health.NotAnalysed.other" xml:space="preserve"><value>{0:N0} sequenze non analizzate</value></data>
<data name="Archive.Health.ProxiesOk" xml:space="preserve"><value>Proxy pronti per tutte</value></data>
<data name="Archive.Health.Unverified.one" xml:space="preserve"><value>{0:N0} sequenza non verificata</value></data>
<data name="Archive.Health.Unverified.other" xml:space="preserve"><value>{0:N0} sequenze non verificate</value></data>
<data name="Archive.Log.DryRun" xml:space="preserve"><value>Prova: nulla è stato modificato.</value></data>
<data name="Archive.Log.FolderFailed" xml:space="preserve"><value>Non indicizzata: {0} ({1})</value></data>
<data name="Archive.Log.FrameChanged" xml:space="preserve"><value>Modificato: {0}</value></data>
@@ -27,4 +47,24 @@
<data name="Archive.Log.ScanDone" xml:space="preserve"><value>{0} cartelle, {1} indicizzate, {2} adottate, {3} rimosse</value></data>
<data name="Archive.Log.SequenceMissing" xml:space="preserve"><value>Sequenza non trovata: {0}</value></data>
<data name="Archive.Log.VerifyDone" xml:space="preserve"><value>{0} foto controllate, {1} integre, {2} con impronta nuova, {3} con problemi</value></data>
<data name="Archive.Orphans.Missing.one" xml:space="preserve"><value>{0:N0} file mancante</value></data>
<data name="Archive.Orphans.Missing.other" xml:space="preserve"><value>{0:N0} file mancanti</value></data>
<data name="Archive.Orphans.NewFolder" xml:space="preserve"><value>Non ancora indicizzata</value></data>
<data name="Archive.Orphans.NoSidecar" xml:space="preserve"><value>Nessun sidecar in {0}</value></data>
<data name="Archive.Orphans.Unlisted.one" xml:space="preserve"><value>{0:N0} foto fuori dal sidecar</value></data>
<data name="Archive.Orphans.Unlisted.other" xml:space="preserve"><value>{0:N0} foto fuori dal sidecar</value></data>
<data name="Archive.Rename.Summary.one" xml:space="preserve"><value>{0:N0} di {1:N0} cambia</value></data>
<data name="Archive.Rename.Summary.other" xml:space="preserve"><value>{0:N0} di {1:N0} cambiano</value></data>
<data name="Archive.Retag.Invalid" xml:space="preserve"><value>Ogni tag ha da 1 a {0} caratteri.</value></data>
<data name="Archive.Tile.Cameras.one" xml:space="preserve"><value>fotocamera</value></data>
<data name="Archive.Tile.Cameras.other" xml:space="preserve"><value>fotocamere</value></data>
<data name="Archive.Tile.Formats.one" xml:space="preserve"><value>{0:N0} formato</value></data>
<data name="Archive.Tile.Formats.other" xml:space="preserve"><value>{0:N0} formati</value></data>
<data name="Archive.Tile.Originals" xml:space="preserve"><value>originali</value></data>
<data name="Archive.Tile.Pending" xml:space="preserve"><value>{0} da analizzare · {1} da verificare</value></data>
<data name="Archive.Tile.Photos.one" xml:space="preserve"><value>foto</value></data>
<data name="Archive.Tile.Photos.other" xml:space="preserve"><value>foto</value></data>
<data name="Archive.Tile.Sequences.one" xml:space="preserve"><value>sequenza</value></data>
<data name="Archive.Tile.Sequences.other" xml:space="preserve"><value>sequenze</value></data>
<data name="Archive.Tree.Undated" xml:space="preserve"><value>Senza data</value></data>
</root>
+49
View File
@@ -4,5 +4,54 @@
<resheader name="version"><value>2.0</value></resheader>
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<data name="Import.Action.Again" xml:space="preserve"><value>Importa di nuovo</value></data>
<data name="Import.Action.Copy" xml:space="preserve"><value>Copia</value></data>
<data name="Import.Action.Duplicate" xml:space="preserve"><value>Già in archivio</value></data>
<data name="Import.Action.ImportedBefore" xml:space="preserve"><value>Già importata</value></data>
<data name="Import.Blocked.Incomplete" xml:space="preserve"><value>Per formattare serve un'importazione completa con copie verificate.</value></data>
<data name="Import.Blocked.NoCard" xml:space="preserve"><value>Solo per una scheda nota e inserita.</value></data>
<data name="Import.Blocked.NoDryRun" xml:space="preserve"><value>Controlla prima la scheda: ogni file e la sua copia vengono verificati di nuovo.</value></data>
<data name="Import.Blocked.NotAllVerified" xml:space="preserve"><value>Alcune foto della scheda non hanno una copia verificata: formattando andrebbero perse.</value></data>
<data name="Import.Blocked.NotArchive" xml:space="preserve"><value>Solo le importazioni nell'archivio di questo PC si possono confrontare con la scheda.</value></data>
<data name="Import.Blocked.NothingImported" xml:space="preserve"><value>Da questa scheda non è ancora stato importato nulla.</value></data>
<data name="Import.Blocked.NothingVerified" xml:space="preserve"><value>Nessuna foto della scheda corrisponde a una copia verificata.</value></data>
<data name="Import.Details.Copy" xml:space="preserve"><value>copia {0} ({1})</value></data>
<data name="Import.Details.Files.one" xml:space="preserve"><value>{0:N0} file</value></data>
<data name="Import.Details.Files.other" xml:space="preserve"><value>{0:N0} file</value></data>
<data name="Import.Details.Session" xml:space="preserve"><value>Sessione {0} · {1}</value></data>
<data name="Import.Details.Unreadable" xml:space="preserve"><value>Impossibile leggere {0}: {1}</value></data>
<data name="Import.Device.Away" xml:space="preserve"><value>{0} · non inserita</value></data>
<data name="Import.Device.Card" xml:space="preserve"><value>{0} · {1} · {2} occupati</value></data>
<data name="Import.Device.NoDcim" xml:space="preserve"><value>{0} · senza DCIM · {1} liberi</value></data>
<data name="Import.Device.Serial" xml:space="preserve"><value>seriale {0}</value></data>
<data name="Import.Device.State.Away" xml:space="preserve"><value>Scheda nota, non inserita</value></data>
<data name="Import.Device.State.Drive" xml:space="preserve"><value>Unità senza DCIM</value></data>
<data name="Import.Device.State.Folder" xml:space="preserve"><value>Cartella</value></data>
<data name="Import.Device.State.Known" xml:space="preserve"><value>Scheda nota</value></data>
<data name="Import.Device.State.New" xml:space="preserve"><value>Nuova scheda</value></data>
<data name="Import.Empty" xml:space="preserve"><value>Inserisci una scheda o una cartella</value></data>
<data name="Import.Erase.Kept.one" xml:space="preserve"><value>{0:N0} conservata</value></data>
<data name="Import.Erase.Kept.other" xml:space="preserve"><value>{0:N0} conservate</value></data>
<data name="Import.Erase.Verdict.CopyMismatch" xml:space="preserve"><value>la copia in archivio è diversa</value></data>
<data name="Import.Erase.Verdict.CopyMissing" xml:space="preserve"><value>manca la copia in archivio</value></data>
<data name="Import.Erase.Verdict.Delete" xml:space="preserve"><value>verificata</value></data>
<data name="Import.Erase.Verdict.SourceChanged" xml:space="preserve"><value>modificata sulla scheda</value></data>
<data name="Import.Erase.Verdict.SourceMissing" xml:space="preserve"><value>non più sulla scheda</value></data>
<data name="Import.Erase.Verified.one" xml:space="preserve"><value>{0:N0} verificata sulla scheda</value></data>
<data name="Import.Erase.Verified.other" xml:space="preserve"><value>{0:N0} verificate sulla scheda</value></data>
<data name="Import.Plan.About" xml:space="preserve"><value>circa {0}</value></data>
<data name="Import.Plan.Duplicates.one" xml:space="preserve"><value>{0:N0} doppione</value></data>
<data name="Import.Plan.Duplicates.other" xml:space="preserve"><value>{0:N0} doppioni</value></data>
<data name="Import.Plan.FreeAfter" xml:space="preserve"><value>{0} liberi dopo l'import</value></data>
<data name="Import.Plan.ImportedBefore.one" xml:space="preserve"><value>{0:N0} già importata</value></data>
<data name="Import.Plan.ImportedBefore.other" xml:space="preserve"><value>{0:N0} già importate</value></data>
<data name="Import.Plan.Sessions.one" xml:space="preserve"><value>{0:N0} sessione</value></data>
<data name="Import.Plan.Sessions.other" xml:space="preserve"><value>{0:N0} sessioni</value></data>
<data name="Import.Plan.SpaceMissing" xml:space="preserve"><value>mancano {0}</value></data>
<data name="Import.Result.NotCommitted" xml:space="preserve"><value>Nessuna copia in archivio dopo l'importazione: vedi il registro dell'attività.</value></data>
<data name="Import.Session.Adds" xml:space="preserve"><value>Si aggiunge a · {0}</value></data>
<data name="Import.Session.New" xml:space="preserve"><value>Nuova · {0}</value></data>
<data name="Import.Session.Server" xml:space="preserve"><value>Server</value></data>
<data name="Import.Start.one" xml:space="preserve"><value>Importa {0:N0} foto</value></data>
<data name="Import.Start.other" xml:space="preserve"><value>Importa {0:N0} foto</value></data>
</root>
+168
View File
@@ -0,0 +1,168 @@
using System.Globalization;
using Titano.App.Infrastructure;
using Titano.Core.Imaging;
using Titano.Core.Localization;
using Titano.Core.Naming;
using Titano.Data.Repositories;
namespace Titano.App.Archiving;
/// <summary>A bar of a chart: label, magnitude, the text after the bar and the hover text.</summary>
public sealed record BarValue(string Label, double Value, string ValueText, string ToolTip);
/// <summary>A heatmap row (one year): photos per month, NaN for months still to come.</summary>
public sealed record HeatRow(string Label, IReadOnlyList<double> Values, IReadOnlyList<string?> ToolTips);
/// <summary>A figure tile of the Archive screen ("618,204" over "photos").</summary>
public sealed record ArchiveTile(string Value, string Caption);
public enum HealthAction
{
None,
Verify,
Proxies,
Analyse,
Duplicates,
Orphans,
}
/// <summary>One line of "Index health": ok or warning, what it found, and the fix it offers.</summary>
public sealed record HealthLine(bool IsOk, string Text, HealthAction Action);
/// <summary>
/// Turns the index statistics into the Archive screen's figures (BRIEF §12.5, Archive.html): tiles, photos per month
/// (heatmap, year rows × 12 months), storage by year, the top cameras and formats by photos (the rest folded into one
/// bar), and the index health lines.
/// </summary>
public static class ArchiveFigures
{
public const int TopCameras = 5;
public const int TopFormats = 6;
public static IReadOnlyList<ArchiveTile> Tiles(ArchiveStats stats)
{
var years = stats.ByYear.Select(y => y.Year).ToList();
var range = years.Count == 0 ? "—"
: years.Min() == years.Max() ? Year(years.Min())
: Loc.L("Archive.Tile.YearRange", Year(years.Min()), Year(years.Max()));
var cameras = stats.ByCamera.Count(c => !string.IsNullOrWhiteSpace(c.Model) || !string.IsNullOrWhiteSpace(c.Make));
return
[
new(Formatting.Count(stats.Photos), Loc.Plural("Archive.Tile.Photos", stats.Photos)),
new(Formatting.Count(stats.Sequences), Loc.Plural("Archive.Tile.Sequences", stats.Sequences)),
new(Formatting.Bytes(stats.Bytes), Loc.L("Archive.Tile.Originals")),
new(Formatting.Count(cameras), Loc.Plural("Archive.Tile.Cameras", cameras) + " · " + Loc.Plural("Archive.Tile.Formats", stats.ByFormat.Count)),
new(range, Loc.L("Archive.Tile.Pending", Formatting.Count(stats.Health.NotAnalyzed), Formatting.Count(stats.Health.Unverified))),
];
}
/// <summary>Culture month initials for the heatmap columns ("J F M …", "G F M …").</summary>
public static IReadOnlyList<string> MonthLabels() =>
[.. Loc.Culture.DateTimeFormat.AbbreviatedMonthNames.Take(12).Select(m => m.Length == 0 ? string.Empty : char.ToUpper(m[0], Loc.Culture).ToString())];
/// <summary>One row per year from the first to the last with photos, oldest first; months after <paramref name="today"/> are NaN.</summary>
public static IReadOnlyList<HeatRow> PhotosPerMonth(ArchiveStats stats, DateTime today)
{
if (stats.ByMonth.Count == 0) return [];
var byKey = stats.ByMonth.ToDictionary(m => (m.Year, m.Month));
var first = stats.ByMonth.Min(m => m.Year);
var last = System.Math.Max(stats.ByMonth.Max(m => m.Year), first);
var rows = new List<HeatRow>(last - first + 1);
for (var year = first; year <= last; year++)
{
var values = new double[12];
var tips = new string?[12];
for (var month = 1; month <= 12; month++)
{
var future = year > today.Year || (year == today.Year && month > today.Month);
byKey.TryGetValue((year, month), out var bucket);
values[month - 1] = future ? double.NaN : bucket?.Photos ?? 0;
tips[month - 1] = future ? null : Loc.L("Archive.Chart.MonthTip", new DateTime(year, month, 1).ToString("Y", Loc.Culture),
Loc.Plural("Shell.Status.Photos", bucket?.Photos ?? 0), Loc.Plural("Shell.Status.Sequences", bucket?.Sequences ?? 0));
}
rows.Add(new HeatRow(Year(year), values, tips));
}
return rows;
}
/// <summary>Bytes of originals per capture year, oldest first.</summary>
public static IReadOnlyList<BarValue> StorageByYear(ArchiveStats stats) =>
[
.. stats.ByYear.OrderBy(y => y.Year).Select(y => new BarValue(Year(y.Year), y.Bytes, Formatting.Bytes(y.Bytes),
Loc.L("Archive.Chart.YearTip", Year(y.Year), Formatting.Bytes(y.Bytes), Loc.Plural("Shell.Status.Sequences", y.Sequences)))),
];
/// <summary>Photos per camera: the <see cref="TopCameras"/> largest, then "n others".</summary>
public static IReadOnlyList<BarValue> ByCamera(ArchiveStats stats)
{
var cameras = stats.ByCamera
.GroupBy(c => PathTemplate.CameraName(c.Make, c.Model) is { Length: > 0 } name ? name : Loc.L("Archive.Chart.UnknownCamera"), StringComparer.Ordinal)
.Select(g => (Name: g.Key, Photos: g.Sum(c => c.Photos)))
.OrderByDescending(c => c.Photos).ThenBy(c => c.Name, StringComparer.CurrentCultureIgnoreCase)
.ToList();
return Shares(cameras, TopCameras, rest => Loc.Plural("Archive.Chart.OtherCameras", rest), stats.Photos);
}
/// <summary>Photos per file format: the <see cref="TopFormats"/> largest, then "Other".</summary>
public static IReadOnlyList<BarValue> ByFormat(ArchiveStats stats)
{
var formats = stats.ByFormat
.GroupBy(f => FormatName(f.Format), StringComparer.Ordinal)
.Select(g => (Name: g.Key, Photos: g.Sum(f => f.Photos)))
.OrderByDescending(f => f.Photos).ThenBy(f => f.Name, StringComparer.Ordinal)
.ToList();
return Shares(formats, TopFormats, _ => Loc.L("Archive.Chart.OtherFormats"), formats.Sum(f => f.Photos));
}
/// <summary>"CR2", "JPEG", "3FR": the format as photographers name it.</summary>
public static string FormatName(ImageFileFormat format) => format switch
{
ImageFileFormat.Unknown => Loc.L("Archive.Chart.OtherFormats"),
ImageFileFormat.ThreeFr => "3FR",
ImageFileFormat.JpegXl => "JXL",
ImageFileFormat.JpegXr => "JXR",
ImageFileFormat.WebP => "WebP",
_ => format.ToString().ToUpperInvariant(),
};
/// <summary>Index health (Archive.html): what needs attention, each with its fix.</summary>
public static IReadOnlyList<HealthLine> Health(ArchiveStats stats, int withoutProxies)
{
var h = stats.Health;
var damaged = h.MissingFrames + h.CorruptFrames;
return
[
h.Unverified == 0
? new HealthLine(true, Loc.L("Archive.Health.AllVerified"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.Unverified", h.Unverified), HealthAction.Verify),
damaged == 0
? new HealthLine(true, Loc.L("Archive.Health.FramesOk"), HealthAction.None)
: new HealthLine(false, Loc.L("Archive.Health.Damaged", Formatting.Count(h.MissingFrames), Formatting.Count(h.CorruptFrames)), HealthAction.Orphans),
withoutProxies == 0
? new HealthLine(true, Loc.L("Archive.Health.ProxiesOk"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.NoProxies", withoutProxies), HealthAction.Proxies),
h.NotAnalyzed == 0
? new HealthLine(true, Loc.L("Archive.Health.AnalysedOk"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.NotAnalysed", h.NotAnalyzed), HealthAction.Analyse),
h.DuplicateFrames == 0
? new HealthLine(true, Loc.L("Archive.Health.NoDuplicates"), HealthAction.None)
: new HealthLine(false, Loc.Plural("Archive.Health.Duplicates", h.DuplicateFrames), HealthAction.Duplicates),
];
}
private static IReadOnlyList<BarValue> Shares(List<(string Name, long Photos)> ranked, int top, Func<int, string> restLabel, long total)
{
var bars = new List<BarValue>(top + 1);
foreach (var (name, photos) in ranked.Take(top)) bars.Add(Bar(name, photos, total));
if (ranked.Count > top) bars.Add(Bar(restLabel(ranked.Count - top), ranked.Skip(top).Sum(c => c.Photos), total));
return bars;
}
private static BarValue Bar(string label, long photos, long total)
{
var share = Formatting.Percent(total > 0 ? photos / (double)total : 0);
return new BarValue(label, photos, share, Loc.L("Archive.Chart.ShareTip", label, Loc.Plural("Shell.Status.Photos", photos), share));
}
private static string Year(int year) => year.ToString(CultureInfo.InvariantCulture);
}
+130
View File
@@ -0,0 +1,130 @@
using System.Globalization;
using Titano.App.Infrastructure;
using Titano.Core.Archiving;
using Titano.Core.Localization;
using Titano.Core.Model;
namespace Titano.App.Archiving;
public enum TreeNodeKind
{
Archive,
Year,
Sequence,
}
/// <summary>A node of the archive tree: the archive, a capture year, or a sequence.</summary>
public sealed class ArchiveTreeNode
{
private readonly List<ArchiveTreeNode> _children = [];
internal ArchiveTreeNode(TreeNodeKind kind, string key, int depth)
{
Kind = kind;
Key = key;
Depth = depth;
}
public TreeNodeKind Kind { get; }
/// <summary>Stable across rebuilds: the archive id, the year, or the sequence id.</summary>
public string Key { get; }
public int Depth { get; }
public required string Title { get; init; }
/// <summary>"18 sequences · 2.1 TB", "3,600 photos · 72 GB".</summary>
public required string Detail { get; init; }
public Sequence? Sequence { get; init; }
public bool IsExpanded { get; set; }
public IReadOnlyList<ArchiveTreeNode> Children => _children;
public bool HasChildren => _children.Count > 0;
internal void Add(ArchiveTreeNode child) => _children.Add(child);
/// <summary>The sequences this node stands for (itself, or all below it).</summary>
public IEnumerable<Sequence> Sequences() => Sequence is { } own ? [own] : _children.SelectMany(c => c.Sequences());
}
/// <summary>
/// The Archive screen's tree (BRIEF §12.5): the archive with its availability, its capture years (newest first) and
/// their sequences (newest first), flattened into the visible rows of a virtualised list. Undated sequences sit under
/// their own node after the years.
/// </summary>
public sealed class ArchiveTree
{
private ArchiveTree(ArchiveTreeNode root, ArchiveAvailability availability)
{
Root = root;
Availability = availability;
}
public ArchiveTreeNode Root { get; }
public ArchiveAvailability Availability { get; }
public static ArchiveTree Build(Core.Model.Archive archive, ArchiveAvailability availability, IReadOnlyList<Sequence> sequences)
{
var root = new ArchiveTreeNode(TreeNodeKind.Archive, archive.Id.ToString("N"), 0)
{
Title = archive.Name,
Detail = Summary(sequences),
IsExpanded = true,
};
foreach (var year in sequences.GroupBy(s => s.FirstCapture?.Year).OrderByDescending(g => g.Key ?? int.MinValue))
{
var members = year.OrderByDescending(s => s.FirstCapture).ThenBy(s => s.DisplayTitle, StringComparer.CurrentCultureIgnoreCase).ToList();
var node = new ArchiveTreeNode(TreeNodeKind.Year, year.Key?.ToString(CultureInfo.InvariantCulture) ?? "undated", 1)
{
Title = year.Key?.ToString(CultureInfo.InvariantCulture) ?? Loc.L("Archive.Tree.Undated"),
Detail = Summary(members),
};
foreach (var s in members)
{
node.Add(new ArchiveTreeNode(TreeNodeKind.Sequence, s.Id.ToString("N"), 2)
{
Title = s.DisplayTitle,
Detail = Loc.Plural("Shell.Status.Photos", s.FrameCount) + " · " + Formatting.Bytes(s.TotalBytes),
Sequence = s,
});
}
root.Add(node);
}
return new ArchiveTree(root, availability);
}
/// <summary>Rows to show: every node whose ancestors are all expanded, depth first.</summary>
public IReadOnlyList<ArchiveTreeNode> Visible()
{
var rows = new List<ArchiveTreeNode>();
void Walk(ArchiveTreeNode node)
{
rows.Add(node);
if (!node.IsExpanded) return;
foreach (var child in node.Children) Walk(child);
}
Walk(Root);
return rows;
}
/// <summary>Expands the nodes whose keys are given (restoring the state after a refresh).</summary>
public void Expand(IReadOnlySet<string> keys)
{
foreach (var year in Root.Children) year.IsExpanded = keys.Contains(year.Key);
}
/// <summary>Keys of the expanded year nodes.</summary>
public IReadOnlySet<string> ExpandedKeys() => Root.Children.Where(c => c.IsExpanded).Select(c => c.Key).ToHashSet(StringComparer.Ordinal);
/// <summary>The set mass operations act on: the sequences of the selected nodes, each once; all of them when nothing is selected.</summary>
public static IReadOnlyList<Sequence> SetOf(ArchiveTree tree, IEnumerable<ArchiveTreeNode> selected)
{
var chosen = selected.ToList();
var source = chosen.Count == 0 ? tree.Root.Sequences() : chosen.SelectMany(n => n.Sequences());
var seen = new HashSet<Guid>();
return [.. source.Where(s => seen.Add(s.Id))];
}
private static string Summary(IReadOnlyCollection<Sequence> sequences) =>
Loc.Plural("Shell.Status.Sequences", sequences.Count) + " · " + Formatting.Bytes(sequences.Sum(s => s.TotalBytes));
}
@@ -0,0 +1,93 @@
using Titano.App.Infrastructure;
using Titano.App.Jobs;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
namespace Titano.App.Archiving;
/// <summary>A group of identical photos: the copies and where they are; the first one counts as the original.</summary>
public sealed record DuplicateRow(string Title, string Detail, IReadOnlyList<string> Places, Guid SequenceId);
/// <summary>What an orphan line is about, which decides its fix.</summary>
public enum OrphanKind
{
/// <summary>Photos in a sequence folder that its sidecar does not list: fix = index them.</summary>
UnlistedFiles,
/// <summary>Frames whose file is gone: fix = reconcile (flags them missing in sidecar and index).</summary>
MissingFiles,
/// <summary>An index row whose folder has no sidecar any more: fix = remove the row (after confirmation).</summary>
MissingSequence,
/// <summary>A folder with photos the index does not know yet: fix = scan.</summary>
NewFolder,
}
/// <summary>One line of the orphans report and the target of its fix.</summary>
public sealed record OrphanRow(OrphanKind Kind, string Title, string Detail, Guid? SequenceId);
/// <summary>A line of "Recent maintenance": date, operation and outcome.</summary>
public sealed record MaintenanceRow(Guid JobId, string DateText, string Text, string StateText, JobState State);
/// <summary>
/// The Maintenance view's reports (BRIEF §12.5): duplicates across sequences, orphans with their safe fixes, and the
/// recent archive jobs. Nothing here deletes a file; the only removal is of index rows whose sidecar is gone.
/// </summary>
public static class MaintenanceReports
{
/// <summary>Kinds of jobs listed under "Recent maintenance".</summary>
public static readonly IReadOnlyList<JobKind> MaintenanceKinds = [JobKind.Scan, JobKind.Rebuild, JobKind.Verify, JobKind.Maintenance];
public static IReadOnlyList<DuplicateRow> Duplicates(IReadOnlyList<DuplicateGroup> groups, IReadOnlyList<SequenceLocation> locations)
{
var titles = locations.ToDictionary(l => l.Id, l => l.Title ?? l.Folder[(l.Folder.LastIndexOf('/') + 1)..]);
return
[
.. groups.Where(g => g.Frames.Count > 1).Select(g =>
{
var first = g.Frames[0];
var name = first.RelativePath[(first.RelativePath.LastIndexOf('/') + 1)..];
var places = g.Frames.Select(f => Loc.L("Archive.Duplicates.Place", titles.GetValueOrDefault(f.SequenceId, f.RelativePath), f.RelativePath)).ToList();
return new DuplicateRow(name, Loc.Plural("Archive.Duplicates.Copies", g.Frames.Count), places, first.SequenceId);
}),
];
}
public static IReadOnlyList<OrphanRow> Orphans(OrphanReport report, IReadOnlyList<SequenceLocation> locations)
{
var byFolder = locations.ToDictionary(l => l.Folder, StringComparer.Ordinal);
var rows = new List<OrphanRow>();
foreach (var s in report.Sequences)
{
byFolder.TryGetValue(s.Folder, out var location);
var title = location?.Title ?? s.Folder;
if (s.UnindexedFiles.Count > 0)
rows.Add(new OrphanRow(OrphanKind.UnlistedFiles, title, Loc.Plural("Archive.Orphans.Unlisted", s.UnindexedFiles.Count), location?.Id));
if (s.MissingFiles.Count > 0)
rows.Add(new OrphanRow(OrphanKind.MissingFiles, title, Loc.Plural("Archive.Orphans.Missing", s.MissingFiles.Count), location?.Id));
}
foreach (var missing in report.MissingSequences)
rows.Add(new OrphanRow(OrphanKind.MissingSequence, missing.Title ?? missing.Folder, Loc.L("Archive.Orphans.NoSidecar", missing.Folder), missing.Id));
foreach (var folder in report.UnindexedFolders)
rows.Add(new OrphanRow(OrphanKind.NewFolder, folder.Length == 0 ? "/" : folder, Loc.L("Archive.Orphans.NewFolder"), null));
return rows;
}
public static IReadOnlyList<MaintenanceRow> Recent(IEnumerable<Job> jobs, int count) =>
[
.. jobs.OrderByDescending(j => j.Finished ?? j.Started ?? j.Created).Take(count).Select(j =>
{
var when = j.Finished ?? j.Started ?? j.Created;
var text = string.IsNullOrWhiteSpace(j.TargetLabel) ? JobText.Kind(j.Kind) : Loc.L("Archive.Recent.Line", JobText.Kind(j.Kind), j.TargetLabel);
var state = j.State switch
{
JobState.Running when j.ProgressTotal > 0 => Formatting.Percent(j.ProgressDone / (double)j.ProgressTotal),
_ => Loc.L("Jobs.State." + j.State),
};
return new MaintenanceRow(j.Id, Formatting.Date(when), text, state, j.State);
}),
];
}
+82
View File
@@ -0,0 +1,82 @@
using Titano.App.Settings;
using Titano.Core.Archiving;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Core.Naming;
using Titano.Data.Repositories;
namespace Titano.App.Archiving;
/// <summary>What a mass rename changes: the sequence titles (Rename) or the folder layout (Move).</summary>
public enum RenameMode
{
Titles,
Folders,
}
/// <summary>One line of the rename dry run: before → after.</summary>
public sealed record RenameRow(Guid SequenceId, string From, string To);
/// <summary>
/// Mass rename and move of sequences (BRIEF §7, §12.5): the template is checked live, the engine's dry run
/// (<see cref="RenamePlan"/>) is listed line by line, and applying makes exactly those changes.
/// </summary>
public static class MassRename
{
/// <summary>The first problem of a template, localized; null when it can be used.</summary>
public static string? Validate(RenameMode mode, string? template)
{
if (string.IsNullOrWhiteSpace(template)) return Loc.L("Errors.Template.Empty", string.Empty, 1);
if (mode == RenameMode.Folders) return NamingPreview.Validate(template, isFolder: true);
var parsed = PathTemplate.Parse(template);
return parsed.Errors is [var error, ..] ? Loc.L("Errors.Template." + error.Code, error.Token ?? string.Empty, error.Position + 1) : null;
}
/// <summary>The changes of a plan as rows: titles or folders, whichever the mode changes.</summary>
public static IReadOnlyList<RenameRow> Rows(RenamePlan plan, RenameMode mode) =>
[
.. plan.Steps.Select(s => mode == RenameMode.Folders
? new RenameRow(s.SequenceId, s.FromFolder, s.ToFolder)
: new RenameRow(s.SequenceId, s.FromTitle ?? string.Empty, s.ToTitle ?? string.Empty)),
];
/// <summary>"12 of 36 sequences change".</summary>
public static string Summary(RenamePlan plan, int setSize) => Loc.Plural("Archive.Rename.Summary", plan.Steps.Count, setSize);
}
/// <summary>Adding and removing tags on a set of sequences (BRIEF §12.5 Retag).</summary>
public static class Retag
{
/// <summary>"sunset, harbour; Holy grail" → normalized, distinct tags; entries that cannot be tags are left out.</summary>
public static IReadOnlyList<string> Parse(string? text)
{
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
return [.. Split(text).Where(IsValid).Select(TagRepository.Normalize).Where(seen.Add)];
}
/// <summary>The problem of a tag list as one sentence; null when every entry can be a tag.</summary>
public static string? Validate(string? text) => Split(text).All(IsValid) ? null : Loc.L("Archive.Retag.Invalid", TagRepository.MaxLength);
private static IEnumerable<string> Split(string? text) =>
string.IsNullOrWhiteSpace(text) ? [] : text.Split([',', ';'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
private static bool IsValid(string tag) => tag.Length is > 0 and <= TagRepository.MaxLength && !tag.Any(char.IsControl);
/// <summary>The new tag list of one sequence: removals first, then additions not already present.</summary>
public static IReadOnlyList<string> Apply(IReadOnlyList<string> current, IReadOnlyList<string> add, IReadOnlyList<string> remove)
{
var removed = new HashSet<string>(remove, StringComparer.OrdinalIgnoreCase);
var result = current.Where(t => !removed.Contains(t)).ToList();
var present = new HashSet<string>(result, StringComparer.OrdinalIgnoreCase);
result.AddRange(add.Where(present.Add));
return result;
}
/// <summary>Only the sequences whose tags change, with their new tags (the dry run).</summary>
public static IReadOnlyList<(Sequence Sequence, IReadOnlyList<string> Tags)> Changes(IEnumerable<Sequence> sequences, IReadOnlyList<string> add,
IReadOnlyList<string> remove) =>
[
.. sequences.Select(s => (Sequence: s, Tags: Apply(s.Tags, add, remove)))
.Where(c => !c.Tags.SequenceEqual(c.Sequence.Tags, StringComparer.Ordinal)),
];
}
+62
View File
@@ -0,0 +1,62 @@
using Titano.App.Infrastructure;
using Titano.Core.Import;
using Titano.Core.Localization;
namespace Titano.App.Import;
/// <summary>
/// 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.
/// </summary>
/// <param name="CardPhotos">Photos on the card now (the scanner's count), for the format check.</param>
public sealed record CardActions(ImportDestinationKind Destination, bool IsKnownCard, bool IsInserted, bool IsRemovable, ImportOutcome? Outcome,
ErasePlan? Erase, int CardPhotos)
{
/// <summary>The dry run of "Delete imported" may start.</summary>
public bool CanCheck => CheckBlocker is null;
public string? CheckBlocker =>
Destination != ImportDestinationKind.Archive ? Loc.L("Import.Blocked.NotArchive")
: !IsKnownCard || !IsInserted ? Loc.L("Import.Blocked.NoCard")
: Outcome is not { Imported: > 0 } and not { Skipped: > 0 } ? Loc.L("Import.Blocked.NothingImported")
: null;
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);
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")
: null);
public bool CanEject => IsInserted && IsRemovable;
public int DeleteCount => Erase?.ToDelete.Count() ?? 0;
public long DeleteBytes => Erase?.ToDelete.Sum(i => i.Candidate.Size) ?? 0;
public int KeptCount => Erase?.Items.Count(i => i.Verdict != EraseVerdict.Delete) ?? 0;
/// <summary>"4,812 verified on the card · 96.2 GB · 2 kept": the dry run in one line; empty before it ran.</summary>
public string EraseText => Erase is null
? string.Empty
: string.Join(" · ", new[]
{
Loc.Plural("Import.Erase.Verified", DeleteCount),
Formatting.Bytes(DeleteBytes),
KeptCount > 0 ? Loc.Plural("Import.Erase.Kept", KeptCount) : string.Empty,
}.Where(t => t.Length > 0));
/// <summary>Why each kept photo stays on the card ("IMG_0042.CR2 · the archive copy no longer matches").</summary>
public IReadOnlyList<string> KeptReasons =>
Erase is null ? [] : [.. Erase.Items.Where(i => i.Verdict != EraseVerdict.Delete).Select(i => Loc.L("Import.Erase.Reason", i.Candidate.SourcePath, Verdict(i.Verdict)))];
public static string Verdict(EraseVerdict verdict) => Loc.L("Import.Erase.Verdict." + verdict);
}
+61
View File
@@ -0,0 +1,61 @@
using System.Globalization;
using Titano.Data.Archiving;
namespace Titano.App.Import;
/// <summary>
/// A volume the Import screen offers (BRIEF §12.4, §13): a removable volume, or any volume with a <c>DCIM</c> folder.
/// Card identity is <c>{serial:X8}-{label}-{capacity}</c>, stable across readers and drive letters.
/// </summary>
/// <param name="Root">Root with trailing separator, e.g. <c>E:\</c>.</param>
public sealed record CardDevice(string Root, string Label, uint SerialNumber, long TotalBytes, long FreeBytes, bool IsRemovable, bool HasDcim)
{
public string Identity => string.Create(CultureInfo.InvariantCulture, $"{SerialNumber:X8}-{Label}-{TotalBytes}");
public long UsedBytes => System.Math.Max(0, TotalBytes - FreeBytes);
public CardVolume ToVolume() => new(Identity, SerialNumber, Label.Length == 0 ? null : Label, TotalBytes);
}
/// <summary>Outcome of a safe removal.</summary>
public enum EjectOutcome
{
Ejected,
/// <summary>Files on the card are still open; nothing changed.</summary>
InUse,
/// <summary>Not a removable drive, or Windows refused.</summary>
Failed,
}
/// <summary>Outcome of the operating system's format dialog (BRIEF §8.6).</summary>
public enum FormatOutcome
{
Formatted,
Cancelled,
Failed,
}
/// <summary>
/// Card detection and the two OS actions on a card (BRIEF §8.6, §13): safe removal and the Windows format dialog.
/// Events may arrive on any thread.
/// </summary>
public interface IDeviceMonitor : IDisposable
{
event Action<CardDevice>? Arrived;
event Action<CardDevice>? Removed;
/// <summary>Candidate volumes present now.</summary>
IReadOnlyList<CardDevice> Devices { get; }
/// <summary>Starts watching (idempotent); the first snapshot raises no events.</summary>
void Start();
/// <summary>Locks, dismounts and ejects the volume; blocks for up to a few seconds.</summary>
EjectOutcome Eject(CardDevice device);
/// <summary>Shows the Windows format dialog for the volume; call on the UI thread.</summary>
FormatOutcome Format(CardDevice device);
}
+102
View File
@@ -0,0 +1,102 @@
using System.Globalization;
using Titano.App.Infrastructure;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Core.Naming;
namespace Titano.App.Import;
public enum SourceKind
{
/// <summary>A card with a DCIM tree, or a remembered card.</summary>
Card,
/// <summary>A removable drive without DCIM: imported as a plain folder tree.</summary>
Drive,
/// <summary>A folder picked with "Folder…".</summary>
Folder,
}
/// <summary>
/// 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. <see cref="Key"/> is the card identity or the folder path.
/// </summary>
public sealed record ImportSource(string Key, SourceKind Kind, CardDevice? Device, MediaCard? Card, string? FolderPath)
{
public bool IsInserted => Device is not null || FolderPath is not null;
public bool IsKnown => Card is not null;
/// <summary>Where the photos are read from; null for a remembered card that is not inserted.</summary>
public string? Root => Device?.Root ?? FolderPath;
/// <summary>"EOS R5 · card A", the volume label, or the drive.</summary>
public string Title => Card?.Nickname is { Length: > 0 } nickname ? nickname
: Device is { Label.Length: > 0 } labelled ? labelled.Label
: Device?.Root ?? FolderPath ?? Card?.Label ?? string.Empty;
/// <summary>"E:\ · 128 GB · 96.2 GB used", "no DCIM · 1.8 TB free", "Canon EOS R5 · not inserted".</summary>
public string Subtitle => Kind switch
{
SourceKind.Folder => FolderPath ?? string.Empty,
SourceKind.Drive => Loc.L("Import.Device.NoDcim", Device!.Root, Formatting.Bytes(Device.FreeBytes)),
_ when Device is not null => Loc.L("Import.Device.Card", Device.Root, Formatting.Bytes(Device.TotalBytes), Formatting.Bytes(Device.UsedBytes)),
_ => Loc.L("Import.Device.Away", CameraText(Card) is { Length: > 0 } camera ? camera : Formatting.Bytes(Card!.Capacity)),
};
/// <summary>"Known card", "New card", … for the state dot's tooltip.</summary>
public string StateText => Loc.L(Kind switch
{
SourceKind.Folder => "Import.Device.State.Folder",
SourceKind.Drive => "Import.Device.State.Drive",
_ when !IsInserted => "Import.Device.State.Away",
_ when IsKnown => "Import.Device.State.Known",
_ => "Import.Device.State.New",
});
/// <summary>"128 GB · serial 3F2A-9C11 · Canon EOS R5 #0431": the identity line of the profile header.</summary>
public string IdentityText
{
get
{
var parts = new List<string>();
if (Device is not null) parts.Add(Device.Root);
var capacity = Device?.TotalBytes ?? Card?.Capacity ?? 0;
if (capacity > 0) parts.Add(Formatting.Bytes(capacity));
var serial = Device?.SerialNumber ?? Card?.VolumeSerial;
if (serial is { } s && Kind != SourceKind.Folder) parts.Add(Loc.L("Import.Device.Serial", SerialText(s)));
if (CameraText(Card) is { Length: > 0 } camera) parts.Add(Card?.CameraSerial is { Length: > 0 } body ? Loc.L("Import.Device.Body", camera, body) : camera);
return string.Join(" · ", parts);
}
}
/// <summary>"3F2A-9C11", as Windows shows a volume serial.</summary>
public static string SerialText(uint serial)
{
var hex = serial.ToString("X8", CultureInfo.InvariantCulture);
return hex[..4] + "-" + hex[4..];
}
private static string CameraText(MediaCard? card) => card is null ? string.Empty : PathTemplate.CameraName(card.CameraMake, card.CameraModel);
}
/// <summary>Builds the Devices list: inserted volumes first (in drive order), then remembered cards, then a picked folder.</summary>
public static class CardPool
{
public static IReadOnlyList<ImportSource> Build(IEnumerable<CardDevice> devices, IEnumerable<MediaCard> cards, string? folder)
{
var known = cards.ToDictionary(c => c.Identity, StringComparer.Ordinal);
var list = new List<ImportSource>();
foreach (var device in devices.OrderBy(d => d.Root, StringComparer.OrdinalIgnoreCase))
{
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));
}
foreach (var card in known.Values.OrderByDescending(c => c.LastSeen))
list.Add(new ImportSource(card.Identity, SourceKind.Card, null, card, null));
if (folder is not null) list.Add(new ImportSource(folder, SourceKind.Folder, null, null, folder));
return list;
}
}
+94
View File
@@ -0,0 +1,94 @@
using Titano.Core.Import;
using Titano.Core.Naming;
using Titano.Data.Archiving;
namespace Titano.App.Import;
/// <summary>Where the photos go (BRIEF §12.4): this PC's archive, a plain folder, or the signed-in server.</summary>
public enum ImportDestinationKind
{
Archive,
Folder,
Server,
}
/// <summary>
/// The Import form (Import.html): scan, sessions, names, duplicates and after-import choices. Remembered per card as
/// its <see cref="CardProfile"/>; a session gap of 0 minutes in a profile means "do not split into sessions".
/// </summary>
public sealed record ImportChoices
{
public ImportDestinationKind Destination { get; init; } = ImportDestinationKind.Archive;
public string? FolderPath { get; init; }
public bool IncludeSubfolders { get; init; } = true;
public bool SplitSessions { get; init; } = true;
public int GapMinutes { get; init; } = 45;
public int MinimumFrames { get; init; } = 8;
public bool VerifyCopies { get; init; } = true;
public string FolderTemplate { get; init; } = TemplateDefaults.Folder;
public string FileTemplate { get; init; } = TemplateDefaults.File;
public DuplicatePolicy Duplicates { get; init; } = DuplicatePolicy.Skip;
public AfterImport AfterImport { get; init; } = AfterImport.Keep;
public bool Eject { get; init; } = true;
public bool DeepAnalysis { get; init; } = true;
/// <summary>A card's remembered choices.</summary>
public static ImportChoices FromProfile(CardProfile profile) => new()
{
IncludeSubfolders = profile.IncludeSubfolders,
SplitSessions = profile.SessionGapMinutes > 0,
GapMinutes = profile.SessionGapMinutes > 0 ? profile.SessionGapMinutes : new CardProfile().SessionGapMinutes,
MinimumFrames = profile.MinimumSessionFrames,
VerifyCopies = profile.VerifyCopies,
FolderTemplate = profile.FolderTemplate,
FileTemplate = profile.FileTemplate,
Duplicates = profile.Duplicates,
AfterImport = profile.AfterImport,
Eject = profile.Eject,
DeepAnalysis = profile.DeepAnalysis,
};
public CardProfile ToProfile() => new()
{
FolderTemplate = FolderTemplate,
FileTemplate = FileTemplate,
SessionGapMinutes = SplitSessions ? GapMinutes : 0,
MinimumSessionFrames = MinimumFrames,
IncludeSubfolders = IncludeSubfolders,
VerifyCopies = VerifyCopies,
Duplicates = Duplicates,
AfterImport = AfterImport,
Eject = Eject,
DeepAnalysis = DeepAnalysis,
};
/// <summary>Planner options; <paramref name="titles"/> are the session titles typed in the plan, by session key.</summary>
public ImportOptions ToOptions(IReadOnlyDictionary<string, string>? 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<string, string>(),
};
/// <summary>True when a choice changes which photos form which session or where they go, so the card must be planned again.</summary>
public bool NeedsNewPlan(ImportChoices previous) =>
previous.Destination != Destination
|| !string.Equals(previous.FolderPath, FolderPath, StringComparison.OrdinalIgnoreCase)
|| previous.IncludeSubfolders != IncludeSubfolders
|| previous.SplitSessions != SplitSessions
|| (SplitSessions && previous.GapMinutes != GapMinutes)
|| previous.MinimumFrames != MinimumFrames
|| previous.FolderTemplate != FolderTemplate
|| previous.FileTemplate != FileTemplate;
/// <summary>Session titles feed the names only when a template uses <c>{title}</c>.</summary>
public bool TitlesChangeNames => UsesTitle(FolderTemplate) || UsesTitle(FileTemplate);
private static bool UsesTitle(string template) => template.Contains("{title", StringComparison.OrdinalIgnoreCase);
}
+147
View File
@@ -0,0 +1,147 @@
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);
}
}
+205
View File
@@ -0,0 +1,205 @@
using Titano.App.Infrastructure;
using Titano.Core.Archiving;
using Titano.Core.Import;
using Titano.Core.Localization;
using Titano.Core.Naming;
namespace Titano.App.Import;
/// <summary>Figures of a plan for the Plan panel header and the Import button (Import.html).</summary>
public sealed record PlanTotals(int Sessions, int Photos, int CopyCount, long CopyBytes, int Duplicates, int ImportedBefore, int Problems,
long? FreeAfter, TimeSpan Estimate)
{
public bool HasEnoughSpace => FreeAfter is not < 0;
/// <summary>"3 sessions · 4,812 photos · 96.2 GB · about 12 min".</summary>
public string Text => string.Join(" · ",
Loc.Plural("Import.Plan.Sessions", Sessions),
Loc.Plural("Shell.Status.Photos", Photos),
Formatting.Bytes(CopyBytes),
Loc.L("Import.Plan.About", Formatting.Duration(Estimate)));
/// <summary>"0 duplicates" (already in the archive by hash).</summary>
public string DuplicatesText => Loc.Plural("Import.Plan.Duplicates", Duplicates);
/// <summary>"12 imported before" (same file from this card), or empty.</summary>
public string ImportedBeforeText => ImportedBefore > 0 ? Loc.Plural("Import.Plan.ImportedBefore", ImportedBefore) : string.Empty;
/// <summary>"2.1 TB free after import", "3.2 GB missing", or empty when the volume does not report its space.</summary>
public string SpaceText => FreeAfter switch
{
null => string.Empty,
< 0 => Loc.L("Import.Plan.SpaceMissing", Formatting.Bytes(-FreeAfter.Value)),
_ => Loc.L("Import.Plan.FreeAfter", Formatting.Bytes(FreeAfter.Value)),
};
/// <summary>"Import 4,812 photos".</summary>
public string ImportText => Loc.Plural("Import.Start", CopyCount);
}
/// <summary>One row of the session table: what the planner grouped and where it goes.</summary>
/// <param name="DefaultTitle">Title the sequence gets when the user types none (its folder name).</param>
/// <param name="IsNew">The session starts a new sequence (false: it continues one imported before).</param>
public sealed record PlanSessionRow(int Number, string SessionKey, string? Title, string DefaultTitle, string TimeText, string FramesText,
string SizeText, string CameraText, string TargetText, bool IsNew);
public enum PlanRowKind
{
Session,
File,
}
/// <summary>A line of "Show plan": a session heading or one file and its destination.</summary>
public sealed record PlanDetailRow(PlanRowKind Kind, string Source, string Destination, string ActionText, string SizeText, bool IsSkipped);
/// <summary>Everything "Show plan" lists: every file to its destination, the totals and the conflicts.</summary>
public sealed record PlanDetails(IReadOnlyList<PlanDetailRow> Rows, string TotalsText, IReadOnlyList<string> Conflicts);
/// <summary>
/// Turns an <see cref="ImportPlan"/> into what the Import screen shows (BRIEF §12.4 "Plan before touching disk") and
/// applies the choices that need no new scan of the card: the duplicate choice and session titles.
/// </summary>
public static class ImportPlans
{
/// <summary>Planning speed assumption for the estimate: a UHS-I card read plus the read-back of the copy.</summary>
public const double BytesPerSecond = 80e6;
public const double VerifiedBytesPerSecond = 55e6;
public static TimeSpan Estimate(long bytes, bool verify) =>
TimeSpan.FromSeconds(System.Math.Ceiling(bytes / (verify ? VerifiedBytesPerSecond : BytesPerSecond)));
public static PlanTotals Totals(ImportPlan plan, bool verify) => new(
plan.Sessions.Count,
plan.FileCount,
plan.CopyCount,
plan.CopyBytes,
plan.Items.Count(i => i.ExistingFrameId is not null),
plan.Items.Count(i => i.Action == ImportAction.SkipImported),
plan.Problems.Count,
plan.AvailableBytes is { } free ? free - plan.CopyBytes : null,
Estimate(plan.CopyBytes, verify));
/// <summary>
/// The plan with every photo already in the archive skipped or imported again, as the planner would have decided
/// (BRIEF §13: skip, or import again; never overwrite).
/// </summary>
public static ImportPlan WithDuplicates(ImportPlan plan, DuplicatePolicy policy) => plan with
{
Sessions =
[
.. plan.Sessions.Select(s => s with
{
Items =
[
.. s.Items.Select(i => i.ExistingFrameId is null || i.Action == ImportAction.SkipImported
? i
: i with { Action = policy == DuplicatePolicy.Skip ? ImportAction.SkipInArchive : ImportAction.Copy }),
],
}),
],
};
/// <summary>The plan with the typed session titles (when no template uses <c>{title}</c>, so no name changes).</summary>
public static ImportPlan WithTitles(ImportPlan plan, IReadOnlyDictionary<string, string> titles) => plan with
{
Sessions = [.. plan.Sessions.Select(s => s with { Title = titles.TryGetValue(s.SessionKey, out var t) && !string.IsNullOrWhiteSpace(t) ? t.Trim() : null })],
};
/// <param name="existingFolders">Folders of sequences already in the archive (a session there continues it).</param>
public static IReadOnlyList<PlanSessionRow> Sessions(ImportPlan plan, ImportDestinationKind destination, IReadOnlySet<string> existingFolders) =>
[
.. plan.Sessions.Select(s =>
{
var isNew = !existingFolders.Contains(s.Folder);
var leaf = s.Folder[(s.Folder.LastIndexOf('/') + 1)..];
var target = destination switch
{
ImportDestinationKind.Server => Loc.L("Import.Session.Server"),
ImportDestinationKind.Folder => s.Folder,
_ => 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);
}),
];
/// <summary>"Show plan": every file and where it goes, with the conflicts the plan resolved or reports.</summary>
public static PlanDetails Details(ImportPlan plan, ImportDestinationKind destination, bool verify)
{
var rows = new List<PlanDetailRow>(plan.FileCount + plan.Sessions.Count);
foreach (var s in plan.Sessions)
{
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)
{
rows.Add(new PlanDetailRow(PlanRowKind.File, item.SourcePath, folder + "/" + item.FileName, ActionText(item),
Formatting.Bytes(item.Size), item.Action != ImportAction.Copy));
}
}
var totals = Totals(plan, verify);
var conflicts = new List<string>();
foreach (var problem in plan.Problems) conflicts.Add(Loc.L("Import.Details.Unreadable", problem.RelativePath, problem.Message));
if (totals.Duplicates > 0) conflicts.Add(totals.DuplicatesText);
if (!totals.HasEnoughSpace) conflicts.Add(totals.SpaceText);
var text = string.Join(" · ", new[]
{
Loc.Plural("Import.Details.Files", plan.FileCount),
Loc.L("Import.Details.Copy", Formatting.Count(plan.CopyCount), Formatting.Bytes(plan.CopyBytes)),
totals.ImportedBeforeText,
totals.Duplicates > 0 ? totals.DuplicatesText : string.Empty,
}.Where(t => t.Length > 0));
return new PlanDetails(rows, text, conflicts);
}
/// <summary>The destination of a session as the user will find it: the archive's originals folder, the folder, or the server.</summary>
public static string DestinationFolder(PlannedSession session, ImportDestinationKind destination) => destination switch
{
ImportDestinationKind.Archive => session.Folder + "/" + ArchiveLayout.OriginalsFolder,
ImportDestinationKind.Server => Loc.L("Import.Session.Server"),
_ => session.Folder,
};
/// <summary>"→ 2026/2026-09-28 Canon EOS R5/2026-09-28_18-02-11_0001.CR2": the first file the plan copies.</summary>
public static string? FirstDestination(ImportPlan plan, ImportDestinationKind destination)
{
foreach (var s in plan.Sessions)
{
if (s.Items.FirstOrDefault(i => i.Action == ImportAction.Copy) is { } item)
return destination == ImportDestinationKind.Server ? item.FileName : s.Folder + "/" + item.FileName;
}
return null;
}
private static string ActionText(PlannedItem item) => item.Action switch
{
ImportAction.SkipImported => Loc.L("Import.Action.ImportedBefore"),
ImportAction.SkipInArchive => Loc.L("Import.Action.Duplicate"),
_ when item.ExistingFrameId is not null => Loc.L("Import.Action.Again"),
_ => Loc.L("Import.Action.Copy"),
};
/// <summary>"28/09/2026 · 18:02 → 20:04".</summary>
private static string TimeText(PlannedSession s) =>
Loc.L("Import.Session.Time", Formatting.Date(s.Start), s.Start.ToString("t", Loc.Culture), s.End.ToString("t", Loc.Culture));
/// <summary>"3,600 · 2 s": photos and the median interval between them.</summary>
private static string FramesText(PlannedSession s)
{
var interval = MedianInterval(s.Items);
var count = Formatting.Count(s.Items.Count);
return interval is { } median ? Loc.L("Import.Session.Frames", count, Formatting.Duration(median)) : count;
}
internal static TimeSpan? MedianInterval(IReadOnlyList<PlannedItem> items)
{
if (items.Count < 2) return null;
var gaps = new List<double>(items.Count - 1);
for (var i = 1; i < items.Count; i++) gaps.Add((items[i].CapturedAt - items[i - 1].CapturedAt).TotalSeconds);
gaps.Sort();
var middle = gaps.Count / 2;
var seconds = gaps.Count % 2 == 1 ? gaps[middle] : (gaps[middle - 1] + gaps[middle]) / 2;
return TimeSpan.FromSeconds(System.Math.Max(0, seconds));
}
}
+51
View File
@@ -0,0 +1,51 @@
using System.IO;
using Titano.Core.Import;
using Titano.Protocol;
using Titano.Protocol.Client;
namespace Titano.App.Import;
/// <summary>
/// Import destination "Server" (BRIEF §12.4, §14.4): each verified copy staged on this PC is uploaded with the
/// protocol's resumable chunks and the hash computed while copying; the server checks that hash before it commits the
/// frame, and only then does the photo count as imported. Photos of one plan session share its session key, so the
/// server files them in one sequence — the same one when an interrupted import is started again. A typed session
/// title is set on that sequence once.
/// </summary>
public sealed class ServerImportSink(TitanoClient client, string stagingDirectory) : IImportSink
{
private readonly HashSet<Guid> _titled = [];
public string StagingDirectory { get; } = stagingDirectory;
public async Task<IReadOnlyList<SinkCommit>> CommitAsync(PlannedSession session, IReadOnlyList<StagedFile> files, CancellationToken cancellationToken)
{
var results = new List<SinkCommit>(files.Count);
foreach (var file in files)
{
// The server records the uploaded file name as the original name: upload the card's name, not the staging name.
var folder = Path.Combine(StagingDirectory, Path.GetFileNameWithoutExtension(file.StagedPath));
var named = Path.Combine(folder, Path.GetFileName(file.Item.SourcePath));
Directory.CreateDirectory(folder);
File.Move(file.StagedPath, named);
try
{
var 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);
}
finally
{
// Our own staged copy; the original stays on the card.
File.Delete(named);
Directory.Delete(folder);
}
}
return results;
}
}
@@ -0,0 +1,243 @@
using System.Globalization;
using Titano.App.Archiving;
using Titano.Core.Archiving;
using Titano.Core.Imaging;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Archiving;
using Titano.Data.Repositories;
using Titano.Testing;
namespace Titano.App.Tests.Archiving;
public sealed class ArchiveLogicTests
{
private static readonly Core.Model.Archive Main = new() { Id = Guid.CreateVersion7(), Name = "TitanoArchive", RootPath = "/archive" };
public ArchiveLogicTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
private static Sequence Seq(string title, DateTime? first, int frames = 100, long bytes = 1_000_000, params string[] tags) => new()
{
Id = Guid.CreateVersion7(),
ArchiveId = Main.Id,
Title = title,
Folder = $"{first?.Year.ToString(CultureInfo.InvariantCulture) ?? "misc"}/{title}",
FirstCapture = first,
FrameCount = frames,
TotalBytes = bytes,
Tags = tags,
};
[Test]
public void TheTreeGroupsByYearNewestFirstWithUndatedLast()
{
var a = Seq("Harbour", new DateTime(2026, 9, 28), 3_600, 72_000_000_000);
var b = Seq("Comet", new DateTime(2026, 8, 13));
var c = Seq("Stars", new DateTime(2025, 8, 2));
var d = Seq("Scans", null);
var tree = ArchiveTree.Build(Main, ArchiveAvailability.Online, [c, d, b, a]);
Assert.Equal("TitanoArchive", tree.Root.Title);
Assert.SequenceEqual(new[] { "2026", "2025", "Undated" }, tree.Root.Children.Select(n => n.Title));
Assert.SequenceEqual(new[] { "Harbour", "Comet" }, tree.Root.Children[0].Children.Select(n => n.Title));
Assert.Equal("3,600 photos · 72 GB", tree.Root.Children[0].Children[0].Detail);
Assert.Equal("2 sequences · 72 GB", tree.Root.Children[0].Detail);
// Collapsed years show only themselves; expanding one reveals its sequences.
Assert.Equal(4, tree.Visible().Count);
tree.Root.Children[1].IsExpanded = true;
Assert.SequenceEqual(new[] { "TitanoArchive", "2026", "2025", "Stars", "Undated" }, tree.Visible().Select(n => n.Title));
var expanded = tree.ExpandedKeys();
var rebuilt = ArchiveTree.Build(Main, ArchiveAvailability.ReadOnly, [a, b, c, d]);
rebuilt.Expand(expanded);
Assert.Equal(5, rebuilt.Visible().Count);
Assert.Equal(ArchiveAvailability.ReadOnly, rebuilt.Availability);
}
[Test]
public void TheMassOperationSetIsTheSelectionOrEverything()
{
var a = Seq("Harbour", new DateTime(2026, 9, 28));
var b = Seq("Comet", new DateTime(2026, 8, 13));
var c = Seq("Stars", new DateTime(2025, 8, 2));
var tree = ArchiveTree.Build(Main, ArchiveAvailability.Online, [a, b, c]);
Assert.Equal(3, ArchiveTree.SetOf(tree, []).Count);
var year = tree.Root.Children[0];
Assert.SequenceEqual(new[] { a.Id, b.Id }, ArchiveTree.SetOf(tree, [year]).Select(s => s.Id));
// A year and one of its sequences count that sequence once.
Assert.Equal(2, ArchiveTree.SetOf(tree, [year, year.Children[1]]).Count);
Assert.Equal(3, ArchiveTree.SetOf(tree, [tree.Root]).Count);
}
private static ArchiveStats Stats() => new(
Sequences: 12,
Photos: 10_000,
Bytes: 9_400_000_000_000,
ByYear: [new YearBucket(2026, 8, 6_000, 2_100_000_000_000), new YearBucket(2024, 4, 4_000, 1_400_000_000_000)],
ByMonth: [new MonthBucket(2024, 3, 2, 1_000), new MonthBucket(2026, 7, 5, 5_000), new MonthBucket(2026, 9, 1, 2_000)],
ByCamera:
[
new CameraBucket("Canon", "Canon EOS R5", 4, 4_100, 0),
new CameraBucket("SONY", "ILCE-7M4", 2, 2_200, 0),
new CameraBucket("NIKON CORPORATION", "NIKON Z 8", 2, 1_400, 0),
new CameraBucket("GoPro", "HERO12 Black", 1, 900, 0),
new CameraBucket("Apple", "iPhone 16 Pro", 1, 600, 0),
new CameraBucket("DJI", "Osmo Action 5 Pro", 1, 500, 0),
new CameraBucket(null, null, 1, 300, 0),
],
ByFormat:
[
new FormatBucket(ImageFileFormat.Cr2, 3_800, 0),
new FormatBucket(ImageFileFormat.Arw, 2_100, 0),
new FormatBucket(ImageFileFormat.Jpeg, 2_000, 0),
new FormatBucket(ImageFileFormat.Nef, 1_400, 0),
new FormatBucket(ImageFileFormat.Dng, 400, 0),
new FormatBucket(ImageFileFormat.Heif, 200, 0),
new FormatBucket(ImageFileFormat.ThreeFr, 60, 0),
new FormatBucket(ImageFileFormat.Unknown, 40, 0),
],
Health: new ArchiveHealth(Unverified: 3, WithWarnings: 1, WithErrors: 0, NotAnalyzed: 2, MissingFrames: 0, CorruptFrames: 0, DuplicateFrames: 5));
[Test]
public void ChartsAggregateYearsCamerasAndFormats()
{
var stats = Stats();
var years = ArchiveFigures.StorageByYear(stats);
Assert.SequenceEqual(new[] { "2024", "2026" }, years.Select(b => b.Label));
Assert.Equal("2.1 TB", years[1].ValueText);
var cameras = ArchiveFigures.ByCamera(stats);
Assert.Equal(ArchiveFigures.TopCameras + 1, cameras.Count);
Assert.Equal("Canon EOS R5", cameras[0].Label);
Assert.Equal("41 %", cameras[0].ValueText);
Assert.Equal("SONY ILCE-7M4", cameras[1].Label);
Assert.Equal("Nikon Z 8", cameras[2].Label);
Assert.Equal("2 others", cameras[^1].Label);
Assert.Equal(800, cameras[^1].Value);
Assert.Equal(stats.Photos, (long)cameras.Sum(c => c.Value));
var formats = ArchiveFigures.ByFormat(stats);
Assert.SequenceEqual(new[] { "CR2", "ARW", "JPEG", "NEF", "DNG", "HEIF", "Other" }, formats.Select(f => f.Label));
Assert.Equal(100, formats[^1].Value);
Assert.Equal("3FR", ArchiveFigures.FormatName(ImageFileFormat.ThreeFr));
}
[Test]
public void ThePhotosPerMonthHeatmapHasOneRowPerYearAndNoFuture()
{
var rows = ArchiveFigures.PhotosPerMonth(Stats(), new DateTime(2026, 10, 2));
Assert.SequenceEqual(new[] { "2024", "2025", "2026" }, rows.Select(r => r.Label));
Assert.Equal(1_000, rows[0].Values[2]);
Assert.Equal(0, rows[1].Values[5]);
Assert.Equal(5_000, rows[2].Values[6]);
Assert.Equal(0, rows[2].Values[9]);
Assert.True(double.IsNaN(rows[2].Values[10]));
Assert.Null(rows[2].ToolTips[11]);
Assert.Equal("July 2026 · 5,000 photos · 5 sequences", rows[2].ToolTips[6]);
Assert.Equal(12, ArchiveFigures.MonthLabels().Count);
Assert.Equal("J", ArchiveFigures.MonthLabels()[0]);
Assert.Empty(ArchiveFigures.PhotosPerMonth(Stats() with { ByMonth = [] }, DateTime.Today));
}
[Test]
public void TilesAndHealthSummariseTheIndex()
{
var tiles = ArchiveFigures.Tiles(Stats());
Assert.Equal(5, tiles.Count);
Assert.Equal("10,000", tiles[0].Value);
Assert.Equal("9.4 TB", tiles[2].Value);
Assert.Equal("6", tiles[3].Value);
Assert.Equal("cameras · 8 formats", tiles[3].Caption);
Assert.Equal("2024 – 2026", tiles[4].Value);
var health = ArchiveFigures.Health(Stats(), withoutProxies: 0);
Assert.Equal(HealthAction.Verify, health[0].Action);
Assert.False(health[0].IsOk);
Assert.Equal("3 sequences unverified", health[0].Text);
Assert.True(health[1].IsOk);
Assert.True(health[2].IsOk);
Assert.Equal(HealthAction.Analyse, health[3].Action);
Assert.Equal(HealthAction.Duplicates, health[4].Action);
}
[Test]
public void RenameTemplatesAreCheckedAndThePlanListedByMode()
{
Assert.Null(MassRename.Validate(RenameMode.Titles, "{title} · {camera}"));
Assert.NotNull(MassRename.Validate(RenameMode.Titles, "{title"));
Assert.NotNull(MassRename.Validate(RenameMode.Titles, " "));
Assert.Null(MassRename.Validate(RenameMode.Folders, "{year}/{date} {title}"));
Assert.NotNull(MassRename.Validate(RenameMode.Folders, "{nope}"));
var a = Seq("Harbour", new DateTime(2026, 9, 28));
var b = Seq("Comet", new DateTime(2026, 8, 13));
var subjects = new[] { a, b }.Select(s => new RenameSubject(s.Id, s.Folder, s.Title, s.FirstCapture, "Canon", "Canon EOS R5", null)).ToList();
var titles = RenamePlanner.Plan(Main.Id, subjects, null, "{title} · {camera}", subjects.Select(s => s.Folder), _ => false);
var rows = MassRename.Rows(titles, RenameMode.Titles);
Assert.Equal("Harbour", rows[0].From);
Assert.Equal("Harbour · Canon EOS R5", rows[0].To);
Assert.Equal("2 of 5 change", MassRename.Summary(titles, 5));
Assert.Empty(RenamePlanner.Plan(Main.Id, subjects, "{year}/{title}", null, subjects.Select(s => s.Folder), _ => false).Steps);
var folders = RenamePlanner.Plan(Main.Id, subjects, "{year}/{date} {title}", null, subjects.Select(s => s.Folder), _ => false);
var moves = MassRename.Rows(folders, RenameMode.Folders);
Assert.SequenceEqual(new[] { "2026/Harbour", "2026/Comet" }, moves.Select(r => r.From));
Assert.SequenceEqual(new[] { "2026/2026-09-28 Harbour", "2026/2026-08-13 Comet" }, moves.Select(r => r.To));
}
[Test]
public void RetagAddsAndRemovesWithoutDuplicates()
{
Assert.SequenceEqual(new[] { "sunset", "harbour", "Holy grail" }, Retag.Parse(" sunset, harbour; Holy grail ,SUNSET"));
Assert.Empty(Retag.Parse(" "));
Assert.Null(Retag.Validate("sunset, dusk"));
Assert.NotNull(Retag.Validate(new string('x', 65)));
Assert.Empty(Retag.Parse(new string('x', 65)));
Assert.SequenceEqual(new[] { "harbour", "dusk" }, Retag.Apply(["Sunset", "harbour"], ["dusk", "HARBOUR"], ["sunset"]));
var a = Seq("A", DateTime.Today, tags: ["sunset"]);
var b = Seq("B", DateTime.Today, tags: ["dusk"]);
var changes = Retag.Changes([a, b], ["dusk"], []);
Assert.Equal(a.Id, changes.Single().Sequence.Id);
Assert.SequenceEqual(new[] { "sunset", "dusk" }, changes.Single().Tags);
}
[Test]
public void ReportsListDuplicatesOrphansAndRecentMaintenance()
{
var a = Seq("Harbour", new DateTime(2026, 9, 28));
var b = Seq("Comet", new DateTime(2026, 8, 13));
var locations = new[] { new SequenceLocation(a.Id, a.Folder, a.Title), new SequenceLocation(b.Id, b.Folder, null) };
var groups = new[]
{
new DuplicateGroup("ab", [new DuplicateFrame("ab", Guid.NewGuid(), a.Id, 0, a.Folder + "/originals/IMG_1.CR2"),
new DuplicateFrame("ab", Guid.NewGuid(), b.Id, 4, b.Folder + "/originals/IMG_9.CR2")]),
new DuplicateGroup("cd", [new DuplicateFrame("cd", Guid.NewGuid(), a.Id, 1, a.Folder + "/originals/IMG_2.CR2")]),
};
var duplicates = MaintenanceReports.Duplicates(groups, locations);
Assert.Equal("IMG_1.CR2", duplicates.Single().Title);
Assert.Equal("2 copies", duplicates.Single().Detail);
Assert.Equal(a.Id, duplicates.Single().SequenceId);
Assert.Contains("Comet", duplicates.Single().Places[1]);
var report = new OrphanReport(
[new SequenceOrphans(a.Folder, ["IMG_7.CR2", "IMG_8.CR2"], [])],
[new SequenceLocation(Guid.NewGuid(), "2024/Gone", "Gone")],
["2026/New shoot"]);
var orphans = MaintenanceReports.Orphans(report, locations);
Assert.SequenceEqual(new[] { OrphanKind.UnlistedFiles, OrphanKind.MissingSequence, OrphanKind.NewFolder }, orphans.Select(o => o.Kind));
Assert.Equal("2 photos not in sidecar", orphans[0].Detail);
Assert.Equal(a.Id, orphans[0].SequenceId);
Assert.Null(orphans[2].SequenceId);
var now = DateTimeOffset.UtcNow;
var jobs = new[]
{
new Job { Id = Guid.NewGuid(), Kind = JobKind.Verify, State = JobState.Running, ProgressDone = 71, ProgressTotal = 100, TargetLabel = "TitanoArchive", Created = now },
new Job { Id = Guid.NewGuid(), Kind = JobKind.Scan, State = JobState.Done, Created = now.AddDays(-2), Finished = now.AddDays(-2) },
};
var recent = MaintenanceReports.Recent(jobs, 5);
Assert.Equal("Verify · TitanoArchive", recent[0].Text);
Assert.Equal("71 %", recent[0].StateText);
Assert.Equal(JobState.Done, recent[1].State);
}
}
@@ -0,0 +1,65 @@
using System.Globalization;
using Titano.App.Import;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Testing;
namespace Titano.App.Tests.Import;
public sealed class CardPoolTests
{
private static readonly CardDevice CardA = new(@"F:\", "EOS_DIGITAL", 0x3F2A9C11, 128_000_000_000, 31_800_000_000, true, true);
private static readonly CardDevice GoPro = new(@"E:\", "GOPRO", 0x11112222, 64_000_000_000, 52_600_000_000, true, true);
private static readonly CardDevice Usb = new(@"G:\", "BACKUP", 0x99990000, 2_000_000_000_000, 1_800_000_000_000, true, false);
public CardPoolTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
private static MediaCard Known(CardDevice device, string nickname, DateTimeOffset seen) => new()
{
Id = Guid.CreateVersion7(),
Identity = device.Identity,
VolumeSerial = device.SerialNumber,
Label = device.Label,
Capacity = device.TotalBytes,
Nickname = nickname,
CameraMake = "Canon",
CameraModel = "Canon EOS R5",
CameraSerial = "0431",
LastSeen = seen,
};
[Test]
public void InsertedVolumesComeFirstThenRememberedCardsThenTheFolder()
{
var away = new CardDevice(@"H:\", "NIKON", 0x55556666, 64_000_000_000, 0, true, true);
var cards = new[] { Known(CardA, "EOS R5 · card A", DateTimeOffset.UtcNow), Known(away, "Z 8 card", DateTimeOffset.UtcNow.AddDays(-3)) };
var pool = CardPool.Build([Usb, CardA, GoPro], cards, @"D:\Shoots\2026");
Assert.SequenceEqual(new[] { GoPro.Identity, CardA.Identity, Usb.Identity, away.Identity, @"D:\Shoots\2026" }, pool.Select(s => s.Key));
Assert.Equal(SourceKind.Card, pool[0].Kind);
Assert.False(pool[0].IsKnown);
Assert.Equal("GOPRO", pool[0].Title);
Assert.Equal("New card", pool[0].StateText);
Assert.Equal("EOS R5 · card A", pool[1].Title);
Assert.Equal("Known card", pool[1].StateText);
Assert.Equal(@"F:\ · 128 GB · 96.2 GB used", pool[1].Subtitle);
Assert.Equal(@"F:\ · 128 GB · serial 3F2A-9C11 · Canon EOS R5 #0431", pool[1].IdentityText);
Assert.Equal(SourceKind.Drive, pool[2].Kind);
Assert.Equal(@"G:\ · no DCIM · 1.8 TB free", pool[2].Subtitle);
Assert.False(pool[3].IsInserted);
Assert.Null(pool[3].Root);
Assert.Equal("Canon EOS R5 · not inserted", pool[3].Subtitle);
Assert.Equal(SourceKind.Folder, pool[4].Kind);
Assert.Equal(@"D:\Shoots\2026", pool[4].Root);
}
[Test]
public void AKnownCardWithoutDcimIsStillACard()
{
var pool = CardPool.Build([Usb], [Known(Usb, "Backup card", DateTimeOffset.UtcNow)], null);
Assert.Equal(SourceKind.Card, pool.Single().Kind);
Assert.Equal("3F2A-9C11", ImportSource.SerialText(0x3F2A9C11));
Assert.Equal("3F2A9C11-EOS_DIGITAL-128000000000", CardA.Identity);
Assert.Equal("EOS_DIGITAL", CardA.ToVolume().Label);
}
}
@@ -0,0 +1,69 @@
using Titano.App.Import;
using Titano.Core.Import;
using Titano.Data.Archiving;
using Titano.Testing;
namespace Titano.App.Tests.Import;
public sealed class ImportChoicesTests
{
[Test]
public void ACardProfileRoundTripsThroughTheForm()
{
var profile = new CardProfile
{
FolderTemplate = "{year}/{title}",
FileTemplate = "{name}",
SessionGapMinutes = 30,
MinimumSessionFrames = 4,
IncludeSubfolders = false,
VerifyCopies = false,
Duplicates = DuplicatePolicy.ImportAgain,
AfterImport = AfterImport.DeleteImported,
Eject = false,
DeepAnalysis = false,
};
var choices = ImportChoices.FromProfile(profile);
Assert.True(choices.SplitSessions);
Assert.Equal(30, choices.GapMinutes);
Assert.Equal(profile, choices.ToProfile());
}
[Test]
public void SessionsOffIsRememberedAsAZeroGapAndNeverSplits()
{
var off = new ImportChoices { SplitSessions = false, GapMinutes = 20 };
Assert.Equal(0, off.ToProfile().SessionGapMinutes);
Assert.Equal(TimeSpan.MaxValue, off.ToOptions().SessionGap);
var back = ImportChoices.FromProfile(off.ToProfile());
Assert.False(back.SplitSessions);
Assert.Equal(new CardProfile().SessionGapMinutes, back.GapMinutes);
Assert.Equal(TimeSpan.FromMinutes(20), (off with { SplitSessions = true }).ToOptions().SessionGap);
}
[Test]
public void OptionsCarryTheFormAndTheTypedTitles()
{
var titles = new Dictionary<string, string> { ["k"] = "Harbour" };
var options = new ImportChoices { MinimumFrames = 0, Duplicates = DuplicatePolicy.ImportAgain, VerifyCopies = false }.ToOptions(titles);
Assert.Equal(1, options.MinimumSessionFrames);
Assert.Equal(DuplicatePolicy.ImportAgain, options.Duplicates);
Assert.False(options.VerifyCopies);
Assert.Equal("Harbour", options.Titles["k"]);
}
[Test]
public void OnlyGroupingAndNamingChoicesNeedANewPlan()
{
var a = new ImportChoices();
Assert.False((a with { Duplicates = DuplicatePolicy.ImportAgain, VerifyCopies = false, Eject = false, AfterImport = AfterImport.FormatCard }).NeedsNewPlan(a));
Assert.False((a with { SplitSessions = false, GapMinutes = 5 }).NeedsNewPlan(a with { SplitSessions = false }));
Assert.True((a with { GapMinutes = 5 }).NeedsNewPlan(a));
Assert.True((a with { MinimumFrames = 2 }).NeedsNewPlan(a));
Assert.True((a with { FileTemplate = "{name}" }).NeedsNewPlan(a));
Assert.True((a with { Destination = ImportDestinationKind.Folder }).NeedsNewPlan(a));
Assert.True((a with { IncludeSubfolders = false }).NeedsNewPlan(a));
Assert.False(a.TitlesChangeNames);
Assert.True((a with { FolderTemplate = "{year}/{Title:upper}" }).TitlesChangeNames);
}
}
@@ -0,0 +1,139 @@
using System.Globalization;
using System.IO;
using Titano.App.Import;
using Titano.Core.Import;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Archiving;
using Titano.Data.Jobs;
using Titano.Testing;
using static Titano.App.Tests.Import.ImportTestData;
namespace Titano.App.Tests.Import;
/// <summary>Card to archive and card to folder on real files and a real index, and the card-erase gate around them.</summary>
[Category("import")]
public sealed class ImportFlowTests(TestContext context)
{
private static readonly CardVolume Volume = new("3F2A9C11-EOS_DIGITAL-127865454592", 0x3F2A9C11, "EOS_DIGITAL", 127_865_454_592);
[Test]
public async Task AVerifiedArchiveImportUnlocksDeleteAndFormat()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
await using var db = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "titano.db")), context.Cancellation);
var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = "test" });
var archive = await archives.AdoptAsync(context.CreateTempDirectory("archive"), "Main", context.Cancellation);
var card = Card(context.CreateTempDirectory("card"), 10, Start, Start.AddHours(3), Start.AddDays(1));
var media = await archives.Imports.RegisterCardAsync(Volume, context.Cancellation);
var destination = new ArchiveDestination(archives, archive);
var choices = new ImportChoices();
var plan = await destination.PlanAsync(card, choices.ToOptions(), media.Id, null, context.Cancellation);
Assert.Equal(3, plan.Sessions.Count);
Assert.Equal(30, plan.CopyCount);
var before = new CardActions(ImportDestinationKind.Archive, true, true, true, null, null, plan.FileCount);
Assert.False(before.CanCheck, "no import, no erase");
Assert.False(before.CanDelete);
Assert.False(before.CanFormat);
Assert.Equal(Loc.L("Import.Blocked.NothingImported"), before.CheckBlocker);
var unverified = await archives.PlanCardEraseAsync(archive.Id, media.Id, card, null, context.Cancellation);
Assert.Empty(unverified.ToDelete);
await using (var engine = new JobEngine(db, archives.CreateJobHandlers(),
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) }))
{
await engine.StartAsync(context.Cancellation);
var job = await engine.EnqueueAsync(ArchiveJobs.Import(archive, plan, media.Id, choices.VerifyCopies), cancellationToken: context.Cancellation);
await WaitDoneAsync(db, job.Id);
}
var outcome = await ImportOutcome.CheckAsync(plan, choices.VerifyCopies,
async (item, ct) => (await db.Frames.FindBySizeAndCaptureAsync(archive.Id, item.Size, item.CapturedAt, ct)).Count > 0, context.Cancellation);
Assert.Equal(30, outcome.Copied);
Assert.True(outcome.IsComplete);
var afterImport = new CardActions(ImportDestinationKind.Archive, true, true, true, outcome, null, plan.FileCount);
Assert.True(afterImport.CanCheck);
Assert.False(afterImport.CanDelete, "the dry run must come first");
Assert.Equal(Loc.L("Import.Blocked.NoDryRun"), afterImport.DeleteBlocker);
var erase = await archives.PlanCardEraseAsync(archive.Id, media.Id, card, null, context.Cancellation);
var ready = afterImport with { Erase = erase };
Assert.True(ready.CanDelete);
Assert.True(ready.CanFormat);
Assert.Equal(30, ready.DeleteCount);
Assert.Equal("30 verified on the card · " + Infrastructure.Formatting.Bytes(ready.DeleteBytes), ready.EraseText);
// A photo added to the card after the import is not verified: formatting would lose it.
Assert.False((ready with { CardPhotos = 31 }).CanFormat);
Assert.False((ready with { Outcome = outcome with { Verified = false } }).CanFormat);
Assert.False((ready with { Destination = ImportDestinationKind.Folder }).CanDelete);
Assert.False((ready with { IsInserted = false }).CanDelete);
Assert.False((ready with { IsRemovable = false }).CanEject);
var result = CardEraser.Execute(erase, context.Cancellation);
Assert.Equal(30, result.Deleted);
Assert.Empty(Directory.EnumerateFiles(Path.Combine(card, "DCIM", "100CANON")));
}
[Test]
public async Task AFolderImportCopiesWithoutAnIndexAndFindsItsOwnCopiesAgain()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var card = Card(context.CreateTempDirectory("card"), 8, Start, Start.AddHours(2));
var target = context.CreateTempDirectory("folder");
var destination = new UnindexedDestination(ImportDestinationKind.Folder, target);
var plan = await destination.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
Assert.Equal(2, plan.Sessions.Count);
Assert.Equal(16, plan.CopyCount);
var reports = new List<ImportProgress>();
var outcome = await LocalImport.RunAsync(plan, new FolderImportSink(target), verify: true, new Progress(reports), context.Cancellation);
Assert.Equal(16, outcome.Copied);
Assert.True(outcome.IsComplete);
Assert.True(outcome.Verified);
Assert.NotEmpty(reports);
foreach (var s in plan.Sessions)
foreach (var item in s.Items)
Assert.FileExists(Path.Combine(target, s.Folder, item.FileName));
var again = await LocalImport.RunAsync(await destination.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation),
new FolderImportSink(target), verify: true, null, context.Cancellation);
Assert.Equal(0, again.Copied);
Assert.Equal(16, again.Duplicates);
Assert.False(new CardActions(ImportDestinationKind.Folder, false, true, true, outcome, null, 16).CanCheck);
}
[Test]
public async Task TheIndexCheckReportsPlannedCopiesThatNeverArrived()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var plan = Plan(null, null, Session(1, "f", Item(1, Start), Item(2, Start.AddSeconds(2)), Item(3, Start.AddSeconds(4), action: ImportAction.SkipImported)));
var outcome = await ImportOutcome.CheckAsync(plan, true, (item, _) => Task.FromResult(!item.SourcePath.EndsWith("0002.CR2", StringComparison.Ordinal)),
context.Cancellation);
Assert.Equal(1, outcome.Copied);
Assert.Equal(1, outcome.Skipped);
Assert.Equal("DCIM/100CANON/IMG_0002.CR2", outcome.Failures.Single().SourcePath);
Assert.False(outcome.IsComplete);
}
private async Task WaitDoneAsync(TitanoDatabase db, Guid jobId)
{
var until = DateTime.UtcNow.AddSeconds(60);
while (DateTime.UtcNow < until)
{
var job = await db.Jobs.GetAsync(jobId, context.Cancellation);
if (job?.State == JobState.Done) return;
if (job?.State is JobState.Failed or JobState.Cancelled) Assert.Fail($"Import job ended {job.State}: {job.Error}\n{job.LogTail}");
await Task.Delay(25, context.Cancellation);
}
Assert.Fail("The import job did not finish");
}
/// <summary>Collects reports synchronously (System.Progress posts to the thread pool).</summary>
private sealed class Progress(List<ImportProgress> into) : IProgress<ImportProgress>
{
public void Report(ImportProgress value) => into.Add(value);
}
}
@@ -0,0 +1,136 @@
using System.Globalization;
using Titano.App.Import;
using Titano.Core.Import;
using Titano.Core.Localization;
using Titano.Testing;
using static Titano.App.Tests.Import.ImportTestData;
namespace Titano.App.Tests.Import;
public sealed class ImportPlansTests
{
public ImportPlansTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
private static ImportPlan ThreeSessions(long? available = 200_000_000)
{
var duplicate = Guid.CreateVersion7();
return Plan(available, [new ImportProblem("DCIM/100CANON/IMG_0099.CR2", "Access denied")],
Session(1, "2026/2026-09-28 Canon EOS R5", Item(1, Start), Item(2, Start.AddSeconds(2)), Item(3, Start.AddSeconds(4)), Item(4, Start.AddSeconds(8))),
Session(2, "2026/2026-09-29 Canon EOS R5", Item(5, Start.AddDays(1)), Item(6, Start.AddDays(1).AddSeconds(120), action: ImportAction.SkipInArchive, existing: duplicate)),
Session(3, "2026/2026-09-30 Canon EOS R5", Item(7, Start.AddDays(2), action: ImportAction.SkipImported)));
}
[Test]
public void TotalsCountWhatIsCopiedSkippedAndDuplicated()
{
var totals = ImportPlans.Totals(ThreeSessions(), verify: true);
Assert.Equal(3, totals.Sessions);
Assert.Equal(7, totals.Photos);
Assert.Equal(5, totals.CopyCount);
Assert.Equal(5 * 24_000_000L, totals.CopyBytes);
Assert.Equal(1, totals.Duplicates);
Assert.Equal(1, totals.ImportedBefore);
Assert.Equal(1, totals.Problems);
Assert.Equal(80_000_000L, totals.FreeAfter);
Assert.True(totals.HasEnoughSpace);
Assert.Equal(TimeSpan.FromSeconds(3), totals.Estimate);
Assert.Equal("3 sessions · 7 photos · 120 MB · about 3 s", totals.Text);
Assert.Equal("1 duplicate", totals.DuplicatesText);
Assert.Equal("Import 5 photos", totals.ImportText);
Assert.Equal("80 MB free after import", totals.SpaceText);
}
[Test]
public void ASmallDestinationReportsTheMissingSpace()
{
var totals = ImportPlans.Totals(ThreeSessions(available: 100_000_000), verify: false);
Assert.False(totals.HasEnoughSpace);
Assert.Equal("20 MB missing", totals.SpaceText);
Assert.Equal(string.Empty, ImportPlans.Totals(ThreeSessions(available: null), verify: false).SpaceText);
Assert.Equal(TimeSpan.FromSeconds(2), ImportPlans.Totals(ThreeSessions(), verify: false).Estimate);
}
[Test]
public void TheDuplicateChoiceFlipsOnlyPhotosAlreadyInTheArchive()
{
var plan = ThreeSessions();
var again = ImportPlans.WithDuplicates(plan, DuplicatePolicy.ImportAgain);
Assert.Equal(6, again.CopyCount);
var duplicate = again.Sessions[1].Items[1];
Assert.Equal(ImportAction.Copy, duplicate.Action);
Assert.NotNull(duplicate.ExistingFrameId);
Assert.Equal(ImportAction.SkipImported, again.Sessions[2].Items[0].Action);
var skipped = ImportPlans.WithDuplicates(again, DuplicatePolicy.Skip);
Assert.Equal(ImportAction.SkipInArchive, skipped.Sessions[1].Items[1].Action);
Assert.Equal(plan.CopyCount, skipped.CopyCount);
Assert.Equal("Import again", ImportPlans.Details(again, ImportDestinationKind.Archive, true).Rows.Single(r => r.Source.EndsWith("0006.CR2", StringComparison.Ordinal)).ActionText);
}
[Test]
public void TypedTitlesNameTheirSessions()
{
var plan = ThreeSessions();
var titled = ImportPlans.WithTitles(plan, new Dictionary<string, string>
{
[plan.Sessions[0].SessionKey] = " Harbour sunset ",
[plan.Sessions[1].SessionKey] = " ",
["unknown"] = "ignored",
});
Assert.Equal("Harbour sunset", titled.Sessions[0].Title);
Assert.Null(titled.Sessions[1].Title);
Assert.Null(titled.Sessions[2].Title);
Assert.SequenceEqual(plan.Items.Select(i => i.FileName), titled.Items.Select(i => i.FileName));
}
[Test]
public void SessionRowsShowTimesIntervalsAndWhereTheyGo()
{
var plan = ThreeSessions();
var rows = ImportPlans.Sessions(plan, ImportDestinationKind.Archive, new HashSet<string>(["2026/2026-09-29 Canon EOS R5"], StringComparer.OrdinalIgnoreCase));
Assert.Equal(3, rows.Count);
Assert.Equal("4 · 2 s", rows[0].FramesText);
Assert.Equal("2 · 2 min", rows[1].FramesText);
Assert.Equal("1", rows[2].FramesText);
Assert.True(rows[0].IsNew);
Assert.Equal("New · 2026/2026-09-28 Canon EOS R5", rows[0].TargetText);
Assert.False(rows[1].IsNew);
Assert.Equal("Adds to · 2026/2026-09-29 Canon EOS R5", rows[1].TargetText);
Assert.Equal("2026-09-28 Canon EOS R5", rows[0].DefaultTitle);
Assert.Equal("Canon EOS R5", rows[0].CameraText);
Assert.Equal("96 MB", rows[0].SizeText);
Assert.Contains("6:02", rows[0].TimeText);
Assert.Equal("Server", ImportPlans.Sessions(plan, ImportDestinationKind.Server, new HashSet<string>())[0].TargetText);
}
[Test]
public void ShowPlanListsEveryFileItsDestinationAndTheConflicts()
{
var plan = ThreeSessions(available: 100_000_000);
var details = ImportPlans.Details(plan, ImportDestinationKind.Archive, verify: true);
Assert.Equal(plan.FileCount + plan.Sessions.Count, details.Rows.Count);
Assert.Equal(3, details.Rows.Count(r => r.Kind == PlanRowKind.Session));
var first = details.Rows[1];
Assert.Equal("DCIM/100CANON/IMG_0001.CR2", first.Source);
Assert.Equal("2026/2026-09-28 Canon EOS R5/originals/" + plan.Sessions[0].Items[0].FileName, first.Destination);
Assert.Equal("Copy", first.ActionText);
Assert.False(first.IsSkipped);
Assert.True(details.Rows.Single(r => r.Source.EndsWith("0007.CR2", StringComparison.Ordinal)).IsSkipped);
Assert.Equal(3, details.Conflicts.Count);
Assert.Contains("IMG_0099.CR2", details.Conflicts[0]);
Assert.Equal("7 files · copy 5 (120 MB) · 1 imported before · 1 duplicate", details.TotalsText);
var folder = ImportPlans.Details(plan, ImportDestinationKind.Folder, verify: true);
Assert.Equal("2026/2026-09-28 Canon EOS R5/" + plan.Sessions[0].Items[0].FileName, folder.Rows[1].Destination);
}
[Test]
public void TheNamePreviewIsTheFirstCopiedFile()
{
var plan = ThreeSessions();
Assert.Equal("2026/2026-09-28 Canon EOS R5/" + plan.Sessions[0].Items[0].FileName, ImportPlans.FirstDestination(plan, ImportDestinationKind.Archive));
Assert.Equal(plan.Sessions[0].Items[0].FileName, ImportPlans.FirstDestination(plan, ImportDestinationKind.Server));
var nothing = ImportPlans.WithDuplicates(Plan(null, null, Session(1, "x", Item(1, Start, action: ImportAction.SkipImported))), DuplicatePolicy.Skip);
Assert.Null(ImportPlans.FirstDestination(nothing, ImportDestinationKind.Archive));
}
}
@@ -0,0 +1,76 @@
using System.IO;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Imaging;
using Titano.Core.Import;
using Titano.Core.Metadata;
namespace Titano.App.Tests.Import;
/// <summary>Synthetic cards (small JPEGs with Exif) and hand-made plans for the Import tests.</summary>
internal static class ImportTestData
{
public static readonly DateTime Start = new(2026, 9, 28, 18, 2, 11);
/// <summary>A real JPEG with Exif make, model, body serial and capture time; each seed gives distinct bytes.</summary>
public static byte[] Jpeg(DateTime captured, int seed)
{
using var image = new ImageBuffer(32, 24);
var random = new Random(seed);
for (var c = 0; c < 3; c++)
{
var plane = image.Plane(c);
var level = (float)random.NextDouble();
for (var i = 0; i < plane.Length; i++) plane[i] = level * (0.5f + 0.5f * (i % 32) / 32f);
}
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { Make = "Canon", Model = "Canon EOS R5", BodySerial = "0431", CapturedAt = captured, Iso = 100 });
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 80, App1Segments = [exif[4..]] });
}
/// <summary>A card folder with <c>DCIM/100CANON</c>: one series per start time, <paramref name="perSession"/> photos 2 s apart.</summary>
public static string Card(string root, int perSession, params DateTime[] starts)
{
var folder = Path.Combine(root, "DCIM", "100CANON");
Directory.CreateDirectory(folder);
var n = 1;
foreach (var start in starts)
{
for (var i = 0; i < perSession; i++, n++)
File.WriteAllBytes(Path.Combine(folder, $"IMG_{n:0000}.JPG"), Jpeg(start.AddSeconds(2 * i), n));
}
return root;
}
public static PlannedItem Item(int n, DateTime captured, long size = 24_000_000, ImportAction action = ImportAction.Copy, Guid? existing = null) => new()
{
SourcePath = $"DCIM/100CANON/IMG_{n:0000}.CR2",
Size = size,
ModifiedUtc = captured,
CapturedAt = captured,
FileName = $"{captured:yyyy-MM-dd_HH-mm-ss}_{n:0000}.CR2",
Action = action,
ExistingFrameId = existing,
};
public static PlannedSession Session(int number, string folder, params PlannedItem[] items) => new()
{
Number = number,
SessionKey = $"card-0000000{number}-key",
CameraMake = "Canon",
CameraModel = "Canon EOS R5",
Format = ImageFileFormat.Cr2,
Start = items[0].CapturedAt,
End = items[^1].CapturedAt,
Folder = folder,
Items = items,
};
public static ImportPlan Plan(long? available, IReadOnlyList<ImportProblem>? problems = null, params PlannedSession[] sessions) => new()
{
Id = Guid.CreateVersion7(),
SourceRoot = "/card",
DestinationRoot = "/archive",
Sessions = sessions,
Problems = problems ?? [],
AvailableBytes = available,
};
}
@@ -0,0 +1,147 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography;
using Titano.App.Import;
using Titano.Core.Import;
using Titano.Protocol;
using Titano.Protocol.Client;
using Titano.Testing;
using static Titano.App.Tests.Import.ImportTestData;
namespace Titano.App.Tests.Import;
/// <summary>The "Server" destination over the real protocol client against an in-memory upload endpoint.</summary>
[Category("import")]
public sealed class ServerImportSinkTests(TestContext context)
{
[Test]
public async Task EveryPhotoIsUploadedInChunksUnderItsCardNameAndVerifiedByHash()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start, Start.AddHours(2));
var staging = context.CreateTempDirectory("staging");
var server = new FakeUploadServer(chunkSize: 256);
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
plan = plan with { Sessions = [plan.Sessions[0] with { Title = "Harbour sunset" }, .. plan.Sessions.Skip(1)] };
var outcome = await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
Assert.Equal(16, outcome.Copied);
Assert.True(outcome.IsComplete);
Assert.Equal(16, server.Committed.Count);
foreach (var item in plan.Items)
{
var bytes = File.ReadAllBytes(Path.Combine(card, item.SourcePath));
var upload = server.Committed.Single(u => u.FileName == Path.GetFileName(item.SourcePath));
Assert.BytesEqual(bytes, upload.Bytes);
Assert.Equal(Convert.ToHexStringLower(SHA256.HashData(bytes)), upload.Sha256);
Assert.True(upload.Chunks > 1, "files larger than a chunk travel in several chunks");
}
Assert.SequenceEqual(plan.Sessions.Select(s => s.SessionKey).Order(StringComparer.Ordinal),
server.Committed.Select(u => u.SessionKey).Distinct().Order(StringComparer.Ordinal));
Assert.Equal("Harbour sunset", server.Titles.Single().Value);
Assert.Empty(Directory.EnumerateFileSystemEntries(staging));
}
[Test]
public async Task PhotosTheServerAlreadyHoldsAreNotSentAgain()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start);
var staging = context.CreateTempDirectory("staging");
var server = new FakeUploadServer(chunkSize: 4096);
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
var destination = new UnindexedDestination(ImportDestinationKind.Server, staging);
var plan = await destination.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
var chunks = server.ChunkCount;
var again = await LocalImport.RunAsync(plan, new ServerImportSink(client, staging), verify: true, null, context.Cancellation);
Assert.Equal(0, again.Copied);
Assert.Equal(8, again.Duplicates);
Assert.Equal(chunks, server.ChunkCount);
}
[Test]
public async Task AHashTheServerRejectsStopsTheImport()
{
var card = Card(context.CreateTempDirectory("card"), 8, Start);
var staging = context.CreateTempDirectory("staging");
var server = new FakeUploadServer(chunkSize: 4096) { Corrupt = true };
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
var error = await Assert.ThrowsAsync<TitanoApiException>(() => LocalImport.RunAsync(plan, new ServerImportSink(client, staging), true, null, context.Cancellation));
Assert.Equal(HttpStatusCode.UnprocessableEntity, error.Status);
Assert.Empty(server.Committed);
}
/// <summary>Start, chunk and complete of BRIEF §14.4, plus the title update; the hash is checked at completion.</summary>
private sealed class FakeUploadServer(int chunkSize) : HttpMessageHandler
{
private readonly Dictionary<Guid, Upload> _open = [];
private readonly Dictionary<string, Guid> _sequences = new(StringComparer.Ordinal);
private readonly Dictionary<string, Guid> _frames = new(StringComparer.Ordinal);
public List<Upload> Committed { get; } = [];
public Dictionary<Guid, string> Titles { get; } = [];
public int ChunkCount { get; private set; }
/// <summary>Flip a byte of every received chunk (a damaged transfer).</summary>
public bool Corrupt { get; init; }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var path = request.RequestUri!.AbsolutePath.Trim('/').Split('/');
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads"])
{
var start = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.StartUploadRequest, cancellationToken))!;
if (_frames.TryGetValue(start.Sha256, out var existing))
return Json(new StartUploadResponse(null, chunkSize, [], true, existing), ProtocolJson.Default.StartUploadResponse);
var upload = new Upload(Guid.NewGuid(), start.FileName, start.Size, start.Sha256, start.SessionKey ?? "user");
_open[upload.Id] = upload;
return Json(new StartUploadResponse(upload.Id, chunkSize, [], false, null), ProtocolJson.Default.StartUploadResponse);
}
if (request.Method == HttpMethod.Put && path is ["api", "v1", "uploads", var id, "chunks", var n])
{
var upload = _open[Guid.Parse(id)];
var bytes = await request.Content!.ReadAsByteArrayAsync(cancellationToken);
if (Corrupt) bytes[0] ^= 0xFF;
upload.Parts[int.Parse(n, System.Globalization.CultureInfo.InvariantCulture)] = bytes;
ChunkCount++;
return Json(new ChunkReceived(int.Parse(n, System.Globalization.CultureInfo.InvariantCulture), [.. upload.Parts.Keys]), ProtocolJson.Default.ChunkReceived);
}
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads", var done, "complete"])
{
var upload = _open[Guid.Parse(done)];
if (Convert.ToHexStringLower(SHA256.HashData(upload.Bytes)) != upload.Sha256)
return Json(new ApiError("hash_mismatch", "Hash mismatch"), ProtocolJson.Default.ApiError, HttpStatusCode.UnprocessableEntity);
_open.Remove(upload.Id);
Committed.Add(upload);
var frame = Guid.NewGuid();
_frames[upload.Sha256] = frame;
if (!_sequences.TryGetValue(upload.SessionKey, out var sequence)) _sequences[upload.SessionKey] = sequence = Guid.NewGuid();
return Json(new CompleteUploadResponse(frame, sequence, Committed.Count(u => u.SessionKey == upload.SessionKey) - 1),
ProtocolJson.Default.CompleteUploadResponse);
}
if (request.Method == HttpMethod.Patch && path is ["api", "v1", "sequences", var sequenceId])
{
var update = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.UpdateSequenceRequest, cancellationToken))!;
Titles[Guid.Parse(sequenceId)] = update.Title!;
return Json(new SequenceDto(Guid.Parse(sequenceId), update.Title, update.Title!, "f", null, null, null, null, null, 0, null, 0, 0, 0, null, [], 0, null,
1, 1, 0, null, DateTimeOffset.UtcNow), ProtocolJson.Default.SequenceDto);
}
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
private static HttpResponseMessage Json<T>(T value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> type, HttpStatusCode status = HttpStatusCode.OK) =>
new(status) { Content = JsonContent.Create(value, type) };
public sealed record Upload(Guid Id, string FileName, long Size, string Sha256, string SessionKey)
{
public SortedDictionary<int, byte[]> Parts { get; } = [];
public byte[] Bytes => [.. Parts.Values.SelectMany(p => p)];
public int Chunks => Parts.Count;
}
}
}