diff --git a/i18n/en/Tooltips.resx b/i18n/en/Tooltips.resx
index 1d5c8702..fcb3a77b 100644
--- a/i18n/en/Tooltips.resx
+++ b/i18n/en/Tooltips.resx
@@ -54,6 +54,43 @@
Collapse navigation to icons
Show navigation names
Stop the current job · it can be resumed later
+ Add a folder to watch
+ Pick files, a folder or a card
+ Pick one or more photos
+ Pick a folder or a card; every photo inside is added
+ Do not connect to this server
+ Choose the sequence
+ Close settings
+ Delete each source file once the server has verified it
+ Delete the local file only after the server has confirmed its hash; asks before turning on
+ Titano Server found on this network · click to use it
+ SHA-256 fingerprint of the server certificate
+ Leave source files where they are
+ Bandwidth limit in MB/s; 0 means no limit
+ Closing the window keeps the uploader running in the tray
+ Title of the new sequence; leave empty to let the server name it
+ Your password; only a sign-in token is kept, encrypted for your Windows account
+ Pause; the queue survives a restart
+ Space left in your quota on the server
+ Stop watching this folder; files already uploaded stay on the server
+ Resume uploading
+ Try again
+ Try this file again now
+ Address of the Titano Server, such as https://nas.local:8443
+ Server, sign in, bandwidth, startup, tray
+ Check the server certificate and sign in
+ Sign out and forget the saved token
+ Run when you sign in to Windows
+ Stop and clear the queue; uploaded files stay on the server
+ Create one new sequence with a title you type
+ Add every file to one existing sequence
+ The server splits the files into sessions and creates the sequences
+ Remember this certificate; trust it only if the fingerprint matches the one on the server
+ Your user name on the Titano Server
+ Checked every {0}; a file is uploaded once it has stopped growing · click to change or remove
+ The folder cannot be reached; it is checked again every {0} · click to change or remove
+ Network share; polled every {0} because change notifications are unreliable on SMB · click to change or remove
+ Check the folder at this interval
Close
Maximize
Minimize
diff --git a/i18n/en/Uploader.resx b/i18n/en/Uploader.resx
new file mode 100644
index 00000000..4dbacf76
--- /dev/null
+++ b/i18n/en/Uploader.resx
@@ -0,0 +1,107 @@
+
+
+ text/microsoft-resx
+ 2.0
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+ Add folder
+ Close
+ Pause
+ Resume
+ Retry
+ Stop
+ Titano Uploaderinvariant
+ All files
+ Browse…
+ Files…
+ Add photos
+ Folder or card…
+ Folder or card
+ Photos
+ Watch a folder
+ https://nas.local:8443invariant
+ Wrong user name or password.long
+ The server reported an error.long
+ Enter an address such as https://nas.local:8443.long
+ This address is not a Titano Server.long
+ The server did not answer.long
+ The server certificate was not trusted.long
+ Found
+ Password
+ Server
+ Sign in
+ Connect to server
+ User
+ Delete after upload
+ Keep files
+ Each file is deleted from this PC only after the server has verified its hash.long
+ Delete source files?
+ Drop photos, folders or a card herelong
+ The file changed while it was read.long
+ The file is no longer there.long
+ The file could not be read.long
+ The server refused the file: {0}long
+ The upload failed; the log has the details.long
+ Not connected
+ {0} free of {1}
+ Limit
+ MB/sinvariant
+ Delete after upload
+ Minimise to tray
+ Start with Windows
+ {0:N0} failed
+ {0:N0} retry
+ {0:N0} retries
+ {0:N0} of {1:N0} sent
+ {0:N0} verified by the server
+ Done
+ Nothing queued
+ {0:N0} file
+ {0:N0} files
+ Queue
+ {0} left
+ Paused
+ Waiting for server
+ Bandwidth
+ Files
+ Server
+ Sign out
+ Startup
+ Settings
+ User
+ Failed
+ Hashing
+ Queued
+ On server
+ Uploading
+ Verified
+ Connected
+ Offline
+ Signed out
+ Signing in
+ Target
+ New
+ Sequence
+ Group into sessions
+ Title
+ {0:N0} file added
+ {0:N0} files added
+ The server certificate changed.long
+ Choose a sequence first.
+ No new photos were found there.long
+ The server ended the session.long
+ {0:N0} file verified by the serverlong
+ {0:N0} files verified by the serverlong
+ Upload complete
+ Exit
+ Show
+ Trust
+ Cancel
+ Compare this fingerprint with the one shown on the server.long
+ Trust this server?
+ {0}/s
+ {0} · every {1}
+ Every {0}
+ Watch folders
+ Remove
+
diff --git a/i18n/it/Tooltips.it.resx b/i18n/it/Tooltips.it.resx
index ca09ebc5..039162d1 100644
--- a/i18n/it/Tooltips.it.resx
+++ b/i18n/it/Tooltips.it.resx
@@ -54,6 +54,43 @@
Riduci la navigazione alle icone
Mostra i nomi della navigazione
Ferma l'attività in corso · potrai riprenderla
+ Aggiungi una cartella da controllare
+ Scegli file, una cartella o una scheda
+ Scegli una o più foto
+ Scegli una cartella o una scheda; vengono aggiunte tutte le foto contenute
+ Non connetterti a questo server
+ Scegli la sequenza
+ Chiudi le impostazioni
+ Elimina ogni file originale dopo la verifica del server
+ Elimina il file locale solo dopo che il server ne ha confermato l'hash; chiede conferma prima di attivarsi
+ Titano Server trovato in questa rete · fai clic per usarlo
+ Impronta SHA-256 del certificato del server
+ Lascia i file originali dove sono
+ Limite di banda in MB/s; 0 significa nessun limite
+ Chiudendo la finestra l'uploader resta attivo nell'area di notifica
+ Titolo della nuova sequenza; lascialo vuoto per farlo scegliere al server
+ La tua password; viene conservato solo un token di accesso, cifrato per il tuo account Windows
+ Metti in pausa; la coda sopravvive a un riavvio
+ Spazio rimasto nella tua quota sul server
+ Smetti di controllare questa cartella; i file già inviati restano sul server
+ Riprendi l'invio
+ Riprova
+ Riprova subito questo file
+ Indirizzo del Titano Server, ad esempio https://nas.local:8443
+ Server, accesso, banda, avvio, area di notifica
+ Controlla il certificato del server e accedi
+ Esci e dimentica il token salvato
+ Avvia quando accedi a Windows
+ Ferma e svuota la coda; i file inviati restano sul server
+ Crea una nuova sequenza con il titolo che scrivi
+ Aggiungi tutti i file a una sequenza esistente
+ Il server divide i file in sessioni e crea le sequenze
+ Ricorda questo certificato; accettalo solo se l'impronta coincide con quella sul server
+ Il tuo nome utente sul Titano Server
+ Controllata ogni {0}; un file viene inviato quando smette di crescere · fai clic per modificare o rimuovere
+ La cartella non è raggiungibile; viene ricontrollata ogni {0} · fai clic per modificare o rimuovere
+ Condivisione di rete; controllata ogni {0} perché le notifiche di modifica non sono affidabili su SMB · fai clic per modificare o rimuovere
+ Controlla la cartella con questo intervallo
Chiudi
Ingrandisci
Riduci a icona
diff --git a/i18n/it/Uploader.it.resx b/i18n/it/Uploader.it.resx
new file mode 100644
index 00000000..e4eb6bb7
--- /dev/null
+++ b/i18n/it/Uploader.it.resx
@@ -0,0 +1,104 @@
+
+
+ text/microsoft-resx
+ 2.0
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+ Aggiungi cartella
+ Chiudi
+ Pausa
+ Riprendi
+ Riprova
+ Ferma
+ Tutti i file
+ Sfoglia…
+ File…
+ Aggiungi foto
+ Cartella o scheda…
+ Cartella o scheda
+ Foto
+ Controlla una cartella
+ Nome utente o password errati.
+ Il server ha segnalato un errore.
+ Inserisci un indirizzo come https://nas.local:8443.
+ Questo indirizzo non è un Titano Server.
+ Il server non ha risposto.
+ Il certificato del server non è stato accettato.
+ Trovati
+ Password
+ Server
+ Accedi
+ Connetti al server
+ Utente
+ Elimina dopo l'invio
+ Mantieni i file
+ Ogni file viene eliminato da questo PC solo dopo che il server ne ha verificato l'hash.
+ Eliminare i file originali?
+ Trascina qui foto, cartelle o una scheda
+ Il file è cambiato durante la lettura.
+ Il file non c'è più.
+ Impossibile leggere il file.
+ Il server ha rifiutato il file: {0}
+ Invio non riuscito; i dettagli sono nel log.
+ Non connesso
+ {0} liberi su {1}
+ Limite
+ Elimina dopo l'invio
+ Riduci nell'area notifiche
+ Avvia con Windows
+ {0:N0} non riusciti
+ {0:N0} nuovo tentativo
+ {0:N0} nuovi tentativi
+ {0:N0} di {1:N0} inviati
+ {0:N0} verificati dal server
+ Completato
+ Coda vuota
+ {0:N0} file
+ {0:N0} file
+ Coda
+ mancano {0}
+ In pausa
+ In attesa del server
+ Banda
+ File
+ Server
+ Esci
+ Avvio
+ Impostazioni
+ Utente
+ Non riuscito
+ Calcolo hash
+ In coda
+ Già sul server
+ Invio
+ Verificato
+ Connesso
+ Non in linea
+ Disconnesso
+ Accesso in corso
+ Destinazione
+ Nuova
+ Sequenza
+ Raggruppa in sessioni
+ Titolo
+ {0:N0} file aggiunto
+ {0:N0} file aggiunti
+ Il certificato del server è cambiato.
+ Scegli prima una sequenza.
+ Nessuna nuova foto trovata.
+ Il server ha chiuso la sessione.
+ {0:N0} file verificato dal server
+ {0:N0} file verificati dal server
+ Invio completato
+ Esci
+ Mostra
+ Accetta
+ Annulla
+ Confronta questa impronta con quella mostrata sul server.
+ Accettare questo server?
+ {0}/s
+ {0} · ogni {1}
+ Ogni {0}
+ Cartelle controllate
+ Rimuovi
+
diff --git a/src/Titano.Uploader/App.xaml b/src/Titano.Uploader/App.xaml
index 6e70f617..ec3359f9 100644
--- a/src/Titano.Uploader/App.xaml
+++ b/src/Titano.Uploader/App.xaml
@@ -1,4 +1,5 @@
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ ShutdownMode="OnExplicitShutdown">
diff --git a/src/Titano.Uploader/App.xaml.cs b/src/Titano.Uploader/App.xaml.cs
index 67a0cc99..f737a506 100644
--- a/src/Titano.Uploader/App.xaml.cs
+++ b/src/Titano.Uploader/App.xaml.cs
@@ -1,7 +1,345 @@
+using System.ComponentModel;
+using System.Globalization;
+using System.IO;
using System.Windows;
+using System.Windows.Threading;
+using Titano.Core.Diagnostics;
+using Titano.Core.Localization;
+using Titano.Ui.Mvvm;
+using Titano.Ui.Theming;
+using Titano.Uploader.DesignTime;
+using Titano.Uploader.Infrastructure;
+using Titano.Uploader.Interop;
+using Titano.Uploader.Server;
+using Titano.Uploader.Shell;
+using Titano.Uploader.Uploads;
+using Titano.Uploader.Watching;
namespace Titano.Uploader;
+///
+/// Titano Uploader (BRIEF §15): one instance per session, logging, language and theme, the persisted queue, the
+/// server session, watch folders and the tray icon. --capture <dir> renders screenshots and exits.
+///
public partial class App : Application
{
+ private const int TrayShow = 1;
+ private const int TrayPause = 2;
+ private const int TrayExit = 3;
+
+ private readonly CoalescingWriter _writer = new();
+ private FileLogSink? _log;
+ private SingleInstance? _instance;
+ private UploaderEnvironment? _environment;
+ private UploaderSettings? _settings;
+ private UploadQueue? _queue;
+ private ProcessedFileMemory? _memory;
+ private ServerSession? _session;
+ private UploadEngine? _engine;
+ private WatchFolderService? _watch;
+ private DiscoveryListener? _discovery;
+ private MainViewModel? _vm;
+ private Shell.MainWindow? _window;
+ private TrayIcon? _tray;
+ private DispatcherTimer? _persist;
+ private bool _queueDirty;
+ private bool _exiting;
+
+ protected override void OnStartup(StartupEventArgs e)
+ {
+ base.OnStartup(e);
+ var command = CommandLine.Parse(e.Args);
+ _environment = UploaderEnvironment.Detect();
+ _log = new FileLogSink(_environment.LogDirectory, "uploader");
+ Log.SetSink(_log);
+ Log.Info("app", $"Titano Uploader {typeof(App).Assembly.GetName().Version} starting; data at {_environment.DataDirectory}");
+ HookUnhandledExceptions();
+
+ if (command.CaptureDirectory is { } captureDirectory)
+ {
+ RunCapture(captureDirectory);
+ return;
+ }
+
+ _instance = SingleInstance.TryAcquire();
+ if (_instance is null)
+ {
+ SingleInstance.HandOver([.. command.Paths.Select(Absolute).OfType()]);
+ Log.Info("app", "Another uploader runs in this session; arguments handed over");
+ Shutdown();
+ return;
+ }
+
+ _settings = UploaderSettings.Load(_environment.SettingsPath);
+ ApplyLanguage(_settings.Language);
+ ThemeManager.Initialize(this, _settings.Theme);
+ BuildServices(_environment, _settings);
+
+ _instance.Listen(args => Dispatcher.BeginInvoke(() => OnHandOver(args)));
+ _session!.Start();
+ _engine!.Start();
+ _vm!.Start();
+ _vm.StartWatching();
+ UpdateTray();
+ UpdateDiscovery();
+ if (command.Paths.Count > 0) _ = _vm.EnqueueAsync(command.Paths);
+
+ if (command.StartInTray && _settings.MinimizeToTray) return;
+ if (command.StartInTray) _window!.WindowState = WindowState.Minimized;
+ _window!.Show();
+ }
+
+ protected override void OnExit(ExitEventArgs e)
+ {
+ _persist?.Stop();
+ _vm?.Dispose();
+ // Cancels transfers; interrupted items are written as queued and resume from the server's chunks.
+ _engine?.Dispose();
+ if (_queue is not null && _environment is not null)
+ {
+ Persist();
+ _writer.Flush(TimeSpan.FromSeconds(10));
+ }
+ if (_window is not null && _settings is not null) _window.SavePlacement();
+ SaveSettings();
+ _watch?.Dispose();
+ _discovery?.Dispose();
+ _session?.Dispose();
+ _tray?.Dispose();
+ _instance?.Dispose();
+ Log.Info("app", "Titano Uploader exiting");
+ _log?.Dispose();
+ base.OnExit(e);
+ }
+
+ private void BuildServices(UploaderEnvironment environment, UploaderSettings settings)
+ {
+ _queue = new UploadQueue();
+ QueueStore.Load(environment.QueuePath, _queue);
+ _memory = ProcessedFileMemory.Load(environment.WatchMemoryPath);
+ _session = new ServerSession(settings, SaveSettings);
+ _engine = new UploadEngine(_queue, _session, () => settings.DeleteAfterUpload);
+ _watch = new WatchFolderService(_memory, _queue.ContainsPath);
+ var startup = new RunKeyStartupRegistration();
+ startup.Refresh();
+ var dialogs = new UploaderDialogs();
+ _vm = new MainViewModel(new UploaderServices(settings, SaveSettings, _session, _queue, _engine, startup, dialogs, _watch, _memory, environment.LogDirectory));
+ _window = new Shell.MainWindow(_vm, settings);
+ dialogs.Owner = _window;
+ MainWindow = _window;
+
+ AsyncRelayCommand.Failed += _vm.ReportError;
+ _engine.ItemConfirmed += (_, item) =>
+ {
+ if (item.WatchRoot is { } root) _memory.Add(root, item.Path, item.Size, item.ModifiedUtc);
+ };
+ _watch.FilesReady += _vm.OnWatchFilesReady;
+ _watch.AvailabilityChanged += _vm.OnWatchAvailabilityChanged;
+ _queue.Changed += (_, _) => _queueDirty = true;
+ _vm.UploadsCompleted += OnUploadsCompleted;
+ _vm.PropertyChanged += OnViewModelChanged;
+ _window.Closing += OnWindowClosing;
+ _window.Closed += (_, _) => ExitApplication();
+ _window.StateChanged += OnWindowStateChanged;
+ _persist = new DispatcherTimer(DispatcherPriority.Background) { Interval = TimeSpan.FromSeconds(2) };
+ _persist.Tick += (_, _) => Persist();
+ _persist.Start();
+ }
+
+ /// Switches the UI language (BRIEF §16); an unknown code falls back to the OS language.
+ private static void ApplyLanguage(string? code)
+ {
+ CultureInfo requested;
+ try
+ {
+ requested = code is null ? CultureInfo.CurrentUICulture : CultureInfo.GetCultureInfo(code);
+ }
+ catch (CultureNotFoundException)
+ {
+ requested = CultureInfo.CurrentUICulture;
+ }
+ var culture = Languages.BestMatch(requested);
+ CultureInfo.DefaultThreadCurrentUICulture = culture;
+ CultureInfo.CurrentUICulture = culture;
+ Loc.SetCulture(culture);
+ }
+
+ /// Writes the queue and the watch memory when they changed (every 2 s and at exit).
+ private void Persist()
+ {
+ if (_queue is null || _memory is null || _environment is null) return;
+ if (_queueDirty)
+ {
+ _queueDirty = false;
+ _writer.Write(_environment.QueuePath, QueueStore.Serialize(_queue));
+ }
+ if (_memory.IsDirty) _writer.Write(_environment.WatchMemoryPath, _memory.Serialize());
+ }
+
+ private void SaveSettings()
+ {
+ if (_settings is null || _environment is null) return;
+ try
+ {
+ _settings.Save(_environment.SettingsPath);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("app", "Settings not saved", ex);
+ }
+ }
+
+ private void OnHandOver(IReadOnlyList paths)
+ {
+ ShowWindow();
+ if (paths.Count > 0) _ = _vm!.EnqueueAsync(paths);
+ }
+
+ private void OnViewModelChanged(object? sender, PropertyChangedEventArgs e)
+ {
+ switch (e.PropertyName)
+ {
+ case nameof(MainViewModel.MinimizeToTray):
+ UpdateTray();
+ break;
+ case nameof(MainViewModel.IsConnectVisible):
+ UpdateDiscovery();
+ break;
+ case nameof(MainViewModel.TrayText):
+ _tray?.SetToolTip(_vm!.TrayText);
+ break;
+ }
+ }
+
+ /// The tray icon exists while "Minimise to tray" is on.
+ private void UpdateTray()
+ {
+ if (_settings!.MinimizeToTray && _tray is null)
+ {
+ _tray = new TrayIcon(_vm!.TrayText, TrayMenu, OnTrayCommand, ShowWindow);
+ }
+ else if (!_settings.MinimizeToTray && _tray is not null)
+ {
+ _tray.Dispose();
+ _tray = null;
+ if (_window is { IsVisible: false }) ShowWindow();
+ }
+ }
+
+ private IReadOnlyList TrayMenu() =>
+ [
+ new(TrayShow, Loc.L("Uploader.Tray.Show")),
+ new(TrayPause, _vm!.PauseLabel),
+ new(0, null),
+ new(TrayExit, Loc.L("Uploader.Tray.Exit")),
+ ];
+
+ private void OnTrayCommand(int id)
+ {
+ switch (id)
+ {
+ case TrayShow:
+ ShowWindow();
+ break;
+ case TrayPause:
+ _vm!.TogglePause();
+ break;
+ case TrayExit:
+ ExitApplication();
+ break;
+ }
+ }
+
+ /// Listens for server beacons only while the sign-in form is shown.
+ private void UpdateDiscovery()
+ {
+ if (_vm!.IsConnectVisible)
+ {
+ if (_discovery is not null) return;
+ _discovery = new DiscoveryListener();
+ _discovery.Found += _vm.Connect.AddDiscovered;
+ _discovery.Start();
+ }
+ else if (_discovery is not null)
+ {
+ _discovery.Dispose();
+ _discovery = null;
+ }
+ }
+
+ private void OnUploadsCompleted(int count)
+ {
+ if (_window is { IsVisible: true, WindowState: not WindowState.Minimized }) return;
+ _tray?.ShowBalloon(Loc.L("Uploader.Tray.Done.Title"), Loc.Plural("Uploader.Tray.Done.Message", count));
+ }
+
+ /// Closing keeps the uploader running in the tray when "Minimise to tray" is on.
+ private void OnWindowClosing(object? sender, CancelEventArgs e)
+ {
+ if (_exiting || !_settings!.MinimizeToTray) return;
+ e.Cancel = true;
+ _window!.SavePlacement();
+ _window.Hide();
+ }
+
+ private void OnWindowStateChanged(object? sender, EventArgs e)
+ {
+ if (_window is { WindowState: WindowState.Minimized } && _settings!.MinimizeToTray) _window.Hide();
+ }
+
+ private void ShowWindow()
+ {
+ if (_window is null) return;
+ _window.Show();
+ if (_window.WindowState == WindowState.Minimized) _window.WindowState = WindowState.Normal;
+ _window.Activate();
+ }
+
+ private void ExitApplication()
+ {
+ if (_exiting) return;
+ _exiting = true;
+ Shutdown();
+ }
+
+ private void RunCapture(string directory)
+ {
+ ApplyLanguage(null);
+ ThemeManager.Initialize(this, AppTheme.Dark);
+ var sample = new SampleData();
+ var vm = sample.CreateViewModel();
+ var window = new Shell.MainWindow(vm, sample.Settings);
+ MainWindow = window;
+ ScreenCapture.Run(window, vm, sample, directory);
+ }
+
+ private void HookUnhandledExceptions()
+ {
+ DispatcherUnhandledException += (_, e) =>
+ {
+ Log.Error("app", "Unhandled UI exception", e.Exception);
+ if (_vm is null) return;
+ _vm.ReportError(e.Exception);
+ e.Handled = true;
+ };
+ AppDomain.CurrentDomain.UnhandledException += (_, e) => Log.Error("app", "Unhandled exception", e.ExceptionObject as Exception);
+ TaskScheduler.UnobservedTaskException += (_, e) =>
+ {
+ Log.Error("app", "Unobserved task exception", e.Exception);
+ e.SetObserved();
+ };
+ }
+
+ /// Paths handed to the running instance must not depend on this process's working directory.
+ private static string? Absolute(string path)
+ {
+ try
+ {
+ return Path.GetFullPath(path);
+ }
+ catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
+ {
+ return null;
+ }
+ }
}
diff --git a/src/Titano.Uploader/DesignTime/SampleData.cs b/src/Titano.Uploader/DesignTime/SampleData.cs
new file mode 100644
index 00000000..62dc5a99
--- /dev/null
+++ b/src/Titano.Uploader/DesignTime/SampleData.cs
@@ -0,0 +1,171 @@
+using System.ComponentModel;
+using System.Globalization;
+using System.Runtime.CompilerServices;
+using Titano.Protocol;
+using Titano.Uploader.Infrastructure;
+using Titano.Uploader.Server;
+using Titano.Uploader.Shell;
+using Titano.Uploader.Uploads;
+using Titano.Uploader.Watching;
+
+namespace Titano.Uploader.DesignTime;
+
+///
+/// Sample content for --capture screenshots ONLY: a fake signed-in session, a running queue of 4,812 files
+/// and two watch folders. Nothing here touches the network, the registry or the user's data files.
+///
+public sealed class SampleData
+{
+ private const int FileCount = 4_812;
+ private const long FileSize = 20_000_000;
+
+ private readonly SampleSession _session = new();
+ private readonly UploadQueue _queue = new();
+ private MainViewModel? _vm;
+
+ public UploaderSettings Settings { get; } = new()
+ {
+ ServerUrl = "https://nas.local:8443/",
+ Username = "alberto",
+ BandwidthLimit = 50,
+ MinimizeToTray = true,
+ WatchFolders =
+ [
+ new UploaderSettings.WatchFolderSettings { Path = @"D:\Cam\Roof", IntervalSeconds = 60 },
+ new UploaderSettings.WatchFolderSettings { Path = @"\\garage-pi\cam", IntervalSeconds = 300 },
+ ],
+ };
+
+ /// A view model over in-memory sample services; settings changes are discarded.
+ public MainViewModel CreateViewModel()
+ {
+ var memory = new ProcessedFileMemory();
+ _session.BandwidthLimit = Settings.BandwidthLimit;
+ _vm = new MainViewModel(new UploaderServices(
+ Settings, () => { }, _session, _queue, new SampleEngine(_queue), new SampleStartup(), new UploaderDialogs(),
+ new WatchFolderService(memory, _queue.ContainsPath), memory, string.Empty));
+ foreach (var w in _vm.WatchFolders) w.IsAvailable = true;
+ return _vm;
+ }
+
+ /// First run: no server yet, one server found on the LAN.
+ public void ShowConnect()
+ {
+ _session.SetState(ConnectionState.NotConfigured);
+ _vm!.Connect.Address = string.Empty;
+ _vm.Connect.Username = string.Empty;
+ _vm.Connect.Discovered.Clear();
+ _vm.Connect.Discovered.Add(new DiscoveredServer("Studio NAS", "https://192.168.1.20:8443"));
+ }
+
+ /// Signed in, a card being uploaded at 38 MB/s.
+ public void ShowQueue()
+ {
+ _session.SetState(ConnectionState.Connected);
+ if (_queue.Items.Count == 0) FillQueue();
+ _vm!.Sequences.Clear();
+ _vm.Sequences.Add(new SequenceOption(Guid.Empty, "Harbour sunset"));
+ _vm.SelectedSequence = _vm.Sequences[0];
+ _vm.RefreshQueue();
+ }
+
+ private void FillQueue()
+ {
+ var batch = new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions };
+ var start = new DateTime(2026, 9, 28, 15, 22, 0);
+ _queue.Add(batch, Enumerable.Range(0, FileCount).Select(i =>
+ {
+ var shot = start.AddSeconds(i * 5);
+ var name = shot.ToString("yyyy-MM-dd_HH-mm-ss", CultureInfo.InvariantCulture) + "_" + (i + 1).ToString("0000", CultureInfo.InvariantCulture) + ".CR2";
+ return new QueueEntry(@"E:\DCIM\100CANON\" + name, FileSize, shot.ToUniversalTime(), shot, "1a2b3c4d-20260928-152200", null);
+ }));
+ for (var i = 0; i < 2_201; i++) _queue.Items[i].State = i % 200 == 7 ? UploadItemState.Skipped : UploadItemState.Verified;
+ _queue.Items[1_640].Retries = 1;
+ _queue.Items[2_201].State = UploadItemState.Uploading;
+ _queue.Items[2_201].BytesSent = FileSize * 46 / 100;
+ _queue.Items[2_202].State = UploadItemState.Hashing;
+ _queue.Fail(_queue.Items[2_203], UploadFailure.FileUnreadable, null, DateTime.UtcNow);
+ }
+
+ private sealed class SampleSession : IServerSession
+ {
+ private ConnectionState _state = ConnectionState.Connected;
+
+ public event PropertyChangedEventHandler? PropertyChanged;
+
+ public event EventHandler? SessionEnded
+ {
+ add { }
+ remove { }
+ }
+
+ public ConnectionState State => _state;
+ public string? Address => "https://nas.local:8443/";
+ public string? Username => "alberto";
+ public long QuotaBytes => 5_000_000_000_000;
+ public long UsedBytes => 3_100_000_000_000;
+ public int BandwidthLimit { get; set; }
+
+ public void SetState(ConnectionState state)
+ {
+ _state = state;
+ Raise(nameof(State));
+ }
+
+ public Task<(SignInResult Result, string? Fingerprint)> SignInAsync(string address, string username, string password, CancellationToken ct) =>
+ Task.FromResult<(SignInResult, string?)>((SignInResult.Failed, null));
+
+ public void Trust(string fingerprint)
+ {
+ }
+
+ public Task SignOutAsync() => Task.CompletedTask;
+
+ public Task RefreshAsync() => Task.CompletedTask;
+
+ public Task> ListSequencesAsync(CancellationToken ct) => Task.FromResult>([]);
+
+ private void Raise([CallerMemberName] string? name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
+ }
+
+ private sealed class SampleEngine(UploadQueue queue) : IUploadEngine
+ {
+ public event PropertyChangedEventHandler? PropertyChanged
+ {
+ add { }
+ remove { }
+ }
+
+ public event EventHandler? Drained
+ {
+ add { }
+ remove { }
+ }
+
+ public EngineStatus Status => queue.IsPaused ? EngineStatus.Paused : EngineStatus.Running;
+ public double BytesPerSecond => 38_000_000;
+
+ public void Pump()
+ {
+ }
+
+ public void Pause() => queue.IsPaused = true;
+
+ public void Resume() => queue.IsPaused = false;
+
+ public void StopAndClear()
+ {
+ }
+
+ public void Retry(UploadItem item)
+ {
+ }
+ }
+
+ private sealed class SampleStartup : IStartupRegistration
+ {
+ public bool IsEnabled { get; private set; } = true;
+
+ public void SetEnabled(bool enabled) => IsEnabled = enabled;
+ }
+}
diff --git a/src/Titano.Uploader/Infrastructure/CoalescingWriter.cs b/src/Titano.Uploader/Infrastructure/CoalescingWriter.cs
new file mode 100644
index 00000000..31f0cdcc
--- /dev/null
+++ b/src/Titano.Uploader/Infrastructure/CoalescingWriter.cs
@@ -0,0 +1,62 @@
+using System.IO;
+using Titano.Core.Diagnostics;
+using Titano.Core.IO;
+
+namespace Titano.Uploader.Infrastructure;
+
+///
+/// Writes state files off the UI thread with . Only the newest content of each file is kept
+/// while a write is in progress, so a slow disk never writes an older snapshot after a newer one.
+///
+public sealed class CoalescingWriter
+{
+ private readonly Lock _gate = new();
+ private readonly Dictionary _pending = new(StringComparer.OrdinalIgnoreCase);
+ private Task _worker = Task.CompletedTask;
+ private bool _running;
+
+ public void Write(string path, byte[] content)
+ {
+ lock (_gate)
+ {
+ _pending[path] = content;
+ if (_running) return;
+ _running = true;
+ _worker = Task.Run(Drain);
+ }
+ }
+
+ /// Waits for pending writes (at exit).
+ public void Flush(TimeSpan timeout)
+ {
+ Task worker;
+ lock (_gate) worker = _worker;
+ if (!worker.Wait(timeout)) Log.Warning("persist", "State files still being written at exit");
+ }
+
+ private void Drain()
+ {
+ while (true)
+ {
+ KeyValuePair next;
+ lock (_gate)
+ {
+ if (_pending.Count == 0)
+ {
+ _running = false;
+ return;
+ }
+ next = _pending.First();
+ _pending.Remove(next.Key);
+ }
+ try
+ {
+ AtomicFile.WriteAllBytes(next.Key, next.Value);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("persist", $"{next.Key} not written", ex);
+ }
+ }
+ }
+}
diff --git a/src/Titano.Uploader/Infrastructure/CommandLine.cs b/src/Titano.Uploader/Infrastructure/CommandLine.cs
new file mode 100644
index 00000000..f7df7dc0
--- /dev/null
+++ b/src/Titano.Uploader/Infrastructure/CommandLine.cs
@@ -0,0 +1,27 @@
+namespace Titano.Uploader.Infrastructure;
+
+///
+/// Titano.Uploader.exe [--tray] [--capture <dir>] [files or folders…]: paths are enqueued, --tray
+/// starts hidden (used by the Run key), --capture renders screenshots and exits.
+///
+public sealed record CommandLine(IReadOnlyList Paths, bool StartInTray, string? CaptureDirectory)
+{
+ public const string TrayOption = "--tray";
+ public const string CaptureOption = "--capture";
+
+ public static CommandLine Parse(IReadOnlyList args)
+ {
+ var paths = new List();
+ var tray = false;
+ string? capture = null;
+ for (var i = 0; i < args.Count; i++)
+ {
+ var a = args[i];
+ if (a.Equals(TrayOption, StringComparison.OrdinalIgnoreCase)) tray = true;
+ else if (a.Equals(CaptureOption, StringComparison.OrdinalIgnoreCase))
+ capture = i + 1 < args.Count && !args[i + 1].StartsWith("--", StringComparison.Ordinal) ? args[++i] : "screenshots";
+ else if (!a.StartsWith("--", StringComparison.Ordinal) && a.Length > 0) paths.Add(a);
+ }
+ return new CommandLine(paths, tray, capture);
+ }
+}
diff --git a/src/Titano.Uploader/Infrastructure/SingleInstance.cs b/src/Titano.Uploader/Infrastructure/SingleInstance.cs
new file mode 100644
index 00000000..1c3c89cf
--- /dev/null
+++ b/src/Titano.Uploader/Infrastructure/SingleInstance.cs
@@ -0,0 +1,98 @@
+using System.Diagnostics;
+using System.IO;
+using System.IO.Pipes;
+using System.Text;
+using Titano.Core.Diagnostics;
+
+namespace Titano.Uploader.Infrastructure;
+
+///
+/// One uploader per Windows session: a named mutex marks the running instance, and a second launch hands its
+/// file arguments over a named pipe (current user only) before exiting.
+///
+public sealed class SingleInstance : IDisposable
+{
+ private const string MutexName = @"Local\Titano.Uploader";
+ private const string Header = "TITANO-UPLOADER/1";
+ private const int MaxMessageBytes = 1 << 20;
+
+ private readonly Mutex _mutex;
+ private readonly CancellationTokenSource _stop = new();
+
+ private SingleInstance(Mutex mutex) => _mutex = mutex;
+
+ private static string PipeName => $"Titano.Uploader.{Process.GetCurrentProcess().SessionId}.{Environment.UserName}";
+
+ /// The instance guard, or null when another uploader already runs in this session.
+ public static SingleInstance? TryAcquire()
+ {
+ var mutex = new Mutex(initiallyOwned: true, MutexName, out var createdNew);
+ if (createdNew) return new SingleInstance(mutex);
+ mutex.Dispose();
+ return null;
+ }
+
+ /// Sends (absolute paths; empty = just show the window) to the running instance.
+ public static void HandOver(IReadOnlyList args)
+ {
+ try
+ {
+ using var pipe = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.CurrentUserOnly);
+ pipe.Connect(3000);
+ pipe.Write(Encode(args));
+ pipe.Flush();
+ }
+ catch (Exception ex) when (ex is IOException or TimeoutException or UnauthorizedAccessException)
+ {
+ Log.Warning("instance", "Could not reach the running uploader", ex);
+ }
+ }
+
+ /// Receives messages from later launches; runs on a pool thread.
+ public void Listen(Action> received) => _ = ListenAsync(received, _stop.Token);
+
+ public void Dispose()
+ {
+ _stop.Cancel();
+ _mutex.ReleaseMutex();
+ _mutex.Dispose();
+ _stop.Dispose();
+ }
+
+ /// UTF-8: a header line, then one argument per line (paths cannot contain line breaks).
+ public static byte[] Encode(IReadOnlyList args) =>
+ Encoding.UTF8.GetBytes(Header + "\n" + string.Join("\n", args.Where(a => !a.Contains('\n', StringComparison.Ordinal))));
+
+ /// Arguments of a message, or null when it is not one of ours.
+ public static IReadOnlyList? Decode(ReadOnlySpan message)
+ {
+ if (message.Length > MaxMessageBytes) return null;
+ var lines = Encoding.UTF8.GetString(message).Split('\n');
+ if (lines[0] != Header) return null;
+ return [.. lines.Skip(1).Where(l => l.Length > 0)];
+ }
+
+ private static async Task ListenAsync(Action> received, CancellationToken ct)
+ {
+ while (!ct.IsCancellationRequested)
+ {
+ try
+ {
+ await using var pipe = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte,
+ PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
+ await pipe.WaitForConnectionAsync(ct).ConfigureAwait(false);
+ using var buffer = new MemoryStream();
+ await pipe.CopyToAsync(buffer, ct).ConfigureAwait(false);
+ if (Decode(buffer.GetBuffer().AsSpan(0, (int)buffer.Length)) is { } args) received(args);
+ }
+ catch (OperationCanceledException)
+ {
+ return;
+ }
+ catch (IOException ex)
+ {
+ Log.Warning("instance", "Hand-over pipe failed; listening again", ex);
+ }
+ }
+ }
+}
diff --git a/src/Titano.Uploader/Infrastructure/StartupRegistration.cs b/src/Titano.Uploader/Infrastructure/StartupRegistration.cs
new file mode 100644
index 00000000..0953ab73
--- /dev/null
+++ b/src/Titano.Uploader/Infrastructure/StartupRegistration.cs
@@ -0,0 +1,61 @@
+using System.Security;
+using Microsoft.Win32;
+using Titano.Core.Diagnostics;
+
+namespace Titano.Uploader.Infrastructure;
+
+/// "Start with Windows".
+public interface IStartupRegistration
+{
+ bool IsEnabled { get; }
+
+ void SetEnabled(bool enabled);
+}
+
+///
+/// HKCU\Software\Microsoft\Windows\CurrentVersion\Run value that starts the uploader in the tray at sign-in.
+/// The registry stays the source of truth because Task Manager can turn it off.
+///
+public sealed class RunKeyStartupRegistration : IStartupRegistration
+{
+ private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
+ private const string ValueName = "Titano Uploader";
+
+ public bool IsEnabled
+ {
+ get
+ {
+ try
+ {
+ using var key = Registry.CurrentUser.OpenSubKey(RunKey);
+ return key?.GetValue(ValueName) is string { Length: > 0 };
+ }
+ catch (SecurityException)
+ {
+ return false;
+ }
+ }
+ }
+
+ private static string Command => $"\"{Environment.ProcessPath}\" {CommandLine.TrayOption}";
+
+ public void SetEnabled(bool enabled)
+ {
+ try
+ {
+ using var key = Registry.CurrentUser.CreateSubKey(RunKey, writable: true);
+ if (enabled) key.SetValue(ValueName, Command, RegistryValueKind.String);
+ else key.DeleteValue(ValueName, throwOnMissingValue: false);
+ }
+ catch (Exception ex) when (ex is SecurityException or UnauthorizedAccessException)
+ {
+ Log.Warning("startup", "Run key not updated", ex);
+ }
+ }
+
+ /// Points an existing entry at the current executable (after an update moved it).
+ public void Refresh()
+ {
+ if (IsEnabled) SetEnabled(true);
+ }
+}
diff --git a/src/Titano.Uploader/Infrastructure/TokenProtector.cs b/src/Titano.Uploader/Infrastructure/TokenProtector.cs
new file mode 100644
index 00000000..d7e9d7b8
--- /dev/null
+++ b/src/Titano.Uploader/Infrastructure/TokenProtector.cs
@@ -0,0 +1,98 @@
+using System.Runtime.InteropServices;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Titano.Uploader.Infrastructure;
+
+///
+/// Encrypts the sign-in token for the current Windows user with DPAPI (CryptProtectData, no machine scope),
+/// so uploader.json copied to another account or PC cannot be used.
+///
+public static partial class TokenProtector
+{
+ private const int CryptProtectUiForbidden = 0x1;
+
+ // Ties the blob to this program; other DPAPI consumers of the same user cannot unwrap it by accident.
+ private static readonly byte[] Entropy = "Titano.Uploader.Token.v1"u8.ToArray();
+
+ /// Base64 DPAPI blob of .
+ public static string Protect(string token)
+ {
+ var plain = Encoding.UTF8.GetBytes(token);
+ try
+ {
+ return Convert.ToBase64String(Transform(plain, protect: true));
+ }
+ finally
+ {
+ CryptographicOperations.ZeroMemory(plain);
+ }
+ }
+
+ /// The token, or null when the blob is corrupt or belongs to another user.
+ public static string? Unprotect(string protectedToken)
+ {
+ byte[] blob;
+ try
+ {
+ blob = Convert.FromBase64String(protectedToken);
+ }
+ catch (FormatException)
+ {
+ return null;
+ }
+ try
+ {
+ var plain = Transform(blob, protect: false);
+ var token = Encoding.UTF8.GetString(plain);
+ CryptographicOperations.ZeroMemory(plain);
+ return token;
+ }
+ catch (CryptographicException)
+ {
+ return null;
+ }
+ }
+
+ private static unsafe byte[] Transform(byte[] input, bool protect)
+ {
+ fixed (byte* inPtr = input)
+ fixed (byte* entropyPtr = Entropy)
+ {
+ var source = new DataBlob { Size = input.Length, Data = (IntPtr)inPtr };
+ var entropy = new DataBlob { Size = Entropy.Length, Data = (IntPtr)entropyPtr };
+ var ok = protect
+ ? CryptProtectData(source, null, entropy, IntPtr.Zero, IntPtr.Zero, CryptProtectUiForbidden, out var output)
+ : CryptUnprotectData(source, IntPtr.Zero, entropy, IntPtr.Zero, IntPtr.Zero, CryptProtectUiForbidden, out output);
+ if (!ok) throw new CryptographicException(Marshal.GetLastPInvokeError());
+ try
+ {
+ var result = new byte[output.Size];
+ Marshal.Copy(output.Data, result, 0, output.Size);
+ return result;
+ }
+ finally
+ {
+ LocalFree(output.Data);
+ }
+ }
+ }
+
+ [StructLayout(LayoutKind.Sequential)]
+ private struct DataBlob
+ {
+ public int Size;
+ public IntPtr Data;
+ }
+
+ [LibraryImport("crypt32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool CryptProtectData(in DataBlob dataIn, string? description, in DataBlob entropy, IntPtr reserved, IntPtr prompt, int flags, out DataBlob dataOut);
+
+ [LibraryImport("crypt32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool CryptUnprotectData(in DataBlob dataIn, IntPtr description, in DataBlob entropy, IntPtr reserved, IntPtr prompt, int flags, out DataBlob dataOut);
+
+ [LibraryImport("kernel32.dll")]
+ private static partial IntPtr LocalFree(IntPtr memory);
+}
diff --git a/src/Titano.Uploader/Infrastructure/UploaderEnvironment.cs b/src/Titano.Uploader/Infrastructure/UploaderEnvironment.cs
new file mode 100644
index 00000000..993a0fdb
--- /dev/null
+++ b/src/Titano.Uploader/Infrastructure/UploaderEnvironment.cs
@@ -0,0 +1,37 @@
+using System.IO;
+
+namespace Titano.Uploader.Infrastructure;
+
+///
+/// Where the uploader keeps its files: %LOCALAPPDATA%\Titano (shared with the client), or .\data next
+/// to the executable when portable.txt exists — the same rule as the desktop client.
+///
+public sealed class UploaderEnvironment
+{
+ private UploaderEnvironment(string dataDirectory)
+ {
+ DataDirectory = dataDirectory;
+ Directory.CreateDirectory(DataDirectory);
+ Directory.CreateDirectory(LogDirectory);
+ }
+
+ public string DataDirectory { get; }
+ public string LogDirectory => Path.Combine(DataDirectory, "logs");
+
+ /// Settings, DPAPI-protected token and trusted certificate fingerprints.
+ public string SettingsPath => Path.Combine(DataDirectory, "uploader.json");
+
+ /// The persisted upload queue.
+ public string QueuePath => Path.Combine(DataDirectory, "uploader-queue.json");
+
+ /// Files of watch folders the server already confirmed.
+ public string WatchMemoryPath => Path.Combine(DataDirectory, "uploader-watch.json");
+
+ public static UploaderEnvironment Detect()
+ {
+ var exeDir = AppContext.BaseDirectory;
+ if (File.Exists(Path.Combine(exeDir, "portable.txt"))) return new UploaderEnvironment(Path.Combine(exeDir, "data"));
+ var local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
+ return new UploaderEnvironment(Path.Combine(local, "Titano"));
+ }
+}
diff --git a/src/Titano.Uploader/Infrastructure/UploaderSettings.cs b/src/Titano.Uploader/Infrastructure/UploaderSettings.cs
new file mode 100644
index 00000000..d1bc3394
--- /dev/null
+++ b/src/Titano.Uploader/Infrastructure/UploaderSettings.cs
@@ -0,0 +1,77 @@
+using System.IO;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using Titano.Core.Diagnostics;
+using Titano.Core.IO;
+using Titano.Ui.Theming;
+using Titano.Uploader.Uploads;
+
+namespace Titano.Uploader.Infrastructure;
+
+///
+/// uploader.json. The bearer token is stored only DPAPI-protected for the current Windows user
+/// (); "Start with Windows" lives in the registry, which stays the source of truth.
+///
+public sealed class UploaderSettings
+{
+ public string? ServerUrl { get; set; }
+ public string? Username { get; set; }
+
+ /// Base64 of the DPAPI (CurrentUser) blob of the bearer token.
+ public string? ProtectedToken { get; set; }
+
+ /// SHA-256 fingerprints (XX:XX:…) of self-signed certificates the user trusted (TOFU).
+ public List TrustedFingerprints { get; set; } = [];
+
+ /// Upload limit in MB/s (decimal megabytes); 0 = unlimited.
+ public int BandwidthLimit { get; set; }
+
+ public bool MinimizeToTray { get; set; } = true;
+ public bool DeleteAfterUpload { get; set; }
+ public UploadTargetMode Target { get; set; } = UploadTargetMode.Sessions;
+ public Guid? TargetSequenceId { get; set; }
+ public string? NewSequenceTitle { get; set; }
+ public List WatchFolders { get; set; } = [];
+ public string? Language { get; set; }
+ public AppTheme Theme { get; set; } = AppTheme.Dark;
+ public WindowPlacement? Window { get; set; }
+
+ public sealed class WatchFolderSettings
+ {
+ public string Path { get; set; } = string.Empty;
+ public int IntervalSeconds { get; set; } = 60;
+ }
+
+ public sealed class WindowPlacement
+ {
+ public double Left { get; set; }
+ public double Top { get; set; }
+ public double Width { get; set; }
+ public double Height { get; set; }
+ }
+
+ public static UploaderSettings Load(string path)
+ {
+ try
+ {
+ if (File.Exists(path))
+ return JsonSerializer.Deserialize(File.ReadAllBytes(path), UploaderSettingsJson.Default.UploaderSettings) ?? new UploaderSettings();
+ }
+ catch (JsonException ex)
+ {
+ // A corrupt settings file must never block startup; defaults apply.
+ Log.Warning("settings", "uploader.json unreadable; using defaults", ex);
+ }
+ catch (IOException ex)
+ {
+ Log.Warning("settings", "uploader.json unreadable; using defaults", ex);
+ }
+ return new UploaderSettings();
+ }
+
+ public void Save(string path) => AtomicFile.WriteAllBytes(path, JsonSerializer.SerializeToUtf8Bytes(this, UploaderSettingsJson.Default.UploaderSettings));
+}
+
+[JsonSourceGenerationOptions(WriteIndented = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true)]
+[JsonSerializable(typeof(UploaderSettings))]
+internal sealed partial class UploaderSettingsJson : JsonSerializerContext;
diff --git a/src/Titano.Uploader/Interop/FileDialogs.cs b/src/Titano.Uploader/Interop/FileDialogs.cs
new file mode 100644
index 00000000..39160398
--- /dev/null
+++ b/src/Titano.Uploader/Interop/FileDialogs.cs
@@ -0,0 +1,187 @@
+using System.Runtime.InteropServices;
+
+namespace Titano.Uploader.Interop;
+
+/// A filter of the Open dialog: display name and patterns such as *.jpg;*.cr2.
+public readonly record struct FileFilter(string Name, string Patterns);
+
+///
+/// The shell's Open dialog through IFileOpenDialog COM (no WinForms): several files at once, or one folder
+/// (which also picks a card root such as E:\).
+///
+public static class FileDialogs
+{
+ private const uint FosPickFolders = 0x20;
+ private const uint FosForceFileSystem = 0x40;
+ private const uint FosAllowMultiSelect = 0x200;
+ private const uint FosPathMustExist = 0x800;
+ private const uint FosFileMustExist = 0x1000;
+ private const uint SigdnFileSysPath = 0x80058000;
+ private const int ErrorCancelled = unchecked((int)0x800704C7);
+
+ /// Selected files; empty when cancelled.
+ public static IReadOnlyList PickFiles(IntPtr owner, string title, IReadOnlyList filters) =>
+ Show(owner, title, FosAllowMultiSelect | FosFileMustExist, filters);
+
+ /// Selected folder; null when cancelled.
+ public static string? PickFolder(IntPtr owner, string title) => Show(owner, title, FosPickFolders, []).FirstOrDefault();
+
+ private static List Show(IntPtr owner, string title, uint options, IReadOnlyList filters)
+ {
+ var dialog = (IFileOpenDialog)new FileOpenDialogClass();
+ try
+ {
+ dialog.GetOptions(out var current);
+ dialog.SetOptions(current | options | FosForceFileSystem | FosPathMustExist);
+ dialog.SetTitle(title);
+ if (filters.Count > 0)
+ {
+ var specs = filters.Select(f => new FilterSpec { Name = f.Name, Spec = f.Patterns }).ToArray();
+ dialog.SetFileTypes((uint)specs.Length, specs);
+ dialog.SetFileTypeIndex(1);
+ }
+ var hr = dialog.Show(owner);
+ if (hr == ErrorCancelled) return [];
+ Marshal.ThrowExceptionForHR(hr);
+
+ dialog.GetResults(out var items);
+ try
+ {
+ items.GetCount(out var count);
+ var paths = new List((int)count);
+ for (uint i = 0; i < count; i++)
+ {
+ items.GetItemAt(i, out var item);
+ try
+ {
+ item.GetDisplayName(SigdnFileSysPath, out var path);
+ paths.Add(path);
+ }
+ finally
+ {
+ Marshal.ReleaseComObject(item);
+ }
+ }
+ return paths;
+ }
+ finally
+ {
+ Marshal.ReleaseComObject(items);
+ }
+ }
+ finally
+ {
+ Marshal.ReleaseComObject(dialog);
+ }
+ }
+
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
+ private struct FilterSpec
+ {
+ [MarshalAs(UnmanagedType.LPWStr)]
+ public string Name;
+
+ [MarshalAs(UnmanagedType.LPWStr)]
+ public string Spec;
+ }
+
+ [ComImport]
+ [Guid("DC1C5A9C-E88A-4dde-A5A1-60F82A20AEF7")]
+ private class FileOpenDialogClass;
+
+ // Classic COM interop does not inherit vtables: IModalWindow and IFileDialog members are repeated in order.
+ [ComImport]
+ [Guid("d57c7288-d4ad-4768-be02-9d969532d960")]
+ [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+ private interface IFileOpenDialog
+ {
+ [PreserveSig]
+ int Show(IntPtr owner);
+
+ void SetFileTypes(uint count, [In, MarshalAs(UnmanagedType.LPArray)] FilterSpec[] filters);
+
+ void SetFileTypeIndex(uint index);
+
+ void GetFileTypeIndex(out uint index);
+
+ void Advise(IntPtr events, out uint cookie);
+
+ void Unadvise(uint cookie);
+
+ void SetOptions(uint options);
+
+ void GetOptions(out uint options);
+
+ void SetDefaultFolder(IShellItem item);
+
+ void SetFolder(IShellItem item);
+
+ void GetFolder(out IShellItem item);
+
+ void GetCurrentSelection(out IShellItem item);
+
+ void SetFileName([MarshalAs(UnmanagedType.LPWStr)] string name);
+
+ void GetFileName([MarshalAs(UnmanagedType.LPWStr)] out string name);
+
+ void SetTitle([MarshalAs(UnmanagedType.LPWStr)] string title);
+
+ void SetOkButtonLabel([MarshalAs(UnmanagedType.LPWStr)] string text);
+
+ void SetFileNameLabel([MarshalAs(UnmanagedType.LPWStr)] string label);
+
+ void GetResult(out IShellItem item);
+
+ void AddPlace(IShellItem item, int placement);
+
+ void SetDefaultExtension([MarshalAs(UnmanagedType.LPWStr)] string extension);
+
+ void Close(int hr);
+
+ void SetClientGuid(ref Guid guid);
+
+ void ClearClientData();
+
+ void SetFilter(IntPtr filter);
+
+ void GetResults(out IShellItemArray items);
+
+ void GetSelectedItems(out IShellItemArray items);
+ }
+
+ [ComImport]
+ [Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
+ [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+ private interface IShellItem
+ {
+ void BindToHandler(IntPtr bindContext, ref Guid handler, ref Guid iid, out IntPtr result);
+
+ void GetParent(out IShellItem parent);
+
+ void GetDisplayName(uint form, [MarshalAs(UnmanagedType.LPWStr)] out string name);
+
+ void GetAttributes(uint mask, out uint attributes);
+
+ void Compare(IShellItem other, uint hint, out int order);
+ }
+
+ [ComImport]
+ [Guid("b63ea76d-1f85-456f-a19c-48159efa858b")]
+ [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
+ private interface IShellItemArray
+ {
+ void BindToHandler(IntPtr bindContext, ref Guid handler, ref Guid iid, out IntPtr result);
+
+ void GetPropertyStore(int flags, ref Guid iid, out IntPtr store);
+
+ void GetPropertyDescriptionList(IntPtr keyType, ref Guid iid, out IntPtr list);
+
+ void GetAttributes(int flags, uint mask, out uint attributes);
+
+ void GetCount(out uint count);
+
+ void GetItemAt(uint index, out IShellItem item);
+
+ void EnumItems(out IntPtr items);
+ }
+}
diff --git a/src/Titano.Uploader/Interop/TrayIcon.cs b/src/Titano.Uploader/Interop/TrayIcon.cs
new file mode 100644
index 00000000..e6c52ec6
--- /dev/null
+++ b/src/Titano.Uploader/Interop/TrayIcon.cs
@@ -0,0 +1,251 @@
+using System.Runtime.InteropServices;
+using System.Windows.Interop;
+using Titano.Core.Diagnostics;
+
+namespace Titano.Uploader.Interop;
+
+/// One entry of the tray menu; an id of 0 is a separator.
+public readonly record struct TrayMenuItem(int Id, string? Text);
+
+///
+/// Notification-area icon through Shell_NotifyIconW: the program icon from the executable's resources, a tooltip,
+/// a native context menu and balloons. A hidden window receives the callbacks and re-adds the icon after Explorer restarts.
+///
+public sealed unsafe partial class TrayIcon : IDisposable
+{
+ private const uint NimAdd = 0;
+ private const uint NimModify = 1;
+ private const uint NimDelete = 2;
+ private const uint NifMessage = 0x1;
+ private const uint NifIcon = 0x2;
+ private const uint NifTip = 0x4;
+ private const uint NifInfo = 0x10;
+ private const uint NiifUser = 0x4;
+ private const int CallbackMessage = 0x8000 + 1; // WM_APP + 1
+ private const int WmContextMenu = 0x007B;
+ private const int WmLButtonUp = 0x0202;
+ private const int WmLButtonDblClk = 0x0203;
+ private const int WmRButtonUp = 0x0205;
+ private const int NinBalloonUserClick = 0x0405;
+ private const uint MfString = 0x0;
+ private const uint MfSeparator = 0x800;
+ private const uint TpmRightButton = 0x2;
+ private const uint TpmNoNotify = 0x80;
+ private const uint TpmReturnCmd = 0x100;
+
+ private readonly HwndSource _window;
+ private readonly IntPtr _icon;
+ private readonly bool _ownsIcon;
+ private readonly uint _taskbarCreated;
+ private readonly Func> _menu;
+ private readonly Action _command;
+ private readonly Action _show;
+ private string _toolTip;
+ private bool _added;
+
+ /// Hover text (up to 127 characters).
+ /// Builds the context menu when it opens.
+ /// Runs a chosen menu id.
+ /// Left click or balloon click.
+ public TrayIcon(string toolTip, Func> menu, Action command, Action show)
+ {
+ _toolTip = toolTip;
+ _menu = menu;
+ _command = command;
+ _show = show;
+ // A hidden top-level window: message-only windows miss the TaskbarCreated broadcast.
+ _window = new HwndSource(new HwndSourceParameters("TitanoUploaderTray") { Width = 0, Height = 0, WindowStyle = 0 });
+ _window.AddHook(WndProc);
+ _taskbarCreated = RegisterWindowMessageW("TaskbarCreated");
+ (_icon, _ownsIcon) = LoadProgramIcon();
+ Add();
+ }
+
+ public void SetToolTip(string text)
+ {
+ _toolTip = text;
+ var data = NewData(NifTip);
+ Copy(_toolTip, data.Tip, 128);
+ Notify(NimModify, &data);
+ }
+
+ /// Shows a balloon (a toast on Windows 10/11) with the program icon.
+ public void ShowBalloon(string title, string text)
+ {
+ var data = NewData(NifInfo);
+ Copy(title, data.InfoTitle, 64);
+ Copy(text, data.Info, 256);
+ data.InfoFlags = NiifUser;
+ Notify(NimModify, &data);
+ }
+
+ public void Dispose()
+ {
+ if (_added)
+ {
+ var data = NewData(0);
+ Notify(NimDelete, &data);
+ _added = false;
+ }
+ if (_ownsIcon) DestroyIcon(_icon);
+ _window.Dispose();
+ }
+
+ private void Add()
+ {
+ var data = NewData(NifMessage | NifIcon | NifTip);
+ data.CallbackMessage = CallbackMessage;
+ data.Icon = _icon;
+ Copy(_toolTip, data.Tip, 128);
+ _added = Notify(NimAdd, &data);
+ }
+
+ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
+ {
+ if (msg == CallbackMessage)
+ {
+ switch ((int)(lParam.ToInt64() & 0xFFFF))
+ {
+ case WmLButtonUp or WmLButtonDblClk or NinBalloonUserClick:
+ _show();
+ break;
+ case WmRButtonUp or WmContextMenu:
+ ShowMenu();
+ break;
+ }
+ handled = true;
+ }
+ else if (_taskbarCreated != 0 && msg == (int)_taskbarCreated)
+ {
+ Add();
+ }
+ return IntPtr.Zero;
+ }
+
+ private void ShowMenu()
+ {
+ var menu = CreatePopupMenu();
+ if (menu == IntPtr.Zero) return;
+ try
+ {
+ foreach (var item in _menu())
+ {
+ if (item.Id == 0 || item.Text is null) AppendMenuW(menu, MfSeparator, 0, null);
+ else AppendMenuW(menu, MfString, (nuint)item.Id, item.Text);
+ }
+ GetCursorPos(out var point);
+ // Without foreground the menu would not close when the user clicks elsewhere.
+ SetForegroundWindow(_window.Handle);
+ var chosen = TrackPopupMenuEx(menu, TpmReturnCmd | TpmRightButton | TpmNoNotify, point.X, point.Y, _window.Handle, IntPtr.Zero);
+ PostMessageW(_window.Handle, 0, IntPtr.Zero, IntPtr.Zero);
+ if (chosen != 0) _command(chosen);
+ }
+ finally
+ {
+ DestroyMenu(menu);
+ }
+ }
+
+ private NotifyIconData NewData(uint flags) => new()
+ {
+ Size = (uint)sizeof(NotifyIconData),
+ Window = _window.Handle,
+ Id = 1,
+ Flags = flags,
+ };
+
+ private static bool Notify(uint message, NotifyIconData* data)
+ {
+ if (Shell_NotifyIconW(message, data)) return true;
+ Log.Info("tray", $"Shell_NotifyIcon({message}) failed");
+ return false;
+ }
+
+ private static void Copy(string text, char* destination, int capacity)
+ {
+ var length = Math.Min(text.Length, capacity - 1);
+ text.AsSpan(0, length).CopyTo(new Span(destination, capacity));
+ destination[length] = '\0';
+ }
+
+ /// The generated titano-uploader icon embedded in the executable; the stock icon if it cannot be read.
+ private static (IntPtr Icon, bool Owned) LoadProgramIcon()
+ {
+ if (Environment.ProcessPath is { } exe)
+ {
+ IntPtr small;
+ if (ExtractIconExW(exe, 0, null, &small, 1) > 0 && small != IntPtr.Zero) return (small, true);
+ }
+ return (LoadIconW(IntPtr.Zero, 32512), false); // IDI_APPLICATION
+ }
+
+ [StructLayout(LayoutKind.Sequential)]
+ private struct NotifyIconData
+ {
+ public uint Size;
+ public IntPtr Window;
+ public uint Id;
+ public uint Flags;
+ public uint CallbackMessage;
+ public IntPtr Icon;
+ public fixed char Tip[128];
+ public uint State;
+ public uint StateMask;
+ public fixed char Info[256];
+ public uint TimeoutOrVersion;
+ public fixed char InfoTitle[64];
+ public uint InfoFlags;
+ public Guid Item;
+ public IntPtr BalloonIcon;
+ }
+
+ [StructLayout(LayoutKind.Sequential)]
+ private struct Point
+ {
+ public int X;
+ public int Y;
+ }
+
+ [LibraryImport("shell32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool Shell_NotifyIconW(uint message, NotifyIconData* data);
+
+ [LibraryImport("shell32.dll", StringMarshalling = StringMarshalling.Utf16)]
+ private static partial uint ExtractIconExW(string file, int index, IntPtr* large, IntPtr* small, uint count);
+
+ [LibraryImport("user32.dll")]
+ private static partial IntPtr LoadIconW(IntPtr instance, nint name);
+
+ [LibraryImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool DestroyIcon(IntPtr icon);
+
+ [LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)]
+ private static partial uint RegisterWindowMessageW(string name);
+
+ [LibraryImport("user32.dll")]
+ private static partial IntPtr CreatePopupMenu();
+
+ [LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool AppendMenuW(IntPtr menu, uint flags, nuint id, string? text);
+
+ [LibraryImport("user32.dll")]
+ private static partial int TrackPopupMenuEx(IntPtr menu, uint flags, int x, int y, IntPtr window, IntPtr parameters);
+
+ [LibraryImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool DestroyMenu(IntPtr menu);
+
+ [LibraryImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool GetCursorPos(out Point point);
+
+ [LibraryImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool SetForegroundWindow(IntPtr window);
+
+ [LibraryImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ private static partial bool PostMessageW(IntPtr window, int message, IntPtr wParam, IntPtr lParam);
+}
diff --git a/src/Titano.Uploader/Server/DiscoveryListener.cs b/src/Titano.Uploader/Server/DiscoveryListener.cs
new file mode 100644
index 00000000..1796ed3d
--- /dev/null
+++ b/src/Titano.Uploader/Server/DiscoveryListener.cs
@@ -0,0 +1,74 @@
+using System.Net;
+using System.Net.Sockets;
+using Titano.Core.Diagnostics;
+using Titano.Protocol;
+
+namespace Titano.Uploader.Server;
+
+/// A Titano Server announced on the LAN.
+public sealed record DiscoveredServer(string Name, string Address);
+
+///
+/// Listens for the server's UDP beacon () while the connect form is open and reports
+/// each server once. Beacons are unauthenticated: they only fill the address field, never trust a certificate.
+///
+public sealed class DiscoveryListener : IDisposable
+{
+ private readonly HashSet _seen = new(StringComparer.OrdinalIgnoreCase);
+ private CancellationTokenSource? _cts;
+
+ /// Raised on the thread that called .
+ public event Action? Found;
+
+ public void Start()
+ {
+ if (_cts is not null) return;
+ _cts = new CancellationTokenSource();
+ var context = SynchronizationContext.Current;
+ _ = Task.Run(() => ListenAsync(context, _cts.Token));
+ }
+
+ public void Dispose()
+ {
+ _cts?.Cancel();
+ _cts?.Dispose();
+ _cts = null;
+ }
+
+ /// The address the client should use for sent from .
+ private static string AddressOf(DiscoveryBeacon beacon, IPAddress sender)
+ {
+ var host = sender.AddressFamily == AddressFamily.InterNetworkV6 ? "[" + sender + "]" : sender.ToString();
+ return beacon.HttpsPort > 0 ? $"https://{host}:{beacon.HttpsPort}" : $"http://{host}:{beacon.HttpPort}";
+ }
+
+ private async Task ListenAsync(SynchronizationContext? context, CancellationToken ct)
+ {
+ try
+ {
+ using var udp = new UdpClient();
+ // The desktop client may listen on the same port.
+ udp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+ udp.Client.Bind(new IPEndPoint(IPAddress.Any, DiscoveryBeacon.Port));
+ while (!ct.IsCancellationRequested)
+ {
+ var datagram = await udp.ReceiveAsync(ct).ConfigureAwait(false);
+ if (DiscoveryBeacon.TryParse(datagram.Buffer) is not { } beacon) continue;
+ var server = new DiscoveredServer(beacon.Name, AddressOf(beacon, datagram.RemoteEndPoint.Address));
+ lock (_seen)
+ {
+ if (!_seen.Add(server.Address)) continue;
+ }
+ if (context is null) Found?.Invoke(server);
+ else context.Post(_ => Found?.Invoke(server), null);
+ }
+ }
+ catch (OperationCanceledException)
+ {
+ }
+ catch (SocketException ex)
+ {
+ Log.Info("discovery", "LAN discovery unavailable: " + ex.Message);
+ }
+ }
+}
diff --git a/src/Titano.Uploader/Server/ServerAddress.cs b/src/Titano.Uploader/Server/ServerAddress.cs
new file mode 100644
index 00000000..6d025400
--- /dev/null
+++ b/src/Titano.Uploader/Server/ServerAddress.cs
@@ -0,0 +1,21 @@
+namespace Titano.Uploader.Server;
+
+/// Server addresses as typed in the sign-in form.
+public static class ServerAddress
+{
+ ///
+ /// Accepts "nas.local:8443" (https assumed) or a full http(s) URL; the result is the API base, ending with "/".
+ ///
+ public static bool TryParse(string? text, out Uri uri)
+ {
+ uri = null!;
+ if (string.IsNullOrWhiteSpace(text)) return false;
+ var trimmed = text.Trim();
+ if (!trimmed.Contains("://", StringComparison.Ordinal)) trimmed = "https://" + trimmed;
+ if (!Uri.TryCreate(trimmed, UriKind.Absolute, out var parsed) || parsed.Scheme is not ("https" or "http") || parsed.Host.Length == 0) return false;
+ var builder = new UriBuilder(parsed) { Query = string.Empty, Fragment = string.Empty };
+ if (!builder.Path.EndsWith('/')) builder.Path += "/";
+ uri = builder.Uri;
+ return true;
+ }
+}
diff --git a/src/Titano.Uploader/Server/ServerSession.cs b/src/Titano.Uploader/Server/ServerSession.cs
new file mode 100644
index 00000000..7a672483
--- /dev/null
+++ b/src/Titano.Uploader/Server/ServerSession.cs
@@ -0,0 +1,460 @@
+using System.ComponentModel;
+using System.Net;
+using System.Net.Http;
+using System.Runtime.CompilerServices;
+using System.Text.Json;
+using Titano.Core.Diagnostics;
+using Titano.Protocol;
+using Titano.Protocol.Client;
+using Titano.Uploader.Infrastructure;
+using Titano.Uploader.Uploads;
+
+namespace Titano.Uploader.Server;
+
+/// Connection shown in the header.
+public enum ConnectionState
+{
+ /// First run: no server chosen yet.
+ NotConfigured,
+ SigningIn,
+ Connected,
+
+ /// Signed in, but the server cannot be reached; probed every 30 s.
+ Offline,
+
+ /// A server is known but there is no valid token.
+ SignedOut,
+}
+
+/// Outcome of the sign-in form.
+public enum SignInResult
+{
+ Success,
+ InvalidAddress,
+ Unreachable,
+ NotTitano,
+
+ /// The certificate is self-signed and unknown: ask the user to trust its fingerprint.
+ Untrusted,
+ BadCredentials,
+ Failed,
+}
+
+/// The server connection as the window sees it.
+public interface IServerSession : INotifyPropertyChanged
+{
+ ConnectionState State { get; }
+
+ /// Server address as the user typed or discovered it.
+ string? Address { get; }
+
+ string? Username { get; }
+
+ /// Quota in bytes; 0 = unlimited.
+ long QuotaBytes { get; }
+
+ long UsedBytes { get; }
+
+ /// Upload limit in MB/s; 0 = unlimited.
+ int BandwidthLimit { get; set; }
+
+ /// Raised when the server ended the session (token rejected or certificate changed).
+ event EventHandler? SessionEnded;
+
+ /// Signs in; comes with the fingerprint to show.
+ Task<(SignInResult Result, string? Fingerprint)> SignInAsync(string address, string username, string password, CancellationToken ct);
+
+ /// Remembers a certificate fingerprint the user confirmed (trust on first use).
+ void Trust(string fingerprint);
+
+ Task SignOutAsync();
+
+ /// Refreshes the account (quota) when connected.
+ Task RefreshAsync();
+
+ Task> ListSequencesAsync(CancellationToken ct);
+}
+
+///
+/// Owns the : sign-in with trust-on-first-use, token restore from DPAPI, offline probing,
+/// and the upload transport. Runs on the UI thread.
+///
+public sealed class ServerSession : IServerSession, IUploadTransport, IDisposable
+{
+ private static readonly TimeSpan ProbeInterval = TimeSpan.FromSeconds(30);
+
+ private readonly UploaderSettings _settings;
+ private readonly Action _saveSettings;
+ private TitanoClient? _client;
+ private ConnectionState _state;
+ private string? _username;
+ private long _quotaBytes;
+ private long _usedBytes;
+ private int _bandwidthLimit;
+ private CancellationTokenSource? _probe;
+
+ public ServerSession(UploaderSettings settings, Action saveSettings)
+ {
+ _settings = settings;
+ _saveSettings = saveSettings;
+ _username = settings.Username;
+ _bandwidthLimit = Math.Max(0, settings.BandwidthLimit);
+ _state = settings.ServerUrl is null ? ConnectionState.NotConfigured : ConnectionState.SignedOut;
+ }
+
+ public event PropertyChangedEventHandler? PropertyChanged;
+
+ public event EventHandler? ReadyChanged;
+
+ public event EventHandler? SessionEnded;
+
+ public ConnectionState State
+ {
+ get => _state;
+ private set
+ {
+ if (_state == value) return;
+ var wasReady = IsReady;
+ _state = value;
+ OnPropertyChanged();
+ if (wasReady != IsReady) ReadyChanged?.Invoke(this, EventArgs.Empty);
+ }
+ }
+
+ public bool IsReady => _state == ConnectionState.Connected && _client is not null;
+
+ public string? Address => _settings.ServerUrl;
+
+ public string? Username
+ {
+ get => _username;
+ private set => Set(ref _username, value);
+ }
+
+ public long QuotaBytes
+ {
+ get => _quotaBytes;
+ private set => Set(ref _quotaBytes, value);
+ }
+
+ public long UsedBytes
+ {
+ get => _usedBytes;
+ private set => Set(ref _usedBytes, value);
+ }
+
+ public int BandwidthLimit
+ {
+ get => _bandwidthLimit;
+ set
+ {
+ if (!Set(ref _bandwidthLimit, Math.Max(0, value))) return;
+ ApplyBandwidth();
+ }
+ }
+
+ /// Restores the saved session, if any, in the background.
+ public void Start()
+ {
+ if (_settings.ServerUrl is null || _settings.ProtectedToken is null) return;
+ var token = TokenProtector.Unprotect(_settings.ProtectedToken);
+ if (token is null || !ServerAddress.TryParse(_settings.ServerUrl, out var uri))
+ {
+ Log.Warning("session", "Saved token cannot be used; sign in again");
+ State = ConnectionState.SignedOut;
+ return;
+ }
+ UseClient(CreateClient(uri));
+ _client!.Token = token;
+ State = ConnectionState.SigningIn;
+ _ = RefreshAsync();
+ }
+
+ public async Task<(SignInResult Result, string? Fingerprint)> SignInAsync(string address, string username, string password, CancellationToken ct)
+ {
+ if (!ServerAddress.TryParse(address, out var uri)) return (SignInResult.InvalidAddress, null);
+ StopProbing();
+ var previous = State;
+ State = ConnectionState.SigningIn;
+ var client = CreateClient(uri);
+ try
+ {
+ await client.GetInfoAsync(ct);
+ var login = await client.LoginAsync(username, password, ct);
+ if (login.Token is null) throw new TitanoApiException(HttpStatusCode.Unauthorized, new ApiError("no_token", "No token issued"));
+
+ _settings.ServerUrl = uri.ToString();
+ _settings.Username = login.User.Username;
+ _settings.ProtectedToken = TokenProtector.Protect(login.Token);
+ _saveSettings();
+ UseClient(client);
+ OnPropertyChanged(nameof(Address));
+ Username = login.User.Username;
+ SetQuota(login.User);
+ State = ConnectionState.Connected;
+ Log.Info("session", $"Signed in to {uri} as {login.User.Username}");
+ return (SignInResult.Success, null);
+ }
+ catch (Exception ex)
+ {
+ client.Dispose();
+ State = previous == ConnectionState.SigningIn ? ConnectionState.NotConfigured : previous;
+ var result = Classify(ex, ct);
+ if (result == SignInResult.Failed) Log.Warning("session", $"Sign-in to {uri} failed", ex);
+ return (result, (ex as UntrustedCertificateException)?.Fingerprint);
+ }
+ }
+
+ public void Trust(string fingerprint)
+ {
+ if (_settings.TrustedFingerprints.Contains(fingerprint, StringComparer.OrdinalIgnoreCase)) return;
+ _settings.TrustedFingerprints.Add(fingerprint);
+ _saveSettings();
+ Log.Info("session", $"Trusted certificate {fingerprint}");
+ }
+
+ public async Task SignOutAsync()
+ {
+ StopProbing();
+ if (_client is { Token: not null } client)
+ {
+ try
+ {
+ using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
+ await client.LogoutAsync(timeout.Token);
+ }
+ catch (Exception ex) when (ex is HttpRequestException or TitanoApiException or OperationCanceledException or UntrustedCertificateException)
+ {
+ // The token is forgotten locally either way.
+ Log.Info("session", "Logout not confirmed by the server: " + ex.Message);
+ }
+ }
+ ForgetToken();
+ }
+
+ public async Task RefreshAsync()
+ {
+ if (_client is not { } client) return;
+ try
+ {
+ var me = await client.MeAsync();
+ Username = me.Username;
+ SetQuota(me);
+ State = ConnectionState.Connected;
+ StopProbing();
+ }
+ catch (Exception ex) when (IsServerProblem(ex, CancellationToken.None, out var problem))
+ {
+ OnServerProblem(problem, ex);
+ }
+ catch (TitanoApiException ex)
+ {
+ Log.Warning("session", "Account refresh failed", ex);
+ }
+ }
+
+ public async Task> ListSequencesAsync(CancellationToken ct)
+ {
+ if (!IsReady) return [];
+ var all = new List();
+ string? cursor = null;
+ try
+ {
+ // Five pages of 200 cover any realistic choice list.
+ for (var page = 0; page < 5; page++)
+ {
+ var result = await _client!.ListSequencesAsync(cursor: cursor, ct: ct);
+ all.AddRange(result.Items);
+ cursor = result.NextCursor;
+ if (cursor is null) break;
+ }
+ }
+ catch (Exception ex) when (IsServerProblem(ex, ct, out var problem))
+ {
+ OnServerProblem(problem, ex);
+ }
+ catch (TitanoApiException ex)
+ {
+ Log.Warning("session", "Sequence list unavailable", ex);
+ }
+ return all;
+ }
+
+ // ---------------- IUploadTransport ----------------
+
+ public async Task<(UploadOutcome Outcome, Guid? FrameId)> UploadAsync(UploadItem item, Guid? sequenceId, string sha256, IProgress progress, CancellationToken ct)
+ {
+ var client = _client ?? throw new ServerUnavailableException(ServerProblem.SignedOut);
+ try
+ {
+ var result = await client.UploadFileAsync(item.Path, sequenceId, item.CapturedAt, item.SessionKey, sha256, progress, ct);
+ // The client reports an existing file as index −1 (or no frame at all).
+ return result is null || result.Index < 0 ? (UploadOutcome.AlreadyOnServer, result?.FrameId) : (UploadOutcome.Verified, result.FrameId);
+ }
+ catch (Exception ex) when (IsServerProblem(ex, ct, out var problem))
+ {
+ OnServerProblem(problem, ex);
+ throw new ServerUnavailableException(problem, ex);
+ }
+ }
+
+ public async Task CreateSequenceAsync(string? title, CancellationToken ct)
+ {
+ var client = _client ?? throw new ServerUnavailableException(ServerProblem.SignedOut);
+ try
+ {
+ var created = await client.CreateSequenceAsync(new CreateSequenceRequest(string.IsNullOrWhiteSpace(title) ? null : title.Trim(), null), ct);
+ Log.Info("session", $"Created sequence {created.Id} \"{created.DisplayTitle}\"");
+ return created.Id;
+ }
+ catch (Exception ex) when (IsServerProblem(ex, ct, out var problem))
+ {
+ OnServerProblem(problem, ex);
+ throw new ServerUnavailableException(problem, ex);
+ }
+ }
+
+ public void Dispose()
+ {
+ StopProbing();
+ _client?.Dispose();
+ }
+
+ // ---------------- Plumbing ----------------
+
+ private TitanoClient CreateClient(Uri uri)
+ {
+ // A fresh pinning per client: the protocol's last-rejected fingerprint must not outlive a trust decision.
+ var client = new TitanoClient(uri, new CertificatePinning(_settings.TrustedFingerprints), $"Titano Uploader ({Environment.MachineName})");
+ client.Bandwidth.BytesPerSecond = _bandwidthLimit * 1_000_000L;
+ return client;
+ }
+
+ private void UseClient(TitanoClient client)
+ {
+ if (ReferenceEquals(_client, client)) return;
+ _client?.Dispose();
+ _client = client;
+ }
+
+ private void ApplyBandwidth()
+ {
+ if (_client is not null) _client.Bandwidth.BytesPerSecond = _bandwidthLimit * 1_000_000L;
+ }
+
+ private void SetQuota(UserDto user)
+ {
+ QuotaBytes = user.QuotaBytes;
+ UsedBytes = user.UsedBytes;
+ }
+
+ private void ForgetToken()
+ {
+ if (_client is not null) _client.Token = null;
+ _settings.ProtectedToken = null;
+ _saveSettings();
+ QuotaBytes = 0;
+ UsedBytes = 0;
+ State = _settings.ServerUrl is null ? ConnectionState.NotConfigured : ConnectionState.SignedOut;
+ }
+
+ private bool IsServerProblem(Exception ex, CancellationToken ct, out ServerProblem problem)
+ {
+ problem = ServerProblem.Unreachable;
+ switch (ex)
+ {
+ case TitanoApiException { Status: HttpStatusCode.Unauthorized }:
+ problem = ServerProblem.SignedOut;
+ return true;
+ case TitanoApiException api:
+ return api.IsTransient;
+ case UntrustedCertificateException untrusted:
+ // The pinning remembers its last rejection; once trusted, a later network error can still carry it.
+ if (!_settings.TrustedFingerprints.Contains(untrusted.Fingerprint, StringComparer.OrdinalIgnoreCase))
+ problem = ServerProblem.CertificateChanged;
+ return true;
+ case HttpRequestException:
+ return true;
+ case OperationCanceledException when !ct.IsCancellationRequested:
+ return true; // request timeout
+ default:
+ return false;
+ }
+ }
+
+ private void OnServerProblem(ServerProblem problem, Exception ex)
+ {
+ switch (problem)
+ {
+ case ServerProblem.SignedOut:
+ Log.Warning("session", "The server rejected the token; signing out");
+ StopProbing();
+ ForgetToken();
+ SessionEnded?.Invoke(this, problem);
+ break;
+ case ServerProblem.CertificateChanged:
+ // Never upload to a server whose certificate changed: the user must sign in and confirm it again.
+ Log.Warning("session", "The server certificate changed; sign in again to review it", ex);
+ StopProbing();
+ ForgetToken();
+ SessionEnded?.Invoke(this, problem);
+ break;
+ default:
+ if (State != ConnectionState.Offline) Log.Warning("session", "Server unreachable; retrying every 30 s", ex);
+ State = ConnectionState.Offline;
+ StartProbing();
+ break;
+ }
+ }
+
+ private void StartProbing()
+ {
+ if (_probe is not null) return;
+ _probe = new CancellationTokenSource();
+ _ = ProbeAsync(_probe.Token);
+ }
+
+ private void StopProbing()
+ {
+ _probe?.Cancel();
+ _probe?.Dispose();
+ _probe = null;
+ }
+
+ private async Task ProbeAsync(CancellationToken ct)
+ {
+ try
+ {
+ while (!ct.IsCancellationRequested && State == ConnectionState.Offline)
+ {
+ await Task.Delay(ProbeInterval, ct);
+ await RefreshAsync();
+ }
+ }
+ catch (OperationCanceledException)
+ {
+ }
+ }
+
+ private static SignInResult Classify(Exception ex, CancellationToken ct) => ex switch
+ {
+ UntrustedCertificateException => SignInResult.Untrusted,
+ TitanoApiException { Status: HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden } => SignInResult.BadCredentials,
+ TitanoApiException { Status: HttpStatusCode.NotFound } => SignInResult.NotTitano,
+ TitanoApiException api when api.IsTransient => SignInResult.Unreachable,
+ JsonException or NotSupportedException => SignInResult.NotTitano,
+ HttpRequestException => SignInResult.Unreachable,
+ OperationCanceledException when !ct.IsCancellationRequested => SignInResult.Unreachable,
+ _ => SignInResult.Failed,
+ };
+
+ private bool Set(ref T field, T value, [CallerMemberName] string? name = null)
+ {
+ if (EqualityComparer.Default.Equals(field, value)) return false;
+ field = value;
+ OnPropertyChanged(name);
+ return true;
+ }
+
+ private void OnPropertyChanged([CallerMemberName] string? name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
+}
diff --git a/src/Titano.Uploader/Shell/ConfirmDialog.xaml b/src/Titano.Uploader/Shell/ConfirmDialog.xaml
new file mode 100644
index 00000000..d3eab487
--- /dev/null
+++ b/src/Titano.Uploader/Shell/ConfirmDialog.xaml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Titano.Uploader/Shell/ConfirmDialog.xaml.cs b/src/Titano.Uploader/Shell/ConfirmDialog.xaml.cs
new file mode 100644
index 00000000..eb7d1102
--- /dev/null
+++ b/src/Titano.Uploader/Shell/ConfirmDialog.xaml.cs
@@ -0,0 +1,47 @@
+using System.Windows;
+using Titano.Ui.Controls;
+
+namespace Titano.Uploader.Shell;
+
+/// Texts of a .
+public sealed record ConfirmRequest
+{
+ public required string Heading { get; init; }
+ public required string Message { get; init; }
+
+ /// Monospaced detail (certificate fingerprint); null hides it.
+ public string? Detail { get; init; }
+
+ public string? DetailToolTip { get; init; }
+ public required string SafeText { get; init; }
+ public required string SafeToolTip { get; init; }
+ public required string ActionText { get; init; }
+ public required string ActionToolTip { get; init; }
+
+ /// Warning-outlined action (never a primary) for data-destroying choices.
+ public bool IsDestructive { get; init; }
+}
+
+/// Modal confirmation used for trusting a certificate and for destructive options (BRIEF §12.1).
+public sealed partial class ConfirmDialog : TitanoWindow
+{
+ private ConfirmDialog(ConfirmRequest request)
+ {
+ DataContext = request;
+ InitializeComponent();
+ Action.SetResourceReference(StyleProperty, request.IsDestructive ? "Button.Destructive" : "Button.Primary");
+ }
+
+ /// True when the user chose the action.
+ public static bool Ask(Window? owner, ConfirmRequest request)
+ {
+ var dialog = new ConfirmDialog(request);
+ if (owner is { IsVisible: true }) dialog.Owner = owner;
+ else dialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
+ return dialog.ShowDialog() == true;
+ }
+
+ private void OnSafe(object sender, RoutedEventArgs e) => DialogResult = false;
+
+ private void OnAction(object sender, RoutedEventArgs e) => DialogResult = true;
+}
diff --git a/src/Titano.Uploader/Shell/ConnectViewModel.cs b/src/Titano.Uploader/Shell/ConnectViewModel.cs
new file mode 100644
index 00000000..54e42a07
--- /dev/null
+++ b/src/Titano.Uploader/Shell/ConnectViewModel.cs
@@ -0,0 +1,101 @@
+using System.Collections.ObjectModel;
+using System.Windows.Input;
+using Titano.Core.Localization;
+using Titano.Ui.Mvvm;
+using Titano.Uploader.Server;
+
+namespace Titano.Uploader.Shell;
+
+///
+/// First-run and signed-out form: server address (with servers found on the LAN), user, password. A self-signed
+/// certificate is shown to the user once (trust on first use) before the password is sent.
+///
+public sealed class ConnectViewModel : ObservableObject
+{
+ private readonly IServerSession _session;
+ private readonly IUploaderDialogs _dialogs;
+ private string _address;
+ private string _username;
+ private string? _errorText;
+
+ public ConnectViewModel(IServerSession session, IUploaderDialogs dialogs, string? address, string? username)
+ {
+ _session = session;
+ _dialogs = dialogs;
+ _address = Display(address);
+ _username = username ?? string.Empty;
+ SignInCommand = new AsyncRelayCommand(SignInAsync);
+ UseServerCommand = new RelayCommand(p =>
+ {
+ if (p is DiscoveredServer s) Address = s.Address;
+ });
+ }
+
+ /// Servers that announced themselves on this network.
+ public ObservableCollection Discovered { get; } = [];
+
+ public ICommand SignInCommand { get; }
+ public ICommand UseServerCommand { get; }
+
+ public string Address
+ {
+ get => _address;
+ set => Set(ref _address, value);
+ }
+
+ public string Username
+ {
+ get => _username;
+ set => Set(ref _username, value);
+ }
+
+ /// Set from the view's PasswordBox; never stored.
+ public string Password { private get; set; } = string.Empty;
+
+ /// One sentence saying what went wrong; the retry action sits next to it.
+ public string? ErrorText
+ {
+ get => _errorText;
+ private set => Set(ref _errorText, value);
+ }
+
+ /// Shows the server address without the trailing slash of the stored base URL.
+ public static string Display(string? address) => address?.TrimEnd('/') ?? string.Empty;
+
+ public void AddDiscovered(DiscoveredServer server)
+ {
+ if (Discovered.Any(d => d.Address == server.Address)) return;
+ Discovered.Add(server);
+ if (Address.Length == 0 && Discovered.Count == 1) Address = server.Address;
+ }
+
+ private async Task SignInAsync()
+ {
+ if (_session.State is not (ConnectionState.NotConfigured or ConnectionState.SignedOut)) return;
+ ErrorText = null;
+ var (result, fingerprint) = await _session.SignInAsync(Address, Username.Trim(), Password, CancellationToken.None);
+ if (result == SignInResult.Untrusted && fingerprint is not null)
+ {
+ if (!_dialogs.ConfirmTrust(Display(Address), fingerprint))
+ {
+ ErrorText = Loc.L("Uploader.Connect.Error.Untrusted");
+ return;
+ }
+ _session.Trust(fingerprint);
+ (result, _) = await _session.SignInAsync(Address, Username.Trim(), Password, CancellationToken.None);
+ }
+ ErrorText = result switch
+ {
+ SignInResult.Success => null,
+ SignInResult.InvalidAddress => Loc.L("Uploader.Connect.Error.InvalidAddress"),
+ SignInResult.Unreachable => Loc.L("Uploader.Connect.Error.Unreachable"),
+ SignInResult.NotTitano => Loc.L("Uploader.Connect.Error.NotTitano"),
+ SignInResult.BadCredentials => Loc.L("Uploader.Connect.Error.BadCredentials"),
+ SignInResult.Untrusted => Loc.L("Uploader.Connect.Error.Untrusted"),
+ _ => Loc.L("Uploader.Connect.Error.Failed"),
+ };
+ if (result != SignInResult.Success) return;
+ Address = Display(_session.Address);
+ Password = string.Empty;
+ }
+}
diff --git a/src/Titano.Uploader/Shell/Converters.cs b/src/Titano.Uploader/Shell/Converters.cs
new file mode 100644
index 00000000..792d1162
--- /dev/null
+++ b/src/Titano.Uploader/Shell/Converters.cs
@@ -0,0 +1,88 @@
+using System.Globalization;
+using System.Windows;
+using System.Windows.Data;
+using Titano.Core.Localization;
+using Titano.Ui.Controls;
+using Titano.Uploader.Text;
+using Titano.Uploader.Uploads;
+
+namespace Titano.Uploader.Shell;
+
+/// → status glyph kind (DESIGN §4: colour always paired with a glyph and a word).
+public sealed class UploadStateKindConverter : IValueConverter
+{
+ public static UploadStateKindConverter Instance { get; } = new();
+
+ public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) => value switch
+ {
+ UploadItemState.Hashing => StatusKind.Analysing,
+ UploadItemState.Uploading => StatusKind.Running,
+ UploadItemState.Verified => StatusKind.Done,
+ UploadItemState.Skipped => StatusKind.Synced,
+ UploadItemState.Failed => StatusKind.Failed,
+ _ => StatusKind.Queued,
+ };
+
+ public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException();
+}
+
+/// → its localized word.
+public sealed class UploadStateTextConverter : IValueConverter
+{
+ public static UploadStateTextConverter Instance { get; } = new();
+
+ public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) => value switch
+ {
+ UploadItemState.Hashing => Loc.L("Uploader.State.Hashing"),
+ UploadItemState.Uploading => Loc.L("Uploader.State.Uploading"),
+ UploadItemState.Verified => Loc.L("Uploader.State.Verified"),
+ UploadItemState.Skipped => Loc.L("Uploader.State.Skipped"),
+ UploadItemState.Failed => Loc.L("Uploader.State.Failed"),
+ _ => Loc.L("Uploader.State.Queued"),
+ };
+
+ public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException();
+}
+
+/// (, detail) → one sentence for the tooltip of a failed item; null otherwise.
+public sealed class FailureTextConverter : IMultiValueConverter
+{
+ public static FailureTextConverter Instance { get; } = new();
+
+ public object? Convert(object?[] values, Type targetType, object? parameter, CultureInfo culture) =>
+ values is [UploadFailure failure, var detail, ..] ? Describe(failure, detail as string) : null;
+
+ public object[] ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotSupportedException();
+
+ private static string? Describe(UploadFailure failure, string? detail) => failure switch
+ {
+ UploadFailure.FileMissing => Loc.L("Uploader.Failure.FileMissing"),
+ UploadFailure.FileUnreadable => Loc.L("Uploader.Failure.FileUnreadable"),
+ UploadFailure.FileChanged => Loc.L("Uploader.Failure.FileChanged"),
+ UploadFailure.ServerRejected => Loc.L("Uploader.Failure.ServerRejected", detail ?? string.Empty),
+ UploadFailure.Unexpected => Loc.L("Uploader.Failure.Unexpected"),
+ _ => null,
+ };
+}
+
+/// Visible only for ; hidden (keeping its space) otherwise so rows stay aligned.
+public sealed class FailedToVisibleConverter : IValueConverter
+{
+ public static FailedToVisibleConverter Instance { get; } = new();
+
+ public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
+ value is UploadItemState.Failed ? Visibility.Visible : Visibility.Hidden;
+
+ public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException();
+}
+
+/// Byte count → "25.4 MB".
+public sealed class SizeConverter : IValueConverter
+{
+ public static SizeConverter Instance { get; } = new();
+
+ public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) =>
+ value is long bytes ? UploaderText.Bytes(bytes, Loc.Culture) : string.Empty;
+
+ public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException();
+}
diff --git a/src/Titano.Uploader/Shell/MainViewModel.cs b/src/Titano.Uploader/Shell/MainViewModel.cs
new file mode 100644
index 00000000..0c1f0279
--- /dev/null
+++ b/src/Titano.Uploader/Shell/MainViewModel.cs
@@ -0,0 +1,554 @@
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Diagnostics;
+using System.Globalization;
+using System.IO;
+using System.Windows.Input;
+using System.Windows.Threading;
+using Titano.Core.Diagnostics;
+using Titano.Core.Localization;
+using Titano.Ui.Controls;
+using Titano.Ui.Mvvm;
+using Titano.Uploader.Infrastructure;
+using Titano.Uploader.Server;
+using Titano.Uploader.Sources;
+using Titano.Uploader.Text;
+using Titano.Uploader.Uploads;
+using Titano.Uploader.Watching;
+
+namespace Titano.Uploader.Shell;
+
+/// A sequence offered as upload target.
+public sealed record SequenceOption(Guid Id, string Title);
+
+/// Everything the window works with; the capture mode passes design-time implementations.
+public sealed record UploaderServices(
+ UploaderSettings Settings,
+ Action SaveSettings,
+ IServerSession Session,
+ UploadQueue Queue,
+ IUploadEngine Engine,
+ IStartupRegistration Startup,
+ IUploaderDialogs Dialogs,
+ WatchFolderService Watch,
+ ProcessedFileMemory Memory,
+ string LogDirectory);
+
+/// State of the uploader window (BRIEF §15, mockup Uploader.html).
+public sealed class MainViewModel : LocalizedViewModel
+{
+ private readonly UploaderServices _s;
+ private readonly DispatcherTimer _refresh;
+ private UploadTargetMode _target;
+ private SequenceOption? _selectedSequence;
+ private bool _loadingSequences;
+ private string _newTitle;
+ private bool _isSettingsOpen;
+ private bool _startWithWindows;
+ private string _queueText = string.Empty;
+ private string _progressText = string.Empty;
+ private string? _currentFileText;
+ private double _progress;
+ private UploadItem? _currentItem;
+
+ public MainViewModel(UploaderServices services)
+ {
+ _s = services;
+ _target = services.Settings.Target;
+ _newTitle = services.Settings.NewSequenceTitle ?? string.Empty;
+ _startWithWindows = services.Startup.IsEnabled;
+ Connect = new ConnectViewModel(services.Session, services.Dialogs, services.Settings.ServerUrl, services.Settings.Username);
+ foreach (var w in services.Settings.WatchFolders) WatchFolders.Add(NewWatchFolder(w.Path, w.IntervalSeconds));
+
+ OpenSettingsCommand = new RelayCommand(() => IsSettingsOpen = true);
+ CloseSettingsCommand = new RelayCommand(() => IsSettingsOpen = false);
+ SignOutCommand = new AsyncRelayCommand(SignOutAsync);
+ BrowseFilesCommand = new AsyncRelayCommand(() => EnqueueAsync(_s.Dialogs.PickFiles()));
+ BrowseFolderCommand = new AsyncRelayCommand(() => _s.Dialogs.PickFolder() is { } folder ? EnqueueAsync([folder]) : Task.CompletedTask);
+ RefreshSequencesCommand = new AsyncRelayCommand(LoadSequencesAsync);
+ PauseCommand = new RelayCommand(TogglePause, () => HasItems);
+ StopCommand = new RelayCommand(() => _s.Engine.StopAndClear(), () => HasItems);
+ RetryItemCommand = new RelayCommand(p =>
+ {
+ if (p is UploadItem item) _s.Engine.Retry(item);
+ });
+ AddWatchFolderCommand = new RelayCommand(AddWatchFolder);
+ ToggleDeleteAfterUploadCommand = new RelayCommand(ToggleDeleteAfterUpload);
+
+ _s.Session.PropertyChanged += OnSessionChanged;
+ _s.Session.SessionEnded += OnSessionEnded;
+ _s.Engine.PropertyChanged += (_, _) => RefreshQueue();
+ _s.Engine.Drained += OnDrained;
+ _s.Queue.Items.CollectionChanged += (_, _) =>
+ {
+ OnPropertyChanged(nameof(HasItems));
+ CommandManager.InvalidateRequerySuggested();
+ };
+ _refresh = new DispatcherTimer(DispatcherPriority.Background) { Interval = TimeSpan.FromMilliseconds(500) };
+ _refresh.Tick += (_, _) => RefreshQueue();
+ RefreshQueue();
+ }
+
+ /// Raised when the queue finished while work was done, with the number of files the server confirmed.
+ public event Action? UploadsCompleted;
+
+ public ConnectViewModel Connect { get; }
+
+ /// In-window notifications; the window forwards them to its .
+ public ObservableCollection PendingToasts { get; } = [];
+
+ public ICommand OpenSettingsCommand { get; }
+ public ICommand CloseSettingsCommand { get; }
+ public ICommand SignOutCommand { get; }
+ public ICommand BrowseFilesCommand { get; }
+ public ICommand BrowseFolderCommand { get; }
+ public ICommand RefreshSequencesCommand { get; }
+ public ICommand PauseCommand { get; }
+ public ICommand StopCommand { get; }
+ public ICommand RetryItemCommand { get; }
+ public ICommand AddWatchFolderCommand { get; }
+ public ICommand ToggleDeleteAfterUploadCommand { get; }
+
+ // ---------------- Header ----------------
+
+ public string ServerText => _s.Session.State == ConnectionState.NotConfigured
+ ? Loc.L("Uploader.Header.NotConnected")
+ : ConnectViewModel.Display(_s.Session.Address);
+
+ public string? UserText => _s.Session.State is ConnectionState.Connected or ConnectionState.Offline or ConnectionState.SigningIn ? _s.Session.Username : null;
+
+ public StatusKind ConnectionKind => _s.Session.State switch
+ {
+ ConnectionState.Connected => StatusKind.Online,
+ ConnectionState.SigningIn => StatusKind.Running,
+ _ => StatusKind.Offline,
+ };
+
+ /// Null on first run, when the server line already says "Not connected".
+ public string? ConnectionText => _s.Session.State switch
+ {
+ ConnectionState.Connected => Loc.L("Uploader.Status.Connected"),
+ ConnectionState.SigningIn => Loc.L("Uploader.Status.SigningIn"),
+ ConnectionState.Offline => Loc.L("Uploader.Status.Offline"),
+ ConnectionState.SignedOut => Loc.L("Uploader.Status.SignedOut"),
+ _ => null,
+ };
+
+ /// "1.9 TB free of 5 TB"; null without a quota.
+ public string? QuotaText => _s.Session.State == ConnectionState.Connected && _s.Session.QuotaBytes > 0
+ ? Loc.L("Uploader.Header.Quota", UploaderText.Bytes(Math.Max(0, _s.Session.QuotaBytes - _s.Session.UsedBytes), Loc.Culture), UploaderText.Bytes(_s.Session.QuotaBytes, Loc.Culture))
+ : null;
+
+ /// The sign-in form replaces the queue on first run and after signing out.
+ public bool IsConnectVisible => _s.Session.State is ConnectionState.NotConfigured or ConnectionState.SignedOut;
+
+ public bool IsSettingsOpen
+ {
+ get => _isSettingsOpen;
+ set => Set(ref _isSettingsOpen, value);
+ }
+
+ // ---------------- Target ----------------
+
+ public bool IsSessionsTarget
+ {
+ get => _target == UploadTargetMode.Sessions;
+ set => SetTarget(value, UploadTargetMode.Sessions);
+ }
+
+ public bool IsSequenceTarget
+ {
+ get => _target == UploadTargetMode.Sequence;
+ set => SetTarget(value, UploadTargetMode.Sequence);
+ }
+
+ public bool IsNewTarget
+ {
+ get => _target == UploadTargetMode.NewSequence;
+ set => SetTarget(value, UploadTargetMode.NewSequence);
+ }
+
+ public ObservableCollection Sequences { get; } = [];
+
+ public SequenceOption? SelectedSequence
+ {
+ get => _selectedSequence;
+ set
+ {
+ if (_loadingSequences || !Set(ref _selectedSequence, value)) return;
+ _s.Settings.TargetSequenceId = value?.Id;
+ _s.SaveSettings();
+ }
+ }
+
+ public string NewTitle
+ {
+ get => _newTitle;
+ set
+ {
+ if (!Set(ref _newTitle, value)) return;
+ _s.Settings.NewSequenceTitle = value;
+ _s.SaveSettings();
+ }
+ }
+
+ // ---------------- Queue ----------------
+
+ public ObservableCollection Items => _s.Queue.Items;
+
+ public bool HasItems => _s.Queue.Items.Count > 0;
+
+ /// "4,812 files · 96.2 GB · 38 MB/s · 41 min left".
+ public string QueueText
+ {
+ get => _queueText;
+ private set => Set(ref _queueText, value);
+ }
+
+ /// "2,214 of 4,812 sent · 2,201 verified by the server · 0 retries".
+ public string ProgressText
+ {
+ get => _progressText;
+ private set => Set(ref _progressText, value);
+ }
+
+ public string? CurrentFileText
+ {
+ get => _currentFileText;
+ private set => Set(ref _currentFileText, value);
+ }
+
+ public double Progress
+ {
+ get => _progress;
+ private set => Set(ref _progress, value);
+ }
+
+ /// The item being uploaded; the list keeps it in view.
+ public UploadItem? CurrentItem
+ {
+ get => _currentItem;
+ private set => Set(ref _currentItem, value);
+ }
+
+ public bool IsPaused => _s.Queue.IsPaused;
+
+ public string PauseToolTip => Loc.L(IsPaused ? "Tooltips.Uploader.Resume" : "Tooltips.Uploader.Pause");
+
+ /// Accessible name of the pause/resume button (also the tray menu entry).
+ public string PauseLabel => Loc.L(IsPaused ? "Uploader.Action.Resume" : "Uploader.Action.Pause");
+
+ /// Upload limit in MB/s as typed; 0 = unlimited. Text that is not a whole number reverts.
+ public string BandwidthLimitText
+ {
+ get => _s.Session.BandwidthLimit.ToString(Loc.Culture);
+ set
+ {
+ if (int.TryParse(value, NumberStyles.Integer, Loc.Culture, out var limit))
+ {
+ limit = Math.Clamp(limit, 0, 100_000);
+ if (limit != _s.Session.BandwidthLimit)
+ {
+ _s.Session.BandwidthLimit = limit;
+ _s.Settings.BandwidthLimit = limit;
+ _s.SaveSettings();
+ }
+ }
+ OnPropertyChanged();
+ }
+ }
+
+ /// Tray hover text.
+ public string TrayText => Loc.L("Uploader.App.Title") + (HasItems ? " · " + QueueText : string.Empty);
+
+ // ---------------- Watch folders ----------------
+
+ public ObservableCollection WatchFolders { get; } = [];
+
+ // ---------------- Options ----------------
+
+ /// Turned on only through , which asks first.
+ public bool DeleteAfterUpload => _s.Settings.DeleteAfterUpload;
+
+ public bool StartWithWindows
+ {
+ get => _startWithWindows;
+ set
+ {
+ _s.Startup.SetEnabled(value);
+ // Read back: the registry may refuse the change.
+ _startWithWindows = _s.Startup.IsEnabled;
+ OnPropertyChanged();
+ }
+ }
+
+ public bool MinimizeToTray
+ {
+ get => _s.Settings.MinimizeToTray;
+ set
+ {
+ if (_s.Settings.MinimizeToTray == value) return;
+ _s.Settings.MinimizeToTray = value;
+ _s.SaveSettings();
+ OnPropertyChanged();
+ }
+ }
+
+ // ---------------- Actions ----------------
+
+ /// Starts the periodic refresh and, when signed in, loads the sequence list.
+ public void Start()
+ {
+ _refresh.Start();
+ if (_s.Session.State == ConnectionState.Connected) _ = LoadSequencesAsync();
+ }
+
+ /// Starts watching every configured folder.
+ public void StartWatching()
+ {
+ foreach (var w in WatchFolders) _s.Watch.Watch(w.Path, TimeSpan.FromSeconds(w.IntervalSeconds));
+ }
+
+ /// Adds the photos under (files, folders, card roots) to the queue.
+ public async Task EnqueueAsync(IReadOnlyList paths)
+ {
+ if (paths.Count == 0) return;
+ if (_target == UploadTargetMode.Sequence && SelectedSequence is null)
+ {
+ ShowToast(Loc.L("Uploader.Toast.ChooseSequence"));
+ return;
+ }
+ var found = await Task.Run(() => PhotoScanner.Scan(paths));
+ var fresh = found.Where(f => !_s.Queue.ContainsPath(f.Path)).ToList();
+ if (fresh.Count == 0)
+ {
+ ShowToast(Loc.L("Uploader.Toast.NothingFound"));
+ return;
+ }
+ var batch = new UploadBatch
+ {
+ Id = Guid.CreateVersion7(),
+ Mode = _target,
+ SequenceId = _target == UploadTargetMode.Sequence ? SelectedSequence?.Id : null,
+ Title = _target == UploadTargetMode.NewSequence ? NewTitle.Trim() : null,
+ };
+ var entries = _target == UploadTargetMode.Sessions
+ ? SessionEntries(fresh, null, null, out _)
+ : [.. fresh.OrderBy(f => f.CapturedAt).Select(f => new QueueEntry(f.Path, f.Size, f.ModifiedUtc, f.CapturedAt, null, null))];
+ var added = Add(batch, entries);
+ Log.Info("queue", $"{added} file(s) added from {paths.Count} path(s), target {_target}");
+ ShowToast(Loc.Plural("Uploader.Toast.Added", added));
+ }
+
+ /// Queues stable new photos of a watch folder; they join the folder's last session when close in time.
+ public void OnWatchFilesReady(string root, IReadOnlyList files)
+ {
+ var fresh = files.Where(f => !_s.Queue.ContainsPath(f.Path)).ToList();
+ if (fresh.Count == 0) return;
+ var entries = SessionEntries(fresh, root, _s.Memory.GetSession(root), out var last);
+ Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions }, entries);
+ _s.Memory.SetSession(root, last);
+ }
+
+ public void OnWatchAvailabilityChanged(string root, bool available)
+ {
+ var folder = WatchFolders.FirstOrDefault(w => string.Equals(w.Path, root, StringComparison.OrdinalIgnoreCase));
+ if (folder is not null) folder.IsAvailable = available;
+ }
+
+ /// Errors are one sentence plus one action (BRIEF §0.2 rule 6).
+ public void ReportError(Exception ex)
+ {
+ Log.Error("ui", "Operation failed", ex);
+ ShowToast(Loc.L("Errors.Unexpected"), Loc.L("Common.OpenLogs"), Loc.L("Tooltips.Common.OpenLogs"), new RelayCommand(OpenLogs));
+ }
+
+ public void TogglePause()
+ {
+ if (_s.Queue.IsPaused) _s.Engine.Resume();
+ else _s.Engine.Pause();
+ RefreshQueue();
+ }
+
+ public override void Dispose()
+ {
+ _refresh.Stop();
+ _s.Session.PropertyChanged -= OnSessionChanged;
+ _s.Session.SessionEnded -= OnSessionEnded;
+ _s.Engine.Drained -= OnDrained;
+ base.Dispose();
+ }
+
+ private int Add(UploadBatch batch, IReadOnlyList entries)
+ {
+ // A new batch after a finished one starts a fresh count.
+ if (!_s.Queue.HasPending) _s.Queue.RemoveCompleted();
+ var added = _s.Queue.Add(batch, entries);
+ _s.Engine.Pump();
+ RefreshQueue();
+ return added;
+ }
+
+ /// Session keys per source folder (BRIEF §13 rules), entries in capture order.
+ private static List SessionEntries(IReadOnlyList files, string? watchRoot, SessionContinuation? previous, out SessionContinuation? last)
+ {
+ var entries = new List(files.Count);
+ last = previous;
+ foreach (var group in files.GroupBy(f => watchRoot ?? f.Root, StringComparer.OrdinalIgnoreCase))
+ {
+ var ordered = group.OrderBy(f => f.CapturedAt).ToList();
+ var assignment = SessionGrouper.Assign([.. ordered.Select(f => f.CapturedAt)], SessionGrouper.SourceTag(group.Key), watchRoot is null ? null : previous);
+ for (var i = 0; i < ordered.Count; i++)
+ {
+ var f = ordered[i];
+ entries.Add(new QueueEntry(f.Path, f.Size, f.ModifiedUtc, f.CapturedAt, assignment.Keys[i], watchRoot));
+ }
+ last = assignment.Last;
+ }
+ return entries;
+ }
+
+ /// Recomputes the queue line, progress and current file (also every 500 ms once started).
+ public void RefreshQueue()
+ {
+ var summary = QueueSummary.From(_s.Queue.Items);
+ var rate = _s.Engine.BytesPerSecond;
+ var activity = summary.Files == 0 ? QueueActivity.Idle : _s.Engine.Status switch
+ {
+ EngineStatus.Paused => QueueActivity.Paused,
+ EngineStatus.WaitingForServer => QueueActivity.WaitingForServer,
+ EngineStatus.Running => QueueActivity.Running,
+ _ => QueueActivity.Done,
+ };
+ QueueText = UploaderText.QueueLine(summary, activity, rate, RateMeter.TimeLeft(summary.RemainingBytes, rate), Loc.Culture);
+ ProgressText = summary.Files == 0 ? string.Empty : UploaderText.ProgressLine(summary, Loc.Culture);
+ Progress = summary.Progress;
+ CurrentFileText = summary.CurrentFile;
+ CurrentItem = _s.Queue.Items.FirstOrDefault(i => i.State == UploadItemState.Uploading) ?? _s.Queue.Items.FirstOrDefault(i => i.IsActive);
+ OnPropertyChanged(nameof(IsPaused));
+ OnPropertyChanged(nameof(PauseToolTip));
+ OnPropertyChanged(nameof(PauseLabel));
+ OnPropertyChanged(nameof(TrayText));
+ }
+
+ private void SetTarget(bool selected, UploadTargetMode mode)
+ {
+ if (!selected || _target == mode) return;
+ _target = mode;
+ _s.Settings.Target = mode;
+ _s.SaveSettings();
+ OnPropertyChanged(nameof(IsSessionsTarget));
+ OnPropertyChanged(nameof(IsSequenceTarget));
+ OnPropertyChanged(nameof(IsNewTarget));
+ if (mode == UploadTargetMode.Sequence && Sequences.Count == 0) _ = LoadSequencesAsync();
+ }
+
+ private async Task LoadSequencesAsync()
+ {
+ var list = await _s.Session.ListSequencesAsync(CancellationToken.None);
+ if (list.Count == 0 && Sequences.Count > 0) return;
+ _loadingSequences = true;
+ try
+ {
+ Sequences.Clear();
+ foreach (var s in list.OrderBy(s => s.DisplayTitle, StringComparer.CurrentCultureIgnoreCase)) Sequences.Add(new SequenceOption(s.Id, s.DisplayTitle));
+ }
+ finally
+ {
+ _loadingSequences = false;
+ }
+ _selectedSequence = Sequences.FirstOrDefault(s => s.Id == _s.Settings.TargetSequenceId);
+ OnPropertyChanged(nameof(SelectedSequence));
+ }
+
+ private async Task SignOutAsync()
+ {
+ IsSettingsOpen = false;
+ await _s.Session.SignOutAsync();
+ }
+
+ private void ToggleDeleteAfterUpload()
+ {
+ var enable = !_s.Settings.DeleteAfterUpload;
+ if (enable && !_s.Dialogs.ConfirmDeleteAfterUpload())
+ {
+ // The check box toggled itself; put it back.
+ OnPropertyChanged(nameof(DeleteAfterUpload));
+ return;
+ }
+ _s.Settings.DeleteAfterUpload = enable;
+ _s.SaveSettings();
+ Log.Info("settings", "Delete after verified upload " + (enable ? "on" : "off"));
+ OnPropertyChanged(nameof(DeleteAfterUpload));
+ }
+
+ private void AddWatchFolder()
+ {
+ if (_s.Dialogs.PickWatchFolder() is not { } path) return;
+ if (WatchFolders.Any(w => string.Equals(w.Path, path, StringComparison.OrdinalIgnoreCase))) return;
+ var interval = (int)WatchFolderService.DefaultInterval(path).TotalSeconds;
+ _s.Settings.WatchFolders.Add(new UploaderSettings.WatchFolderSettings { Path = path, IntervalSeconds = interval });
+ _s.SaveSettings();
+ WatchFolders.Add(NewWatchFolder(path, interval));
+ _s.Watch.Watch(path, TimeSpan.FromSeconds(interval));
+ }
+
+ private WatchFolderViewModel NewWatchFolder(string path, int intervalSeconds) =>
+ new(path, intervalSeconds, WatchFolderService.IsNetworkPath(path), SetWatchInterval, RemoveWatchFolder);
+
+ private void SetWatchInterval(WatchFolderViewModel folder, int seconds)
+ {
+ folder.IntervalSeconds = seconds;
+ foreach (var w in _s.Settings.WatchFolders.Where(w => string.Equals(w.Path, folder.Path, StringComparison.OrdinalIgnoreCase))) w.IntervalSeconds = seconds;
+ _s.SaveSettings();
+ _s.Watch.Watch(folder.Path, TimeSpan.FromSeconds(seconds));
+ }
+
+ private void RemoveWatchFolder(WatchFolderViewModel folder)
+ {
+ _s.Watch.Stop(folder.Path);
+ _s.Memory.RemoveFolder(folder.Path);
+ _s.Settings.WatchFolders.RemoveAll(w => string.Equals(w.Path, folder.Path, StringComparison.OrdinalIgnoreCase));
+ _s.SaveSettings();
+ WatchFolders.Remove(folder);
+ }
+
+ private void OnSessionChanged(object? sender, PropertyChangedEventArgs e)
+ {
+ OnPropertyChanged(nameof(ServerText));
+ OnPropertyChanged(nameof(UserText));
+ OnPropertyChanged(nameof(ConnectionKind));
+ OnPropertyChanged(nameof(ConnectionText));
+ OnPropertyChanged(nameof(QuotaText));
+ OnPropertyChanged(nameof(IsConnectVisible));
+ OnPropertyChanged(nameof(BandwidthLimitText));
+ if (e.PropertyName != nameof(IServerSession.State)) return;
+ if (_s.Session.State == ConnectionState.Connected) _ = LoadSequencesAsync();
+ if (IsConnectVisible)
+ {
+ Connect.Address = ConnectViewModel.Display(_s.Session.Address);
+ Connect.Username = _s.Session.Username ?? Connect.Username;
+ }
+ CommandManager.InvalidateRequerySuggested();
+ }
+
+ private void OnSessionEnded(object? sender, ServerProblem problem) =>
+ ShowToast(Loc.L(problem == ServerProblem.CertificateChanged ? "Uploader.Toast.CertificateChanged" : "Uploader.Toast.SignedOut"));
+
+ private void OnDrained(object? sender, EventArgs e)
+ {
+ // The queue restarts its count with each new batch, so every confirmed item belongs to this run.
+ var confirmed = _s.Queue.Items.Count(i => i.IsDone);
+ RefreshQueue();
+ _ = _s.Session.RefreshAsync();
+ if (confirmed > 0) UploadsCompleted?.Invoke(confirmed);
+ }
+
+ private void ShowToast(string title, string? actionText = null, string? actionToolTip = null, ICommand? action = null) =>
+ PendingToasts.Add(new Toast { Title = title, ActionText = actionText, ActionToolTip = actionToolTip, ActionCommand = action });
+
+ private void OpenLogs()
+ {
+ if (Directory.Exists(_s.LogDirectory)) Process.Start(new ProcessStartInfo(_s.LogDirectory) { UseShellExecute = true, Verb = "explore" });
+ }
+}
diff --git a/src/Titano.Uploader/Shell/MainWindow.xaml b/src/Titano.Uploader/Shell/MainWindow.xaml
new file mode 100644
index 00000000..ad977433
--- /dev/null
+++ b/src/Titano.Uploader/Shell/MainWindow.xaml
@@ -0,0 +1,433 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Titano.Uploader/Shell/MainWindow.xaml.cs b/src/Titano.Uploader/Shell/MainWindow.xaml.cs
new file mode 100644
index 00000000..3d69f4e4
--- /dev/null
+++ b/src/Titano.Uploader/Shell/MainWindow.xaml.cs
@@ -0,0 +1,139 @@
+using System.Collections.Specialized;
+using System.ComponentModel;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Controls.Primitives;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Shapes;
+using Titano.Ui.Controls;
+using Titano.Uploader.Infrastructure;
+
+namespace Titano.Uploader.Shell;
+
+/// The uploader's single window (mockup Uploader.html).
+public sealed partial class MainWindow : TitanoWindow
+{
+ private readonly MainViewModel _vm;
+ private readonly UploaderSettings _settings;
+
+ public MainWindow(MainViewModel vm, UploaderSettings settings)
+ {
+ _vm = vm;
+ _settings = settings;
+ DataContext = vm;
+ InitializeComponent();
+ vm.PendingToasts.CollectionChanged += OnToastRequested;
+ vm.PropertyChanged += OnViewModelChanged;
+ PreviewKeyDown += OnPreviewKeyDown;
+ RestorePlacement();
+ }
+
+ /// Remembers size and position (also when the window only hides to the tray).
+ public void SavePlacement()
+ {
+ var b = RestoreBounds;
+ if (b.IsEmpty || b.Width < MinWidth || b.Height < MinHeight) return;
+ _settings.Window = new UploaderSettings.WindowPlacement { Left = b.Left, Top = b.Top, Width = b.Width, Height = b.Height };
+ }
+
+ protected override void OnDragOver(DragEventArgs e)
+ {
+ var accepted = CanDrop(e);
+ e.Effects = accepted ? DragDropEffects.Copy : DragDropEffects.None;
+ SetDropHighlight(accepted);
+ e.Handled = true;
+ }
+
+ protected override void OnDragLeave(DragEventArgs e) => SetDropHighlight(false);
+
+ protected override void OnDrop(DragEventArgs e)
+ {
+ SetDropHighlight(false);
+ if (!CanDrop(e) || e.Data.GetData(DataFormats.FileDrop) is not string[] paths) return;
+ e.Handled = true;
+ _ = EnqueueAsync(paths);
+ }
+
+ private async Task EnqueueAsync(IReadOnlyList paths)
+ {
+ try
+ {
+ await _vm.EnqueueAsync(paths);
+ }
+ catch (Exception ex)
+ {
+ // UI boundary: a drop must never take the window down.
+ _vm.ReportError(ex);
+ }
+ }
+
+ private bool CanDrop(DragEventArgs e) => !_vm.IsConnectVisible && e.Data.GetDataPresent(DataFormats.FileDrop);
+
+ private void SetDropHighlight(bool on)
+ {
+ DropBorder.SetResourceReference(Shape.StrokeProperty, on ? "Brush.Accent" : "Brush.Stroke");
+ if (on) DropBorder.SetResourceReference(Shape.FillProperty, "Brush.AccentSubtle");
+ else DropBorder.Fill = Brushes.Transparent;
+ }
+
+ private void OnBrowseClick(object sender, RoutedEventArgs e) => OpenMenu((Button)sender);
+
+ private void OnWatchChipClick(object sender, RoutedEventArgs e) => OpenMenu((Button)sender);
+
+ /// Opens a button's menu under it on a normal click.
+ private static void OpenMenu(Button button)
+ {
+ if (button.ContextMenu is not { } menu) return;
+ menu.DataContext = button.DataContext;
+ menu.PlacementTarget = button;
+ menu.Placement = PlacementMode.Bottom;
+ menu.IsOpen = true;
+ }
+
+ private void OnSequencesDropDownOpened(object? sender, EventArgs e) => _vm.RefreshSequencesCommand.Execute(null);
+
+ private void OnPasswordChanged(object sender, RoutedEventArgs e) => _vm.Connect.Password = Password.Password;
+
+ /// DESIGN §4 inputs: Enter commits, Esc reverts; Esc also closes the settings panel.
+ private void OnPreviewKeyDown(object sender, KeyEventArgs e)
+ {
+ if (e.OriginalSource is TextBox box && (e.Key is Key.Enter or Key.Escape) && box.GetBindingExpression(TextBox.TextProperty) is { } binding)
+ {
+ if (e.Key == Key.Enter) binding.UpdateSource();
+ else binding.UpdateTarget();
+ if (e.Key == Key.Escape) e.Handled = true;
+ return;
+ }
+ if (e.Key == Key.Escape && _vm.IsSettingsOpen)
+ {
+ _vm.IsSettingsOpen = false;
+ e.Handled = true;
+ }
+ }
+
+ private void OnViewModelChanged(object? sender, PropertyChangedEventArgs e)
+ {
+ if (e.PropertyName == nameof(MainViewModel.CurrentItem) && _vm.CurrentItem is { } item) ItemList.ScrollIntoView(item);
+ if (e.PropertyName == nameof(MainViewModel.IsConnectVisible) && _vm.IsConnectVisible) Password.Clear();
+ }
+
+ private void OnToastRequested(object? sender, NotifyCollectionChangedEventArgs e)
+ {
+ if (e.Action != NotifyCollectionChangedAction.Add || e.NewItems is null) return;
+ foreach (Toast t in e.NewItems) Toasts.Show(t);
+ Dispatcher.BeginInvoke(() => _vm.PendingToasts.Clear());
+ }
+
+ private void RestorePlacement()
+ {
+ if (_settings.Window is not { } p || p.Width < MinWidth || p.Height < MinHeight) return;
+ var screen = new Rect(SystemParameters.VirtualScreenLeft, SystemParameters.VirtualScreenTop, SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight);
+ if (!screen.IntersectsWith(new Rect(p.Left, p.Top, p.Width, p.Height))) return;
+ WindowStartupLocation = WindowStartupLocation.Manual;
+ Left = p.Left;
+ Top = p.Top;
+ Width = p.Width;
+ Height = p.Height;
+ }
+}
diff --git a/src/Titano.Uploader/Shell/ScreenCapture.cs b/src/Titano.Uploader/Shell/ScreenCapture.cs
new file mode 100644
index 00000000..9a061f74
--- /dev/null
+++ b/src/Titano.Uploader/Shell/ScreenCapture.cs
@@ -0,0 +1,79 @@
+using System.IO;
+using System.Windows;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Threading;
+using Titano.Core.Diagnostics;
+using Titano.Ui.Theming;
+using Titano.Uploader.DesignTime;
+
+namespace Titano.Uploader.Shell;
+
+///
+/// Titano.Uploader.exe --capture <dir>: renders the window at 760×560 in the dark and light themes —
+/// first run (connect), queue running, settings panel — from , then exits.
+///
+public static class ScreenCapture
+{
+ public static void Run(MainWindow window, MainViewModel vm, SampleData sample, string directory)
+ {
+ Directory.CreateDirectory(directory);
+ window.WindowStartupLocation = WindowStartupLocation.Manual;
+ window.Left = -20000;
+ window.Top = -20000;
+ window.Width = 760;
+ window.Height = 560;
+ window.ShowActivated = false;
+ window.ShowInTaskbar = false;
+ window.Show();
+
+ window.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, () =>
+ {
+ try
+ {
+ foreach (var theme in new[] { AppTheme.Dark, AppTheme.Light })
+ {
+ var suffix = theme.ToString().ToLowerInvariant();
+ ThemeManager.Apply(theme);
+ sample.ShowConnect();
+ Save(window, Path.Combine(directory, $"uploader-connect-{suffix}.png"));
+ sample.ShowQueue();
+ Save(window, Path.Combine(directory, $"uploader-queue-{suffix}.png"));
+ vm.IsSettingsOpen = true;
+ Save(window, Path.Combine(directory, $"uploader-settings-{suffix}.png"));
+ vm.IsSettingsOpen = false;
+ }
+ Log.Info("capture", $"Screenshots written to {Path.GetFullPath(directory)}");
+ }
+ catch (Exception ex)
+ {
+ Log.Error("capture", "Capture failed", ex);
+ Environment.ExitCode = 1;
+ }
+ finally
+ {
+ Application.Current.Shutdown();
+ }
+ });
+ }
+
+ private static void Save(Window window, string path)
+ {
+ // Let bindings, templates and deferred layout complete.
+ window.UpdateLayout();
+ window.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, () => { });
+ window.UpdateLayout();
+ if (VisualTreeHelper.GetChildrenCount(window) == 0) return;
+ var root = (FrameworkElement)VisualTreeHelper.GetChild(window, 0);
+ var dpi = VisualTreeHelper.GetDpi(window);
+ var width = (int)Math.Ceiling(root.ActualWidth * dpi.DpiScaleX);
+ var height = (int)Math.Ceiling(root.ActualHeight * dpi.DpiScaleY);
+ if (width <= 0 || height <= 0) return;
+ var bitmap = new RenderTargetBitmap(width, height, dpi.PixelsPerInchX, dpi.PixelsPerInchY, PixelFormats.Pbgra32);
+ bitmap.Render(root);
+ var encoder = new PngBitmapEncoder();
+ encoder.Frames.Add(BitmapFrame.Create(bitmap));
+ using var stream = File.Create(path);
+ encoder.Save(stream);
+ }
+}
diff --git a/src/Titano.Uploader/Shell/UploaderDialogs.cs b/src/Titano.Uploader/Shell/UploaderDialogs.cs
new file mode 100644
index 00000000..711abada
--- /dev/null
+++ b/src/Titano.Uploader/Shell/UploaderDialogs.cs
@@ -0,0 +1,80 @@
+using System.Windows;
+using System.Windows.Interop;
+using Titano.Core.Imaging;
+using Titano.Core.Localization;
+using Titano.Uploader.Interop;
+
+namespace Titano.Uploader.Shell;
+
+/// Pickers and confirmations the view model asks for.
+public interface IUploaderDialogs
+{
+ /// Photos to add; empty when cancelled.
+ IReadOnlyList PickFiles();
+
+ /// A folder or card root to add; null when cancelled.
+ string? PickFolder();
+
+ /// A folder to watch; null when cancelled.
+ string? PickWatchFolder();
+
+ /// Trust-on-first-use: shows the certificate fingerprint; true when the user trusts it.
+ bool ConfirmTrust(string address, string fingerprint);
+
+ /// Destructive confirmation for "Delete after upload"; true when the user turns it on.
+ bool ConfirmDeleteAfterUpload();
+}
+
+/// Window-owned implementation: shell Open dialogs and .
+public sealed class UploaderDialogs : IUploaderDialogs
+{
+ /// Owner of the dialogs; set once the main window exists.
+ public Window? Owner { get; set; }
+
+ private IntPtr OwnerHandle => Owner is null ? IntPtr.Zero : new WindowInteropHelper(Owner).Handle;
+
+ public IReadOnlyList PickFiles()
+ {
+ var photos = string.Join(';', ImageFormatInfo.Known.SelectMany(f => f.Extensions).Select(e => "*." + e));
+ return FileDialogs.PickFiles(OwnerHandle, Loc.L("Uploader.Browse.FilesTitle"),
+ [
+ new FileFilter(Loc.L("Uploader.Browse.Photos"), photos),
+ new FileFilter(Loc.L("Uploader.Browse.AllFiles"), "*.*"),
+ ]);
+ }
+
+ public string? PickFolder() => FileDialogs.PickFolder(OwnerHandle, Loc.L("Uploader.Browse.FolderTitle"));
+
+ public string? PickWatchFolder() => FileDialogs.PickFolder(OwnerHandle, Loc.L("Uploader.Browse.WatchTitle"));
+
+ public bool ConfirmTrust(string address, string fingerprint) => ConfirmDialog.Ask(Owner, new ConfirmRequest
+ {
+ Heading = Loc.L("Uploader.Trust.Title"),
+ Message = Loc.L("Uploader.Trust.Message"),
+ Detail = address + "\n" + FormatFingerprint(fingerprint),
+ DetailToolTip = Loc.L("Tooltips.Uploader.Fingerprint"),
+ SafeText = Loc.L("Uploader.Trust.Cancel"),
+ SafeToolTip = Loc.L("Tooltips.Uploader.CancelTrust"),
+ ActionText = Loc.L("Uploader.Trust.Accept"),
+ ActionToolTip = Loc.L("Tooltips.Uploader.Trust"),
+ IsDestructive = false,
+ });
+
+ public bool ConfirmDeleteAfterUpload() => ConfirmDialog.Ask(Owner, new ConfirmRequest
+ {
+ Heading = Loc.L("Uploader.Delete.Title"),
+ Message = Loc.L("Uploader.Delete.Message"),
+ SafeText = Loc.L("Uploader.Delete.Keep"),
+ SafeToolTip = Loc.L("Tooltips.Uploader.KeepFiles"),
+ ActionText = Loc.L("Uploader.Delete.Accept"),
+ ActionToolTip = Loc.L("Tooltips.Uploader.ConfirmDelete"),
+ IsDestructive = true,
+ });
+
+ /// Splits the 32-byte SHA-256 fingerprint into two readable lines of 16 bytes.
+ public static string FormatFingerprint(string fingerprint)
+ {
+ var bytes = fingerprint.Split(':');
+ return bytes.Length <= 16 ? fingerprint : string.Join(':', bytes.Take(16)) + "\n" + string.Join(':', bytes.Skip(16));
+ }
+}
diff --git a/src/Titano.Uploader/Shell/WatchFolderViewModel.cs b/src/Titano.Uploader/Shell/WatchFolderViewModel.cs
new file mode 100644
index 00000000..7950b876
--- /dev/null
+++ b/src/Titano.Uploader/Shell/WatchFolderViewModel.cs
@@ -0,0 +1,78 @@
+using System.Windows.Input;
+using Titano.Core.Localization;
+using Titano.Ui.Mvvm;
+using Titano.Uploader.Text;
+
+namespace Titano.Uploader.Shell;
+
+/// An entry of a watch folder chip's menu: an interval choice, a separator (no text) or "Remove".
+public sealed record WatchMenuOption(string? Text, string? ToolTip, ICommand? Command, bool IsChecked)
+{
+ public bool IsSeparator => Text is null;
+}
+
+/// One watch folder chip: path, interval, reachability.
+public sealed class WatchFolderViewModel : ObservableObject
+{
+ /// Intervals offered in the chip menu.
+ public static readonly int[] IntervalChoices = [30, 60, 300, 900];
+
+ private readonly Action _setInterval;
+ private readonly Action _remove;
+ private int _intervalSeconds;
+ private bool? _isAvailable;
+
+ public WatchFolderViewModel(string path, int intervalSeconds, bool isNetwork, Action setInterval, Action remove)
+ {
+ Path = path;
+ IsNetwork = isNetwork;
+ _intervalSeconds = intervalSeconds;
+ _setInterval = setInterval;
+ _remove = remove;
+ }
+
+ public string Path { get; }
+ public bool IsNetwork { get; }
+
+ public int IntervalSeconds
+ {
+ get => _intervalSeconds;
+ set
+ {
+ if (!Set(ref _intervalSeconds, value)) return;
+ OnPropertyChanged(nameof(Text));
+ OnPropertyChanged(nameof(ToolTip));
+ OnPropertyChanged(nameof(MenuOptions));
+ }
+ }
+
+ /// Null until the first check finished.
+ public bool? IsAvailable
+ {
+ get => _isAvailable;
+ set
+ {
+ if (Set(ref _isAvailable, value)) OnPropertyChanged(nameof(ToolTip));
+ }
+ }
+
+ /// "D:\Cam\Roof · every 1 min".
+ public string Text => Loc.L("Uploader.Watch.Chip", Path, Interval(IntervalSeconds));
+
+ public string ToolTip => IsAvailable == false ? Loc.L("Tooltips.Uploader.WatchFolderMissing", Interval(IntervalSeconds))
+ : IsNetwork ? Loc.L("Tooltips.Uploader.WatchFolderNetwork", Interval(IntervalSeconds))
+ : Loc.L("Tooltips.Uploader.WatchFolder", Interval(IntervalSeconds));
+
+ public IReadOnlyList MenuOptions =>
+ [
+ .. IntervalChoices.Select(s => new WatchMenuOption(
+ Loc.L("Uploader.Watch.Every", Interval(s)),
+ Loc.L("Tooltips.Uploader.WatchInterval"),
+ new RelayCommand(() => _setInterval(this, s)),
+ s == IntervalSeconds)),
+ new WatchMenuOption(null, null, null, false),
+ new WatchMenuOption(Loc.L("Uploader.Watch.Remove"), Loc.L("Tooltips.Uploader.RemoveWatchFolder"), new RelayCommand(() => _remove(this)), false),
+ ];
+
+ private static string Interval(int seconds) => UploaderText.Duration(TimeSpan.FromSeconds(seconds), Loc.Culture);
+}
diff --git a/src/Titano.Uploader/Sources/PhotoScanner.cs b/src/Titano.Uploader/Sources/PhotoScanner.cs
new file mode 100644
index 00000000..85ce2461
--- /dev/null
+++ b/src/Titano.Uploader/Sources/PhotoScanner.cs
@@ -0,0 +1,82 @@
+using System.IO;
+using Titano.Core.Diagnostics;
+using Titano.Core.Imaging;
+
+namespace Titano.Uploader.Sources;
+
+/// A photo found on disk. is the dropped folder (or the file's folder) it was found under.
+public sealed record SourceFile(string Path, long Size, DateTime ModifiedUtc, DateTime CapturedAt, string Root);
+
+/// Finds supported photos under dropped files, folders and card roots (BRIEF §15: zero analysis).
+public static class PhotoScanner
+{
+ private static readonly EnumerationOptions Recursive = new()
+ {
+ RecurseSubdirectories = true,
+ IgnoreInaccessible = true,
+ // Skips "System Volume Information", "$RECYCLE.BIN" and camera housekeeping files.
+ AttributesToSkip = FileAttributes.Hidden | FileAttributes.System,
+ MatchType = MatchType.Simple,
+ };
+
+ /// Supported photos under , in path order, without duplicates.
+ public static IReadOnlyList Scan(IEnumerable paths, CancellationToken ct = default)
+ {
+ var seen = new HashSet(StringComparer.OrdinalIgnoreCase);
+ var result = new List();
+ foreach (var raw in paths)
+ {
+ ct.ThrowIfCancellationRequested();
+ string full;
+ try
+ {
+ full = Path.GetFullPath(raw);
+ }
+ catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
+ {
+ Log.Warning("scan", $"Ignored path {raw}", ex);
+ continue;
+ }
+ if (Directory.Exists(full))
+ {
+ try
+ {
+ foreach (var file in Files(full, ct))
+ {
+ if (seen.Add(file.FullName)) result.Add(ToSource(file, full));
+ }
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ // A card pulled mid-scan: keep what was found; the rest can be dropped again.
+ Log.Warning("scan", $"Listing of {full} stopped early", ex);
+ }
+ }
+ else if (File.Exists(full) && ImageFormatInfo.IsSupportedExtension(full) && seen.Add(full))
+ {
+ var info = new FileInfo(full);
+ result.Add(ToSource(info, info.DirectoryName ?? full));
+ }
+ }
+ return result;
+ }
+
+ /// Supported photos under , recursively. I/O errors while listing propagate.
+ public static IEnumerable Files(string folder, CancellationToken ct = default)
+ {
+ foreach (var file in new DirectoryInfo(folder).EnumerateFiles("*", Recursive))
+ {
+ ct.ThrowIfCancellationRequested();
+ if (ImageFormatInfo.IsSupportedExtension(file.Name)) yield return file;
+ }
+ }
+
+ public static SourceFile ToSource(FileInfo file, string root) =>
+ new(file.FullName, file.Length, file.LastWriteTimeUtc, CaptureTime(file), root);
+
+ ///
+ /// Capture time read cheaply: the file's last-write time in local wall-clock, which cameras set to the shot time.
+ /// Exif dates replace it when the metadata reader lands in Titano.Core.
+ ///
+ public static DateTime CaptureTime(FileInfo file) => DateTime.SpecifyKind(file.LastWriteTime, DateTimeKind.Unspecified);
+}
diff --git a/src/Titano.Uploader/Sources/SessionGrouper.cs b/src/Titano.Uploader/Sources/SessionGrouper.cs
new file mode 100644
index 00000000..8102d68c
--- /dev/null
+++ b/src/Titano.Uploader/Sources/SessionGrouper.cs
@@ -0,0 +1,89 @@
+using System.Globalization;
+using System.IO;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Titano.Uploader.Sources;
+
+/// The last session of a source, so later files within the gap join it (watch folders).
+public sealed record SessionContinuation(string Key, DateTime LastCapture);
+
+/// Session key per input frame (input order) and the session the next batch may continue.
+public sealed record SessionAssignment(IReadOnlyList Keys, SessionContinuation? Last);
+
+///
+/// Splits frames of one source into shooting sessions with the card-import rules (BRIEF §13): a gap longer than
+/// starts a session; sessions with fewer than frames (test shots) merge
+/// into the previous session, or into the next one when they come first. The server turns each key into a sequence.
+///
+public static class SessionGrouper
+{
+ /// SessionGapMinutes default.
+ public static readonly TimeSpan Gap = TimeSpan.FromMinutes(45);
+
+ /// MinimumSessionFrames default.
+ public const int MinimumFrames = 8;
+
+ /// Groups ; keys start with so sources never collide.
+ public static SessionAssignment Assign(IReadOnlyList captureTimes, string sourceTag, SessionContinuation? previous = null)
+ {
+ if (captureTimes.Count == 0) return new SessionAssignment([], previous);
+ var order = Enumerable.Range(0, captureTimes.Count).OrderBy(i => captureTimes[i]).ToArray();
+
+ // Split on gaps; a group that continues the previous batch keeps its key and is never "small".
+ var groups = new List();
+ var current = new Group { Continues = previous is not null && (captureTimes[order[0]] - previous.LastCapture).Duration() <= Gap };
+ for (var n = 0; n < order.Length; n++)
+ {
+ var i = order[n];
+ if (n > 0 && captureTimes[i] - captureTimes[order[n - 1]] > Gap)
+ {
+ groups.Add(current);
+ current = new Group();
+ }
+ current.Members.Add(i);
+ }
+ groups.Add(current);
+
+ var merged = new List();
+ foreach (var g in groups)
+ {
+ if (merged.Count > 0 && g.Members.Count < MinimumFrames) merged[^1].Members.AddRange(g.Members);
+ else merged.Add(g);
+ }
+ if (merged.Count > 1 && merged[0].Members.Count < MinimumFrames && !merged[0].Continues)
+ {
+ merged[0].Members.AddRange(merged[1].Members);
+ merged.RemoveAt(1);
+ }
+
+ var keys = new string[captureTimes.Count];
+ string? lastKey = null;
+ foreach (var g in merged)
+ {
+ var first = g.Members.Min(i => captureTimes[i]);
+ lastKey = g.Continues ? previous!.Key : Key(sourceTag, first);
+ foreach (var i in g.Members) keys[i] = lastKey;
+ }
+ var lastCapture = captureTimes[order[^1]];
+ if (previous is not null && previous.LastCapture > lastCapture && merged[^1].Continues) lastCapture = previous.LastCapture;
+ return new SessionAssignment(keys, new SessionContinuation(lastKey!, lastCapture));
+ }
+
+ /// Short stable tag for a source folder: 8 hex digits of the SHA-256 of its normalised path.
+ public static string SourceTag(string root)
+ {
+ var normalized = Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)).ToUpperInvariant();
+ return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(normalized)))[..8];
+ }
+
+ /// {tag}-{yyyyMMdd-HHmmss} of the session's first frame.
+ public static string Key(string sourceTag, DateTime firstCapture) =>
+ sourceTag + "-" + firstCapture.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture);
+
+ private sealed class Group
+ {
+ public List Members { get; } = [];
+ public bool Continues { get; init; }
+ }
+}
diff --git a/src/Titano.Uploader/Text/UploaderText.cs b/src/Titano.Uploader/Text/UploaderText.cs
new file mode 100644
index 00000000..bd05ce4f
--- /dev/null
+++ b/src/Titano.Uploader/Text/UploaderText.cs
@@ -0,0 +1,114 @@
+using System.Globalization;
+using Titano.Core.Localization;
+using Titano.Uploader.Uploads;
+
+namespace Titano.Uploader.Text;
+
+/// What the queue is doing, for the end of the queue line.
+public enum QueueActivity
+{
+ Idle,
+ Running,
+ Paused,
+ WaitingForServer,
+ Done,
+}
+
+///
+/// Culture-aware texts of the uploader: sizes, rates, durations and the queue/progress lines
+/// ("4,812 files · 96.2 GB · 38 MB/s · 41 min left"). Every method takes the culture so results are deterministic.
+///
+public static class UploaderText
+{
+ private const string Separator = " · ";
+ private static readonly string[] ByteUnits = ["B", "KB", "MB", "GB", "TB", "PB"];
+
+ /// Localized string for in , formatted with .
+ public static string L(string key, CultureInfo culture, params object?[] args)
+ {
+ var format = Loc.Get(key, culture) ?? "⟦" + key + "⟧";
+ return args.Length == 0 ? format : string.Format(culture, format, args);
+ }
+
+ /// Plural-aware lookup of key.one/few/many/other; {0} receives the count.
+ public static string Plural(string key, long count, CultureInfo culture)
+ {
+ var category = PluralRules.Suffix(PluralRules.Select(culture, count));
+ var format = Loc.Get(key + "." + category, culture) ?? Loc.Get(key + ".other", culture) ?? "⟦" + key + "⟧";
+ return string.Format(culture, format, count);
+ }
+
+ /// 1,536 → "1.5 KB" (decimal units, as Explorer shows for drives).
+ public static string Bytes(long bytes, CultureInfo culture)
+ {
+ var (value, unit) = Scale(bytes);
+ var format = unit == 0 || value >= 100 ? "0" : "0.#";
+ return value.ToString(format, culture) + " " + ByteUnits[unit];
+ }
+
+ /// 38,400,000 B/s → "38 MB/s"; one decimal below 10 units.
+ public static string Rate(double bytesPerSecond, CultureInfo culture)
+ {
+ var (value, unit) = Scale(Math.Max(0, bytesPerSecond));
+ var format = unit == 0 || value >= 10 ? "0" : "0.#";
+ return L("Uploader.Unit.PerSecond", culture, value.ToString(format, culture) + " " + ByteUnits[unit]);
+ }
+
+ /// Compact duration: "2 h 05 m", "41 min", "40 s".
+ public static string Duration(TimeSpan t, CultureInfo culture)
+ {
+ if (t.TotalHours >= 24) return L("Common.Duration.Days", culture, (int)t.TotalDays, t.Hours);
+ if (t.TotalMinutes >= 59.5) return L("Common.Duration.Hours", culture, (int)Math.Round(t.TotalMinutes) / 60, ((int)Math.Round(t.TotalMinutes) % 60).ToString("00", culture));
+ if (t.TotalSeconds >= 59.5) return L("Common.Duration.Minutes", culture, (int)Math.Round(t.TotalMinutes));
+ return L("Common.Duration.Seconds", culture, (int)Math.Round(t.TotalSeconds));
+ }
+
+ /// "4,812 files · 96.2 GB · 38 MB/s · 41 min left", or the files and size followed by the activity.
+ public static string QueueLine(QueueSummary summary, QueueActivity activity, double bytesPerSecond, TimeSpan? left, CultureInfo culture)
+ {
+ if (summary.Files == 0) return L("Uploader.Queue.Empty", culture);
+ var parts = new List { Plural("Uploader.Queue.Files", summary.Files, culture), Bytes(summary.Bytes, culture) };
+ switch (activity)
+ {
+ case QueueActivity.Running:
+ if (bytesPerSecond > 0) parts.Add(Rate(bytesPerSecond, culture));
+ if (left is { } l) parts.Add(L("Uploader.Queue.Left", culture, Duration(l, culture)));
+ break;
+ case QueueActivity.Paused:
+ parts.Add(L("Uploader.Queue.Paused", culture));
+ break;
+ case QueueActivity.WaitingForServer:
+ parts.Add(L("Uploader.Queue.Waiting", culture));
+ break;
+ case QueueActivity.Done:
+ parts.Add(L("Uploader.Queue.Done", culture));
+ break;
+ }
+ return string.Join(Separator, parts);
+ }
+
+ /// "2,214 of 4,812 sent · 2,201 verified by the server · 0 retries", plus failures when there are any.
+ public static string ProgressLine(QueueSummary summary, CultureInfo culture)
+ {
+ var parts = new List
+ {
+ L("Uploader.Progress.Sent", culture, summary.Sent, summary.Files),
+ L("Uploader.Progress.Verified", culture, summary.Verified),
+ Plural("Uploader.Progress.Retries", summary.Retries, culture),
+ };
+ if (summary.Failed > 0) parts.Add(L("Uploader.Progress.Failed", culture, summary.Failed));
+ return string.Join(Separator, parts);
+ }
+
+ private static (double Value, int Unit) Scale(double bytes)
+ {
+ var value = bytes;
+ var unit = 0;
+ while (value >= 999.95 && unit < ByteUnits.Length - 1)
+ {
+ value /= 1000;
+ unit++;
+ }
+ return (value, unit);
+ }
+}
diff --git a/src/Titano.Uploader/Uploads/IUploadTransport.cs b/src/Titano.Uploader/Uploads/IUploadTransport.cs
new file mode 100644
index 00000000..33298391
--- /dev/null
+++ b/src/Titano.Uploader/Uploads/IUploadTransport.cs
@@ -0,0 +1,47 @@
+using Titano.Protocol.Client;
+
+namespace Titano.Uploader.Uploads;
+
+/// How the server answered a successful upload.
+public enum UploadOutcome
+{
+ /// Committed after the server checked the SHA-256 (CompleteUploadResponse).
+ Verified,
+
+ /// The server already held this SHA-256 (AlreadyExists).
+ AlreadyOnServer,
+}
+
+/// What the upload needs from the server connection.
+public interface IUploadTransport
+{
+ /// True while signed in and reachable; the engine waits otherwise.
+ bool IsReady { get; }
+
+ /// Raised (on the UI thread) when changes.
+ event EventHandler? ReadyChanged;
+
+ ///
+ /// Sends one file whose hash is known. Throws when the server cannot be
+ /// used right now (offline, signed out, certificate changed); other failures concern this file only.
+ ///
+ Task<(UploadOutcome Outcome, Guid? FrameId)> UploadAsync(UploadItem item, Guid? sequenceId, string sha256, IProgress progress, CancellationToken ct);
+
+ /// Creates a sequence for a "new sequence" batch.
+ Task CreateSequenceAsync(string? title, CancellationToken ct);
+}
+
+/// Why the server cannot be used.
+public enum ServerProblem
+{
+ Unreachable,
+ SignedOut,
+ CertificateChanged,
+}
+
+/// The server cannot take uploads right now; the item goes back in line without spending a retry.
+public sealed class ServerUnavailableException(ServerProblem problem, Exception? inner = null)
+ : Exception("Server unavailable: " + problem, inner)
+{
+ public ServerProblem Problem { get; } = problem;
+}
diff --git a/src/Titano.Uploader/Uploads/QueueStore.cs b/src/Titano.Uploader/Uploads/QueueStore.cs
new file mode 100644
index 00000000..3b70be25
--- /dev/null
+++ b/src/Titano.Uploader/Uploads/QueueStore.cs
@@ -0,0 +1,108 @@
+using System.IO;
+using System.Text.Json.Serialization;
+using System.Text.Json;
+using Titano.Core.Diagnostics;
+
+namespace Titano.Uploader.Uploads;
+
+///
+/// uploader-queue.json: the queue survives restarts (BRIEF §15). Server-side upload ids are not stored —
+/// restarting an item calls POST /uploads again with the same SHA-256 and the server answers with the chunks
+/// it already holds.
+///
+public static class QueueStore
+{
+ public const int Version = 1;
+
+ /// Serializes the queue on the calling (owning) thread; the bytes can be written anywhere.
+ public static byte[] Serialize(UploadQueue queue)
+ {
+ var file = new QueueFile
+ {
+ Version = Version,
+ Paused = queue.IsPaused,
+ Batches = [.. queue.Batches.Select(b => new BatchRecord(b.Id, b.Mode, b.SequenceId, b.Title))],
+ Items =
+ [
+ .. queue.Items.Select(i => new ItemRecord(
+ i.Id, i.BatchId, i.Path, i.Size, i.ModifiedUtc, i.CapturedAt, i.SessionKey, i.WatchRoot,
+ i.IsActive ? UploadItemState.Queued : i.State, i.Failure, i.FailureDetail, i.Retries, i.Sha256, i.FrameId, i.NextRetryUtc)),
+ ],
+ };
+ return JsonSerializer.SerializeToUtf8Bytes(file, QueueJson.Default.QueueFile);
+ }
+
+ /// Loads into ; a missing or corrupt file leaves it empty.
+ public static void Load(string path, UploadQueue queue)
+ {
+ QueueFile? file = null;
+ try
+ {
+ if (File.Exists(path)) file = JsonSerializer.Deserialize(File.ReadAllBytes(path), QueueJson.Default.QueueFile);
+ }
+ catch (JsonException ex)
+ {
+ Log.Warning("queue", "Queue file unreadable; starting empty", ex);
+ }
+ catch (IOException ex)
+ {
+ Log.Warning("queue", "Queue file unreadable; starting empty", ex);
+ }
+ if (file is null || file.Version != Version) return;
+ queue.Restore(
+ file.Batches.Select(b => new UploadBatch { Id = b.Id, Mode = b.Mode, SequenceId = b.SequenceId, Title = b.Title }),
+ file.Items.Select(r => new UploadItem
+ {
+ Id = r.Id,
+ BatchId = r.BatchId,
+ Path = r.Path,
+ Size = r.Size,
+ ModifiedUtc = r.ModifiedUtc,
+ CapturedAt = r.CapturedAt,
+ SessionKey = r.SessionKey,
+ WatchRoot = r.WatchRoot,
+ State = r.State,
+ Failure = r.Failure,
+ FailureDetail = r.FailureDetail,
+ Retries = r.Retries,
+ Sha256 = r.Sha256,
+ FrameId = r.FrameId,
+ NextRetryUtc = r.NextRetryUtc,
+ }),
+ file.Paused);
+ }
+
+ internal sealed class QueueFile
+ {
+ public int Version { get; set; }
+ public bool Paused { get; set; }
+ public List Batches { get; set; } = [];
+ public List Items { get; set; } = [];
+ }
+
+ internal sealed record BatchRecord(Guid Id, UploadTargetMode Mode, Guid? SequenceId, string? Title);
+
+ internal sealed record ItemRecord(
+ Guid Id,
+ Guid BatchId,
+ string Path,
+ long Size,
+ DateTime ModifiedUtc,
+ DateTime? CapturedAt,
+ string? SessionKey,
+ string? WatchRoot,
+ UploadItemState State,
+ UploadFailure Failure,
+ string? FailureDetail,
+ int Retries,
+ string? Sha256,
+ Guid? FrameId,
+ DateTime? NextRetryUtc);
+}
+
+[JsonSourceGenerationOptions(
+ PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
+ DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
+ UseStringEnumConverter = true)]
+[JsonSerializable(typeof(QueueStore.QueueFile))]
+internal sealed partial class QueueJson : JsonSerializerContext;
diff --git a/src/Titano.Uploader/Uploads/QueueSummary.cs b/src/Titano.Uploader/Uploads/QueueSummary.cs
new file mode 100644
index 00000000..d361b9b8
--- /dev/null
+++ b/src/Titano.Uploader/Uploads/QueueSummary.cs
@@ -0,0 +1,115 @@
+namespace Titano.Uploader.Uploads;
+
+/// Aggregate figures behind the queue line and the progress bar.
+public readonly record struct QueueSummary(
+ int Files,
+ long Bytes,
+ int Sent,
+ int Verified,
+ int Failed,
+ int Retries,
+ long DoneBytes,
+ string? CurrentFile)
+{
+ /// 0–1 share of bytes already on the server or in flight.
+ public double Progress => Bytes <= 0 ? 0 : Math.Clamp(DoneBytes / (double)Bytes, 0, 1);
+
+ /// Bytes still to send, counting only items that can still succeed.
+ public long RemainingBytes { get; init; }
+
+ public static QueueSummary From(IEnumerable items)
+ {
+ int files = 0, sent = 0, verified = 0, failed = 0, retries = 0;
+ long bytes = 0, done = 0, remaining = 0;
+ UploadItem? current = null;
+ foreach (var item in items)
+ {
+ files++;
+ bytes += item.Size;
+ retries += item.Retries;
+ switch (item.State)
+ {
+ case UploadItemState.Verified or UploadItemState.Skipped:
+ sent++;
+ verified++;
+ done += item.Size;
+ break;
+ case UploadItemState.Uploading:
+ if (item.BytesSent >= item.Size) sent++;
+ done += Math.Min(item.BytesSent, item.Size);
+ remaining += Math.Max(0, item.Size - item.BytesSent);
+ current = item;
+ break;
+ case UploadItemState.Hashing:
+ remaining += item.Size;
+ current ??= item;
+ break;
+ case UploadItemState.Failed:
+ failed++;
+ if (item.NextRetryUtc is not null) remaining += item.Size;
+ break;
+ default:
+ remaining += item.Size;
+ break;
+ }
+ }
+ return new QueueSummary(files, bytes, sent, verified, failed, retries, done, current?.FileName) { RemainingBytes = remaining };
+ }
+}
+
+/// Throughput over a sliding window of cumulative byte counts.
+public sealed class RateMeter(TimeSpan window)
+{
+ private readonly List<(TimeSpan At, long Total)> _samples = [];
+ private long _total;
+
+ public RateMeter()
+ : this(TimeSpan.FromSeconds(10))
+ {
+ }
+
+ /// Adds sent at (monotonic clock).
+ public void Add(long bytes, TimeSpan now)
+ {
+ // The first sample is the zero baseline so the first chunk counts.
+ if (_samples.Count == 0) _samples.Add((now, _total));
+ _total += bytes;
+ _samples.Add((now, _total));
+ Trim(now);
+ }
+
+ /// Bytes per second over the window; 0 until the window spans at least one second.
+ public double BytesPerSecond(TimeSpan now)
+ {
+ Trim(now);
+ if (_samples.Count == 0) return 0;
+ var (at, total) = _samples[0];
+ // Measured up to now, so a stalled transfer decays towards zero.
+ var span = (now - at).TotalSeconds;
+ return span < 1 ? 0 : (_total - total) / span;
+ }
+
+ public void Reset()
+ {
+ _samples.Clear();
+ _total = 0;
+ }
+
+ /// Keeps the newest sample at or before now − window as the baseline and drops older ones.
+ private void Trim(TimeSpan now)
+ {
+ var cutoff = now - window;
+ var drop = 0;
+ while (drop + 1 < _samples.Count && _samples[drop + 1].At <= cutoff) drop++;
+ if (drop > 0) _samples.RemoveRange(0, drop);
+ }
+
+ /// Time to send at ; null without a usable rate.
+ public static TimeSpan? TimeLeft(long remainingBytes, double bytesPerSecond)
+ {
+ if (remainingBytes <= 0) return TimeSpan.Zero;
+ if (bytesPerSecond < 1024) return null;
+ var seconds = Math.Ceiling(remainingBytes / bytesPerSecond);
+ return seconds >= TimeSpan.FromDays(365).TotalSeconds ? null : TimeSpan.FromSeconds(seconds);
+ }
+}
diff --git a/src/Titano.Uploader/Uploads/UploadEngine.cs b/src/Titano.Uploader/Uploads/UploadEngine.cs
new file mode 100644
index 00000000..a6f26697
--- /dev/null
+++ b/src/Titano.Uploader/Uploads/UploadEngine.cs
@@ -0,0 +1,365 @@
+using System.ComponentModel;
+using System.IO;
+using Titano.Core.Diagnostics;
+using Titano.Protocol.Client;
+
+namespace Titano.Uploader.Uploads;
+
+/// What the upload engine is doing.
+public enum EngineStatus
+{
+ Idle,
+ Running,
+ Paused,
+ WaitingForServer,
+}
+
+/// Queue control used by the window and the tray.
+public interface IUploadEngine : INotifyPropertyChanged
+{
+ EngineStatus Status { get; }
+
+ /// Current throughput over the last seconds.
+ double BytesPerSecond { get; }
+
+ /// Raised when the last pending item finished (balloon on completion).
+ event EventHandler? Drained;
+
+ /// Starts work for newly queued items without waiting for the next tick.
+ void Pump();
+
+ void Pause();
+ void Resume();
+
+ /// Stops every transfer and clears the queue; files already uploaded stay on the server.
+ void StopAndClear();
+
+ /// Puts a failed item back in line with a fresh retry budget.
+ void Retry(UploadItem item);
+}
+
+///
+/// Works through the with concurrent files: hash, upload
+/// (resumable chunks; the server verifies the SHA-256), then optionally delete the source the server confirmed.
+/// Lives on the UI thread: state changes happen there while hashing and HTTP run on the thread pool.
+///
+public sealed class UploadEngine : IUploadEngine, IDisposable
+{
+ public const int Parallelism = 2;
+
+ private readonly UploadQueue _queue;
+ private readonly IUploadTransport _transport;
+ private readonly Func _deleteAfterUpload;
+ private readonly TimeProvider _time;
+ private readonly long _origin;
+ private readonly RateMeter _meter = new();
+ private readonly Dictionary _running = [];
+ private readonly Dictionary> _creating = [];
+ private readonly CancellationTokenSource _lifetime = new();
+ private EngineStatus _status;
+ private double _bytesPerSecond;
+ private bool _hadWork;
+
+ public UploadEngine(UploadQueue queue, IUploadTransport transport, Func deleteAfterUpload, TimeProvider? time = null)
+ {
+ _queue = queue;
+ _transport = transport;
+ _deleteAfterUpload = deleteAfterUpload;
+ _time = time ?? TimeProvider.System;
+ _origin = _time.GetTimestamp();
+ _transport.ReadyChanged += (_, _) => Pump();
+ }
+
+ public event PropertyChangedEventHandler? PropertyChanged;
+
+ public event EventHandler? Drained;
+
+ /// Raised when the server confirmed an item (verified or already present).
+ public event EventHandler? ItemConfirmed;
+
+ public EngineStatus Status
+ {
+ get => _status;
+ private set
+ {
+ if (_status == value) return;
+ _status = value;
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Status)));
+ }
+ }
+
+ public double BytesPerSecond
+ {
+ get => _bytesPerSecond;
+ private set
+ {
+ if (_bytesPerSecond.Equals(value)) return;
+ _bytesPerSecond = value;
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BytesPerSecond)));
+ }
+ }
+
+ private TimeSpan Now => _time.GetElapsedTime(_origin);
+ private DateTime UtcNow => _time.GetUtcNow().UtcDateTime;
+
+ /// Starts the once-a-second pump on the calling (UI) thread.
+ public void Start() => _ = TickAsync(_lifetime.Token);
+
+ /// Starts work for newly queued items without waiting for the next tick.
+ public void Pump()
+ {
+ if (_lifetime.IsCancellationRequested) return;
+ while (!_queue.IsPaused && _transport.IsReady && _running.Count < Parallelism)
+ {
+ var item = _queue.NextReady(UtcNow);
+ if (item is null) break;
+ Begin(item);
+ }
+ Refresh();
+ }
+
+ public void Pause()
+ {
+ _queue.IsPaused = true;
+ CancelRunning();
+ Refresh();
+ }
+
+ public void Resume()
+ {
+ _queue.IsPaused = false;
+ Pump();
+ }
+
+ public void StopAndClear()
+ {
+ CancelRunning();
+ _queue.Clear();
+ _creating.Clear();
+ _meter.Reset();
+ _hadWork = false;
+ Refresh();
+ }
+
+ public void Retry(UploadItem item)
+ {
+ _queue.Requeue(item);
+ Pump();
+ }
+
+ public void Dispose()
+ {
+ _lifetime.Cancel();
+ CancelRunning();
+ _lifetime.Dispose();
+ }
+
+ private async Task TickAsync(CancellationToken ct)
+ {
+ using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1), _time);
+ try
+ {
+ do
+ {
+ Pump();
+ }
+ while (await timer.WaitForNextTickAsync(ct));
+ }
+ catch (OperationCanceledException)
+ {
+ }
+ }
+
+ private void Begin(UploadItem item)
+ {
+ if (item.State == UploadItemState.Failed)
+ {
+ item.Retries++;
+ item.NextRetryUtc = null;
+ item.Failure = UploadFailure.None;
+ item.FailureDetail = null;
+ }
+ // Leaving Queued/Failed synchronously keeps NextReady from handing the item out twice.
+ item.State = UploadItemState.Hashing;
+ var cts = CancellationTokenSource.CreateLinkedTokenSource(_lifetime.Token);
+ _running[item] = cts;
+ _hadWork = true;
+ _ = RunAsync(item, cts);
+ }
+
+ private async Task RunAsync(UploadItem item, CancellationTokenSource cts)
+ {
+ var ct = cts.Token;
+ try
+ {
+ await HashIfNeededAsync(item, ct);
+ if (item.State == UploadItemState.Failed) return;
+
+ item.State = UploadItemState.Uploading;
+ item.BytesSent = 0;
+ var batch = _queue.GetBatch(item.BatchId);
+ Guid? sequenceId = batch?.Mode switch
+ {
+ UploadTargetMode.Sequence => batch.SequenceId,
+ UploadTargetMode.NewSequence => await EnsureSequenceAsync(batch, ct),
+ _ => null,
+ };
+
+ var baseline = true;
+ var progress = new Progress(p =>
+ {
+ if (item.State != UploadItemState.Uploading) return;
+ // The first report of a resumed upload counts chunks the server already had: not throughput.
+ if (!baseline) _meter.Add(Math.Max(0, p.BytesSent - item.BytesSent), Now);
+ baseline = false;
+ item.BytesSent = p.BytesSent;
+ });
+ var (outcome, frameId) = await _transport.UploadAsync(item, sequenceId, item.Sha256!, progress, ct);
+
+ item.FrameId = frameId;
+ item.BytesSent = item.Size;
+ item.State = outcome == UploadOutcome.Verified ? UploadItemState.Verified : UploadItemState.Skipped;
+ _queue.OnChanged();
+ ItemConfirmed?.Invoke(this, item);
+ if (_deleteAfterUpload()) DeleteConfirmedSource(item);
+ }
+ catch (OperationCanceledException) when (ct.IsCancellationRequested)
+ {
+ Requeue(item);
+ }
+ catch (ServerUnavailableException ex)
+ {
+ Log.Info("upload", $"{item.FileName} waits for the server ({ex.Problem})");
+ Requeue(item);
+ }
+ catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException)
+ {
+ _queue.Fail(item, UploadFailure.FileMissing, null, UtcNow);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("upload", $"Cannot read {item.Path}", ex);
+ _queue.Fail(item, File.Exists(item.Path) ? UploadFailure.FileUnreadable : UploadFailure.FileMissing, null, UtcNow);
+ }
+ catch (TitanoApiException ex)
+ {
+ Log.Warning("upload", $"Server refused {item.Path}: {ex.Error.Code}", ex);
+ _queue.Fail(item, UploadFailure.ServerRejected, ex.Error.Message, UtcNow);
+ }
+ catch (Exception ex)
+ {
+ // Worker boundary: nothing may leave an item stuck in an active state.
+ Log.Error("upload", $"Upload of {item.Path} failed", ex);
+ _queue.Fail(item, UploadFailure.Unexpected, null, UtcNow);
+ }
+ finally
+ {
+ _running.Remove(item);
+ cts.Dispose();
+ Pump();
+ }
+ }
+
+ /// Hashes when the file is new or changed; a file that changes while being read fails for a later retry.
+ private async Task HashIfNeededAsync(UploadItem item, CancellationToken ct)
+ {
+ var file = new FileInfo(item.Path);
+ if (!file.Exists)
+ {
+ _queue.Fail(item, UploadFailure.FileMissing, null, UtcNow);
+ return;
+ }
+ if (file.Length != item.Size || file.LastWriteTimeUtc != item.ModifiedUtc)
+ {
+ item.Sha256 = null;
+ item.Size = file.Length;
+ item.ModifiedUtc = file.LastWriteTimeUtc;
+ }
+ if (item.Sha256 is not null) return;
+
+ var sha = await Task.Run(() => TitanoClient.HashFileAsync(item.Path, ct), ct);
+ file.Refresh();
+ if (!file.Exists || file.Length != item.Size || file.LastWriteTimeUtc != item.ModifiedUtc)
+ {
+ _queue.Fail(item, UploadFailure.FileChanged, null, UtcNow);
+ return;
+ }
+ item.Sha256 = sha;
+ _queue.OnChanged();
+ }
+
+ /// Creates the batch's sequence once, shared by concurrent items of the same batch.
+ private async Task EnsureSequenceAsync(UploadBatch batch, CancellationToken ct)
+ {
+ if (batch.SequenceId is { } known) return known;
+ if (!_creating.TryGetValue(batch.Id, out var creation))
+ {
+ creation = _transport.CreateSequenceAsync(batch.Title, _lifetime.Token);
+ _creating[batch.Id] = creation;
+ }
+ try
+ {
+ var id = await creation.WaitAsync(ct);
+ batch.SequenceId = id;
+ _queue.OnChanged();
+ return id;
+ }
+ catch when (creation.IsFaulted || creation.IsCanceled)
+ {
+ _creating.Remove(batch.Id);
+ throw;
+ }
+ }
+
+ ///
+ /// BRIEF §0.2 rule 8: deletes only a file the server confirmed by hash, and only when it is still exactly the
+ /// bytes that were hashed (same size and write time).
+ ///
+ private static void DeleteConfirmedSource(UploadItem item)
+ {
+ try
+ {
+ var file = new FileInfo(item.Path);
+ if (!file.Exists) return;
+ if (file.Length != item.Size || file.LastWriteTimeUtc != item.ModifiedUtc)
+ {
+ Log.Warning("upload", $"Kept {item.Path}: it changed after the server verified it");
+ return;
+ }
+ file.Delete();
+ Log.Info("upload", $"Deleted {item.Path} after the server confirmed SHA-256 {item.Sha256}");
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Log.Warning("upload", $"Could not delete {item.Path}", ex);
+ }
+ }
+
+ private void Requeue(UploadItem item)
+ {
+ if (!item.IsActive) return;
+ item.State = UploadItemState.Queued;
+ item.BytesSent = 0;
+ }
+
+ private void CancelRunning()
+ {
+ foreach (var cts in _running.Values.ToList()) cts.Cancel();
+ }
+
+ private void Refresh()
+ {
+ BytesPerSecond = _meter.BytesPerSecond(Now);
+ var pending = _queue.HasPending;
+ Status = _queue.IsPaused && pending ? EngineStatus.Paused
+ : pending && !_transport.IsReady ? EngineStatus.WaitingForServer
+ : pending || _running.Count > 0 ? EngineStatus.Running
+ : EngineStatus.Idle;
+ if (_hadWork && !pending && _running.Count == 0)
+ {
+ _hadWork = false;
+ _meter.Reset();
+ Drained?.Invoke(this, EventArgs.Empty);
+ }
+ }
+}
diff --git a/src/Titano.Uploader/Uploads/UploadItem.cs b/src/Titano.Uploader/Uploads/UploadItem.cs
new file mode 100644
index 00000000..39c7cc12
--- /dev/null
+++ b/src/Titano.Uploader/Uploads/UploadItem.cs
@@ -0,0 +1,139 @@
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+
+namespace Titano.Uploader.Uploads;
+
+/// Per-file state in the upload queue (BRIEF §15).
+public enum UploadItemState
+{
+ Queued,
+ Hashing,
+ Uploading,
+
+ /// The server committed the file after checking its SHA-256.
+ Verified,
+
+ /// The server already held a file with the same SHA-256.
+ Skipped,
+
+ Failed,
+}
+
+/// Why an item failed; the UI turns it into one localized sentence.
+public enum UploadFailure
+{
+ None,
+ FileMissing,
+ FileUnreadable,
+
+ /// The file changed while it was being hashed (still being written).
+ FileChanged,
+
+ ServerRejected,
+ Unexpected,
+}
+
+/// Where a batch of files goes on the server.
+public enum UploadTargetMode
+{
+ /// The server groups files by into auto-created sequences.
+ Sessions,
+
+ /// Every file goes to one existing sequence.
+ Sequence,
+
+ /// One new sequence is created for the batch with the given title.
+ NewSequence,
+}
+
+/// Files added together share a target; a new sequence is created once per batch, on first upload.
+public sealed class UploadBatch
+{
+ public required Guid Id { get; init; }
+ public required UploadTargetMode Mode { get; init; }
+
+ /// Target sequence; for it is filled once the server created it.
+ public Guid? SequenceId { get; set; }
+
+ public string? Title { get; init; }
+}
+
+/// One file in the queue. Mutated only on the queue's owning (UI) thread.
+public sealed class UploadItem : INotifyPropertyChanged
+{
+ private UploadItemState _state;
+ private UploadFailure _failure;
+ private string? _failureDetail;
+ private int _retries;
+ private long _bytesSent;
+
+ public required Guid Id { get; init; }
+ public required Guid BatchId { get; init; }
+ public required string Path { get; init; }
+
+ /// Size and write time when the file was last hashed or enqueued; a change forces a new hash.
+ public required long Size { get; set; }
+
+ public required DateTime ModifiedUtc { get; set; }
+
+ /// Camera wall-clock capture time (unspecified kind).
+ public DateTime? CapturedAt { get; init; }
+
+ public string? SessionKey { get; init; }
+
+ /// Watch folder the file came from; null for files added by hand.
+ public string? WatchRoot { get; init; }
+
+ public string? Sha256 { get; set; }
+ public Guid? FrameId { get; set; }
+
+ /// Earliest time a failed item may be retried by the queue.
+ public DateTime? NextRetryUtc { get; set; }
+
+ public string FileName => System.IO.Path.GetFileName(Path);
+
+ public UploadItemState State
+ {
+ get => _state;
+ set => Set(ref _state, value);
+ }
+
+ public UploadFailure Failure
+ {
+ get => _failure;
+ set => Set(ref _failure, value);
+ }
+
+ /// Server message for , otherwise null.
+ public string? FailureDetail
+ {
+ get => _failureDetail;
+ set => Set(ref _failureDetail, value);
+ }
+
+ /// Queue-level retries already spent on this item.
+ public int Retries
+ {
+ get => _retries;
+ set => Set(ref _retries, value);
+ }
+
+ /// Bytes sent in the current attempt (progress only; not persisted).
+ public long BytesSent
+ {
+ get => _bytesSent;
+ set => Set(ref _bytesSent, value);
+ }
+
+ public bool IsDone => _state is UploadItemState.Verified or UploadItemState.Skipped;
+ public bool IsActive => _state is UploadItemState.Hashing or UploadItemState.Uploading;
+
+ public event PropertyChangedEventHandler? PropertyChanged;
+
+ private void Set(ref T field, T value, [CallerMemberName] string? name = null)
+ {
+ if (EqualityComparer.Default.Equals(field, value)) return;
+ field = value;
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
+ }
+}
diff --git a/src/Titano.Uploader/Uploads/UploadQueue.cs b/src/Titano.Uploader/Uploads/UploadQueue.cs
new file mode 100644
index 00000000..ffb08ad4
--- /dev/null
+++ b/src/Titano.Uploader/Uploads/UploadQueue.cs
@@ -0,0 +1,167 @@
+using System.Collections.Concurrent;
+using System.Collections.ObjectModel;
+
+namespace Titano.Uploader.Uploads;
+
+/// A file about to be enqueued.
+public sealed record QueueEntry(string Path, long Size, DateTime ModifiedUtc, DateTime? CapturedAt, string? SessionKey, string? WatchRoot);
+
+///
+/// Ordered upload queue with its batches. Owned by the UI thread: every mutation happens there, so bindings can
+/// observe directly. is the only member safe from other threads.
+///
+public sealed class UploadQueue
+{
+ /// Queue-level retries per item after the client's own transient retries (BRIEF §15).
+ public const int MaxRetries = 3;
+
+ /// Delay before a failed item is tried again.
+ public static readonly TimeSpan RetryDelay = TimeSpan.FromSeconds(30);
+
+ private readonly Dictionary _batches = [];
+ private readonly ConcurrentDictionary _paths = new(StringComparer.OrdinalIgnoreCase);
+ private bool _isPaused;
+
+ public ObservableCollection Items { get; } = [];
+
+ public IReadOnlyCollection Batches => _batches.Values;
+
+ /// True while the user has paused the queue; persisted so a restart keeps it paused.
+ public bool IsPaused
+ {
+ get => _isPaused;
+ set
+ {
+ if (_isPaused == value) return;
+ _isPaused = value;
+ OnChanged();
+ }
+ }
+
+ /// Raised after anything that must be persisted changed.
+ public event EventHandler? Changed;
+
+ /// True while an item for is queued (any state). Thread-safe.
+ public bool ContainsPath(string path) => _paths.ContainsKey(path);
+
+ /// True while some item still waits for, or is in, an upload attempt (retryable failures included).
+ public bool HasPending => Items.Any(IsPending);
+
+ /// Appends files not already queued, in the given order, under one batch; returns how many were added.
+ public int Add(UploadBatch batch, IEnumerable entries)
+ {
+ var added = 0;
+ foreach (var e in entries)
+ {
+ if (!_paths.TryAdd(e.Path, 0)) continue;
+ Items.Add(new UploadItem
+ {
+ Id = Guid.CreateVersion7(),
+ BatchId = batch.Id,
+ Path = e.Path,
+ Size = e.Size,
+ ModifiedUtc = e.ModifiedUtc,
+ CapturedAt = e.CapturedAt,
+ SessionKey = e.SessionKey,
+ WatchRoot = e.WatchRoot,
+ });
+ added++;
+ }
+ if (added > 0)
+ {
+ _batches[batch.Id] = batch;
+ OnChanged();
+ }
+ return added;
+ }
+
+ public UploadBatch? GetBatch(Guid id) => _batches.GetValueOrDefault(id);
+
+ ///
+ /// The next item to work on: the first queued one, otherwise the first failed one whose retry time has come.
+ ///
+ public UploadItem? NextReady(DateTime utcNow)
+ {
+ UploadItem? retry = null;
+ foreach (var item in Items)
+ {
+ if (item.State == UploadItemState.Queued) return item;
+ if (retry is null && IsRetryDue(item, utcNow)) retry = item;
+ }
+ return retry;
+ }
+
+ /// Records a failure and schedules the queue-level retry when retries are left.
+ public void Fail(UploadItem item, UploadFailure failure, string? detail, DateTime utcNow)
+ {
+ item.State = UploadItemState.Failed;
+ item.Failure = failure;
+ item.FailureDetail = detail;
+ item.BytesSent = 0;
+ item.NextRetryUtc = item.Retries < MaxRetries ? utcNow + RetryDelay : null;
+ OnChanged();
+ }
+
+ /// Puts a failed item back in line now, with a fresh retry budget (user action).
+ public void Requeue(UploadItem item)
+ {
+ if (item.State != UploadItemState.Failed) return;
+ item.State = UploadItemState.Queued;
+ item.Failure = UploadFailure.None;
+ item.FailureDetail = null;
+ item.Retries = 0;
+ item.NextRetryUtc = null;
+ OnChanged();
+ }
+
+ /// Drops finished items (verified or skipped) so a new batch starts a fresh count.
+ public void RemoveCompleted()
+ {
+ var removed = false;
+ for (var i = Items.Count - 1; i >= 0; i--)
+ {
+ if (!Items[i].IsDone) continue;
+ _paths.TryRemove(Items[i].Path, out _);
+ Items.RemoveAt(i);
+ removed = true;
+ }
+ if (!removed) return;
+ var used = Items.Select(i => i.BatchId).ToHashSet();
+ foreach (var id in _batches.Keys.Where(id => !used.Contains(id)).ToList()) _batches.Remove(id);
+ OnChanged();
+ }
+
+ public void Clear()
+ {
+ Items.Clear();
+ _paths.Clear();
+ _batches.Clear();
+ OnChanged();
+ }
+
+ /// Signals a persisted-state change made directly on an item.
+ public void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
+
+ /// Restores a persisted queue; interrupted attempts go back to .
+ public void Restore(IEnumerable batches, IEnumerable items, bool paused)
+ {
+ Items.Clear();
+ _paths.Clear();
+ _batches.Clear();
+ foreach (var b in batches) _batches[b.Id] = b;
+ foreach (var item in items)
+ {
+ if (!_batches.ContainsKey(item.BatchId) || !_paths.TryAdd(item.Path, 0)) continue;
+ if (item.IsActive) item.State = UploadItemState.Queued;
+ Items.Add(item);
+ }
+ _isPaused = paused;
+ }
+
+ private static bool IsPending(UploadItem item) =>
+ item.State is UploadItemState.Queued or UploadItemState.Hashing or UploadItemState.Uploading
+ || (item.State == UploadItemState.Failed && item.NextRetryUtc is not null);
+
+ private static bool IsRetryDue(UploadItem item, DateTime utcNow) =>
+ item.State == UploadItemState.Failed && item.NextRetryUtc is { } due && due <= utcNow;
+}
diff --git a/src/Titano.Uploader/Watching/ProcessedFileMemory.cs b/src/Titano.Uploader/Watching/ProcessedFileMemory.cs
new file mode 100644
index 00000000..9669fbd4
--- /dev/null
+++ b/src/Titano.Uploader/Watching/ProcessedFileMemory.cs
@@ -0,0 +1,165 @@
+using System.IO;
+using System.Text.Json.Serialization;
+using System.Text.Json;
+using Titano.Core.Diagnostics;
+using Titano.Uploader.Sources;
+
+namespace Titano.Uploader.Watching;
+
+///
+/// Files of each watch folder the server already confirmed, keyed by path + size + write time, so a poll never
+/// uploads them again; plus the last session of each folder so new frames keep joining it. Thread-safe.
+/// Persisted in uploader-watch.json.
+///
+public sealed class ProcessedFileMemory
+{
+ public const int Version = 1;
+
+ private readonly Lock _gate = new();
+ private readonly Dictionary _folders = new(StringComparer.OrdinalIgnoreCase);
+ private bool _dirty;
+
+ /// True when something changed since the last .
+ public bool IsDirty
+ {
+ get
+ {
+ lock (_gate) return _dirty;
+ }
+ }
+
+ public bool Contains(string root, string path, long size, DateTime modifiedUtc)
+ {
+ lock (_gate)
+ {
+ return _folders.TryGetValue(root, out var f)
+ && f.Files.TryGetValue(Relative(root, path), out var known)
+ && known.Size == size && known.ModifiedTicks == modifiedUtc.Ticks;
+ }
+ }
+
+ public void Add(string root, string path, long size, DateTime modifiedUtc)
+ {
+ lock (_gate)
+ {
+ GetOrAdd(root).Files[Relative(root, path)] = new Entry(size, modifiedUtc.Ticks);
+ _dirty = true;
+ }
+ }
+
+ /// Forgets files of that a complete scan no longer found.
+ public void Prune(string root, IEnumerable presentPaths)
+ {
+ lock (_gate)
+ {
+ if (!_folders.TryGetValue(root, out var f) || f.Files.Count == 0) return;
+ var present = new HashSet(presentPaths.Select(p => Relative(root, p)), StringComparer.OrdinalIgnoreCase);
+ foreach (var key in f.Files.Keys.Where(k => !present.Contains(k)).ToList())
+ {
+ f.Files.Remove(key);
+ _dirty = true;
+ }
+ }
+ }
+
+ public void RemoveFolder(string root)
+ {
+ lock (_gate)
+ {
+ if (_folders.Remove(root)) _dirty = true;
+ }
+ }
+
+ public SessionContinuation? GetSession(string root)
+ {
+ lock (_gate) return _folders.TryGetValue(root, out var f) ? f.Session : null;
+ }
+
+ public void SetSession(string root, SessionContinuation? session)
+ {
+ lock (_gate)
+ {
+ GetOrAdd(root).Session = session;
+ _dirty = true;
+ }
+ }
+
+ public byte[] Serialize()
+ {
+ lock (_gate)
+ {
+ var file = new MemoryFile
+ {
+ Version = Version,
+ Folders =
+ [
+ .. _folders.Select(kv => new FolderRecord(
+ kv.Key,
+ kv.Value.Session?.Key,
+ kv.Value.Session?.LastCapture,
+ [.. kv.Value.Files.Select(f => new FileRecord(f.Key, f.Value.Size, f.Value.ModifiedTicks))])),
+ ],
+ };
+ _dirty = false;
+ return JsonSerializer.SerializeToUtf8Bytes(file, WatchJson.Default.MemoryFile);
+ }
+ }
+
+ /// Loads ; a missing or corrupt file gives an empty memory.
+ public static ProcessedFileMemory Load(string path)
+ {
+ var memory = new ProcessedFileMemory();
+ MemoryFile? file = null;
+ try
+ {
+ if (File.Exists(path)) file = JsonSerializer.Deserialize(File.ReadAllBytes(path), WatchJson.Default.MemoryFile);
+ }
+ catch (JsonException ex)
+ {
+ Log.Warning("watch", "Watch memory unreadable; starting empty", ex);
+ }
+ catch (IOException ex)
+ {
+ Log.Warning("watch", "Watch memory unreadable; starting empty", ex);
+ }
+ if (file is null || file.Version != Version) return memory;
+ foreach (var record in file.Folders)
+ {
+ var folder = memory.GetOrAdd(record.Root);
+ if (record.SessionKey is not null && record.LastCapture is { } last) folder.Session = new SessionContinuation(record.SessionKey, last);
+ foreach (var f in record.Files) folder.Files[f.Path] = new Entry(f.Size, f.Modified);
+ }
+ return memory;
+ }
+
+ private Folder GetOrAdd(string root)
+ {
+ if (!_folders.TryGetValue(root, out var f)) _folders[root] = f = new Folder();
+ return f;
+ }
+
+ /// Paths are stored relative to the folder to keep the file small.
+ private static string Relative(string root, string path) => Path.GetRelativePath(root, path);
+
+ private readonly record struct Entry(long Size, long ModifiedTicks);
+
+ private sealed class Folder
+ {
+ public Dictionary Files { get; } = new(StringComparer.OrdinalIgnoreCase);
+ public SessionContinuation? Session { get; set; }
+ }
+
+ internal sealed class MemoryFile
+ {
+ public int Version { get; set; }
+ public List Folders { get; set; } = [];
+ }
+
+ internal sealed record FolderRecord(string Root, string? SessionKey, DateTime? LastCapture, List Files);
+
+ internal sealed record FileRecord(string Path, long Size, long Modified);
+}
+
+[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
+[JsonSerializable(typeof(ProcessedFileMemory.MemoryFile))]
+internal sealed partial class WatchJson : JsonSerializerContext;
diff --git a/src/Titano.Uploader/Watching/StableFileTracker.cs b/src/Titano.Uploader/Watching/StableFileTracker.cs
new file mode 100644
index 00000000..4758cbb8
--- /dev/null
+++ b/src/Titano.Uploader/Watching/StableFileTracker.cs
@@ -0,0 +1,47 @@
+namespace Titano.Uploader.Watching;
+
+/// Size and last-write time of a file at one check.
+public readonly record struct FileObservation(string Path, long Size, DateTime ModifiedUtc);
+
+///
+/// Decides when a file in a watch folder has stopped growing: it is reported once its size and write time were
+/// identical in consecutive checks, then forgotten (the caller enqueues it).
+///
+public sealed class StableFileTracker
+{
+ public const int RequiredChecks = 2;
+
+ private readonly Dictionary _seen = new(StringComparer.OrdinalIgnoreCase);
+
+ /// Files seen but not yet stable.
+ public int PendingCount => _seen.Count;
+
+ ///
+ /// Records a complete scan: returns the files that are now stable and forgets files missing from the scan.
+ ///
+ public IReadOnlyList Check(IEnumerable scan)
+ {
+ var present = new HashSet(StringComparer.OrdinalIgnoreCase);
+ var stable = new List();
+ foreach (var o in scan)
+ {
+ present.Add(o.Path);
+ if (Observe(o)) stable.Add(o);
+ }
+ foreach (var path in _seen.Keys.Where(p => !present.Contains(p)).ToList()) _seen.Remove(path);
+ return stable;
+ }
+
+ /// Records one observation; true when the file just became stable.
+ public bool Observe(FileObservation o)
+ {
+ var count = _seen.TryGetValue(o.Path, out var last) && last.Size == o.Size && last.Modified == o.ModifiedUtc ? last.Count + 1 : 1;
+ if (count >= RequiredChecks)
+ {
+ _seen.Remove(o.Path);
+ return true;
+ }
+ _seen[o.Path] = (o.Size, o.ModifiedUtc, count);
+ return false;
+ }
+}
diff --git a/src/Titano.Uploader/Watching/WatchFolderService.cs b/src/Titano.Uploader/Watching/WatchFolderService.cs
new file mode 100644
index 00000000..779a3612
--- /dev/null
+++ b/src/Titano.Uploader/Watching/WatchFolderService.cs
@@ -0,0 +1,188 @@
+using System.IO;
+using Titano.Core.Diagnostics;
+using Titano.Uploader.Sources;
+
+namespace Titano.Uploader.Watching;
+
+///
+/// Polls each watch folder at its interval, with hints on local disks (change
+/// notifications are unreliable on network shares, which are only polled). A photo is reported once it is stable
+/// (), not yet confirmed by the server () and not queued.
+///
+public sealed class WatchFolderService(ProcessedFileMemory memory, Func isQueued) : IDisposable
+{
+ /// Re-check cadence while some file is still growing.
+ private static readonly TimeSpan QuickCheck = TimeSpan.FromSeconds(10);
+
+ /// Lets a burst of change notifications settle before scanning.
+ private static readonly TimeSpan HintDebounce = TimeSpan.FromSeconds(2);
+
+ private readonly Dictionary _folders = new(StringComparer.OrdinalIgnoreCase);
+ private readonly ProcessedFileMemory _memory = memory;
+ private readonly Func _isQueued = isQueued;
+
+ /// Stable new photos of a folder; raised on the thread that called .
+ public event Action>? FilesReady;
+
+ /// A folder became reachable (true) or unreachable (false); raised like .
+ public event Action? AvailabilityChanged;
+
+ /// UNC paths and mapped network drives.
+ public static bool IsNetworkPath(string path)
+ {
+ if (path.StartsWith(@"\\", StringComparison.Ordinal)) return true;
+ try
+ {
+ var root = Path.GetPathRoot(Path.GetFullPath(path));
+ return !string.IsNullOrEmpty(root) && new DriveInfo(root).DriveType == DriveType.Network;
+ }
+ catch (Exception ex) when (ex is ArgumentException or IOException)
+ {
+ return false;
+ }
+ }
+
+ /// 60 s on local disks; 5 min on network shares, where every poll lists the share.
+ public static TimeSpan DefaultInterval(string path) => IsNetworkPath(path) ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(60);
+
+ /// Starts (or restarts with a new interval) watching .
+ public void Watch(string root, TimeSpan interval)
+ {
+ Stop(root);
+ var folder = new Folder(root, interval, IsNetworkPath(root), SynchronizationContext.Current, this);
+ _folders[root] = folder;
+ folder.Start();
+ }
+
+ public void Stop(string root)
+ {
+ if (_folders.Remove(root, out var folder)) folder.Dispose();
+ }
+
+ public void Dispose()
+ {
+ foreach (var f in _folders.Values) f.Dispose();
+ _folders.Clear();
+ }
+
+ private void Raise(SynchronizationContext? context, Action action)
+ {
+ if (context is null) action();
+ else context.Post(_ => action(), null);
+ }
+
+ private sealed class Folder(string root, TimeSpan interval, bool network, SynchronizationContext? context, WatchFolderService owner) : IDisposable
+ {
+ private readonly StableFileTracker _tracker = new();
+ private readonly CancellationTokenSource _cts = new();
+ private FileSystemWatcher? _watcher;
+ private TaskCompletionSource _hint = NewHint();
+ private bool? _available;
+
+ public void Start() => _ = Task.Run(() => LoopAsync(_cts.Token));
+
+ public void Dispose()
+ {
+ _cts.Cancel();
+ _watcher?.Dispose();
+ _watcher = null;
+ }
+
+ private async Task LoopAsync(CancellationToken ct)
+ {
+ while (!ct.IsCancellationRequested)
+ {
+ // Replaced before the scan so notifications arriving during it trigger the next one.
+ Volatile.Write(ref _hint, NewHint());
+ var available = Scan(ct);
+ if (available != _available)
+ {
+ _available = available;
+ if (available && !network) StartWatcher();
+ owner.Raise(context, () => owner.AvailabilityChanged?.Invoke(root, available));
+ }
+ var wait = _tracker.PendingCount > 0 && QuickCheck < interval ? QuickCheck : interval;
+ try
+ {
+ var hint = Volatile.Read(ref _hint).Task;
+ if (await Task.WhenAny(Task.Delay(wait, ct), hint).ConfigureAwait(false) == hint)
+ await Task.Delay(HintDebounce, ct).ConfigureAwait(false);
+ }
+ catch (OperationCanceledException)
+ {
+ return;
+ }
+ }
+ }
+
+ /// One complete listing; false when the folder cannot be reached.
+ private bool Scan(CancellationToken ct)
+ {
+ if (!Directory.Exists(root)) return false;
+ var present = new List();
+ var candidates = new Dictionary(StringComparer.OrdinalIgnoreCase);
+ try
+ {
+ foreach (var file in PhotoScanner.Files(root, ct))
+ {
+ present.Add(file.FullName);
+ if (IsKnown(file)) continue;
+ candidates[file.FullName] = file;
+ }
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ // A partial listing must not prune the memory or report files.
+ Log.Info("watch", $"{root} not readable: {ex.Message}");
+ return false;
+ }
+ catch (OperationCanceledException)
+ {
+ return _available ?? false;
+ }
+
+ owner._memory.Prune(root, present);
+ var stable = _tracker.Check(candidates.Values.Select(f => new FileObservation(f.FullName, f.Length, f.LastWriteTimeUtc)));
+ if (stable.Count > 0)
+ {
+ var files = stable.Select(o => PhotoScanner.ToSource(candidates[o.Path], root)).ToList();
+ Log.Info("watch", $"{files.Count} new file(s) in {root}");
+ owner.Raise(context, () => owner.FilesReady?.Invoke(root, files));
+ }
+ return true;
+ }
+
+ /// Already confirmed by the server, or already in the queue.
+ private bool IsKnown(FileInfo file) =>
+ owner._memory.Contains(root, file.FullName, file.Length, file.LastWriteTimeUtc) || owner._isQueued(file.FullName);
+
+ private void StartWatcher()
+ {
+ _watcher?.Dispose();
+ try
+ {
+ _watcher = new FileSystemWatcher(root)
+ {
+ IncludeSubdirectories = true,
+ NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.LastWrite,
+ };
+ _watcher.Created += (_, _) => Hint();
+ _watcher.Changed += (_, _) => Hint();
+ _watcher.Renamed += (_, _) => Hint();
+ // A lost buffer means unknown changes: scan.
+ _watcher.Error += (_, _) => Hint();
+ _watcher.EnableRaisingEvents = true;
+ }
+ catch (Exception ex) when (ex is ArgumentException or IOException or PlatformNotSupportedException)
+ {
+ Log.Info("watch", $"No change notifications for {root}; polling only: {ex.Message}");
+ _watcher?.Dispose();
+ _watcher = null;
+ }
+ }
+
+ private void Hint() => Volatile.Read(ref _hint).TrySetResult();
+
+ private static TaskCompletionSource NewHint() => new(TaskCreationOptions.RunContinuationsAsynchronously);
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/CoalescingWriterTests.cs b/tests/Titano.App.Tests/Uploader/CoalescingWriterTests.cs
new file mode 100644
index 00000000..cf616673
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/CoalescingWriterTests.cs
@@ -0,0 +1,18 @@
+using System.IO;
+using Titano.Testing;
+using Titano.Uploader.Infrastructure;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class CoalescingWriterTests(TestContext context)
+{
+ [Test]
+ public void KeepsTheNewestContent()
+ {
+ var path = Path.Combine(context.CreateTempDirectory(), "uploader-queue.json");
+ var writer = new CoalescingWriter();
+ for (var i = 0; i < 50; i++) writer.Write(path, [(byte)i]);
+ writer.Flush(TimeSpan.FromSeconds(10));
+ Assert.SequenceEqual([(byte)49], File.ReadAllBytes(path));
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/CommandLineTests.cs b/tests/Titano.App.Tests/Uploader/CommandLineTests.cs
new file mode 100644
index 00000000..52d381de
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/CommandLineTests.cs
@@ -0,0 +1,33 @@
+using Titano.Testing;
+using Titano.Uploader.Infrastructure;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class CommandLineTests
+{
+ [Test]
+ public void PathsAndOptionsAreSeparated()
+ {
+ var c = CommandLine.Parse([@"E:\", "--tray", @"D:\Cam\IMG_0001.CR2"]);
+ Assert.SequenceEqual([@"E:\", @"D:\Cam\IMG_0001.CR2"], c.Paths);
+ Assert.True(c.StartInTray);
+ Assert.Null(c.CaptureDirectory);
+ }
+
+ [Test]
+ public void CaptureTakesAnOptionalDirectory()
+ {
+ Assert.Equal("out/shots", CommandLine.Parse(["--capture", "out/shots"]).CaptureDirectory);
+ Assert.Equal("screenshots", CommandLine.Parse(["--capture"]).CaptureDirectory);
+ Assert.Equal("screenshots", CommandLine.Parse(["--capture", "--tray"]).CaptureDirectory);
+ }
+
+ [Test]
+ public void HandOverMessageRoundTrips()
+ {
+ string[] args = [@"E:\", @"\\garage-pi\cam\IMG 1.jpg"];
+ Assert.SequenceEqual(args, SingleInstance.Decode(SingleInstance.Encode(args))!);
+ Assert.Empty(SingleInstance.Decode(SingleInstance.Encode([]))!);
+ Assert.Null(SingleInstance.Decode("hello\nE:\\"u8));
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/ProcessedFileMemoryTests.cs b/tests/Titano.App.Tests/Uploader/ProcessedFileMemoryTests.cs
new file mode 100644
index 00000000..203c771a
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/ProcessedFileMemoryTests.cs
@@ -0,0 +1,83 @@
+using System.IO;
+using Titano.Testing;
+using Titano.Uploader.Sources;
+using Titano.Uploader.Watching;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class ProcessedFileMemoryTests(TestContext context)
+{
+ private static readonly DateTime Written = new(2026, 9, 28, 16, 2, 11, DateTimeKind.Utc);
+
+ [Test]
+ public void RemembersPathSizeAndWriteTime()
+ {
+ var root = Path.Combine(Path.GetTempPath(), "roof");
+ var photo = Path.Combine(root, "100CANON", "IMG_0413.CR2");
+ var memory = new ProcessedFileMemory();
+ Assert.False(memory.Contains(root, photo, 25_000_000, Written));
+
+ memory.Add(root, photo, 25_000_000, Written);
+ Assert.True(memory.IsDirty);
+ Assert.True(memory.Contains(root, photo, 25_000_000, Written));
+ Assert.False(memory.Contains(root, photo, 25_000_001, Written), "a resized file is new");
+ Assert.False(memory.Contains(root, photo, 25_000_000, Written.AddSeconds(1)), "a rewritten file is new");
+ Assert.False(memory.Contains(Path.Combine(Path.GetTempPath(), "garden"), photo, 25_000_000, Written), "folders are separate");
+ }
+
+ [Test]
+ public void PruneForgetsFilesNoLongerInTheFolder()
+ {
+ var root = Path.Combine(Path.GetTempPath(), "roof");
+ var kept = Path.Combine(root, "a.jpg");
+ var gone = Path.Combine(root, "b.jpg");
+ var memory = new ProcessedFileMemory();
+ memory.Add(root, kept, 1, Written);
+ memory.Add(root, gone, 2, Written);
+
+ memory.Prune(root, [kept]);
+ Assert.True(memory.Contains(root, kept, 1, Written));
+ Assert.False(memory.Contains(root, gone, 2, Written));
+ }
+
+ [Test]
+ public void RoundTripKeepsFilesAndTheLastSession()
+ {
+ var dir = context.CreateTempDirectory();
+ var root = Path.Combine(dir, "cam");
+ var photo = Path.Combine(root, "IMG_0001.JPG");
+ var memory = new ProcessedFileMemory();
+ memory.Add(root, photo, 4_200_000, Written);
+ memory.SetSession(root, new SessionContinuation("1a2b3c4d-20260928-180000", new DateTime(2026, 9, 28, 18, 40, 0)));
+
+ var path = Path.Combine(dir, "uploader-watch.json");
+ File.WriteAllBytes(path, memory.Serialize());
+ Assert.False(memory.IsDirty);
+ var restored = ProcessedFileMemory.Load(path);
+
+ Assert.True(restored.Contains(root, photo, 4_200_000, Written));
+ Assert.Equal(new SessionContinuation("1a2b3c4d-20260928-180000", new DateTime(2026, 9, 28, 18, 40, 0)), restored.GetSession(root));
+ Assert.False(restored.IsDirty);
+ }
+
+ [Test]
+ public void RemovedFolderIsForgotten()
+ {
+ var root = Path.Combine(Path.GetTempPath(), "roof");
+ var memory = new ProcessedFileMemory();
+ memory.Add(root, Path.Combine(root, "a.jpg"), 1, Written);
+ memory.SetSession(root, new SessionContinuation("k", Written));
+ memory.RemoveFolder(root);
+ Assert.False(memory.Contains(root, Path.Combine(root, "a.jpg"), 1, Written));
+ Assert.Null(memory.GetSession(root));
+ }
+
+ [Test]
+ public void CorruptFileGivesAnEmptyMemory()
+ {
+ var path = Path.Combine(context.CreateTempDirectory(), "uploader-watch.json");
+ File.WriteAllText(path, "[1,2");
+ var memory = ProcessedFileMemory.Load(path);
+ Assert.False(memory.Contains("x", "x/a.jpg", 1, Written));
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/ServerAddressTests.cs b/tests/Titano.App.Tests/Uploader/ServerAddressTests.cs
new file mode 100644
index 00000000..1d28c92c
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/ServerAddressTests.cs
@@ -0,0 +1,25 @@
+using Titano.Testing;
+using Titano.Uploader.Server;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class ServerAddressTests
+{
+ [Theory]
+ [Case("nas.local:8443", "https://nas.local:8443/")]
+ [Case("https://nas.local:8443", "https://nas.local:8443/")]
+ [Case(" http://192.168.1.20:8080/titano ", "http://192.168.1.20:8080/titano/")]
+ [Case("https://nas.local/?x=1#y", "https://nas.local/")]
+ public void ServerAddressesBecomeApiBases(string typed, string expected)
+ {
+ Assert.True(ServerAddress.TryParse(typed, out var uri));
+ Assert.Equal(expected, uri.ToString());
+ }
+
+ [Theory]
+ [Case("")]
+ [Case(" ")]
+ [Case("ftp://nas.local")]
+ [Case("https://")]
+ public void InvalidServerAddressesAreRejected(string typed) => Assert.False(ServerAddress.TryParse(typed, out _));
+}
diff --git a/tests/Titano.App.Tests/Uploader/SessionGrouperTests.cs b/tests/Titano.App.Tests/Uploader/SessionGrouperTests.cs
new file mode 100644
index 00000000..2335a9d2
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/SessionGrouperTests.cs
@@ -0,0 +1,99 @@
+using System.IO;
+using Titano.Testing;
+using Titano.Uploader.Sources;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class SessionGrouperTests
+{
+ private static readonly DateTime Evening = new(2026, 9, 28, 18, 0, 0);
+
+ [Test]
+ public void SplitsWhereTheGapExceedsFortyFiveMinutes()
+ {
+ var times = Series(Evening, 10).Concat(Series(Evening.AddMinutes(9 + 46), 10)).ToList();
+ var result = SessionGrouper.Assign(times, "abcd1234");
+ Assert.Equal(2, result.Keys.Distinct().Count());
+ Assert.Equal("abcd1234-20260928-180000", result.Keys[0]);
+ Assert.Equal("abcd1234-20260928-185500", result.Keys[^1]);
+ }
+
+ [Test]
+ public void GapOfExactlyFortyFiveMinutesStaysInOneSession()
+ {
+ var times = Series(Evening, 10).Concat(Series(Evening.AddMinutes(9 + 45), 10)).ToList();
+ Assert.Equal(1, SessionGrouper.Assign(times, "t").Keys.Distinct().Count());
+ }
+
+ [Test]
+ public void TestShotsAfterASessionMergeIntoIt()
+ {
+ var times = Series(Evening, 20).Concat(Series(Evening.AddHours(3), 3)).ToList();
+ var result = SessionGrouper.Assign(times, "t");
+ Assert.Equal(1, result.Keys.Distinct().Count());
+ Assert.Equal(Evening.AddHours(3).AddMinutes(2), result.Last!.LastCapture);
+ }
+
+ [Test]
+ public void LeadingTestShotsJoinTheFollowingSession()
+ {
+ var times = Series(Evening, 3).Concat(Series(Evening.AddHours(2), 30)).ToList();
+ var result = SessionGrouper.Assign(times, "t");
+ Assert.Equal(1, result.Keys.Distinct().Count());
+ Assert.Equal("t-20260928-180000", result.Keys[0]);
+ }
+
+ [Test]
+ public void KeysFollowTheInputOrder()
+ {
+ var first = Series(Evening, 10);
+ var second = Series(Evening.AddHours(5), 10);
+ var shuffled = new List { second[3], first[7], second[0], first[0] };
+ shuffled.AddRange(first.Skip(1).Take(6));
+ shuffled.AddRange(second.Skip(4));
+ var result = SessionGrouper.Assign(shuffled, "t");
+ Assert.Equal("t-20260928-230000", result.Keys[0]);
+ Assert.Equal("t-20260928-180000", result.Keys[1]);
+ Assert.Equal(result.Keys[0], result.Keys[2]);
+ Assert.Equal(result.Keys[1], result.Keys[3]);
+ }
+
+ [Test]
+ public void NewFramesWithinTheGapContinueThePreviousSession()
+ {
+ var previous = new SessionContinuation("t-20260928-180000", Evening.AddMinutes(30));
+ var result = SessionGrouper.Assign(Series(Evening.AddMinutes(40), 2), "t", previous);
+ Assert.SequenceEqual(["t-20260928-180000", "t-20260928-180000"], result.Keys);
+ Assert.Equal(Evening.AddMinutes(41), result.Last!.LastCapture);
+ }
+
+ [Test]
+ public void NewFramesAfterTheGapStartANewSession()
+ {
+ var previous = new SessionContinuation("t-20260928-180000", Evening.AddMinutes(30));
+ var result = SessionGrouper.Assign(Series(Evening.AddHours(2), 10), "t", previous);
+ Assert.Equal("t-20260928-200000", result.Keys[0]);
+ Assert.Equal("t-20260928-200000", result.Last!.Key);
+ }
+
+ [Test]
+ public void EmptyInputKeepsThePreviousSession()
+ {
+ var previous = new SessionContinuation("k", Evening);
+ var result = SessionGrouper.Assign([], "t", previous);
+ Assert.Empty(result.Keys);
+ Assert.Same(previous, result.Last);
+ }
+
+ [Test]
+ public void SourceTagIsStableShortAndPerFolder()
+ {
+ var a = SessionGrouper.SourceTag(Path.Combine(Path.GetTempPath(), "cam", "roof"));
+ Assert.Equal(8, a.Length);
+ Assert.True(a.All(Uri.IsHexDigit));
+ Assert.Equal(a, SessionGrouper.SourceTag(Path.Combine(Path.GetTempPath(), "cam", "roof") + Path.DirectorySeparatorChar));
+ Assert.NotEqual(a, SessionGrouper.SourceTag(Path.Combine(Path.GetTempPath(), "cam", "garden")));
+ }
+
+ private static List Series(DateTime start, int count) => [.. Enumerable.Range(0, count).Select(i => start.AddMinutes(i))];
+}
diff --git a/tests/Titano.App.Tests/Uploader/SingleThreadedContext.cs b/tests/Titano.App.Tests/Uploader/SingleThreadedContext.cs
new file mode 100644
index 00000000..c1d1c46c
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/SingleThreadedContext.cs
@@ -0,0 +1,41 @@
+using System.Collections.Concurrent;
+
+namespace Titano.App.Tests.Uploader;
+
+/// Runs async test code on one thread, as the UI dispatcher does for the upload engine.
+internal sealed class SingleThreadedContext : SynchronizationContext
+{
+ private readonly BlockingCollection<(SendOrPostCallback Callback, object? State)> _queue = [];
+
+ public override void Post(SendOrPostCallback d, object? state)
+ {
+ try
+ {
+ _queue.Add((d, state));
+ }
+ catch (InvalidOperationException)
+ {
+ // Late progress reports after the test body finished are dropped.
+ }
+ }
+
+ public override void Send(SendOrPostCallback d, object? state) => throw new NotSupportedException();
+
+ public static void Run(Func body)
+ {
+ var previous = Current;
+ var context = new SingleThreadedContext();
+ SetSynchronizationContext(context);
+ try
+ {
+ var task = body();
+ task.ContinueWith(_ => context._queue.CompleteAdding(), TaskScheduler.Default);
+ foreach (var (callback, state) in context._queue.GetConsumingEnumerable()) callback(state);
+ task.GetAwaiter().GetResult();
+ }
+ finally
+ {
+ SetSynchronizationContext(previous);
+ }
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/StableFileTrackerTests.cs b/tests/Titano.App.Tests/Uploader/StableFileTrackerTests.cs
new file mode 100644
index 00000000..5be3cb8e
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/StableFileTrackerTests.cs
@@ -0,0 +1,71 @@
+using Titano.Testing;
+using Titano.Uploader.Watching;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class StableFileTrackerTests
+{
+ private static readonly DateTime T0 = new(2026, 9, 28, 16, 0, 0, DateTimeKind.Utc);
+
+ [Test]
+ public void FileIsStableAfterTwoIdenticalChecks()
+ {
+ var tracker = new StableFileTracker();
+ var photo = new FileObservation("a.cr2", 25_000_000, T0);
+ Assert.Empty(tracker.Check([photo]));
+ Assert.Equal(1, tracker.PendingCount);
+ Assert.SequenceEqual([photo], tracker.Check([photo]));
+ Assert.Equal(0, tracker.PendingCount);
+ }
+
+ [Test]
+ public void GrowingFileWaitsUntilItStopsGrowing()
+ {
+ var tracker = new StableFileTracker();
+ Assert.Empty(tracker.Check([new("a.cr2", 1_000, T0)]));
+ Assert.Empty(tracker.Check([new("a.cr2", 9_000, T0.AddSeconds(1))]));
+ Assert.Empty(tracker.Check([new("a.cr2", 25_000, T0.AddSeconds(2))]));
+ Assert.Count(1, tracker.Check([new("a.cr2", 25_000, T0.AddSeconds(2))]));
+ }
+
+ [Test]
+ public void TouchedFileWithSameSizeRestartsTheCount()
+ {
+ var tracker = new StableFileTracker();
+ Assert.Empty(tracker.Check([new("a.jpg", 500, T0)]));
+ Assert.Empty(tracker.Check([new("a.jpg", 500, T0.AddSeconds(5))]));
+ Assert.Count(1, tracker.Check([new("a.jpg", 500, T0.AddSeconds(5))]));
+ }
+
+ [Test]
+ public void FileMissingFromAScanIsForgotten()
+ {
+ var tracker = new StableFileTracker();
+ var photo = new FileObservation("a.jpg", 500, T0);
+ Assert.Empty(tracker.Check([photo]));
+ Assert.Empty(tracker.Check([]));
+ Assert.Empty(tracker.Check([photo]));
+ Assert.Count(1, tracker.Check([photo]));
+ }
+
+ [Test]
+ public void FilesProgressIndependently()
+ {
+ var tracker = new StableFileTracker();
+ var done = new FileObservation("done.jpg", 500, T0);
+ Assert.Empty(tracker.Check([done, new("growing.jpg", 10, T0)]));
+ var stable = tracker.Check([done, new("growing.jpg", 20, T0.AddSeconds(1))]);
+ Assert.SequenceEqual([done], stable);
+ Assert.Equal(1, tracker.PendingCount);
+ }
+
+ [Test]
+ public void SingleObservationsUseTheSameRule()
+ {
+ var tracker = new StableFileTracker();
+ var photo = new FileObservation("a.nef", 42, T0);
+ Assert.False(tracker.Observe(photo));
+ Assert.True(tracker.Observe(photo));
+ Assert.False(tracker.Observe(photo));
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/TokenProtectorTests.cs b/tests/Titano.App.Tests/Uploader/TokenProtectorTests.cs
new file mode 100644
index 00000000..0c928070
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/TokenProtectorTests.cs
@@ -0,0 +1,27 @@
+using Titano.Testing;
+using Titano.Uploader.Infrastructure;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class TokenProtectorTests
+{
+ [Test]
+ [WindowsOnly]
+ public void TokenRoundTripsThroughDpapiWithoutPlainText()
+ {
+ const string token = "ttn_4f9a1c2e7b";
+ var blob = TokenProtector.Protect(token);
+ Assert.False(blob.Contains(token, StringComparison.Ordinal));
+ Assert.Equal(token, TokenProtector.Unprotect(blob));
+ }
+
+ [Test]
+ [WindowsOnly]
+ public void DamagedBlobsGiveNoToken()
+ {
+ var blob = Convert.FromBase64String(TokenProtector.Protect("secret"));
+ blob[^1] ^= 0xFF;
+ Assert.Null(TokenProtector.Unprotect(Convert.ToBase64String(blob)));
+ Assert.Null(TokenProtector.Unprotect("not base64!"));
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/UploadEngineTests.cs b/tests/Titano.App.Tests/Uploader/UploadEngineTests.cs
new file mode 100644
index 00000000..f2d940bb
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/UploadEngineTests.cs
@@ -0,0 +1,224 @@
+using System.Diagnostics;
+using System.IO;
+using System.Net;
+using System.Security.Cryptography;
+using Titano.Protocol;
+using Titano.Protocol.Client;
+using Titano.Testing;
+using Titano.Uploader.Uploads;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class UploadEngineTests(TestContext context)
+{
+ private readonly ManualTime _time = new();
+
+ [Test]
+ public void VerifiedUploadsKeepTheSourceByDefault() => SingleThreadedContext.Run(async () =>
+ {
+ var queue = Queue(UploadTargetMode.Sessions, 2);
+ var transport = new FakeTransport();
+ using var engine = new UploadEngine(queue, transport, () => false, _time);
+ var confirmed = new List();
+ engine.ItemConfirmed += (_, item) => confirmed.Add(item);
+
+ engine.Pump();
+ await Until(() => queue.Items.All(i => i.IsDone));
+
+ Assert.True(queue.Items.All(i => i.State == UploadItemState.Verified && i.FrameId is not null));
+ Assert.True(queue.Items.All(i => File.Exists(i.Path)));
+ // Two workers hash in parallel, so calls arrive in either order.
+ foreach (var item in queue.Items) Assert.Equal(Sha256(item.Path), transport.Calls.Single(c => c.Path == item.Path).Sha256);
+ Assert.Equal(2, confirmed.Count);
+ });
+
+ [Test]
+ public void DeleteAfterUploadRemovesOnlyConfirmedUnchangedFiles() => SingleThreadedContext.Run(async () =>
+ {
+ var queue = Queue(UploadTargetMode.Sessions, 3);
+ var (verified, existing, changed) = (queue.Items[0], queue.Items[1], queue.Items[2]);
+ var transport = new FakeTransport
+ {
+ Respond = item =>
+ {
+ if (item == existing) return (UploadOutcome.AlreadyOnServer, null);
+ // Written to after it was hashed: the bytes on disk are no longer the verified ones.
+ if (item == changed) File.AppendAllText(item.Path, "more");
+ return (UploadOutcome.Verified, Guid.CreateVersion7());
+ },
+ };
+ using var engine = new UploadEngine(queue, transport, () => true, _time);
+
+ engine.Pump();
+ await Until(() => queue.Items.All(i => i.IsDone));
+
+ Assert.Equal(UploadItemState.Skipped, existing.State);
+ Assert.False(File.Exists(verified.Path));
+ Assert.False(File.Exists(existing.Path));
+ Assert.True(File.Exists(changed.Path), "a file changed after verification is kept");
+ });
+
+ [Test]
+ public void RejectedFileIsRetriedAfterThirtySeconds() => SingleThreadedContext.Run(async () =>
+ {
+ var queue = Queue(UploadTargetMode.Sessions, 1);
+ var item = queue.Items[0];
+ var attempts = 0;
+ var transport = new FakeTransport
+ {
+ Respond = _ => ++attempts == 1
+ ? throw new TitanoApiException(HttpStatusCode.UnprocessableEntity, new ApiError("hash_mismatch", "Hash mismatch"))
+ : (UploadOutcome.Verified, Guid.CreateVersion7()),
+ };
+ using var engine = new UploadEngine(queue, transport, () => false, _time);
+
+ engine.Pump();
+ await Until(() => item.State == UploadItemState.Failed);
+ Assert.Equal(UploadFailure.ServerRejected, item.Failure);
+ Assert.Equal("Hash mismatch", item.FailureDetail);
+ Assert.Equal(_time.UtcNow + UploadQueue.RetryDelay, item.NextRetryUtc);
+
+ _time.Advance(TimeSpan.FromSeconds(29));
+ engine.Pump();
+ Assert.Equal(UploadItemState.Failed, item.State);
+
+ _time.Advance(TimeSpan.FromSeconds(1));
+ engine.Pump();
+ await Until(() => item.IsDone);
+ Assert.Equal(1, item.Retries);
+ Assert.Equal(UploadFailure.None, item.Failure);
+ });
+
+ [Test]
+ public void UnreachableServerRequeuesWithoutSpendingARetry() => SingleThreadedContext.Run(async () =>
+ {
+ var queue = Queue(UploadTargetMode.Sessions, 1);
+ var item = queue.Items[0];
+ var transport = new FakeTransport();
+ transport.Respond = _ =>
+ {
+ transport.SetReady(false);
+ throw new ServerUnavailableException(ServerProblem.Unreachable);
+ };
+ using var engine = new UploadEngine(queue, transport, () => false, _time);
+
+ engine.Pump();
+ await Until(() => item.State == UploadItemState.Queued && transport.Calls.Count == 1);
+ Assert.Equal(0, item.Retries);
+ Assert.NotNull(item.Sha256);
+ Assert.Equal(EngineStatus.WaitingForServer, engine.Status);
+
+ transport.Respond = _ => (UploadOutcome.Verified, Guid.CreateVersion7());
+ transport.SetReady(true);
+ await Until(() => item.IsDone);
+ });
+
+ [Test]
+ public void NewSequenceIsCreatedOnceForTheBatch() => SingleThreadedContext.Run(async () =>
+ {
+ var queue = Queue(UploadTargetMode.NewSequence, 3);
+ var transport = new FakeTransport();
+ using var engine = new UploadEngine(queue, transport, () => false, _time);
+
+ engine.Pump();
+ await Until(() => queue.Items.All(i => i.IsDone));
+
+ Assert.Equal(1, transport.SequencesCreated);
+ Assert.True(transport.Calls.All(c => c.SequenceId == FakeTransport.CreatedSequence));
+ Assert.Equal(FakeTransport.CreatedSequence, queue.Batches.Single().SequenceId);
+ });
+
+ [Test]
+ public void AtMostTwoFilesAreInFlightAndPauseRequeuesThem() => SingleThreadedContext.Run(async () =>
+ {
+ var queue = Queue(UploadTargetMode.Sessions, 4);
+ var transport = new FakeTransport { Block = true };
+ using var engine = new UploadEngine(queue, transport, () => false, _time);
+
+ engine.Pump();
+ await Until(() => queue.Items.Count(i => i.State == UploadItemState.Uploading) == UploadEngine.Parallelism);
+ Assert.Equal(UploadEngine.Parallelism, queue.Items.Count(i => i.IsActive));
+
+ engine.Pause();
+ await Until(() => queue.Items.All(i => i.State == UploadItemState.Queued));
+ Assert.Equal(EngineStatus.Paused, engine.Status);
+
+ transport.Block = false;
+ engine.Resume();
+ await Until(() => queue.Items.All(i => i.IsDone));
+ Assert.False(queue.IsPaused);
+ });
+
+ private UploadQueue Queue(UploadTargetMode mode, int count)
+ {
+ var dir = context.CreateTempDirectory();
+ var queue = new UploadQueue();
+ var entries = Enumerable.Range(0, count).Select(i =>
+ {
+ var path = Path.Combine(dir, $"IMG_{i:0000}.JPG");
+ File.WriteAllBytes(path, RandomNumberGenerator.GetBytes(4096 + i));
+ var info = new FileInfo(path);
+ return new QueueEntry(path, info.Length, info.LastWriteTimeUtc, info.LastWriteTime, "k", null);
+ }).ToList();
+ queue.Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = mode, Title = mode == UploadTargetMode.NewSequence ? "Roof" : null }, entries);
+ return queue;
+ }
+
+ private static string Sha256(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
+
+ private static async Task Until(Func condition)
+ {
+ var clock = Stopwatch.StartNew();
+ while (!condition())
+ {
+ if (clock.Elapsed > TimeSpan.FromSeconds(15)) throw new TimeoutException("Condition not reached");
+ await Task.Delay(5);
+ }
+ }
+
+ private sealed class ManualTime : TimeProvider
+ {
+ private DateTimeOffset _now = new(2026, 9, 28, 18, 0, 0, TimeSpan.Zero);
+
+ public DateTime UtcNow => _now.UtcDateTime;
+
+ public void Advance(TimeSpan by) => _now += by;
+
+ public override DateTimeOffset GetUtcNow() => _now;
+ }
+
+ private sealed class FakeTransport : IUploadTransport
+ {
+ public static readonly Guid CreatedSequence = Guid.CreateVersion7();
+
+ public event EventHandler? ReadyChanged;
+
+ public bool IsReady { get; private set; } = true;
+ public bool Block { get; set; }
+ public int SequencesCreated { get; private set; }
+ public List<(string Path, Guid? SequenceId, string Sha256)> Calls { get; } = [];
+ public Func Respond { get; set; } = _ => (UploadOutcome.Verified, Guid.CreateVersion7());
+
+ public void SetReady(bool ready)
+ {
+ IsReady = ready;
+ ReadyChanged?.Invoke(this, EventArgs.Empty);
+ }
+
+ public async Task<(UploadOutcome Outcome, Guid? FrameId)> UploadAsync(UploadItem item, Guid? sequenceId, string sha256, IProgress progress, CancellationToken ct)
+ {
+ Calls.Add((item.Path, sequenceId, sha256));
+ progress.Report(new UploadProgress(0, item.Size));
+ while (Block) await Task.Delay(5, ct);
+ await Task.Yield();
+ progress.Report(new UploadProgress(item.Size, item.Size));
+ return Respond(item);
+ }
+
+ public Task CreateSequenceAsync(string? title, CancellationToken ct)
+ {
+ SequencesCreated++;
+ return Task.FromResult(CreatedSequence);
+ }
+ }
+}
diff --git a/tests/Titano.App.Tests/Uploader/UploadQueueTests.cs b/tests/Titano.App.Tests/Uploader/UploadQueueTests.cs
new file mode 100644
index 00000000..6d1841bb
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/UploadQueueTests.cs
@@ -0,0 +1,143 @@
+using System.IO;
+using Titano.Testing;
+using Titano.Uploader.Uploads;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class UploadQueueTests(TestContext context)
+{
+ private static readonly DateTime Now = new(2026, 9, 28, 18, 30, 0, DateTimeKind.Utc);
+
+ [Test]
+ public void PersistenceRoundTripKeepsBatchesItemsAndStates()
+ {
+ var queue = new UploadQueue();
+ var sessions = new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions };
+ var created = new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.NewSequence, Title = "Harbour sunset", SequenceId = Guid.CreateVersion7() };
+ queue.Add(sessions, [Entry("a.cr2", "k1", @"D:\Cam\Roof"), Entry("b.cr2", "k1", null)]);
+ queue.Add(created, [Entry("c.jpg", null, null)]);
+ var verified = queue.Items[0];
+ verified.State = UploadItemState.Verified;
+ verified.Sha256 = new string('a', 64);
+ verified.FrameId = Guid.CreateVersion7();
+ var failed = queue.Items[1];
+ failed.Retries = 2;
+ queue.Fail(failed, UploadFailure.ServerRejected, "quota exceeded", Now);
+ queue.IsPaused = true;
+
+ var path = Path.Combine(context.CreateTempDirectory(), "uploader-queue.json");
+ File.WriteAllBytes(path, QueueStore.Serialize(queue));
+ var restored = new UploadQueue();
+ QueueStore.Load(path, restored);
+
+ Assert.True(restored.IsPaused);
+ Assert.Equal(2, restored.Batches.Count);
+ Assert.Equal("Harbour sunset", restored.GetBatch(created.Id)!.Title);
+ Assert.Equal(created.SequenceId, restored.GetBatch(created.Id)!.SequenceId);
+ Assert.Equal(3, restored.Items.Count);
+ for (var i = 0; i < 3; i++)
+ {
+ var (a, b) = (queue.Items[i], restored.Items[i]);
+ Assert.Equal(a.Id, b.Id);
+ Assert.Equal(a.BatchId, b.BatchId);
+ Assert.Equal(a.Path, b.Path);
+ Assert.Equal(a.Size, b.Size);
+ Assert.Equal(a.ModifiedUtc, b.ModifiedUtc);
+ Assert.Equal(a.CapturedAt, b.CapturedAt);
+ Assert.Equal(a.SessionKey, b.SessionKey);
+ Assert.Equal(a.WatchRoot, b.WatchRoot);
+ Assert.Equal(a.State, b.State);
+ }
+ Assert.Equal(verified.Sha256, restored.Items[0].Sha256);
+ Assert.Equal(verified.FrameId, restored.Items[0].FrameId);
+ Assert.Equal(UploadFailure.ServerRejected, restored.Items[1].Failure);
+ Assert.Equal("quota exceeded", restored.Items[1].FailureDetail);
+ Assert.Equal(2, restored.Items[1].Retries);
+ Assert.Equal(Now + UploadQueue.RetryDelay, restored.Items[1].NextRetryUtc);
+ Assert.True(restored.ContainsPath("c.jpg"));
+ }
+
+ [Test]
+ public void InterruptedTransfersResumeAsQueued()
+ {
+ var queue = new UploadQueue();
+ queue.Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions }, [Entry("a.cr2", null, null), Entry("b.cr2", null, null)]);
+ queue.Items[0].State = UploadItemState.Uploading;
+ queue.Items[0].Sha256 = new string('b', 64);
+ queue.Items[1].State = UploadItemState.Hashing;
+
+ var path = Path.Combine(context.CreateTempDirectory(), "q.json");
+ File.WriteAllBytes(path, QueueStore.Serialize(queue));
+ var restored = new UploadQueue();
+ QueueStore.Load(path, restored);
+
+ Assert.Equal(UploadItemState.Queued, restored.Items[0].State);
+ Assert.Equal(UploadItemState.Queued, restored.Items[1].State);
+ // The known hash is kept: the server resumes from the chunks it already holds.
+ Assert.Equal(new string('b', 64), restored.Items[0].Sha256);
+ }
+
+ [Test]
+ public void CorruptOrMissingFileGivesAnEmptyQueue()
+ {
+ var dir = context.CreateTempDirectory();
+ var corrupt = Path.Combine(dir, "q.json");
+ File.WriteAllText(corrupt, "{ not json");
+ var queue = new UploadQueue();
+ QueueStore.Load(corrupt, queue);
+ QueueStore.Load(Path.Combine(dir, "missing.json"), queue);
+ Assert.Empty(queue.Items);
+ Assert.False(queue.IsPaused);
+ }
+
+ [Test]
+ public void AddingSkipsPathsAlreadyQueued()
+ {
+ var queue = new UploadQueue();
+ var batch = new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions };
+ Assert.Equal(2, queue.Add(batch, [Entry("a.jpg", null, null), Entry("b.jpg", null, null)]));
+ Assert.Equal(1, queue.Add(batch, [Entry("b.jpg", null, null), Entry("c.jpg", null, null)]));
+ Assert.Equal(3, queue.Items.Count);
+ }
+
+ [Test]
+ public void FailedItemsComeBackAfterTheDelayUntilRetriesRunOut()
+ {
+ var queue = new UploadQueue();
+ queue.Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions }, [Entry("a.jpg", null, null)]);
+ var item = queue.Items[0];
+ item.State = UploadItemState.Hashing;
+ queue.Fail(item, UploadFailure.FileUnreadable, null, Now);
+ Assert.Null(queue.NextReady(Now.AddSeconds(29)));
+ Assert.Same(item, queue.NextReady(Now + UploadQueue.RetryDelay));
+ Assert.True(queue.HasPending);
+
+ item.Retries = UploadQueue.MaxRetries;
+ queue.Fail(item, UploadFailure.FileUnreadable, null, Now);
+ Assert.Null(item.NextRetryUtc);
+ Assert.Null(queue.NextReady(Now.AddDays(1)));
+ Assert.False(queue.HasPending);
+
+ queue.Requeue(item);
+ Assert.Equal(0, item.Retries);
+ Assert.Same(item, queue.NextReady(Now));
+ }
+
+ [Test]
+ public void RemovingCompletedItemsKeepsTheRest()
+ {
+ var queue = new UploadQueue();
+ var batch = new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions };
+ queue.Add(batch, [Entry("a.jpg", null, null), Entry("b.jpg", null, null), Entry("c.jpg", null, null)]);
+ queue.Items[0].State = UploadItemState.Verified;
+ queue.Items[2].State = UploadItemState.Skipped;
+ queue.RemoveCompleted();
+ Assert.Equal(1, queue.Items.Count);
+ Assert.False(queue.ContainsPath("a.jpg"));
+ Assert.True(queue.ContainsPath("b.jpg"));
+ Assert.Equal(1, queue.Batches.Count);
+ }
+
+ private static QueueEntry Entry(string name, string? session, string? watchRoot) =>
+ new(name, 1_000 + name.Length, new DateTime(2026, 9, 28, 16, 2, 11, DateTimeKind.Utc), new DateTime(2026, 9, 28, 18, 2, 11), session, watchRoot);
+}
diff --git a/tests/Titano.App.Tests/Uploader/UploaderTextTests.cs b/tests/Titano.App.Tests/Uploader/UploaderTextTests.cs
new file mode 100644
index 00000000..d9289fad
--- /dev/null
+++ b/tests/Titano.App.Tests/Uploader/UploaderTextTests.cs
@@ -0,0 +1,124 @@
+using System.Globalization;
+using Titano.Testing;
+using Titano.Uploader.Text;
+using Titano.Uploader.Uploads;
+
+namespace Titano.App.Tests.Uploader;
+
+public sealed class UploaderTextTests
+{
+ private static readonly CultureInfo En = CultureInfo.GetCultureInfo("en");
+ private static readonly CultureInfo It = CultureInfo.GetCultureInfo("it");
+
+ private static readonly QueueSummary Mockup = new(4_812, 96_200_000_000, 2_214, 2_201, 0, 0, 44_000_000_000, "2026-09-28_18-02-11_0413.CR2")
+ {
+ RemainingBytes = 93_480_000_000,
+ };
+
+ [Test]
+ public void QueueLineMatchesTheMockup()
+ {
+ var left = RateMeter.TimeLeft(Mockup.RemainingBytes, 38_000_000);
+ Assert.Equal("4,812 files · 96.2 GB · 38 MB/s · 41 min left", UploaderText.QueueLine(Mockup, QueueActivity.Running, 38_000_000, left, En));
+ }
+
+ [Test]
+ public void QueueLineFollowsTheCulture()
+ {
+ Assert.Equal("4.812 file · 96,2 GB · 38 MB/s · mancano 41 min", UploaderText.QueueLine(Mockup, QueueActivity.Running, 38_000_000, TimeSpan.FromMinutes(41), It));
+ }
+
+ [Test]
+ public void QueueLineNamesTheActivityWhenNotRunning()
+ {
+ Assert.Equal("4,812 files · 96.2 GB · Paused", UploaderText.QueueLine(Mockup, QueueActivity.Paused, 0, null, En));
+ Assert.Equal("4,812 files · 96.2 GB · Waiting for server", UploaderText.QueueLine(Mockup, QueueActivity.WaitingForServer, 0, null, En));
+ Assert.Equal("1 file · 25 MB · Done", UploaderText.QueueLine(Mockup with { Files = 1, Bytes = 25_000_000 }, QueueActivity.Done, 0, null, En));
+ Assert.Equal("Nothing queued", UploaderText.QueueLine(default, QueueActivity.Idle, 0, null, En));
+ }
+
+ [Test]
+ public void RunningLineOmitsRateAndTimeUntilKnown()
+ {
+ Assert.Equal("4,812 files · 96.2 GB", UploaderText.QueueLine(Mockup, QueueActivity.Running, 0, null, En));
+ }
+
+ [Test]
+ public void ProgressLineMatchesTheMockup()
+ {
+ Assert.Equal("2,214 of 4,812 sent · 2,201 verified by the server · 0 retries", UploaderText.ProgressLine(Mockup, En));
+ Assert.Equal("2,214 of 4,812 sent · 2,201 verified by the server · 1 retry · 3 failed", UploaderText.ProgressLine(Mockup with { Retries = 1, Failed = 3 }, En));
+ }
+
+ [Theory]
+ [Case(0.0, "0 B/s")]
+ [Case(950_000.0, "950 KB/s")]
+ [Case(4_240_000.0, "4.2 MB/s")]
+ [Case(38_400_000.0, "38 MB/s")]
+ [Case(1_260_000_000.0, "1.3 GB/s")]
+ public void RatesUseDecimalUnits(double bytesPerSecond, string expected) =>
+ Assert.Equal(expected, UploaderText.Rate(bytesPerSecond, En));
+
+ [Theory]
+ [Case(40.0, "40 s")]
+ [Case(299.0, "5 min")]
+ [Case(3_590.0, "1 h 00 m")]
+ [Case(7_500.0, "2 h 05 m")]
+ [Case(93_600.0, "1 d 2 h")]
+ public void DurationsAreCompact(double seconds, string expected) =>
+ Assert.Equal(expected, UploaderText.Duration(TimeSpan.FromSeconds(seconds), En));
+
+ [Theory]
+ [Case(999L, "999 B")]
+ [Case(1_536L, "1.5 KB")]
+ [Case(96_200_000_000L, "96.2 GB")]
+ [Case(1_900_000_000_000L, "1.9 TB")]
+ public void SizesUseDecimalUnits(long bytes, string expected) => Assert.Equal(expected, UploaderText.Bytes(bytes, En));
+
+ [Test]
+ public void TimeLeftNeedsAUsableRate()
+ {
+ Assert.Equal(TimeSpan.FromMinutes(41), RateMeter.TimeLeft(93_480_000_000, 38_000_000));
+ Assert.Equal(TimeSpan.Zero, RateMeter.TimeLeft(0, 0));
+ Assert.Null(RateMeter.TimeLeft(1_000, 10));
+ }
+
+ [Test]
+ public void RateMeterAveragesTheWindowAndDecaysWhenStalled()
+ {
+ var meter = new RateMeter(TimeSpan.FromSeconds(10));
+ Assert.Equal(0.0, meter.BytesPerSecond(TimeSpan.Zero));
+ for (var s = 0; s <= 20; s++) meter.Add(4_000_000, TimeSpan.FromSeconds(s));
+ Assert.Approximately(4_000_000, meter.BytesPerSecond(TimeSpan.FromSeconds(20)), 1);
+ // Nothing sent for 20 s: the window holds no transfer any more.
+ Assert.Equal(0.0, meter.BytesPerSecond(TimeSpan.FromSeconds(40)));
+ meter.Reset();
+ Assert.Equal(0.0, meter.BytesPerSecond(TimeSpan.FromSeconds(40)));
+ }
+
+ [Test]
+ public void SummaryCountsStatesAndBytes()
+ {
+ var queue = new UploadQueue();
+ queue.Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = UploadTargetMode.Sessions },
+ Enumerable.Range(0, 5).Select(i => new QueueEntry($"{i}.jpg", 100, DateTime.UtcNow, null, null, null)));
+ queue.Items[0].State = UploadItemState.Verified;
+ queue.Items[1].State = UploadItemState.Skipped;
+ queue.Items[2].State = UploadItemState.Uploading;
+ queue.Items[2].BytesSent = 40;
+ queue.Items[3].State = UploadItemState.Failed;
+ queue.Items[3].Retries = 1;
+
+ var s = QueueSummary.From(queue.Items);
+ Assert.Equal(5, s.Files);
+ Assert.Equal(500L, s.Bytes);
+ Assert.Equal(2, s.Sent);
+ Assert.Equal(2, s.Verified);
+ Assert.Equal(1, s.Failed);
+ Assert.Equal(1, s.Retries);
+ Assert.Equal(240L, s.DoneBytes);
+ Assert.Equal(160L, s.RemainingBytes);
+ Assert.Equal("2.jpg", s.CurrentFile);
+ Assert.Approximately(0.48, s.Progress, 1e-9);
+ }
+}