diff --git a/deploy/docker/docker-compose.yml b/deploy/docker/docker-compose.yml index 9cc7dd66..041f38cb 100644 --- a/deploy/docker/docker-compose.yml +++ b/deploy/docker/docker-compose.yml @@ -14,6 +14,7 @@ services: environment: TITANO_ADMIN_USER: admin TITANO_ADMIN_PASSWORD: ${TITANO_ADMIN_PASSWORD:?set TITANO_ADMIN_PASSWORD in .env} + # postgres (with the TITANO_DB_* values below) or a full connection string: Host=…;Database=…;Username=…;Password=… TITANO_DB: postgres TITANO_DB_HOST: postgres TITANO_DB_NAME: titano diff --git a/i18n/en/Admin.resx b/i18n/en/Admin.resx index d63f15e8..0933d7a3 100644 --- a/i18n/en/Admin.resx +++ b/i18n/en/Admin.resx @@ -4,17 +4,199 @@ 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 + API version + Database TLS SHA-256 + Server name Titano Server - API version + Running for + Version + {0} changed the archive layoutlong + {0} started {1}long + {0} signed in · {1}long + failed sign-in as {0}long + {0} changed their passwordlong + {0} reset the password of {1}long + {0} created {1}long + {0} deleted {1}long + {0} edited a sequence · {1}long + {0} changed the server settingslong + {0} created user {1}long + {0} deleted user {1}long + {0} changed a user · {1}long + Cameras + Formats + File template + Name of new originals, e.g. {date}_{time}_{n:0000} + Folder template + Folder of new sequences, e.g. {year}/{date} {camera}; existing folders are never renamed + free + Health + Layout + Maintenance + Clean up stale caches and database entries + Name + Name of the archive shown to clients + Originals + Proxies + Long edge of the JPEG proxies built for editing and viewing + {0} px + Full size + Off + Job queued. + Rebuild + Rebuild the index from the sidecars on disk; files are not touched + Scan + Look for new and changed sequences on disk + Sequences + unverified + Verify + Read every original again and compare its SHA-256 + Cancel + Discard the changes + Close + Close this panel + Keep + Leave everything as it is + Load more + Show the next page + Refresh + Save + Save the changes on the server + Saved. + Recent activity + All logs + Open the audit log Archive - Server + Storage by year + discovery off + discovery on + {0} free + {0} queued · {1} done today · {2} failedlong + Jobs + up {0} · {1} · TLS {2} · {3}long + No activity yet + Nothing here yet + {0} sequences without proxieslong + Render nodes + of {0} + online + originals {0} + {0} photos · {1} cameras + Queue and history + Open the Jobs page + running + sequences + Storage + frames per day · {0} total · peak {1}long + Uploads · last 30 days + {0} workers The server did not answer; try again.long + Adopt + Deep analysis + Duplicates + Import + Light analysis + Maintenance + Move + Orphans + Proxies + Rebuild + Render + Scan + Thumbnails + Upload + Verify + busy + cancelled + done + failed + idle + offline + paused + queued + running + Cancel + Cancel this job + Created + No jobs + Encoders + All + Cancelled + Done + Failed + Paused + Queued + Running + Show only jobs in this state + Forget + Remove this node; its running jobs go back to the queue + Forget {0}? + Last heartbeat + Kind + Show or hide the job log + {0} running · {1} nodes onlinelong + No render nodes + Node + Node + Render nodes + Owner + Pause + Stop at the next checkpoint and keep the progress + Background + Normal + Order in the queue: user jobs run before normal and background ones + User + Progress + Remove + Remove from the queue + Resume + Put the job back in the queue from its checkpoint + Retry + Queue the job again from its last checkpoint + server + State + Target + Language of this panel Wrong user name or password.long + Only administrators can use this panel.long Password + Your password; it is checked by the server only Sign in Sign in to administer this server User + Your user name on this server + Archive layout + Job started + Sign-in + Failed sign-in + Password changed + Password reset + Sequence created + Sequence deleted + Sequence edited + Server settings + User created + User deleted + User changed + Details + Download + Save this log file + Nothing recorded yet + Log files + From + {0} lines + How many of the latest lines to show + Read the log again + Audit + Changes to users, sequences, the archive and settings + Server log + The latest lines of the server log, and the log files + Sign-ins + Who signed in, from where and when + What + When + Who About Version and certificate fingerprint Archive @@ -29,9 +211,113 @@ Server name, ports, TLS, limits and language Users Accounts, roles and quotas + Current password + The password you signed in with + The two passwords differ.long + New password + At least 10 characters; other sessions of this account end + Repeat + Type the new password again + Change password + Replace the password and continue to the panel + Choose a new password Administrator Editor Uploader Viewer + Chunk size (MiB) + Size of each upload chunk; smaller chunks resume faster on poor links + LAN discovery + Announce this server on the local network so clients find it + Fingerprint + SHA-256 of the certificate; clients ask the user to compare it once + HTTP port + Port of the admin panel and API over HTTP + HTTPS port + Port of the API over TLS for Titano clients + Job history (days) + Days finished jobs are kept + Language + Language of server messages when a client asks for none + Limits and retention + Set by the environment variable {0} + Max file (MB) + Largest single upload accepted + Network and TLS + Restart required + Saved ports, TLS, workers, chunk size or job history apply after a restart + Server + Server name + Name shown to clients and on the LAN + Self-signed + Own certificate + Reverse proxy + Certificate + PEM certificate file used in Own certificate mode + Private key + PEM private key file used in Own certificate mode + TLS mode + How HTTPS is provided + Session days + Days a client stays signed in without being used + Abandoned after (h) + Hours after which unfinished uploads are discarded + Workers + Jobs this server runs at the same time + Sign out Sign out of the admin panel + Active + Password copied. + Copy the password + Create + Create the account + Delete… + Delete the account; its sequences are reassigned to you + The account and its sessions are removed; its sequences become yours.long + Delete + Delete {0}? + Disable + Disable: sessions end, files stay + Disabled + Display name + Shown in the client and in activity + Edit + Change name, role, quota or render-node right + Edit user + No users match + Enable + Re-enable the account + {0} free on the server + Last seen + {0} accounts · {1} active + must change password + Must change password + Always required for accounts created here + New user + Create an account; the user must change the password at first sign-in + Passwords: PBKDF2-SHA512, {0} characters minimum · tokens expire after {1} days idlelong + Quota + Originals uploaded by this user count against the quota; 0 = unlimited + {0} of {1} + Generate another + May act as render node + Lets this user's PC take render jobs from the server + Reset password + Set a temporary password; the user must replace it and their sessions end + Role + Everything, including users and settings + Import, edit, render, upload and download + Upload only; cannot edit sequences + Browse and download renders only + Search + Filter by user or display name + Sign-in log + Who signed in from where, and when + Status + Temporary password + Give this to the user; they must replace it at first sign-in + Uploads 30 d + User + Username + Letters, digits, dot and dash; cannot be changed later diff --git a/i18n/en/Server.resx b/i18n/en/Server.resx index 24a55d5c..512ef763 100644 --- a/i18n/en/Server.resx +++ b/i18n/en/Server.resx @@ -14,14 +14,62 @@ Connect Disconnect Not connected to a server + Too many failed sign-ins; the account is locked for a while.long Wrong user name or password.long + The request is not valid.long The server certificate changed; confirm it and sign in again.long + This upload has no such chunk.long + The chunk has the wrong size.long + The file could not be stored in the archive; try again.long + The security token is missing; reload the page.long The connection failed; the log has the details.long + Your account cannot do this.long + The file arrived damaged; upload it again.long + Open the admin panel over HTTPS to sign in.long + Some chunks are still missing.long + The server is out of disk space.long + Unexpected server error; try again.long That is not a valid server address.long + Wrong user name or password.long + The paging cursor is not valid; start from the first page.long + The file name is not valid.long + A filter value is not valid.long + The SHA-256 must be 64 hexadecimal characters.long + This job kind cannot be started here.long + The job parameters are not valid.long + The priority must be background, normal or user.long + The role must be admin, editor, uploader or viewer.long + The setting {0} is not valid.long + The settings must be valid JSON.long + The job cannot do that in its current state.long + The naming template is not valid.long + Use letters, digits, dot, dash or underscore.long + This job is no longer assigned to this node.long + This server cannot run that kind of job.long + The last administrator cannot be removed or disabled.long + This method is not allowed here.long + Not found.long That address is not a Titano Server.long + Change your password first.long + This upload would exceed your quota.long + Too many attempts; wait a minute and try again.long + Choose a password different from the current one.long + You cannot do this to your own account.long The session expired; sign in again.long + An environment variable sets this; change it there.long + The sequence changed on the server; reload it and apply your edit again.long + The request is larger than the server accepts.long + Too many uploads at once; retry shortly.long + Sign in to continue.long The server does not answer; check the address and the network.long + Send the body as JSON.long The certificate was not trusted, so nothing was sent.long + This upload is not accepting that request now.long + That user name is taken.long + Use at least {0} characters.long + Connect with a WebSocket.long + The current password is wrong.long + This upload belongs to a render job.long Servers found Account Server @@ -39,6 +87,7 @@ Editor Uploader Viewer + Titano time-lapse archive, uploads, jobs and rendering server.long Sign out Connected Connecting… diff --git a/i18n/it/Admin.it.resx b/i18n/it/Admin.it.resx index 4e1f7a66..e1bdb0f1 100644 --- a/i18n/it/Admin.it.resx +++ b/i18n/it/Admin.it.resx @@ -4,17 +4,199 @@ 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 + Versione API + Database TLS SHA-256 + Nome server Titano Server - Versione API + In esecuzione da + Versione + {0} ha modificato l'organizzazione dell'archivio + {0} ha avviato {1} + {0} ha effettuato l'accesso · {1} + accesso fallito come {0} + {0} ha cambiato la propria password + {0} ha reimpostato la password di {1} + {0} ha creato {1} + {0} ha eliminato {1} + {0} ha modificato una sequenza · {1} + {0} ha modificato le impostazioni del server + {0} ha creato l'utente {1} + {0} ha eliminato l'utente {1} + {0} ha modificato un utente · {1} + Fotocamere + Formati + Modello file + Nome dei nuovi originali, es. {date}_{time}_{n:0000} + Modello cartelle + Cartella delle nuove sequenze, es. {year}/{date} {camera}; le cartelle esistenti non vengono mai rinominate + liberi + Stato + Organizzazione + Manutenzione + Pulisci cache obsolete e voci del database + Nome + Nome dell'archivio mostrato ai client + Originali + Proxy + Lato lungo dei proxy JPEG creati per modifica e visione + {0} px + Dimensione piena + Disattivati + Job in coda. + Ricostruisci + Ricostruisci l'indice dai file sidecar su disco; i file non vengono toccati + Scansiona + Cerca sequenze nuove e modificate sul disco + Sequenze + non verificate + Verifica + Rileggi ogni originale e confronta il suo SHA-256 + Annulla + Scarta le modifiche + Chiudi + Chiudi questo pannello + Mantieni + Lascia tutto com'è + Carica altri + Mostra la pagina successiva + Aggiorna + Salva + Salva le modifiche sul server + Salvato. + Attività recenti + Tutti i log + Apri il registro di controllo Archivio - Server + Spazio per anno + rilevamento spento + rilevamento attivo + {0} liberi + {0} in coda · {1} completati oggi · {2} falliti + Job + attivo da {0} · {1} · TLS {2} · {3} + Nessuna attività + Ancora niente qui + {0} sequenze senza proxy + Nodi di rendering + di {0} + online + originali {0} + {0} foto · {1} fotocamere + Coda e cronologia + Apri la pagina Job + in corso + sequenze + Spazio + foto al giorno · {0} totali · picco {1} + Caricamenti · ultimi 30 giorni + {0} worker Il server non ha risposto; riprova. + Adozione + Analisi completa + Duplicati + Importazione + Analisi rapida + Manutenzione + Spostamento + Orfani + Proxy + Ricostruzione + Rendering + Scansione + Miniature + Caricamento + Verifica + occupato + annullato + completato + fallito + inattivo + offline + in pausa + in coda + in corso + Annulla + Annulla questo job + Creato + Nessun job + Encoder + Tutti + Annullati + Completati + Falliti + In pausa + In coda + In corso + Mostra solo i job in questo stato + Dimentica + Rimuovi questo nodo; i suoi job in corso tornano in coda + Dimenticare {0}? + Ultimo segnale + Tipo + Mostra o nascondi il log del job + {0} in corso · {1} nodi online + Nessun nodo di rendering + Nodo + Nodo + Nodi di rendering + Proprietario + Pausa + Fermati al prossimo punto di controllo e mantieni l'avanzamento + Background + Normale + Ordine in coda: i job utente partono prima di quelli normali e in background + Utente + Avanzamento + Rimuovi + Rimuovi dalla coda + Riprendi + Rimetti il job in coda dal suo punto di controllo + Riprova + Rimetti in coda il job dall'ultimo punto di controllo + server + Stato + Oggetto + Lingua di questo pannello Nome utente o password errati. + Solo gli amministratori possono usare questo pannello. Password + La tua password; la verifica solo il server Accedi Accedi per amministrare questo server Utente + Il tuo nome utente su questo server + Organizzazione archivio + Job avviato + Accesso + Accesso fallito + Password cambiata + Password reimpostata + Sequenza creata + Sequenza eliminata + Sequenza modificata + Impostazioni server + Utente creato + Utente eliminato + Utente modificato + Dettagli + Scarica + Salva questo file di log + Ancora nessuna registrazione + File di log + Da + {0} righe + Quante delle ultime righe mostrare + Rileggi il log + Controllo + Modifiche a utenti, sequenze, archivio e impostazioni + Log del server + Le ultime righe del log del server e i file di log + Accessi + Chi ha effettuato l'accesso, da dove e quando + Cosa + Quando + Chi Informazioni Versione e impronta del certificato Archivio @@ -29,9 +211,113 @@ Nome server, porte, TLS, limiti e lingua Utenti Account, ruoli e quote + Password attuale + La password con cui hai effettuato l'accesso + Le due password non coincidono. + Nuova password + Almeno 10 caratteri; le altre sessioni di questo account terminano + Ripeti + Digita di nuovo la nuova password + Cambia password + Sostituisci la password e continua nel pannello + Scegli una nuova password Amministratore Editor Caricamento Lettore + Dimensione blocco (MiB) + Dimensione di ogni blocco di caricamento; blocchi piccoli riprendono prima su linee lente + Rilevamento LAN + Annuncia questo server nella rete locale così che i client lo trovino + Impronta + SHA-256 del certificato; i client chiedono all'utente di confrontarlo una volta + Porta HTTP + Porta del pannello e delle API in HTTP + Porta HTTPS + Porta delle API in TLS per i client Titano + Cronologia job (giorni) + Giorni di conservazione dei job conclusi + Lingua + Lingua dei messaggi del server se il client non ne chiede una + Limiti e conservazione + Impostato dalla variabile d'ambiente {0} + File massimo (MB) + Caricamento singolo più grande accettato + Rete e TLS + Riavvio richiesto + Porte, TLS, worker, dimensione blocchi o cronologia job salvati valgono dopo un riavvio + Server + Nome server + Nome mostrato ai client e nella rete locale + Autofirmato + Certificato proprio + Reverse proxy + Certificato + File del certificato PEM usato in modalità Certificato proprio + Chiave privata + File della chiave privata PEM usato in modalità Certificato proprio + Modalità TLS + Come viene fornito HTTPS + Giorni di sessione + Giorni in cui un client resta connesso senza essere usato + Abbandono dopo (h) + Ore dopo le quali i caricamenti incompleti vengono scartati + Worker + Job che questo server esegue contemporaneamente + Esci Esci dal pannello di amministrazione + Attivo + Password copiata. + Copia la password + Crea + Crea l'account + Elimina… + Elimina l'account; le sue sequenze vengono assegnate a te + L'account e le sue sessioni vengono rimossi; le sue sequenze diventano tue. + Elimina + Eliminare {0}? + Disattiva + Disattiva: le sessioni terminano, i file restano + Disattivato + Nome visualizzato + Mostrato nel client e nelle attività + Modifica + Cambia nome, ruolo, quota o diritto di nodo di rendering + Modifica utente + Nessun utente corrisponde + Attiva + Riattiva l'account + {0} liberi sul server + Ultimo accesso + {0} account · {1} attivi + deve cambiare password + Cambio password obbligatorio + Sempre richiesto per gli account creati qui + Nuovo utente + Crea un account; l'utente deve cambiare la password al primo accesso + Password: PBKDF2-SHA512, almeno {0} caratteri · i token scadono dopo {1} giorni di inattività + Quota + Gli originali caricati da questo utente contano nella quota; 0 = illimitata + {0} di {1} + Generane un'altra + Può fare da nodo di rendering + Permette al PC di questo utente di prendere job di rendering dal server + Reimposta password + Imposta una password temporanea; l'utente deve sostituirla e le sue sessioni terminano + Ruolo + Tutto, compresi utenti e impostazioni + Importa, modifica, esegue rendering, carica e scarica + Solo caricamento; non può modificare le sequenze + Solo consultazione e download dei rendering + Cerca + Filtra per nome utente o nome visualizzato + Registro accessi + Chi ha effettuato l'accesso, da dove e quando + Stato + Password temporanea + Comunicala all'utente; dovrà sostituirla al primo accesso + Caricamenti 30 g + Utente + Nome utente + Lettere, cifre, punto e trattino; non si può cambiare in seguito diff --git a/i18n/it/Server.it.resx b/i18n/it/Server.it.resx index 8fdb24f3..fb5bdb68 100644 --- a/i18n/it/Server.it.resx +++ b/i18n/it/Server.it.resx @@ -13,14 +13,62 @@ Connetti Disconnetti Non connesso a un server + Troppi accessi falliti; l'account è bloccato per un po'. Nome utente o password errati. + La richiesta non è valida. Il certificato del server è cambiato; confermalo e accedi di nuovo. + Questo caricamento non ha quel blocco. + Il blocco ha la dimensione sbagliata. + Il file non è stato salvato nell'archivio; riprova. + Manca il token di sicurezza; ricarica la pagina. La connessione non è riuscita; i dettagli sono nel log. + Il tuo account non può farlo. + Il file è arrivato danneggiato; caricalo di nuovo. + Apri il pannello di amministrazione in HTTPS per accedere. + Mancano ancora alcuni blocchi. + Il server ha esaurito lo spazio su disco. + Errore imprevisto del server; riprova. Questo non è un indirizzo di server valido. + Nome utente o password errati. + Il cursore di paginazione non è valido; ricomincia dalla prima pagina. + Il nome del file non è valido. + Un valore di filtro non è valido. + Lo SHA-256 deve avere 64 caratteri esadecimali. + Questo tipo di job non si può avviare qui. + I parametri del job non sono validi. + La priorità deve essere background, normal o user. + Il ruolo deve essere admin, editor, uploader o viewer. + L'impostazione {0} non è valida. + Le impostazioni devono essere JSON valido. + Il job non può farlo nello stato attuale. + Il modello dei nomi non è valido. + Usa lettere, cifre, punto, trattino o trattino basso. + Questo job non è più assegnato a questo nodo. + Questo server non può eseguire quel tipo di job. + L'ultimo amministratore non può essere rimosso o disattivato. + Metodo non consentito qui. + Non trovato. Questo indirizzo non è un Titano Server. + Cambia prima la password. + Questo caricamento supererebbe la tua quota. + Troppi tentativi; attendi un minuto e riprova. + Scegli una password diversa da quella attuale. + Non puoi farlo sul tuo account. La sessione è scaduta; accedi di nuovo. + È impostato da una variabile d'ambiente; modificala lì. + La sequenza è cambiata sul server; ricaricala e riapplica la modifica. + La richiesta supera la dimensione accettata dal server. + Troppi caricamenti insieme; riprova tra poco. + Accedi per continuare. Il server non risponde; controlla l'indirizzo e la rete. + Invia il corpo come JSON. Il certificato non è stato accettato, quindi non è stato inviato nulla. + Questo caricamento ora non accetta la richiesta. + Questo nome utente è già in uso. + Usa almeno {0} caratteri. + Connettiti con un WebSocket. + La password attuale è errata. + Questo caricamento appartiene a un job di rendering. Server trovati Account Server @@ -38,6 +86,7 @@ Editor Caricatore Visualizzatore + Server Titano per archivio time-lapse, caricamenti, job e rendering. Esci Connesso Connessione… diff --git a/src/Titano.Data/Migrations/postgres/0003_server_uploads.sql b/src/Titano.Data/Migrations/postgres/0003_server_uploads.sql new file mode 100644 index 00000000..271f679a --- /dev/null +++ b/src/Titano.Data/Migrations/postgres/0003_server_uploads.sql @@ -0,0 +1,9 @@ +-- Titano Server additions (PostgreSQL): automatic grouping key of frame uploads, render-result uploads of +-- render-node jobs, and the sign-in log lookup. Mirrors sqlite/0005_server_uploads.sql. + +ALTER TABLE uploads ADD COLUMN session_key text; +ALTER TABLE uploads ADD COLUMN job_id uuid; +ALTER TABLE uploads ADD COLUMN render_json jsonb; +CREATE INDEX ix_uploads_job ON uploads (job_id); + +CREATE INDEX ix_audit_log_action ON audit_log (action, at); diff --git a/src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql b/src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql new file mode 100644 index 00000000..ce6e5d64 --- /dev/null +++ b/src/Titano.Data/Migrations/sqlite/0003_server_uploads.sql @@ -0,0 +1,9 @@ +-- Titano Server additions (SQLite): automatic grouping key of frame uploads, render-result uploads of render-node +-- jobs, and the sign-in log lookup. Mirrors postgres/0005_server_uploads.sql. + +ALTER TABLE uploads ADD COLUMN session_key TEXT; +ALTER TABLE uploads ADD COLUMN job_id TEXT; +ALTER TABLE uploads ADD COLUMN render_json TEXT; +CREATE INDEX ix_uploads_job ON uploads (job_id); + +CREATE INDEX ix_audit_log_action ON audit_log (action, at); diff --git a/src/Titano.Data/Repositories/AuditRepository.cs b/src/Titano.Data/Repositories/AuditRepository.cs index 46e13c31..d2199979 100644 --- a/src/Titano.Data/Repositories/AuditRepository.cs +++ b/src/Titano.Data/Repositories/AuditRepository.cs @@ -17,11 +17,16 @@ public sealed class AuditRepository cancellationToken).ConfigureAwait(false); /// Entries newest first, optionally of one user. - public Task> ListAsync(Guid? userId = null, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) + public Task> ListAsync(Guid? userId = null, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) => + ListAsync(userId, action: null, limit, cursor, cancellationToken); + + /// Entries newest first, optionally of one user and one action (e.g. the sign-in log). + public Task> ListAsync(Guid? userId, string? action, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) { var p = new SqlParameters(); var where = new List(); if (userId is { } user) where.Add($"user_id = {p.Add(user)}"); + if (action is not null) where.Add($"action = {p.Add(action)}"); if (cursor is not null) { var (value, id) = Cursor.Decode("audit", cursor); diff --git a/src/Titano.Data/Repositories/FrameRepository.cs b/src/Titano.Data/Repositories/FrameRepository.cs index ae064902..cb10e5e7 100644 --- a/src/Titano.Data/Repositories/FrameRepository.cs +++ b/src/Titano.Data/Repositories/FrameRepository.cs @@ -16,6 +16,9 @@ public sealed record DuplicateFrame(string Sha256, Guid FrameId, Guid SequenceId /// Light statistics of one frame, written by the analysis job. public readonly record struct FrameLightStats(Guid FrameId, double? MeanLogLuminance, double? ClipLow, double? ClipHigh); +/// Per-format totals and flagged-frame counts of one sequence. +public sealed record SequenceFrameStats(IReadOnlyList ByFormat, long Missing, long Corrupt, long Duplicate, long Excluded); + /// Frames of sequences: bulk import, paged listing, duplicate lookup and bulk updates. public sealed class FrameRepository { @@ -210,6 +213,23 @@ public sealed class FrameRepository return updated; }, cancellationToken); + /// Formats and flagged-frame counters of one sequence. + public Task GetSequenceStatsAsync(Guid sequenceId, CancellationToken cancellationToken = default) => + _db.RunAsync(async session => + { + var byFormat = await session.QueryListAsync( + "SELECT format, count(*), COALESCE(SUM(size), 0) FROM frames WHERE sequence_id = $1 GROUP BY format ORDER BY 2 DESC, 1", + [sequenceId], r => new FormatBucket((ImageFileFormat)r.GetInt32(0), r.GetInt64(1), r.GetInt64(2)), cancellationToken).ConfigureAwait(false); + var flags = await session.QuerySingleAsync( + "SELECT " + + $"COALESCE(SUM(CASE WHEN (flags & {(int)FrameFlags.Missing}) <> 0 THEN 1 ELSE 0 END), 0), " + + $"COALESCE(SUM(CASE WHEN (flags & {(int)FrameFlags.Corrupt}) <> 0 THEN 1 ELSE 0 END), 0), " + + $"COALESCE(SUM(CASE WHEN (flags & {(int)FrameFlags.Duplicate}) <> 0 THEN 1 ELSE 0 END), 0), " + + $"COALESCE(SUM(CASE WHEN (flags & {(int)FrameFlags.Excluded}) <> 0 THEN 1 ELSE 0 END), 0) " + + "FROM frames WHERE sequence_id = $1", [sequenceId], cancellationToken).ConfigureAwait(false); + return new SequenceFrameStats(byFormat, flags.GetInt64(0), flags.GetInt64(1), flags.GetInt64(2), flags.GetInt64(3)); + }, cancellationToken); + /// Removes every frame of a sequence; returns how many. public Task DeleteBySequenceAsync(Guid sequenceId, CancellationToken cancellationToken = default) => _db.ExecuteAsync("DELETE FROM frames WHERE sequence_id = $1", [sequenceId], cancellationToken); diff --git a/src/Titano.Data/Repositories/JobRepository.cs b/src/Titano.Data/Repositories/JobRepository.cs index 6736de5c..0a663325 100644 --- a/src/Titano.Data/Repositories/JobRepository.cs +++ b/src/Titano.Data/Repositories/JobRepository.cs @@ -3,6 +3,9 @@ using Titano.Core.Model; namespace Titano.Data.Repositories; +/// Jobs per state; and count those finished since a given time. +public sealed record JobCounts(int Queued, int Running, int Paused, int DoneSince, int FailedSince); + /// /// The persistent job queue (BRIEF §14.6): workers claim the oldest job of the highest priority atomically, /// write checkpoints while running, and on startup requeue what a crash left running. @@ -74,11 +77,16 @@ public sealed class JobRepository } /// Jobs, newest first, optionally of one state. - public Task> ListAsync(JobState? state = null, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default) + public Task> ListAsync(JobState? state = null, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default) => + ListAsync(state, kind: null, limit, cursor, cancellationToken); + + /// Jobs, newest first, optionally of one state and one kind. + public Task> ListAsync(JobState? state, JobKind? kind, int limit = 50, string? cursor = null, CancellationToken cancellationToken = default) { var p = new SqlParameters(); var where = new List(); if (state is { } s) where.Add($"state = {p.Add((int)s)}"); + if (kind is { } k) where.Add($"kind = {p.Add((int)k)}"); if (cursor is not null) { var (value, id) = Cursor.Decode("jobs", cursor); @@ -117,6 +125,74 @@ public sealed class JobRepository (nodeId is null ? string.Empty : " AND (node_id = $1 OR substr(node_id, 1, length($2)) = $2)"), nodeId is null ? [] : [nodeId, nodeId + "#"], cancellationToken); + public async Task SetPriorityAsync(Guid id, JobPriority priority, CancellationToken cancellationToken = default) => + await _db.ExecuteAsync("UPDATE jobs SET priority = $2 WHERE id = $1", [id, (int)priority], cancellationToken).ConfigureAwait(false) > 0; + + /// Queued jobs of one kind in claim order (highest priority, then oldest). + public Task> ListQueuedAsync(JobKind kind, int limit = 50, CancellationToken cancellationToken = default) => + _db.ListAsync( + $"SELECT {Columns} FROM jobs WHERE state = {(int)JobState.Queued} AND kind = $1 ORDER BY priority DESC, created, id LIMIT $2", + [(int)kind, Cursor.ClampLimit(limit)], Map, cancellationToken); + + /// Running jobs claimed by . + public Task> ListRunningByNodeAsync(string nodeId, CancellationToken cancellationToken = default) => + _db.ListAsync($"SELECT {Columns} FROM jobs WHERE state = {(int)JobState.Running} AND node_id = $1 ORDER BY started, id", + [nodeId], Map, cancellationToken); + + /// True when a job of for exactly is waiting in the queue. + public async Task IsQueuedAsync(JobKind kind, Guid targetId, CancellationToken cancellationToken = default) => + await _db.ScalarAsync( + $"SELECT count(*) FROM jobs WHERE state = {(int)JobState.Queued} AND kind = $1 AND target_ids = $2", + [(int)kind, targetId.ToString()], cancellationToken).ConfigureAwait(false) > 0; + + /// + /// Claims one specific queued job for (render nodes pick by capability); null when another + /// claimer was faster or the job is no longer queued. + /// + public Task TryClaimAsync(Guid id, string nodeId, CancellationToken cancellationToken = default) => + _db.InTransactionAsync(async session => + { + var claimed = await session.ExecuteAsync( + $"UPDATE jobs SET state = {(int)JobState.Running}, node_id = $2, started = $3, finished = NULL, error = NULL, attempts = attempts + 1 " + + $"WHERE id = $1 AND state = {(int)JobState.Queued}", + [id, nodeId, DateTimeOffset.UtcNow], cancellationToken).ConfigureAwait(false); + if (claimed == 0) return null; + return await session.QuerySingleOrDefaultAsync($"SELECT {Columns} FROM jobs WHERE id = $1", [id], Map, cancellationToken).ConfigureAwait(false); + }, cancellationToken); + + /// Progress of a job a render node runs; false when the job is no longer running on that node. + public async Task UpdateNodeProgressAsync(Guid id, string nodeId, long done, long total, double? itemsPerSecond, TimeSpan? eta, + string? logTail, CancellationToken cancellationToken = default) => + await _db.ExecuteAsync( + "UPDATE jobs SET progress_done = $3, progress_total = $4, items_per_second = $5, eta_seconds = $6, log_tail = $7 " + + $"WHERE id = $1 AND node_id = $2 AND state = {(int)JobState.Running}", + [id, nodeId, done, total, itemsPerSecond, eta?.TotalSeconds, logTail], cancellationToken).ConfigureAwait(false) > 0; + + /// Ends a job a render node runs (done or failed); false when it is no longer running on that node. + public async Task FinishNodeJobAsync(Guid id, string nodeId, JobState state, string? error, string? logTail, CancellationToken cancellationToken = default) + { + if (state is not (JobState.Done or JobState.Failed)) throw new ArgumentOutOfRangeException(nameof(state), state, "Only done or failed end a node job."); + return await _db.ExecuteAsync( + "UPDATE jobs SET state = $3, error = $4, finished = $5, log_tail = COALESCE($6, log_tail) " + + $"WHERE id = $1 AND node_id = $2 AND state = {(int)JobState.Running}", + [id, nodeId, (int)state, error, DateTimeOffset.UtcNow, logTail], cancellationToken).ConfigureAwait(false) > 0; + } + + /// Queue and history counters for dashboards; finished counts are those since . + public Task CountAsync(DateTimeOffset finishedSince, CancellationToken cancellationToken = default) => + _db.RunAsync(async session => + { + var row = await session.QuerySingleAsync( + "SELECT " + + $"COALESCE(SUM(CASE WHEN state = {(int)JobState.Queued} THEN 1 ELSE 0 END), 0), " + + $"COALESCE(SUM(CASE WHEN state = {(int)JobState.Running} THEN 1 ELSE 0 END), 0), " + + $"COALESCE(SUM(CASE WHEN state = {(int)JobState.Paused} THEN 1 ELSE 0 END), 0), " + + $"COALESCE(SUM(CASE WHEN state = {(int)JobState.Done} AND finished >= $1 THEN 1 ELSE 0 END), 0), " + + $"COALESCE(SUM(CASE WHEN state = {(int)JobState.Failed} AND finished >= $1 THEN 1 ELSE 0 END), 0) " + + "FROM jobs", [finishedSince], cancellationToken).ConfigureAwait(false); + return new JobCounts(row.GetInt32(0), row.GetInt32(1), row.GetInt32(2), row.GetInt32(3), row.GetInt32(4)); + }, cancellationToken); + private static Job Map(DbRow r) => new() { Id = r.GetGuid(0), diff --git a/src/Titano.Data/Repositories/RenderNodeRepository.cs b/src/Titano.Data/Repositories/RenderNodeRepository.cs index 898f1500..85099532 100644 --- a/src/Titano.Data/Repositories/RenderNodeRepository.cs +++ b/src/Titano.Data/Repositories/RenderNodeRepository.cs @@ -37,6 +37,15 @@ public sealed class RenderNodeRepository public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default) => await _db.ExecuteAsync("DELETE FROM render_nodes WHERE id = $1", [id], cancellationToken).ConfigureAwait(false) > 0; + /// Changes the state only (a node that stopped heart-beating keeps its last heartbeat time). + public async Task SetStateAsync(Guid id, string state, CancellationToken cancellationToken = default) => + await _db.ExecuteAsync("UPDATE render_nodes SET state = $2 WHERE id = $1", [id, state], cancellationToken).ConfigureAwait(false) > 0; + + /// Nodes not in whose last heartbeat is older than . + public Task> ListSilentAsync(DateTimeOffset before, string exceptState, CancellationToken cancellationToken = default) => + _db.ListAsync($"SELECT {Columns} FROM render_nodes WHERE last_heartbeat < $1 AND state <> $2 ORDER BY last_heartbeat, id", + [before, exceptState], Map, cancellationToken); + private static RenderNode Map(DbRow r) => new() { Id = r.GetGuid(0), diff --git a/src/Titano.Data/Repositories/SequenceRepository.cs b/src/Titano.Data/Repositories/SequenceRepository.cs index eb566ded..31619615 100644 --- a/src/Titano.Data/Repositories/SequenceRepository.cs +++ b/src/Titano.Data/Repositories/SequenceRepository.cs @@ -60,6 +60,28 @@ public sealed record ArchiveStats( IReadOnlyList ByFormat, ArchiveHealth Health); +/// Fields a client may change on a sequence; null leaves a field unchanged, an empty title or notes clears it. +public sealed record SequenceEdit +{ + public string? Title { get; init; } + public IReadOnlyList? Tags { get; init; } + public int? Rating { get; init; } + public string? Notes { get; init; } + public string? SettingsJson { get; init; } +} + +public enum SequenceEditOutcome +{ + Updated, + NotFound, + + /// The settings version moved since the client read it. + Conflict, +} + +/// Result of : the updated sequence, or the stored one on conflict. +public sealed record SequenceEditResult(SequenceEditOutcome Outcome, Sequence? Sequence); + public sealed record YearBucket(int Year, int Sequences, long Photos, long Bytes); /// One heatmap cell: sequences and photos whose first capture falls in the month. @@ -175,6 +197,38 @@ public sealed class SequenceRepository return updated; }, cancellationToken); + /// + /// Applies a client edit atomically and bumps settings_version. With + /// the edit is refused (, carrying the stored sequence) when someone else + /// changed the sequence first (BRIEF §14.5). + /// + public Task EditAsync(Guid id, SequenceEdit edit, int? expectedSettingsVersion, CancellationToken cancellationToken = default) => + _db.InTransactionAsync(async session => + { + var current = await session.QuerySingleOrDefaultAsync($"{_select} WHERE s.id = $1{_db.Sql.ForUpdate}", [id], Map, cancellationToken) + .ConfigureAwait(false); + if (current is null) return new SequenceEditResult(SequenceEditOutcome.NotFound, null); + if (expectedSettingsVersion is { } expected && expected != current.SettingsVersion) + return new SequenceEditResult(SequenceEditOutcome.Conflict, current); + var updated = current with + { + Title = edit.Title is null ? current.Title : NullIfEmpty(edit.Title), + Notes = edit.Notes is null ? current.Notes : NullIfEmpty(edit.Notes), + Rating = edit.Rating ?? current.Rating, + SettingsJson = edit.SettingsJson ?? current.SettingsJson, + SettingsVersion = current.SettingsVersion + 1, + UpdatedAt = DateTimeOffset.UtcNow, + }; + await UpsertRowAsync(session, updated, cancellationToken).ConfigureAwait(false); + if (edit.Tags is not null) await ReplaceTagsAsync(session, id, edit.Tags, cancellationToken).ConfigureAwait(false); + var stored = await session.QuerySingleAsync($"{_select} WHERE s.id = $1", [id], cancellationToken).ConfigureAwait(false); + return new SequenceEditResult(SequenceEditOutcome.Updated, Map(stored)); + }, cancellationToken); + + /// Gives every sequence owned by to ; returns how many. + public Task ReassignOwnerAsync(Guid fromUserId, Guid toUserId, CancellationToken cancellationToken = default) => + _db.ExecuteAsync("UPDATE sequences SET owner_user_id = $2 WHERE owner_user_id = $1", [fromUserId, toUserId], cancellationToken); + /// One page of sequences matching . public Task> ListAsync(SequenceQuery query, CancellationToken cancellationToken = default) { @@ -393,6 +447,8 @@ public sealed class SequenceRepository } } + private static string? NullIfEmpty(string value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim(); + /// Camera wall-clock times are stored without zone, whatever kind the caller's value has. internal static DateTime? Wall(DateTime? value) => value is { } v ? DateTime.SpecifyKind(v, DateTimeKind.Unspecified) : null; diff --git a/src/Titano.Data/Repositories/TokenRepository.cs b/src/Titano.Data/Repositories/TokenRepository.cs index 8b88c0fc..1fed9606 100644 --- a/src/Titano.Data/Repositories/TokenRepository.cs +++ b/src/Titano.Data/Repositories/TokenRepository.cs @@ -37,6 +37,22 @@ public sealed class TokenRepository public Task RevokeAllAsync(Guid userId, CancellationToken cancellationToken = default) => _db.ExecuteAsync("UPDATE tokens SET revoked = TRUE WHERE user_id = $1 AND revoked = FALSE", [userId], cancellationToken); + /// Revokes every live token of a user except (password changed in that session). + public Task RevokeAllExceptAsync(Guid userId, string keepTokenHash, CancellationToken cancellationToken = default) => + _db.ExecuteAsync("UPDATE tokens SET revoked = TRUE WHERE user_id = $1 AND token_hash <> $2 AND revoked = FALSE", + [userId, keepTokenHash], cancellationToken); + + /// The most recently used token of every user (last seen time and client), keyed by user id. + public async Task> LatestByUserAsync(CancellationToken cancellationToken = default) + { + var tokens = await _db.ListAsync( + $"SELECT {Columns} FROM tokens t WHERE last_used = (SELECT MAX(last_used) FROM tokens l WHERE l.user_id = t.user_id)", + [], Map, cancellationToken).ConfigureAwait(false); + var latest = new Dictionary(); + foreach (var token in tokens) latest[token.UserId] = token; + return latest; + } + public Task> ListByUserAsync(Guid userId, CancellationToken cancellationToken = default) => _db.ListAsync($"SELECT {Columns} FROM tokens WHERE user_id = $1 ORDER BY last_used DESC", [userId], Map, cancellationToken); diff --git a/src/Titano.Data/Repositories/UploadRepository.cs b/src/Titano.Data/Repositories/UploadRepository.cs index 355511ec..35c0a500 100644 --- a/src/Titano.Data/Repositories/UploadRepository.cs +++ b/src/Titano.Data/Repositories/UploadRepository.cs @@ -36,6 +36,15 @@ public sealed record Upload public DateTimeOffset Created { get; init; } public DateTimeOffset UpdatedAt { get; init; } + /// Automatic grouping key of a frame upload without a target sequence (BRIEF §14.4). + public string? SessionKey { get; init; } + + /// The render job whose output this upload carries; null for frame uploads. + public Guid? JobId { get; init; } + + /// Metadata of the render a render-result upload becomes (JSON). + public string? RenderJson { get; init; } + public int ChunkCount => (int)((Size + ChunkSize - 1) / ChunkSize); public bool IsComplete => ReceivedCount >= ChunkCount; @@ -44,11 +53,15 @@ public sealed record Upload index >= 0 && index / 8 < Received.Length && (Received[index / 8] & (1 << (index % 8))) != 0; } +/// Committed uploads of one UTC day. +public sealed record UploadDay(DateOnly Day, long Count, long Bytes); + /// Resumable uploads: creation, per-chunk bookkeeping, state changes and cleanup. public sealed class UploadRepository { private const string Columns = - "id, user_id, sequence_id, file_name, size, sha256, captured_at, chunk_size, received, received_count, state, frame_id, created, updated_at"; + "id, user_id, sequence_id, file_name, size, sha256, captured_at, chunk_size, received, received_count, state, frame_id, created, updated_at, " + + "session_key, job_id, render_json"; private readonly DbAccess _db; @@ -61,10 +74,11 @@ public sealed class UploadRepository ArgumentOutOfRangeException.ThrowIfNegative(upload.Size); var stored = upload with { Received = new byte[(upload.ChunkCount + 7) / 8], ReceivedCount = 0, Sha256 = upload.Sha256.ToLowerInvariant() }; await _db.ExecuteAsync( - $"INSERT INTO uploads ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)", + $"INSERT INTO uploads ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)", [ stored.Id, stored.UserId, stored.SequenceId, stored.FileName, stored.Size, stored.Sha256, SequenceRepository.Wall(stored.CapturedAt), stored.ChunkSize, stored.Received, 0, (int)stored.State, stored.FrameId, stored.Created, stored.UpdatedAt, + stored.SessionKey, stored.JobId, DbParam.Json(stored.RenderJson), ], cancellationToken).ConfigureAwait(false); return stored; } @@ -72,10 +86,11 @@ public sealed class UploadRepository public Task GetAsync(Guid id, CancellationToken cancellationToken = default) => _db.SingleAsync($"SELECT {Columns} FROM uploads WHERE id = $1", [id], Map, cancellationToken); - /// An upload of the same content by the same user that is still receiving, so a client can resume it. + /// A frame upload of the same content by the same user that is still receiving, so a client can resume it. public Task FindReceivingAsync(Guid userId, string sha256, CancellationToken cancellationToken = default) => _db.SingleAsync( - $"SELECT {Columns} FROM uploads WHERE user_id = $1 AND sha256 = $2 AND state = {(int)UploadState.Receiving} ORDER BY created DESC LIMIT 1", + $"SELECT {Columns} FROM uploads WHERE user_id = $1 AND sha256 = $2 AND state = {(int)UploadState.Receiving} AND job_id IS NULL " + + "ORDER BY created DESC LIMIT 1", [userId, sha256.ToLowerInvariant()], Map, cancellationToken); /// @@ -115,6 +130,54 @@ public sealed class UploadRepository public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default) => await _db.ExecuteAsync("DELETE FROM uploads WHERE id = $1", [id], cancellationToken).ConfigureAwait(false) > 0; + /// + /// Moves an upload from to only if it is still in , + /// so two concurrent completions cannot both commit it. Returns false when the state had changed. + /// + public async Task TryTransitionAsync(Guid id, UploadState from, UploadState to, Guid? frameId = null, CancellationToken cancellationToken = default) => + await _db.ExecuteAsync("UPDATE uploads SET state = $3, frame_id = COALESCE($4, frame_id), updated_at = $5 WHERE id = $1 AND state = $2", + [id, (int)from, (int)to, frameId, DateTimeOffset.UtcNow], cancellationToken).ConfigureAwait(false) > 0; + + /// The newest upload carrying the output of render job . + public Task FindByJobAsync(Guid jobId, CancellationToken cancellationToken = default) => + _db.SingleAsync($"SELECT {Columns} FROM uploads WHERE job_id = $1 ORDER BY created DESC, id DESC LIMIT 1", [jobId], Map, cancellationToken); + + /// Every upload in , oldest change first. + public Task> ListByStateAsync(UploadState state, CancellationToken cancellationToken = default) => + _db.ListAsync($"SELECT {Columns} FROM uploads WHERE state = $1 ORDER BY updated_at, id", [(int)state], Map, cancellationToken); + + /// Bytes a user has announced in frame uploads that are not committed yet (reserved against the quota). + public async Task PendingBytesAsync(Guid userId, CancellationToken cancellationToken = default) => + await _db.ScalarAsync( + $"SELECT COALESCE(SUM(size), 0) FROM uploads WHERE user_id = $1 AND job_id IS NULL AND state IN ({(int)UploadState.Receiving}, {(int)UploadState.Committing})", + [userId], cancellationToken).ConfigureAwait(false); + + /// Committed frame uploads per UTC day since , oldest day first. + public Task> CommittedPerDayAsync(DateTimeOffset since, CancellationToken cancellationToken = default) + { + var day = _db.Sql.IsPostgres ? "to_char(updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD')" : "substr(updated_at, 1, 10)"; + return _db.ListAsync( + $"SELECT {day}, count(*), COALESCE(SUM(size), 0) FROM uploads WHERE state = {(int)UploadState.Committed} AND job_id IS NULL AND updated_at >= $1 " + + "GROUP BY 1 ORDER BY 1", + [since], r => new UploadDay(DateOnly.ParseExact(r.GetString(0), "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture), r.GetInt64(1), r.GetInt64(2)), + cancellationToken); + } + + /// Committed frame uploads per user since . + public async Task> CommittedCountByUserAsync(DateTimeOffset since, CancellationToken cancellationToken = default) + { + var rows = await _db.ListAsync( + $"SELECT user_id, count(*) FROM uploads WHERE state = {(int)UploadState.Committed} AND job_id IS NULL AND updated_at >= $1 GROUP BY user_id", + [since], r => KeyValuePair.Create(r.GetGuid(0), r.GetInt64(1)), cancellationToken).ConfigureAwait(false); + return new Dictionary(rows); + } + + /// Deletes committed, failed and cancelled uploads last changed before ; returns how many. + public Task PurgeFinishedAsync(DateTimeOffset updatedBefore, CancellationToken cancellationToken = default) => + _db.ExecuteAsync( + $"DELETE FROM uploads WHERE state IN ({(int)UploadState.Committed}, {(int)UploadState.Failed}, {(int)UploadState.Cancelled}) AND updated_at < $1", + [updatedBefore], cancellationToken); + private static Upload Map(DbRow r) => new() { Id = r.GetGuid(0), @@ -131,5 +194,8 @@ public sealed class UploadRepository FrameId = r.GetGuidOrNull(11), Created = r.GetDateTimeOffset(12), UpdatedAt = r.GetDateTimeOffset(13), + SessionKey = r.GetStringOrNull(14), + JobId = r.GetGuidOrNull(15), + RenderJson = r.GetStringOrNull(16), }; } diff --git a/src/Titano.Protocol/Admin.cs b/src/Titano.Protocol/Admin.cs new file mode 100644 index 00000000..17abbd28 --- /dev/null +++ b/src/Titano.Protocol/Admin.cs @@ -0,0 +1,53 @@ +namespace Titano.Protocol; + +/// +/// GET/PUT /api/v1/server/settings (admin): the editable part of the server configuration. Settings named in +/// come from environment variables and cannot be changed here; says +/// that saved values (ports, TLS, workers, chunk size) wait for a restart. +/// +public sealed record ServerSettingsDto( + string ServerName, + int HttpPort, + int HttpsPort, + string TlsMode, + string? TlsCert, + string? TlsKey, + string Language, + int Workers, + bool Discovery, + long MaxUploadBytes, + int ChunkSizeBytes, + int JobHistoryDays, + int TokenLifetimeDays, + int UploadRetentionHours, + IReadOnlyList Locked, + bool RestartRequired); + +/// A server log file (GET /api/v1/logs/files). +public sealed record LogFileDto(string Name, long Size, DateTimeOffset Modified); + +/// The last lines of the current server log (GET /api/v1/logs). +public sealed record LogTailDto(string File, IReadOnlyList Lines); + +/// Jobs per state; done and failed count today's (UTC) jobs. +public sealed record JobCountsDto(int Queued, int Running, int Paused, int DoneToday, int FailedToday); + +/// Frames committed by uploads on one UTC day. +public sealed record UploadDayDto(DateOnly Day, long Frames, long Bytes); + +/// GET /api/v1/server/dashboard (admin): everything the dashboard page shows in one call (BRIEF §14.8). +public sealed record DashboardDto( + string ServerName, + string Version, + long UptimeSeconds, + string Database, + string TlsMode, + bool Discovery, + int Users, + ArchiveStatsDto Archive, + JobCountsDto Jobs, + int Workers, + IReadOnlyList Nodes, + IReadOnlyList Uploads, + IReadOnlyList Activity, + IReadOnlyList ActiveJobs); diff --git a/src/Titano.Protocol/Auth.cs b/src/Titano.Protocol/Auth.cs index 077009c1..fdf07086 100644 --- a/src/Titano.Protocol/Auth.cs +++ b/src/Titano.Protocol/Auth.cs @@ -29,3 +29,21 @@ public sealed record UpdateUserRequest(string? DisplayName, string? Role, long? /// Response of POST /api/v1/users and /users/{id}/reset-password: a temporary password shown once. public sealed record TemporaryPasswordResponse(UserDto User, string TemporaryPassword); + +/// POST /api/v1/users/{id}/reset-password (admin); without a password the server generates one. +public sealed record ResetPasswordRequest(string? Password); + +/// One row of the admin user list: the account plus when and from which client it was last used. +public sealed record UserSummaryDto(UserDto User, DateTimeOffset? LastSeen, string? LastClient, long UploadsLast30Days); + +/// One audit log entry (who did what to which user, sequence or setting). +public sealed record AuditEntryDto( + Guid Id, + DateTimeOffset At, + Guid? UserId, + string? Username, + string Action, + string? TargetKind, + string? TargetId, + string? Details, + string? RemoteAddress); diff --git a/src/Titano.Protocol/Client/TitanoClient.cs b/src/Titano.Protocol/Client/TitanoClient.cs index d8f57f51..d843ca82 100644 --- a/src/Titano.Protocol/Client/TitanoClient.cs +++ b/src/Titano.Protocol/Client/TitanoClient.cs @@ -154,37 +154,59 @@ public sealed class TitanoClient : IDisposable } var uploadId = start.UploadId ?? throw new InvalidOperationException("Server returned no upload id."); - var chunkSize = start.ChunkSize; - var received = new HashSet(start.Received); - var chunks = (int)((info.Length + chunkSize - 1) / chunkSize); - var buffer = new byte[chunkSize]; - await using (var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1 << 16, FileOptions.Asynchronous | FileOptions.SequentialScan)) - { - long sent = received.Count == 0 ? 0 : received.Sum(i => (long)Math.Min(chunkSize, info.Length - (long)i * chunkSize)); - progress?.Report(new UploadProgress(sent, info.Length)); - for (var n = 0; n < chunks; n++) - { - if (received.Contains(n)) continue; - var offset = (long)n * chunkSize; - var length = (int)Math.Min(chunkSize, info.Length - offset); - file.Position = offset; - await file.ReadExactlyAsync(buffer.AsMemory(0, length), ct).ConfigureAwait(false); - await Bandwidth.AcquireAsync(length, ct).ConfigureAwait(false); - await RetryAsync(async () => - { - using var content = new ByteArrayContent(buffer, 0, length); - content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); - using var response = await _http.PutAsync($"api/v1/uploads/{uploadId}/chunks/{n}", content, ct).ConfigureAwait(false); - await EnsureSuccessAsync(response, ct).ConfigureAwait(false); - return true; - }, ct).ConfigureAwait(false); - sent += length; - progress?.Report(new UploadProgress(sent, info.Length)); - } - } + await SendChunksAsync(path, info.Length, uploadId, start.ChunkSize, start.Received, progress, ct).ConfigureAwait(false); return await RetryAsync(() => SendAsync(HttpMethod.Post, $"api/v1/uploads/{uploadId}/complete", ProtocolJson.Default.CompleteUploadResponse, ct), ct).ConfigureAwait(false); } + /// + /// Render node: uploads the output of a claimed job in resumable chunks, then completes the job; the server verifies + /// the SHA-256 in and files the render under the sequence's exports (BRIEF §14.7). + /// + public async Task UploadRenderResultAsync(Guid nodeId, Guid jobId, string path, RenderResultRequest result, + IProgress? progress = null, CancellationToken ct = default) + { + var length = new FileInfo(path).Length; + if (length != result.Size) throw new ArgumentException("The result size does not match the file.", nameof(result)); + var start = await RetryAsync(() => SendAsync(HttpMethod.Post, $"api/v1/nodes/{nodeId}/jobs/{jobId}/result", result, + ProtocolJson.Default.RenderResultRequest, ProtocolJson.Default.StartUploadResponse, ct), ct).ConfigureAwait(false); + var uploadId = start.UploadId ?? throw new InvalidOperationException("Server returned no upload id."); + await SendChunksAsync(path, length, uploadId, start.ChunkSize, start.Received, progress, ct).ConfigureAwait(false); + return await RetryAsync(() => SendAsync(HttpMethod.Post, $"api/v1/nodes/{nodeId}/jobs/{jobId}/result/complete", ProtocolJson.Default.RenderDto, ct), ct) + .ConfigureAwait(false); + } + + /// Sends every chunk the server does not have yet; each PUT is idempotent and retried on transient errors. + private async Task SendChunksAsync(string path, long length, Guid uploadId, int chunkSize, IReadOnlyList alreadyReceived, + IProgress? progress, CancellationToken ct) + { + var received = new HashSet(alreadyReceived); + var chunks = (int)((length + chunkSize - 1) / chunkSize); + var buffer = new byte[chunkSize]; + await using var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1 << 16, FileOptions.Asynchronous | FileOptions.SequentialScan); + long sent = received.Sum(i => Math.Min(chunkSize, length - (long)i * chunkSize)); + progress?.Report(new UploadProgress(sent, length)); + for (var n = 0; n < chunks; n++) + { + if (received.Contains(n)) continue; + var offset = (long)n * chunkSize; + var size = (int)Math.Min(chunkSize, length - offset); + file.Position = offset; + await file.ReadExactlyAsync(buffer.AsMemory(0, size), ct).ConfigureAwait(false); + await Bandwidth.AcquireAsync(size, ct).ConfigureAwait(false); + var index = n; + await RetryAsync(async () => + { + using var content = new ByteArrayContent(buffer, 0, size); + content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); + using var response = await _http.PutAsync($"api/v1/uploads/{uploadId}/chunks/{index}", content, ct).ConfigureAwait(false); + await EnsureSuccessAsync(response, ct).ConfigureAwait(false); + return true; + }, ct).ConfigureAwait(false); + sent += size; + progress?.Report(new UploadProgress(sent, length)); + } + } + /// Lower-case hex SHA-256 of a file, streamed. public static async Task HashFileAsync(string path, CancellationToken ct = default) { @@ -201,10 +223,22 @@ public sealed class TitanoClient : IDisposable public Task CreateJobAsync(CreateJobRequest request, CancellationToken ct = default) => SendAsync(HttpMethod.Post, "api/v1/jobs", request, ProtocolJson.Default.CreateJobRequest, ProtocolJson.Default.JobDto, ct); + public Task GetJobAsync(Guid id, CancellationToken ct = default) => GetAsync($"api/v1/jobs/{id}", ProtocolJson.Default.JobDto, ct); + public Task CancelJobAsync(Guid id, CancellationToken ct = default) => SendAsync(HttpMethod.Post, $"api/v1/jobs/{id}/cancel", ProtocolJson.Default.JobDto, ct); public Task RetryJobAsync(Guid id, CancellationToken ct = default) => SendAsync(HttpMethod.Post, $"api/v1/jobs/{id}/retry", ProtocolJson.Default.JobDto, ct); + public Task PauseJobAsync(Guid id, CancellationToken ct = default) => SendAsync(HttpMethod.Post, $"api/v1/jobs/{id}/pause", ProtocolJson.Default.JobDto, ct); + + public Task ResumeJobAsync(Guid id, CancellationToken ct = default) => SendAsync(HttpMethod.Post, $"api/v1/jobs/{id}/resume", ProtocolJson.Default.JobDto, ct); + + /// Moves a waiting job up or down the queue: is background, normal or user. + public Task SetJobPriorityAsync(Guid id, string priority, CancellationToken ct = default) => + SendAsync(HttpMethod.Post, $"api/v1/jobs/{id}/priority", new JobPriorityRequest(priority), ProtocolJson.Default.JobPriorityRequest, ProtocolJson.Default.JobDto, ct); + + public Task> ListNodesAsync(CancellationToken ct = default) => GetAsync("api/v1/nodes", ProtocolJson.Default.ListRenderNodeDto, ct); + public Task RegisterNodeAsync(RegisterNodeRequest request, CancellationToken ct = default) => SendAsync(HttpMethod.Post, "api/v1/nodes/register", request, ProtocolJson.Default.RegisterNodeRequest, ProtocolJson.Default.RenderNodeDto, ct); @@ -222,6 +256,10 @@ public sealed class TitanoClient : IDisposable public Task ReportNodeProgressAsync(Guid nodeId, Guid jobId, NodeProgress progress, CancellationToken ct = default) => SendAsync(HttpMethod.Post, $"api/v1/nodes/{nodeId}/jobs/{jobId}/progress", progress, ProtocolJson.Default.NodeProgress, ct); + /// Gives a claimed job back as failed; a 409 means it was cancelled or reassigned meanwhile. + public Task ReportNodeFailureAsync(Guid nodeId, Guid jobId, string error, CancellationToken ct = default) => + SendAsync(HttpMethod.Post, $"api/v1/nodes/{nodeId}/jobs/{jobId}/failed", new NodeJobFailure(error), ProtocolJson.Default.NodeJobFailure, ct); + // ---------------- Events ---------------- /// Server events over WebSocket until cancelled; reconnect is the caller's policy. diff --git a/src/Titano.Protocol/Dtos.cs b/src/Titano.Protocol/Dtos.cs index d841902d..52934196 100644 --- a/src/Titano.Protocol/Dtos.cs +++ b/src/Titano.Protocol/Dtos.cs @@ -5,8 +5,8 @@ namespace Titano.Protocol; /// Error body of every failed API call: { code, message, details } (BRIEF §14.3). public sealed record ApiError(string Code, string Message, string? Details = null); -/// Upload limits announced by the server. -public sealed record ServerLimits(long MaxUploadBytes, int ChunkSizeBytes); +/// Upload and account limits announced by the server. +public sealed record ServerLimits(long MaxUploadBytes, int ChunkSizeBytes, int PasswordMinLength = 10); /// GET /api/v1/server/info. public sealed record ServerInfo( diff --git a/src/Titano.Protocol/Jobs.cs b/src/Titano.Protocol/Jobs.cs index e5655af4..6a6d8e5c 100644 --- a/src/Titano.Protocol/Jobs.cs +++ b/src/Titano.Protocol/Jobs.cs @@ -34,3 +34,27 @@ public sealed record ClaimedJob(JobDto Job, Guid SequenceId, string SettingsJson /// POST /api/v1/nodes/{id}/jobs/{jobId}/progress. public sealed record NodeProgress(long Done, long Total, double? ItemsPerSecond, string? LogLine); + +/// POST /api/v1/jobs/{id}/priority: background, normal or user. +public sealed record JobPriorityRequest(string Priority); + +/// +/// parametersJson of a render job: what to encode. Render nodes claim the job when they have an encoder for +/// at × (BRIEF §14.7). +/// +public sealed record RenderJobParameters(string PresetName, string PresetJson, string Codec, int Width, int Height, bool UseProxies, string? SettingsJson); + +/// POST /api/v1/nodes/{id}/jobs/{jobId}/result: the finished output a node is about to upload in chunks. +public sealed record RenderResultRequest( + string FileName, + long Size, + string Sha256, + string PresetName, + string? Codec, + int Width, + int Height, + double Fps, + double DurationSeconds); + +/// POST /api/v1/nodes/{id}/jobs/{jobId}/failed: the node could not render the job. +public sealed record NodeJobFailure(string Error); diff --git a/src/Titano.Protocol/ProtocolJson.cs b/src/Titano.Protocol/ProtocolJson.cs index 2f3dad46..2db29cac 100644 --- a/src/Titano.Protocol/ProtocolJson.cs +++ b/src/Titano.Protocol/ProtocolJson.cs @@ -45,4 +45,21 @@ namespace Titano.Protocol; [JsonSerializable(typeof(NodeProgress))] [JsonSerializable(typeof(ServerEvent))] [JsonSerializable(typeof(DiscoveryBeacon))] +[JsonSerializable(typeof(ResetPasswordRequest))] +[JsonSerializable(typeof(UserSummaryDto))] +[JsonSerializable(typeof(List))] +[JsonSerializable(typeof(AuditEntryDto))] +[JsonSerializable(typeof(Page))] +[JsonSerializable(typeof(SequenceStatsDto))] +[JsonSerializable(typeof(SequenceDeletePlan))] +[JsonSerializable(typeof(ArchiveSettingsDto))] +[JsonSerializable(typeof(JobPriorityRequest))] +[JsonSerializable(typeof(RenderJobParameters))] +[JsonSerializable(typeof(RenderResultRequest))] +[JsonSerializable(typeof(NodeJobFailure))] +[JsonSerializable(typeof(ServerSettingsDto))] +[JsonSerializable(typeof(LogFileDto))] +[JsonSerializable(typeof(List))] +[JsonSerializable(typeof(LogTailDto))] +[JsonSerializable(typeof(DashboardDto))] public sealed partial class ProtocolJson : JsonSerializerContext; diff --git a/src/Titano.Protocol/Sequences.cs b/src/Titano.Protocol/Sequences.cs index ab3bb730..eaebcc0d 100644 --- a/src/Titano.Protocol/Sequences.cs +++ b/src/Titano.Protocol/Sequences.cs @@ -102,3 +102,26 @@ public sealed record ArchiveStatsDto( int UnverifiedSequences); public sealed record KeyValueCount(string Key, long Count, long Bytes); + +/// GET /api/v1/sequences/{id}/stats: frame totals by format and flagged frames. +public sealed record SequenceStatsDto( + Guid SequenceId, + int FrameCount, + long TotalBytes, + DateTime? FirstCapture, + DateTime? LastCapture, + double? MedianInterval, + IReadOnlyList ByFormat, + long MissingFrames, + long CorruptFrames, + long DuplicateFrames, + long ExcludedFrames); + +/// +/// DELETE /api/v1/sequences/{id}: what the delete does (with ?dryRun=true) or did. The folder is moved +/// to inside the archive, never erased. +/// +public sealed record SequenceDeletePlan(Guid SequenceId, string? Folder, string? TrashFolder, int FrameCount, long TotalBytes, int RenderCount, bool DryRun); + +/// GET/PUT /api/v1/archive/settings (admin). The root path comes from the server configuration. +public sealed record ArchiveSettingsDto(Guid Id, string Name, string RootPath, string FolderTemplate, string FileTemplate, int ProxyLongEdge); diff --git a/src/Titano.Server/Admin/web/admin.css b/src/Titano.Server/Admin/web/admin.css index ee54015d..feb6c1a8 100644 --- a/src/Titano.Server/Admin/web/admin.css +++ b/src/Titano.Server/Admin/web/admin.css @@ -1,4 +1,4 @@ -/* Titano Server admin — DESIGN.md light tokens (the web admin is light-only). */ +/* Titano Server admin — DESIGN.md light tokens (the web admin is light-only), mockups Admin.html and AdminUsers.html. */ :root { --bg: #f3f3f3; --chrome: #ffffff; @@ -15,12 +15,14 @@ --on-accent: #ffffff; --accent-selected: #e8f1fa; --sun: #e08a1e; + --sun-soft: #fff4e5; --success: #0f7b4f; --warning: #9a6700; --error: #c42b1c; --radius: 6px; --radius-card: 8px; --font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "Noto Sans", sans-serif; + --mono: Consolas, "Cascadia Mono", ui-monospace, monospace; } * { box-sizing: border-box; } @@ -28,73 +30,190 @@ html, body { height: 100%; } body { margin: 0; font: 13px/1.45 var(--font); color: var(--text); background: var(--bg); } button, input, select { font: inherit; color: inherit; } a { color: var(--accent); } +a:hover { color: #004c8c; } +h1, h2 { margin: 0; } +h2 { font-size: 14px; font-weight: 600; } .num { font-variant-numeric: tabular-nums; } +.mono { font-family: var(--mono); } +.muted { color: var(--text-muted); } +.right { text-align: right; justify-self: end; } +.nowrap { white-space: nowrap; } +.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; } +.spacer { flex: 1; } +[hidden] { display: none !important; } /* Shell */ .shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; } -.sidebar { background: var(--chrome); border-right: 1px solid var(--stroke); display: flex; flex-direction: column; padding: 16px 12px; } -.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; } -.brand img { width: 28px; height: 28px; } +.sidebar { background: var(--chrome); border-right: 1px solid var(--stroke); display: flex; flex-direction: column; padding: 16px 12px; position: sticky; top: 0; height: 100vh; } +.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; } +.brand img { width: 22px; height: 22px; } .brand strong { display: block; font-size: 14px; } .brand span { display: block; font-size: 11px; color: var(--text-muted); } -.nav a { display: flex; align-items: center; gap: 12px; height: 36px; padding: 0 12px; border-radius: var(--radius); color: var(--text); text-decoration: none; margin-bottom: 2px; } +.brand.large img { width: 40px; height: 40px; } +.brand.large strong { font-size: 18px; } +.nav { display: grid; gap: 2px; } +.nav a { display: flex; align-items: center; gap: 10px; height: 36px; padding: 0 10px; border-radius: var(--radius); color: var(--text); text-decoration: none; } .nav a:hover { background: var(--card-hover); } .nav a[aria-current="page"] { background: var(--accent-selected); color: var(--accent); font-weight: 600; } -.nav svg { width: 16px; height: 16px; flex: none; } -.nav .badge { margin-left: auto; min-width: 18px; height: 16px; padding: 0 5px; border-radius: 8px; background: var(--accent); color: var(--on-accent); font-size: 10px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; } -.sidebar .user { margin-top: auto; border-top: 1px solid var(--stroke); padding: 12px 8px 0; display: flex; align-items: center; gap: 10px; } -.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent-selected); color: var(--accent); display: grid; place-items: center; font-weight: 600; } +.nav svg { flex: none; } +.badge { margin-left: auto; min-width: 18px; padding: 0 6px; border-radius: 8px; background: var(--accent); color: var(--on-accent); font-size: 11px; font-weight: 600; text-align: center; } +.badge.quiet { background: none; color: var(--text-muted); font-weight: 400; } +.sidebar .user { margin-top: auto; border-top: 1px solid var(--stroke); padding: 8px 10px 0; display: flex; align-items: center; gap: 10px; } .user .who { flex: 1; min-width: 0; } .user .who strong { display: block; } .user .who span { color: var(--text-muted); font-size: 11px; } .main { display: flex; flex-direction: column; min-width: 0; } -.header { height: 60px; display: flex; align-items: center; gap: 12px; padding: 0 24px; border-bottom: 1px solid var(--stroke); background: var(--chrome); } -.header h1 { font-size: 18px; font-weight: 600; margin: 0; } +.header { min-height: 60px; display: flex; align-items: center; gap: 14px; padding: 0 24px; border-bottom: 1px solid var(--stroke); background: var(--chrome); flex-wrap: wrap; } +.header h1 { font-size: 18px; font-weight: 600; } .header .meta { color: var(--text-muted); } -.header .spacer { flex: 1; } -.content { padding: 24px; display: grid; gap: 20px; } +.header .actions { display: flex; align-items: center; gap: 8px; } +.header .language { width: auto; } +.content { padding: 24px; display: grid; gap: 20px; align-content: start; } /* Controls */ -.btn { height: 32px; min-width: 44px; padding: 0 12px; border: 1px solid var(--stroke); border-radius: var(--radius); background: var(--card); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px; } +.btn { height: 32px; min-width: 44px; padding: 0 12px; border: 1px solid var(--stroke); border-radius: var(--radius); background: var(--card); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; color: var(--text); white-space: nowrap; } .btn:hover { background: var(--card-hover); border-color: var(--stroke-strong); } -.btn:focus-visible, .input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; } +.btn:focus-visible, .input:focus-visible, .chip:focus-visible, .link:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; } .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; } .btn.primary:hover { background: var(--accent-hover); } +.btn.destructive { border-color: var(--error); color: var(--error); background: transparent; } .btn.subtle { border-color: transparent; background: transparent; } -.btn.icon { width: 32px; padding: 0; } +.btn.icon { width: 32px; min-width: 32px; padding: 0; } +.btn.small { height: 28px; padding: 0 10px; font-size: 12px; } .btn:disabled { opacity: .4; cursor: default; } +.btn[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); } +.link { border: 0; background: none; padding: 0; color: var(--text); cursor: pointer; text-align: left; } +.link:hover { color: var(--accent); } .input { height: 32px; width: 100%; padding: 0 10px; border: 1px solid var(--stroke); border-radius: var(--radius); background: var(--card); } .input:hover { border-color: var(--stroke-strong); } .input:focus { border-color: var(--accent); outline: none; } .input[aria-invalid="true"] { border-color: var(--error); } -select.input { padding-right: 24px; } +.input:disabled, .input[readonly] { background: var(--bg); color: var(--text-muted); } +.input.small { height: 28px; width: auto; font-size: 12px; } +.input.narrow { width: 120px; } +.input.search { width: 220px; } +input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); margin: 0; } +.chips, .button-row { display: flex; gap: 8px; flex-wrap: wrap; } +.chip { height: 24px; padding: 0 10px; border: 1px solid var(--stroke); border-radius: 12px; background: var(--card); font-size: 11px; cursor: pointer; } +.chip[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); } +.chip.warn { border-color: var(--warning); color: var(--warning); cursor: default; display: inline-flex; align-items: center; } +.segmented { display: inline-flex; } +.segmented .btn { border-radius: 0; margin-left: -1px; } +.segmented .btn:first-child { border-radius: var(--radius) 0 0 var(--radius); } +.segmented .btn:last-child { border-radius: 0 var(--radius) var(--radius) 0; } -.panel { background: var(--surface); border: 1px solid var(--stroke); border-radius: var(--radius-card); padding: 18px 20px; } -.panel h2 { font-size: 14px; font-weight: 600; margin: 0 0 12px; } -.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; } -.tile .label { color: var(--text-muted); } -.tile .value { font-size: 28px; font-weight: 600; margin-top: 4px; } -.tile .value small { font-size: 13px; font-weight: 400; color: var(--text-muted); margin-left: 6px; } -.tile .sub { color: var(--text-muted); margin-top: 6px; } -.progress { height: 6px; border-radius: 3px; background: var(--stroke); overflow: hidden; margin-top: 10px; } -.progress > div { height: 6px; background: var(--accent); } +/* Panels and tiles */ +.panel { background: var(--surface); border: 1px solid var(--stroke); border-radius: var(--radius-card); padding: 16px 18px; display: grid; gap: 10px; align-content: start; min-width: 0; } +.panel.flush { padding: 0; gap: 0; overflow-x: auto; } +.panel-head { display: flex; align-items: center; gap: 12px; } +.panel-head h2 { flex: 1; } +.panel-head.padded { padding: 12px 18px; border-bottom: 1px solid var(--stroke); } +.empty { padding: 24px 18px; color: var(--text-muted); } +.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 20px; } +.tile { gap: 8px; } +.tile .label, .tile .sub { font-size: 12px; color: var(--text-muted); } +.tile .value { font-size: 26px; font-weight: 600; display: flex; align-items: baseline; gap: 6px; } +.tile .value small { font-size: 13px; font-weight: 400; color: var(--text-muted); } +.grid { display: grid; gap: 20px; } +.grid.two { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); } +.grid.wide-right { grid-template-columns: minmax(300px, 1fr) minmax(400px, 1.4fr); } -.status-ok { color: var(--success); } -.status-warn { color: var(--warning); } -.status-err { color: var(--error); } +/* Bars and charts (SVG attributes only: the panel runs under a strict CSP) */ +svg.bar { width: 100%; height: 6px; display: block; } +svg.bar .track { fill: var(--stroke); } +svg.bar .fill { fill: var(--accent); } +svg.bar.muted .fill { fill: var(--text-faint); } +svg.chart { width: 100%; height: auto; max-height: 220px; } +svg.chart .axis { stroke: var(--stroke); } +svg.chart .column { fill: var(--accent); } +svg.chart.sun .column { fill: var(--sun); } +svg.chart text { font: 10px var(--font); text-anchor: middle; } +svg.chart .value { fill: var(--text); } +svg.chart .label { fill: var(--text-muted); } +.progress-cell { display: flex; align-items: center; gap: 8px; min-width: 0; } +.progress-cell svg.bar { flex: 1; min-width: 40px; } + +/* Tables (grid rows, 52 px in the admin) */ +.table { display: grid; min-width: 0; } +.table .thead, .table .row { display: grid; gap: 12px; align-items: center; padding: 0 18px; } +.table .thead { font-size: 11px; color: var(--text-muted); padding-top: 10px; padding-bottom: 10px; border-bottom: 1px solid var(--stroke); } +.table .row { min-height: 52px; border-bottom: 1px solid var(--stroke); } +.table .row:hover { background: var(--card-hover); } +.table .row.dim { color: var(--text-muted); } +.table.compact .row { min-height: 32px; padding: 0; grid-template-columns: 1fr auto auto auto; border-bottom: 0; } +.table.compact .row:hover { background: none; } +.table.users .thead, .table.users .row { grid-template-columns: minmax(110px, 1.4fr) 90px minmax(100px, 1.2fr) minmax(80px, 1fr) 70px 70px 230px; } +.table.jobs .thead, .table.jobs .row { grid-template-columns: 110px minmax(140px, 1.5fr) 100px minmax(110px, 1fr) 100px 80px 280px; } +.table.jobs-mini .thead, .table.jobs-mini .row { grid-template-columns: 96px minmax(120px, 1.5fr) 92px minmax(100px, 1fr) 90px 80px; padding: 0; min-height: 40px; } +.table.jobs-mini .row { border-bottom: 0; } +.table.nodes .thead, .table.nodes .row { grid-template-columns: minmax(140px, 1fr) 120px minmax(140px, 1fr) 170px 100px 100px; } +.table.audit .thead, .table.audit .row { grid-template-columns: 170px 120px 170px minmax(160px, 1fr) 130px; min-height: 40px; } +.table-foot { display: flex; align-items: center; gap: 16px; padding: 10px 18px; font-size: 12px; } +.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; } +.person { display: flex; align-items: center; gap: 10px; min-width: 0; } +.stack { display: flex; flex-direction: column; min-width: 0; } +.stack small { font-size: 11px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } +.avatar { width: 30px; height: 30px; flex: none; border-radius: 50%; background: var(--accent-selected); color: var(--accent); display: grid; place-items: center; font-weight: 600; } +.avatar.uploader { background: var(--sun-soft); color: var(--warning); } +.row.dim .avatar { background: #eeeeee; color: var(--text-muted); } +.state { white-space: nowrap; } +.state.running { color: var(--accent); } +.state.done { color: var(--success); } +.state.failed { color: var(--error); } +.state.paused { color: var(--warning); } +.state.queued, .state.cancelled { color: var(--text-muted); } +.activity { display: grid; grid-template-columns: 64px 1fr; column-gap: 12px; row-gap: 9px; font-size: 12px; } +pre.log { margin: 0; padding: 12px 18px; background: var(--bg); border-top: 1px solid var(--stroke); font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow: auto; max-height: 260px; } +pre.log.tall { max-height: 60vh; border-top: 0; } + +/* Forms */ +.split { display: grid; grid-template-columns: 1fr auto; gap: 20px; align-items: start; } +.side { width: 340px; } +.form { display: grid; gap: 12px; } +.form label { display: grid; gap: 6px; color: var(--text-muted); } +.form label.check, label.check { display: flex; align-items: center; gap: 8px; color: var(--text); } +.form fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 8px; } +.form legend { color: var(--text-muted); margin-bottom: 8px; } +.form h2 { font-size: 16px; } +.inline { display: flex; align-items: center; gap: 8px; } +.buttons { display: flex; gap: 8px; justify-content: flex-end; } +.error { color: var(--error); min-height: 18px; } +.settings { display: grid; gap: 20px; } +.field { display: grid; grid-template-columns: 180px minmax(0, 420px); align-items: center; gap: 12px; color: var(--text-muted); } +.settings-form .field { grid-template-columns: 140px minmax(0, 1fr); } +.fingerprint { font: 12px var(--mono); color: var(--text); overflow-wrap: anywhere; } + +/* Dialogs and toasts */ +dialog.dialog { border: 1px solid var(--stroke); border-radius: 12px; padding: 20px 22px; width: 420px; max-width: calc(100vw - 32px); box-shadow: 0 8px 24px rgba(0, 0, 0, .18); } +dialog.dialog::backdrop { background: rgba(0, 0, 0, .25); } +dialog.dialog form { display: grid; gap: 14px; } +dialog.dialog h2 { font-size: 16px; } +dialog.dialog p { margin: 0; color: var(--text-muted); } +#toasts { position: fixed; right: 24px; bottom: 24px; display: grid; gap: 8px; z-index: 10; } +.toast { width: 420px; max-width: calc(100vw - 48px); padding: 12px 14px; background: var(--card); border: 1px solid var(--stroke); border-left: 3px solid var(--accent); border-radius: var(--radius-card); box-shadow: 0 4px 16px rgba(0, 0, 0, .12); } +.toast.error { border-left-color: var(--error); } + +/* About */ +.about dl { display: grid; grid-template-columns: 180px 1fr; gap: 8px 16px; margin: 0; } +.about dt { color: var(--text-muted); } +.about dd { margin: 0; overflow-wrap: anywhere; } /* Login */ .login { min-height: 100vh; display: grid; place-items: center; padding: 16px; } .login form { width: 360px; max-width: 100%; display: grid; gap: 14px; } .login .brand { padding: 0 0 8px; } +.login h1 { font-size: 16px; font-weight: 600; } .login label { display: grid; gap: 6px; color: var(--text-muted); } -.login .error { color: var(--error); min-height: 18px; } +.login .row { display: flex; gap: 8px; } -@media (max-width: 800px) { +@media (max-width: 900px) { .shell { grid-template-columns: 1fr; } - .sidebar { flex-direction: row; overflow-x: auto; padding: 8px; } + .sidebar { flex-direction: row; overflow-x: auto; padding: 8px; position: static; height: auto; } .sidebar .brand, .sidebar .user { display: none; } .nav { display: flex; gap: 4px; } - .nav a span { display: none; } + .nav a span:not(.badge) { display: none; } + .split, .grid.wide-right { grid-template-columns: 1fr; } + .side { width: auto; } + .field { grid-template-columns: 1fr; } } diff --git a/src/Titano.Server/Admin/web/api.js b/src/Titano.Server/Admin/web/api.js index 55d66e9d..b64edf04 100644 --- a/src/Titano.Server/Admin/web/api.js +++ b/src/Titano.Server/Admin/web/api.js @@ -1,13 +1,16 @@ -// Same API as the desktop clients: cookie session + double-submit CSRF token for the web admin (BRIEF §14.2). +// Same API as the desktop clients: cookie session + CSRF header for the web admin (BRIEF §14.2). +import { culture } from "./i18n.js"; + export class ApiFailure extends Error { - constructor(status, code, message) { + constructor(status, code, message, details) { super(message); this.status = status; this.code = code; + this.details = details; } } -const SESSION_KEY = "titano.admin.user"; +let currentUser = null; function csrfToken() { const m = document.cookie.match(/(?:^|; )titano_csrf=([^;]+)/); @@ -15,15 +18,27 @@ function csrfToken() { } async function request(method, url, body) { - const headers = { Accept: "application/json" }; + const headers = { Accept: "application/json", "Accept-Language": culture() }; if (body !== undefined) headers["Content-Type"] = "application/json"; if (method !== "GET") headers["X-CSRF-Token"] = csrfToken(); - const res = await fetch(url, { method, headers, credentials: "same-origin", body: body === undefined ? undefined : JSON.stringify(body) }); - if (res.status === 401 && url !== "/api/v1/auth/login") sessionStorage.removeItem(SESSION_KEY); + let res; + try { + res = await fetch(url, { method, headers, credentials: "same-origin", body: body === undefined ? undefined : JSON.stringify(body) }); + } catch { + throw new ApiFailure(0, "unreachable", ""); + } + if (res.status === 401 && url !== "/api/v1/auth/login") { + currentUser = null; + window.dispatchEvent(new Event("titano:signed-out")); + } if (!res.ok) { let err = { code: "http_" + res.status, message: res.statusText }; - try { err = await res.json(); } catch { /* non-JSON error */ } - throw new ApiFailure(res.status, err.code, err.message); + try { + err = await res.json(); + } catch { + // Not a JSON error body. + } + throw new ApiFailure(res.status, err.code, err.message, err.details); } return res.status === 204 ? null : res.json(); } @@ -31,17 +46,71 @@ async function request(method, url, body) { export const api = { get: (url) => request("GET", url), post: (url, body) => request("POST", url, body ?? {}), + put: (url, body) => request("PUT", url, body), patch: (url, body) => request("PATCH", url, body), del: (url) => request("DELETE", url), - isSignedIn: () => sessionStorage.getItem(SESSION_KEY) !== null, - user: () => JSON.parse(sessionStorage.getItem(SESSION_KEY) ?? "null"), + user: () => currentUser, + + /** Restores the session from the cookie; null when signed out. */ + async resume() { + try { + currentUser = await request("GET", "/api/v1/auth/me"); + } catch { + currentUser = null; + } + return currentUser; + }, + async signIn(username, password) { const r = await request("POST", "/api/v1/auth/login", { username, password, clientName: "admin-web", cookie: true }); - sessionStorage.setItem(SESSION_KEY, JSON.stringify(r.user)); + currentUser = r.user; return r; }, + + async changePassword(currentPassword, newPassword) { + await request("POST", "/api/v1/auth/password", { currentPassword, newPassword }); + currentUser = await request("GET", "/api/v1/auth/me"); + }, + async signOut() { - try { await request("POST", "/api/v1/auth/logout", {}); } catch { /* already gone */ } - sessionStorage.removeItem(SESSION_KEY); + try { + await request("POST", "/api/v1/auth/logout", {}); + } catch { + // Already signed out. + } + currentUser = null; + }, + + /** Server events over WebSocket (cookie session), reconnecting with back-off until stop() is called. */ + events(onEvent) { + let socket = null; + let stopped = false; + let delay = 1000; + const connect = () => { + if (stopped) return; + socket = new WebSocket(`${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/api/v1/events`); + socket.onopen = () => { + delay = 1000; + }; + socket.onmessage = (m) => { + try { + onEvent(JSON.parse(m.data)); + } catch { + // Ignore malformed or unknown events. + } + }; + socket.onclose = () => { + if (stopped) return; + setTimeout(connect, delay); + delay = Math.min(delay * 2, 30000); + }; + }; + connect(); + return { + stop() { + stopped = true; + socket?.close(); + }, + }; }, }; diff --git a/src/Titano.Server/Admin/web/app.js b/src/Titano.Server/Admin/web/app.js index c6295f82..29d8834a 100644 --- a/src/Titano.Server/Admin/web/app.js +++ b/src/Titano.Server/Admin/web/app.js @@ -1,42 +1,53 @@ // Titano Server admin panel — vanilla ES modules, no build step (BRIEF §14.8). import { api, ApiFailure } from "./api.js"; -import { t, loadStrings, applyStrings } from "./i18n.js"; +import { t, loadStrings, applyStrings, setLanguage, languages, culture } from "./i18n.js"; import { icons } from "./icons.js"; +import { escapeHtml, attr, errorText, nativeName } from "./ui.js"; const app = document.getElementById("app"); const pages = ["dashboard", "users", "archive", "jobs", "settings", "logs", "about"]; -const pageIcon = { dashboard: "dashboard", users: "users", archive: "archive", jobs: "jobs", settings: "settings", logs: "logs", about: "about" }; let info = null; +let events = null; +const listeners = new Set(); async function start() { - await loadStrings(navigator.language); + await loadStrings(); info = await api.get("/api/v1/server/info").catch(() => null); + await api.resume(); window.addEventListener("hashchange", render); + window.addEventListener("titano:signed-out", () => { + events?.stop(); + events = null; + render(); + }); render(); } function currentPage() { - const p = location.hash.replace(/^#\/?/, ""); + const p = location.hash.replace(/^#\/?/, "").split("?")[0]; return pages.includes(p) ? p : "dashboard"; } function render() { - if (!api.isSignedIn()) { - renderLogin(); - return; - } + const user = api.user(); + if (!user) return renderLogin(); + if (user.mustChangePassword) return renderPasswordChange(); renderShell(currentPage()); } -function renderLogin() { +function brand() { + return `
Titano Server${escapeHtml(info?.name ?? "")} · v${escapeHtml(info?.version ?? "")}
`; +} + +function renderLogin(message = "") { app.innerHTML = `
-
Titano Server${escapeHtml(info?.name ?? "")} · v${escapeHtml(info?.version ?? "")}
- - - + ${brand()} + + +
`; @@ -48,10 +59,56 @@ function renderLogin() { error.textContent = ""; const data = new FormData(form); try { - await api.signIn(String(data.get("username")), String(data.get("password"))); + const r = await api.signIn(String(data.get("username")), String(data.get("password"))); + if (r.user.role !== "admin") { + await api.signOut(); + error.textContent = t("Admin.Login.NotAdmin"); + return; + } render(); } catch (err) { - error.textContent = err instanceof ApiFailure && err.status === 401 ? t("Admin.Login.Failed") : t("Admin.Error.Unreachable"); + error.textContent = err instanceof ApiFailure && err.status === 401 ? t("Admin.Login.Failed") : errorText(err); + } + }); + form.querySelector("input").focus(); +} + +function renderPasswordChange() { + app.innerHTML = ` +
+
+ ${brand()} +

+ + + + +
+ + + +
+
+
`; + applyStrings(app); + const form = app.querySelector("form"); + const error = form.querySelector(".error"); + app.querySelector("#cancel").addEventListener("click", async () => { + await api.signOut(); + render(); + }); + form.addEventListener("submit", async (e) => { + e.preventDefault(); + const data = new FormData(form); + if (data.get("next") !== data.get("repeat")) { + error.textContent = t("Admin.Password.Mismatch"); + return; + } + try { + await api.changePassword(String(data.get("current")), String(data.get("next"))); + render(); + } catch (err) { + error.textContent = errorText(err); } }); form.querySelector("input").focus(); @@ -59,40 +116,82 @@ function renderLogin() { function renderShell(page) { const nav = pages.map((p) => ` - ${icons[pageIcon[p]]}`).join(""); + ${icons[p]}`).join(""); const user = api.user(); + const options = languages().map((code) => ``).join(""); app.innerHTML = `
-

+
+

+ + + + +
`; applyStrings(app); app.querySelector("#signout").addEventListener("click", async () => { + events?.stop(); + events = null; await api.signOut(); render(); }); + app.querySelector("#language").addEventListener("change", async (e) => { + await setLanguage(e.target.value); + render(); + }); + events ??= api.events((e) => listeners.forEach((fn) => fn(e))); + listeners.clear(); + updateBadges(); + listeners.add((e) => { + if (e.type === "jobState") updateBadges(); + }); + const content = app.querySelector("#content"); + const context = { + api, + info, + meta: app.querySelector("#meta"), + actions: app.querySelector("#actions"), + onEvent: (fn) => listeners.add(fn), + }; import(`./pages/${page}.js`) - .then((m) => m.render(content, { api, t, info, meta: app.querySelector("#meta") })) - .catch(() => { - content.innerHTML = `
`; - applyStrings(content); + .then((m) => m.render(content, context)) + .catch((err) => { + content.innerHTML = `
${escapeHtml(errorText(err))}
`; }); } -export function escapeHtml(s) { - return String(s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c])); +let badgeTimer = null; + +/** Running jobs and accounts, as nav badges (refreshed at most every two seconds). */ +function updateBadges() { + if (badgeTimer) return; + badgeTimer = setTimeout(() => { + badgeTimer = null; + }, 2000); + api.get("/api/v1/jobs?state=running&limit=100").then((page) => setBadge("jobs", page.items.length, true)).catch(() => {}); + api.get("/api/v1/users").then((users) => setBadge("users", users.length, false)).catch(() => {}); +} + +function setBadge(page, count, accent) { + const badge = document.getElementById(`badge-${page}`); + if (!badge) return; + badge.hidden = count === 0; + badge.textContent = String(count); + badge.classList.toggle("quiet", !accent); } function cap(s) { diff --git a/src/Titano.Server/Admin/web/i18n.js b/src/Titano.Server/Admin/web/i18n.js index 3b50350c..5014e5ef 100644 --- a/src/Titano.Server/Admin/web/i18n.js +++ b/src/Titano.Server/Admin/web/i18n.js @@ -1,9 +1,17 @@ // Strings come from /api/v1/i18n/{culture}, generated from the same .resx catalog as the desktop apps. +const LANGUAGE_KEY = "titano.admin.language"; let strings = {}; -export async function loadStrings(culture) { +/** Loads the strings of the chosen language (remembered per browser), else of the browser's language. */ +export async function loadStrings(requested) { + let preferred = requested; try { - const res = await fetch(`/api/v1/i18n/${encodeURIComponent(culture || "en")}`); + preferred ??= localStorage.getItem(LANGUAGE_KEY); + } catch { + // Storage may be blocked; the browser language is used. + } + try { + const res = await fetch(`/api/v1/i18n/${encodeURIComponent(preferred || navigator.language || "en")}`); strings = await res.json(); } catch { strings = {}; @@ -12,16 +20,47 @@ export async function loadStrings(culture) { document.documentElement.dir = strings.$rtl === "true" ? "rtl" : "ltr"; } +export async function setLanguage(code) { + try { + localStorage.setItem(LANGUAGE_KEY, code); + } catch { + // Only this session uses the language then. + } + await loadStrings(code); +} + +/** The current culture (e.g. "it"), used for numbers, dates and the Accept-Language of API calls. */ +export function culture() { + return strings.$culture || "en"; +} + +/** Languages with translated strings, as codes. */ +export function languages() { + return (strings.$languages || "en").split(","); +} + /** Localized string for key with {0}, {1}… placeholders filled from args. */ export function t(key, ...args) { const s = strings[key]; if (s === undefined) return `⟦${key}⟧`; - return s.replace(/\{(\d+)(?:[,:][^}]*)?\}/g, (_, i) => (args[Number(i)] ?? "")); + return s.replace(/\{(\d+)(?:[,:][^}]*)?\}/g, (_, i) => String(args[Number(i)] ?? "")); } -/** Fills data-i18n (text), data-i18n-title (tooltip) and data-i18n-placeholder attributes. */ +/** True when the catalog has the key (for keys built from data, e.g. audit actions). */ +export function has(key) { + return strings[key] !== undefined; +} + +/** Fills data-i18n (text), data-i18n-title (tooltip, also the accessible name of icon buttons) and data-i18n-placeholder. */ export function applyStrings(root) { - root.querySelectorAll("[data-i18n]").forEach((el) => { el.textContent = t(el.dataset.i18n); }); - root.querySelectorAll("[data-i18n-title]").forEach((el) => { el.title = t(el.dataset.i18nTitle); }); - root.querySelectorAll("[data-i18n-placeholder]").forEach((el) => { el.placeholder = t(el.dataset.i18nPlaceholder); }); + root.querySelectorAll("[data-i18n]").forEach((el) => { + el.textContent = t(el.dataset.i18n); + }); + root.querySelectorAll("[data-i18n-title]").forEach((el) => { + el.title = t(el.dataset.i18nTitle); + if (el.classList.contains("icon")) el.setAttribute("aria-label", el.title); + }); + root.querySelectorAll("[data-i18n-placeholder]").forEach((el) => { + el.placeholder = t(el.dataset.i18nPlaceholder); + }); } diff --git a/src/Titano.Server/Admin/web/icons.js b/src/Titano.Server/Admin/web/icons.js index 1ea443a0..73e78d2d 100644 --- a/src/Titano.Server/Admin/web/icons.js +++ b/src/Titano.Server/Admin/web/icons.js @@ -12,5 +12,6 @@ export const icons = { refresh: ``, close: ``, plus: ``, + copy: ``, search: ``, }; diff --git a/src/Titano.Server/Admin/web/jobs.js b/src/Titano.Server/Admin/web/jobs.js new file mode 100644 index 00000000..bd0d6b41 --- /dev/null +++ b/src/Titano.Server/Admin/web/jobs.js @@ -0,0 +1,41 @@ +// Job and node labels shared by the dashboard and the Jobs page. +import { t, has } from "./i18n.js"; +import { escapeHtml, attr, bar } from "./ui.js"; + +export function pascal(text) { + return String(text).split(/[-_]/).map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join(""); +} + +export function kindLabel(kind) { + const key = `Admin.Job.Kind.${pascal(kind)}`; + return has(key) ? t(key) : kind; +} + +export function stateLabel(state) { + const key = `Admin.Job.State.${pascal(state)}`; + return has(key) ? t(key) : state; +} + +/** Where a job runs: this server, a render node by name, or nowhere yet. */ +export function nodeLabel(job, nodes) { + if (!job.nodeId) return "—"; + if (job.nodeId.startsWith("server:")) return t("Admin.Jobs.Server"); + return nodes.find((n) => n.id === job.nodeId)?.clientName ?? "—"; +} + +export function progressCell(job) { + if (job.progressTotal <= 0) return `
${bar(0, true)}—
`; + const fraction = job.progressDone / job.progressTotal; + return `
${bar(fraction)}${Math.floor(fraction * 100)} %
`; +} + +export function stateCell(state) { + const glyph = { running: "◐", queued: "○", paused: "❚❚", done: "●", failed: "✕", cancelled: "○" }[state] ?? ""; + return `${glyph} ${escapeHtml(stateLabel(state))}`; +} + +/** Node encoders as "NVENC HEVC/H.264/AV1". */ +export function encoders(node) { + const names = [...new Set(node.encoders.map((e) => e.codec.toUpperCase()))]; + return names.join("/"); +} diff --git a/src/Titano.Server/Admin/web/pages/about.js b/src/Titano.Server/Admin/web/pages/about.js index 1228e1fe..f287dd9c 100644 --- a/src/Titano.Server/Admin/web/pages/about.js +++ b/src/Titano.Server/Admin/web/pages/about.js @@ -1,12 +1,21 @@ +// About: version, API, database, uptime and the certificate fingerprint clients are asked to trust. import { applyStrings } from "../i18n.js"; -import { escapeHtml } from "../app.js"; +import { escapeHtml, formatDuration } from "../ui.js"; -export async function render(root, { info }) { +export async function render(root, { api, info }) { + const dashboard = await api.get("/api/v1/server/dashboard").catch(() => null); + const rows = [ + ["Admin.About.Name", info?.name], + ["Admin.About.Version", info?.version], + ["Admin.About.Api", info?.apiVersion], + ["Admin.About.Database", dashboard?.database], + ["Admin.About.Uptime", dashboard ? formatDuration(dashboard.uptimeSeconds) : null], + ["Admin.About.Fingerprint", info?.tlsFingerprint], + ]; root.innerHTML = ` -
-

-

Titano Server ${escapeHtml(info?.version ?? "")} · API ${escapeHtml(String(info?.apiVersion ?? ""))}

-

${escapeHtml(info?.tlsFingerprint ?? "–")}

+
+
+
${rows.map(([key, value]) => `
${escapeHtml(value ?? "—")}
`).join("")}
`; applyStrings(root); } diff --git a/src/Titano.Server/Admin/web/pages/archive.js b/src/Titano.Server/Admin/web/pages/archive.js new file mode 100644 index 00000000..ddd7c760 --- /dev/null +++ b/src/Titano.Server/Admin/web/pages/archive.js @@ -0,0 +1,109 @@ +// Archive (BRIEF §14.8): naming templates, proxy policy, scan/rebuild/verify/maintenance, statistics. +import { t, applyStrings } from "../i18n.js"; +import { escapeHtml, attr, formatBytes, formatNumber, bar, columns, toast, errorText } from "../ui.js"; + +const PROXY_SIZES = [-1, 1280, 1920, 2560, 3840, 0]; +const ACTIONS = ["scan", "rebuild", "verify", "maintenance"]; + +export async function render(root, { api, meta }) { + let settings; + let stats; + try { + [settings, stats] = await Promise.all([api.get("/api/v1/archive/settings"), api.get("/api/v1/archive/stats")]); + } catch (err) { + root.innerHTML = `
${escapeHtml(errorText(err))}
`; + return; + } + meta.textContent = settings.rootPath; + const used = stats.capacityBytes > 0 ? (stats.capacityBytes - stats.freeBytes) / stats.capacityBytes : 0; + const sizes = PROXY_SIZES.includes(settings.proxyLongEdge) ? PROXY_SIZES : [settings.proxyLongEdge, ...PROXY_SIZES]; + const proxyOptions = sizes.map((size) => ``).join(""); + + root.innerHTML = ` +
+
${formatNumber(stats.sequences)} + ${escapeHtml(t("Admin.Dashboard.PhotosCameras", formatNumber(stats.photos), formatNumber(stats.cameras)))}
+
${escapeHtml(formatBytes(stats.bytes))} + ${escapeHtml(stats.firstYear ? `${stats.firstYear}–${stats.lastYear}` : "—")}
+
${escapeHtml(formatBytes(stats.freeBytes))} + ${bar(used)}${escapeHtml(t("Admin.Dashboard.Of", formatBytes(stats.capacityBytes)))}
+
${formatNumber(stats.unverifiedSequences)} + ${escapeHtml(t("Admin.Dashboard.NoProxies", formatNumber(stats.sequencesWithoutProxies)))}
+
+
+
+

+ + + + +
+
+
+

+
${ACTIONS.map((a) => ``).join("")}
+

+
+ ${stats.byCamera.slice(0, 8).map((c) => `
${escapeHtml(c.key || "—")}${formatNumber(c.count)}${escapeHtml(formatBytes(c.bytes))}
`).join("") + || `

`} +
+
+
+
+

+ ${stats.byYear.length === 0 ? `

` + : columns(stats.byYear.map((y) => ({ label: y.key, value: y.bytes, title: `${y.key} · ${formatBytes(y.bytes)}` })), { valueLabel: (i) => formatBytes(i.value) })}
+

+
+ ${stats.byFormat.map((f) => `
${escapeHtml(f.key.toUpperCase())}${formatNumber(f.count)}${escapeHtml(formatBytes(f.bytes))}
`).join("") + || `

`} +
+
`; + applyStrings(root); + + const form = root.querySelector("form"); + form.addEventListener("submit", async (e) => { + e.preventDefault(); + form.querySelectorAll("[aria-invalid]").forEach((el) => el.removeAttribute("aria-invalid")); + const data = new FormData(form); + try { + settings = await api.put("/api/v1/archive/settings", { + ...settings, + name: String(data.get("name")).trim(), + folderTemplate: String(data.get("folderTemplate")), + fileTemplate: String(data.get("fileTemplate")), + proxyLongEdge: Number(data.get("proxyLongEdge")), + }); + toast(t("Admin.Common.Saved")); + } catch (err) { + // Errors name the field in details; the field gets the error border and the message as its tooltip. + const field = err.details && form.elements[err.details]; + if (field) { + field.setAttribute("aria-invalid", "true"); + field.title = errorText(err); + } + toast(errorText(err), true); + } + }); + root.querySelectorAll("[data-action]").forEach((button) => button.addEventListener("click", async () => { + button.disabled = true; + try { + await api.post(`/api/v1/archive/${button.dataset.action}`); + toast(t("Admin.Archive.Queued")); + } catch (err) { + toast(errorText(err), true); + } finally { + button.disabled = false; + } + })); +} + +function proxyLabel(size) { + if (size === -1) return t("Admin.Archive.ProxiesOff"); + if (size === 0) return t("Admin.Archive.ProxiesFull"); + return t("Admin.Archive.ProxiesEdge", size); +} + +function cap(s) { + return s.charAt(0).toUpperCase() + s.slice(1); +} diff --git a/src/Titano.Server/Admin/web/pages/dashboard.js b/src/Titano.Server/Admin/web/pages/dashboard.js index 4a4ab432..697d83b4 100644 --- a/src/Titano.Server/Admin/web/pages/dashboard.js +++ b/src/Titano.Server/Admin/web/pages/dashboard.js @@ -1,18 +1,131 @@ -// Dashboard: storage, archive, jobs, render nodes (BRIEF §14.8). Data from the public API only. -import { applyStrings } from "../i18n.js"; +// Dashboard (mockup Admin.html, BRIEF §14.8): storage, archive, jobs, render nodes, charts, activity, active jobs. +import { t, has, applyStrings } from "../i18n.js"; +import { escapeHtml, attr, formatBytes, formatNumber, formatTime, formatDuration, bar, columns, toast, errorText } from "../ui.js"; +import { kindLabel, nodeLabel, progressCell, stateCell, stateLabel, encoders, pascal } from "../jobs.js"; -export async function render(root, { api, t, info, meta }) { - const health = await api.get("/health").catch(() => null); - meta.textContent = info ? `v${info.version} · TLS ${info.tlsFingerprint ? "auto" : "proxy"}` : ""; - root.innerHTML = ` -
-
-
${health?.status === "ok" ? `●` : `✕`}${health?.status ?? ""}
-
${info?.name ?? ""}
-
-
${health?.checks?.archive === "ok" ? `●` : `▲`}${health?.checks?.archive ?? ""}
-
-
${info?.apiVersion ?? "–"}
${(info?.capabilities ?? []).join(" · ")}
-
`; - applyStrings(root); +export async function render(root, { api, meta, onEvent }) { + let timer = null; + const refresh = () => { + clearTimeout(timer); + timer = setTimeout(load, 1500); + }; + + async function load() { + let d; + try { + d = await api.get("/api/v1/server/dashboard"); + } catch (err) { + root.innerHTML = `
${escapeHtml(errorText(err))}
`; + return; + } + meta.textContent = t("Admin.Dashboard.Meta", formatDuration(d.uptimeSeconds), d.database, d.tlsMode, + t(d.discovery ? "Admin.Dashboard.DiscoveryOn" : "Admin.Dashboard.DiscoveryOff")); + const a = d.archive; + const used = a.capacityBytes > 0 ? (a.capacityBytes - a.freeBytes) / a.capacityBytes : 0; + const online = d.nodes.filter((n) => n.state !== "offline"); + const uploads = lastThirtyDays(d.uploads); + const totalUploads = uploads.reduce((s, u) => s + u.value, 0); + const peak = Math.max(0, ...uploads.map((u) => u.value)); + + root.innerHTML = ` +
+
+ + ${escapeHtml(formatBytes(a.capacityBytes - a.freeBytes))}${escapeHtml(t("Admin.Dashboard.Of", formatBytes(a.capacityBytes)))} + ${bar(used)} + ${escapeHtml(t("Admin.Dashboard.Free", formatBytes(a.freeBytes)))} · ${escapeHtml(t("Admin.Dashboard.Originals", formatBytes(a.bytes)))} +
+
+ + ${formatNumber(a.sequences)} + ${escapeHtml(t("Admin.Dashboard.PhotosCameras", formatNumber(a.photos), formatNumber(a.cameras)))} + ${escapeHtml(t("Admin.Dashboard.NoProxies", formatNumber(a.sequencesWithoutProxies)))} +
+
+ + ${formatNumber(d.jobs.running)} + ${escapeHtml(t("Admin.Dashboard.JobCounts", formatNumber(d.jobs.queued), formatNumber(d.jobs.doneToday), formatNumber(d.jobs.failedToday)))} + ${escapeHtml(t("Admin.Dashboard.Workers", formatNumber(d.workers)))} +
+
+ + ${formatNumber(online.length)} + ${online.slice(0, 2).map((n) => `${escapeHtml(n.clientName)} · ${escapeHtml(encoders(n))} · ${escapeHtml(stateLabel(n.state))}`).join("")} +
+
+
+
+

+ ${a.byYear.length === 0 ? `

` + : columns(a.byYear.map((y) => ({ label: y.key, value: y.bytes, title: `${y.key} · ${formatBytes(y.bytes)} · ${formatNumber(y.count)}` })), { valueLabel: (i) => formatBytes(i.value) })} +
+
+

+ ${escapeHtml(t("Admin.Dashboard.UploadTotals", formatNumber(totalUploads), formatNumber(peak)))}
+ ${columns(uploads, { sun: true })} +
+
+
+
+

+
${d.activity.map((e) => `${escapeHtml(formatTime(e.at))}${escapeHtml(activityText(e))}`).join("")}
+ ${d.activity.length === 0 ? `

` : ""} +
+
+

+ ${d.activeJobs.length === 0 ? `

` : ` +
+
+ ${d.activeJobs.map((j) => ` +
+ ${escapeHtml(kindLabel(j.kind))}${escapeHtml(j.targetLabel ?? "")}${escapeHtml(nodeLabel(j, d.nodes))} + ${progressCell(j)}${stateCell(j.state)} + +
`).join("")} +
`} +
+
`; + applyStrings(root); + root.querySelectorAll("[data-cancel]").forEach((button) => button.addEventListener("click", async () => { + button.disabled = true; + try { + await api.post(`/api/v1/jobs/${button.dataset.cancel}/cancel`); + refresh(); + } catch (err) { + toast(errorText(err), true); + button.disabled = false; + } + })); + } + + onEvent((e) => { + if (e.type === "jobProgress") { + const cell = root.querySelector(`[data-job="${CSS.escape(e.jobId)}"] .progress`); + if (cell) cell.innerHTML = progressCell({ progressDone: e.done, progressTotal: e.total }); + } else { + refresh(); + } + }); + await load(); +} + +/** One sentence per audit entry, e.g. "alberto signed in" (unknown actions show their code). */ +function activityText(entry) { + const key = `Admin.Activity.${pascal(entry.action)}`; + return has(key) ? t(key, entry.username ?? "—", entry.details ?? "") : `${entry.username ?? ""} ${entry.action}`; +} + +/** Frames per UTC day for the last 30 days, zero-filled. */ +function lastThirtyDays(days) { + const byDay = new Map(days.map((d) => [d.day, d.frames])); + const items = []; + const today = new Date(); + for (let i = 29; i >= 0; i--) { + const day = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate() - i)); + const key = day.toISOString().slice(0, 10); + const value = byDay.get(key) ?? 0; + const label = i === 29 || i === 0 ? new Intl.DateTimeFormat(document.documentElement.lang, { day: "numeric", month: "short" }).format(day) : ""; + items.push({ value, label, title: `${key} · ${formatNumber(value)}` }); + } + return items; } diff --git a/src/Titano.Server/Admin/web/pages/jobs.js b/src/Titano.Server/Admin/web/pages/jobs.js new file mode 100644 index 00000000..a4ce6966 --- /dev/null +++ b/src/Titano.Server/Admin/web/pages/jobs.js @@ -0,0 +1,163 @@ +// Jobs (BRIEF §14.8): queue and history with cancel/pause/resume/retry/priority, log tails, render nodes. +import { t, applyStrings } from "../i18n.js"; +import { escapeHtml, attr, formatTime, formatDateTime, toast, errorText, confirmDialog } from "../ui.js"; +import { kindLabel, nodeLabel, progressCell, stateCell, stateLabel, encoders } from "../jobs.js"; + +const FILTERS = ["all", "running", "queued", "paused", "failed", "done", "cancelled"]; +const PRIORITIES = ["background", "normal", "user"]; + +export async function render(root, { api, meta, onEvent }) { + let filter = "all"; + let jobs = []; + let nodes = []; + let cursor = null; + const expanded = new Set(); + let timer = null; + + root.innerHTML = ` +
${FILTERS.map((f) => ``).join("")}
+
+
+
+
+
+
+ +
+
+

+
+
+
+
+
+
`; + applyStrings(root); + root.querySelectorAll("[data-filter]").forEach((chip) => chip.addEventListener("click", () => { + filter = chip.dataset.filter; + root.querySelectorAll("[data-filter]").forEach((c) => c.setAttribute("aria-pressed", String(c === chip))); + load(); + })); + root.querySelector("#more").addEventListener("click", () => load(true)); + + async function load(more = false) { + try { + const state = filter === "all" ? "" : `&state=${filter}`; + const page = await api.get(`/api/v1/jobs?limit=50${state}${more && cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""}`); + jobs = more ? jobs.concat(page.items) : page.items; + cursor = page.nextCursor; + nodes = await api.get("/api/v1/nodes"); + } catch (err) { + toast(errorText(err), true); + return; + } + const running = jobs.filter((j) => j.state === "running").length; + meta.textContent = t("Admin.Jobs.Meta", running, nodes.filter((n) => n.state !== "offline").length); + draw(); + } + + function draw() { + const host = root.querySelector("#rows"); + host.innerHTML = jobs.map((j) => ` +
+ + ${escapeHtml(j.targetLabel ?? "")} + ${escapeHtml(nodeLabel(j, nodes))} + ${progressCell(j)} + ${stateCell(j.state)} + ${escapeHtml(formatTime(j.created))} + ${buttons(j)} +
+ ${expanded.has(j.id) ? `
${escapeHtml(j.error ? `${j.logTail ?? ""}\n${j.error}`.trim() : j.logTail ?? "")}
` : ""}`).join("") + || `
`; + applyStrings(host); + host.querySelectorAll("[data-toggle]").forEach((b) => b.addEventListener("click", () => { + const id = b.dataset.toggle; + if (!expanded.delete(id)) expanded.add(id); + draw(); + })); + host.querySelectorAll("[data-act]").forEach((b) => b.addEventListener("click", () => act(b.closest(".row").dataset.job, b.dataset.act))); + host.querySelectorAll("select[data-priority]").forEach((s) => s.addEventListener("change", () => setPriority(s.dataset.priority, s.value))); + root.querySelector("#foot").hidden = !cursor; + + const nodeHost = root.querySelector("#nodes"); + nodeHost.innerHTML = nodes.map((n) => ` +
+ ${escapeHtml(n.clientName)}${escapeHtml(n.username)} + ${escapeHtml(encoders(n))} + ${escapeHtml(formatDateTime(n.lastHeartbeat))} + ${n.state === "offline" ? "○" : "●"} ${escapeHtml(stateLabel(n.state))} + +
`).join("") || `
`; + applyStrings(nodeHost); + nodeHost.querySelectorAll("[data-forget]").forEach((b) => b.addEventListener("click", () => forget(nodes.find((n) => n.id === b.dataset.forget)))); + } + + function buttons(job) { + const button = (act, key) => ``; + const priority = ``; + switch (job.state) { + case "running": return button("pause", "Pause") + button("cancel", "Cancel"); + case "queued": return priority + button("pause", "Pause") + button("cancel", "Remove"); + case "paused": return priority + button("resume", "Resume") + button("cancel", "Cancel"); + case "failed": + case "cancelled": return button("retry", "Retry"); + default: return ""; + } + } + + async function act(id, action) { + try { + await api.post(`/api/v1/jobs/${id}/${action}`); + await load(); + } catch (err) { + toast(errorText(err), true); + } + } + + async function setPriority(id, priority) { + try { + await api.post(`/api/v1/jobs/${id}/priority`, { priority }); + } catch (err) { + toast(errorText(err), true); + } + } + + async function forget(node) { + const confirmed = await confirmDialog({ + title: t("Admin.Jobs.ForgetTitle", node.clientName), + confirm: t("Admin.Jobs.Forget"), + confirmTip: t("Admin.Jobs.Forget.Tip"), + cancel: t("Admin.Common.Keep"), + cancelTip: t("Admin.Common.Keep.Tip"), + }); + if (!confirmed) return; + try { + await api.del(`/api/v1/nodes/${node.id}`); + await load(); + } catch (err) { + toast(errorText(err), true); + } + } + + onEvent((e) => { + if (e.type === "jobProgress") { + const job = jobs.find((j) => j.id === e.jobId); + if (job) { + job.progressDone = e.done; + job.progressTotal = e.total; + const cell = root.querySelector(`[data-job="${CSS.escape(e.jobId)}"] .progress`); + if (cell) cell.innerHTML = progressCell(job); + } + } else if (e.type === "jobState" || e.type === "nodeState") { + clearTimeout(timer); + timer = setTimeout(load, 800); + } + }); + await load(); +} + +function cap(s) { + return s.charAt(0).toUpperCase() + s.slice(1); +} diff --git a/src/Titano.Server/Admin/web/pages/logs.js b/src/Titano.Server/Admin/web/pages/logs.js new file mode 100644 index 00000000..fd097cfc --- /dev/null +++ b/src/Titano.Server/Admin/web/pages/logs.js @@ -0,0 +1,104 @@ +// Logs (BRIEF §14.8): server log tail and downloads, sign-in log, audit log. +import { t, has, applyStrings } from "../i18n.js"; +import { escapeHtml, attr, formatBytes, formatDateTime, toast, errorText, routeParams } from "../ui.js"; +import { pascal } from "../jobs.js"; + +const VIEWS = ["server", "signins", "audit"]; +const LINES = [100, 300, 1000]; + +export async function render(root, { api, meta, actions }) { + let view = VIEWS.includes(routeParams().get("view")) ? routeParams().get("view") : "server"; + let lines = 300; + let audit = []; + let cursor = null; + + actions.innerHTML = `
${VIEWS.map((v) => + ``).join("")}
`; + applyStrings(actions); + actions.querySelectorAll("[data-view]").forEach((b) => b.addEventListener("click", () => { + view = b.dataset.view; + actions.querySelectorAll("[data-view]").forEach((x) => x.setAttribute("aria-pressed", String(x === b))); + history.replaceState(null, "", `#/logs?view=${view}`); + show(); + })); + + async function show() { + meta.textContent = ""; + if (view === "server") await showServerLog(); + else await showAudit(false); + } + + async function showServerLog() { + let tail; + let files; + try { + [tail, files] = await Promise.all([api.get(`/api/v1/logs?lines=${lines}`), api.get("/api/v1/logs/files")]); + } catch (err) { + toast(errorText(err), true); + return; + } + meta.textContent = tail.file; + root.innerHTML = ` +
+
+ + + +
+
${escapeHtml(tail.lines.join("\n"))}
+
+
+

+
${files.map((f) => ` +
${escapeHtml(f.name)}${escapeHtml(formatBytes(f.size))} + ${escapeHtml(formatDateTime(f.modified))} +
`).join("")} +
+
`; + applyStrings(root); + const pre = root.querySelector("pre"); + pre.scrollTop = pre.scrollHeight; + root.querySelector("#lines").addEventListener("change", (e) => { + lines = Number(e.target.value); + showServerLog(); + }); + root.querySelector("#refresh").addEventListener("click", showServerLog); + } + + async function showAudit(more) { + const action = view === "signins" ? "&action=login" : ""; + try { + const page = await api.get(`/api/v1/audit?limit=100${action}${more && cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""}`); + audit = more ? audit.concat(page.items) : page.items; + cursor = page.nextCursor; + } catch (err) { + toast(errorText(err), true); + return; + } + root.innerHTML = ` +
+
+
+
+ ${audit.map((e) => ` +
${escapeHtml(formatDateTime(e.at))}${escapeHtml(e.username ?? "—")} + ${escapeHtml(actionLabel(e.action))}${escapeHtml(e.details ?? "")} + ${escapeHtml(e.remoteAddress ?? "")}
`).join("") || `
`} +
+ ${cursor ? `
` : ""} +
`; + applyStrings(root); + root.querySelector("#more")?.addEventListener("click", () => showAudit(true)); + } + + await show(); +} + +function actionLabel(action) { + const key = `Admin.Logs.Action.${pascal(action)}`; + return has(key) ? t(key) : action; +} + +function cap(s) { + return s.charAt(0).toUpperCase() + s.slice(1); +} diff --git a/src/Titano.Server/Admin/web/pages/settings.js b/src/Titano.Server/Admin/web/pages/settings.js new file mode 100644 index 00000000..8659c01b --- /dev/null +++ b/src/Titano.Server/Admin/web/pages/settings.js @@ -0,0 +1,108 @@ +// Settings (BRIEF §14.8): server name, ports, TLS, discovery, limits, language, retention. Values fixed by environment +// variables are read-only here; ports, TLS, workers, chunk size and job history apply at the next restart. +import { t, applyStrings, languages } from "../i18n.js"; +import { escapeHtml, attr, toast, errorText, nativeName } from "../ui.js"; + +/** Setting → environment variable that can fix it (BRIEF §14.9). */ +const VARIABLES = { + serverName: "TITANO_SERVER_NAME", httpPort: "TITANO_HTTP_PORT", httpsPort: "TITANO_HTTPS_PORT", tlsMode: "TITANO_TLS_MODE", + tlsCert: "TITANO_TLS_CERT", tlsKey: "TITANO_TLS_KEY", language: "TITANO_LANG", workers: "TITANO_WORKERS", discovery: "TITANO_DISCOVERY", +}; +const MB = 1e6; +const MIB = 1024 * 1024; + +export async function render(root, { api, info, meta }) { + let settings; + try { + settings = await api.get("/api/v1/server/settings"); + } catch (err) { + root.innerHTML = `
${escapeHtml(errorText(err))}
`; + return; + } + + const locked = (name) => settings.locked.some((l) => l.toLowerCase() === name.toLowerCase()); + const lock = (name) => locked(name) ? `disabled title="${attr(t("Admin.Settings.Locked.Tip", VARIABLES[name] ?? name))}"` : `data-i18n-title="Admin.Settings.${cap(name)}.Tip"`; + const field = (name, input) => ``; + const number = (name, value, attrs = "") => field(name, ``); + const text = (name, value) => field(name, ``); + + function draw() { + meta.textContent = ""; + root.innerHTML = ` +
+
+

+ ${settings.restartRequired ? `` : ""}
+ ${text("serverName", settings.serverName)} + ${field("language", ``)} + ${number("workers", settings.workers, 'min="1" max="64"')} + ${field("discovery", ``)} +
+
+

+ ${number("httpPort", settings.httpPort, 'min="1" max="65535"')} + ${number("httpsPort", settings.httpsPort, 'min="1" max="65535"')} + ${field("tlsMode", ``)} + ${text("tlsCert", settings.tlsCert)} + ${text("tlsKey", settings.tlsKey)} + ${field("fingerprint", `${escapeHtml(info?.tlsFingerprint ?? "—")}`)} +
+
+

+ ${number("maxUploadMb", Math.round(settings.maxUploadBytes / MB), 'min="1"')} + ${number("chunkSizeMib", settings.chunkSizeBytes / MIB, 'min="0.0625" max="64" step="0.0625"')} + ${number("tokenLifetimeDays", settings.tokenLifetimeDays, 'min="1" max="365"')} + ${number("uploadRetentionHours", settings.uploadRetentionHours, 'min="1" max="8760"')} + ${number("jobHistoryDays", settings.jobHistoryDays, 'min="1" max="3650"')} +
+
+
`; + applyStrings(root); + root.querySelector("form").addEventListener("submit", save); + } + + async function save(e) { + e.preventDefault(); + const form = e.target; + const data = new FormData(form); + const value = (name, fallback) => (form.elements[name]?.disabled ? fallback : data.get(name)); + const request = { + ...settings, + serverName: String(value("serverName", settings.serverName)).trim(), + language: String(value("language", settings.language)), + workers: Number(value("workers", settings.workers)), + discovery: form.elements.discovery.disabled ? settings.discovery : form.elements.discovery.checked, + httpPort: Number(value("httpPort", settings.httpPort)), + httpsPort: Number(value("httpsPort", settings.httpsPort)), + tlsMode: String(value("tlsMode", settings.tlsMode)), + tlsCert: String(value("tlsCert", settings.tlsCert ?? "")).trim() || null, + tlsKey: String(value("tlsKey", settings.tlsKey ?? "")).trim() || null, + maxUploadBytes: Math.round(Number(data.get("maxUploadMb")) * MB), + chunkSizeBytes: Math.round(Number(data.get("chunkSizeMib")) * MIB), + tokenLifetimeDays: Number(data.get("tokenLifetimeDays")), + uploadRetentionHours: Number(data.get("uploadRetentionHours")), + jobHistoryDays: Number(data.get("jobHistoryDays")), + }; + try { + settings = await api.put("/api/v1/server/settings", request); + draw(); + toast(t("Admin.Common.Saved")); + } catch (err) { + toast(errorText(err), true); + const name = err.details && Object.keys(form.elements).find((k) => k.toLowerCase() === String(err.details).toLowerCase()); + const input = name ? form.elements[name] : null; + if (input) { + input.setAttribute("aria-invalid", "true"); + input.title = errorText(err); + } + } + } + + draw(); +} + +function cap(s) { + return s.charAt(0).toUpperCase() + s.slice(1); +} diff --git a/src/Titano.Server/Admin/web/pages/users.js b/src/Titano.Server/Admin/web/pages/users.js new file mode 100644 index 00000000..14eea30a --- /dev/null +++ b/src/Titano.Server/Admin/web/pages/users.js @@ -0,0 +1,234 @@ +// Users (mockup AdminUsers.html, BRIEF §14.8): accounts table and a side panel to create, edit and reset. +import { t, applyStrings } from "../i18n.js"; +import { escapeHtml, attr, formatBytes, formatNumber, formatTime, bar, toast, confirmDialog, errorText } from "../ui.js"; +import { icons } from "../icons.js"; + +const ROLES = ["viewer", "uploader", "editor", "admin"]; +const TB = 1e12; +const RENDER_NODE = 32; + +export async function render(root, { api, info, meta, actions }) { + let users = []; + let filter = ""; + let freeBytes = 0; + let tokenDays = 30; + + actions.innerHTML = ` + + `; + applyStrings(actions); + actions.querySelector("#search").addEventListener("input", (e) => { + filter = e.target.value.trim().toLowerCase(); + drawTable(); + }); + actions.querySelector("#new").addEventListener("click", () => openForm(null)); + + root.innerHTML = ` +
+
+
+
+
+
+
+
+
+
+ +
`; + applyStrings(root); + + async function load() { + try { + const [list, stats, settings] = await Promise.all([ + api.get("/api/v1/users"), + api.get("/api/v1/archive/stats").catch(() => null), + api.get("/api/v1/server/settings").catch(() => null), + ]); + users = list; + freeBytes = stats?.freeBytes ?? 0; + tokenDays = settings?.tokenLifetimeDays ?? tokenDays; + } catch (err) { + toast(errorText(err), true); + return; + } + const active = users.filter((u) => !u.user.disabled).length; + meta.textContent = t("Admin.Users.Meta", formatNumber(users.length), formatNumber(active)); + root.querySelector("#policy").textContent = t("Admin.Users.Policy", info?.limits?.passwordMinLength ?? 10, tokenDays); + drawTable(); + } + + function drawTable() { + const rows = users.filter((u) => !filter || u.user.username.toLowerCase().includes(filter) || (u.user.displayName ?? "").toLowerCase().includes(filter)); + const host = root.querySelector("#rows"); + host.innerHTML = rows.map((row) => { + const u = row.user; + const subtitle = [u.displayName, u.mustChangePassword ? t("Admin.Users.MustChange") : null, u.disabled ? t("Admin.Users.Disabled") : null] + .filter(Boolean).join(" · "); + const quota = u.quotaBytes > 0 + ? `
${bar(u.usedBytes / u.quotaBytes, u.disabled)}${escapeHtml(t("Admin.Users.QuotaOf", formatBytes(u.usedBytes), formatBytes(u.quotaBytes)))}
` + : `${escapeHtml(u.usedBytes > 0 ? formatBytes(u.usedBytes) : "—")}`; + const seen = row.lastSeen ? `${formatTime(row.lastSeen)}${row.lastClient ? " · " + row.lastClient : ""}` : "—"; + const buttons = u.disabled + ? ` + ` + : ` + + ${u.id === api.user().id ? "" : ``}`; + return ` +
+ ${escapeHtml(u.username.slice(0, 1).toUpperCase())} + ${escapeHtml(u.username)}${escapeHtml(subtitle)} + + ${quota} + ${escapeHtml(seen)} + ${formatNumber(row.uploadsLast30Days)} + ${u.disabled ? "○" : "●"} + ${buttons} +
`; + }).join("") || `
`; + applyStrings(host); + host.querySelectorAll("[data-act]").forEach((button) => button.addEventListener("click", () => { + const row = users.find((u) => u.user.id === button.closest(".row").dataset.id); + act(button.dataset.act, row.user); + })); + } + + async function act(action, user) { + try { + if (action === "edit") return openForm(user); + if (action === "reset") { + const r = await api.post(`/api/v1/users/${user.id}/reset-password`, {}); + showPassword(r); + } else if (action === "disable" || action === "enable") { + await api.patch(`/api/v1/users/${user.id}`, { disabled: action === "disable" }); + } else if (action === "delete") { + const confirmed = await confirmDialog({ + title: t("Admin.Users.DeleteTitle", user.username), + body: t("Admin.Users.DeleteBody"), + confirm: t("Admin.Users.DeleteConfirm"), + confirmTip: t("Admin.Users.Delete.Tip"), + cancel: t("Admin.Common.Keep"), + cancelTip: t("Admin.Common.Keep.Tip"), + typed: user.username, + }); + if (!confirmed) return; + await api.del(`/api/v1/users/${user.id}`); + } + await load(); + } catch (err) { + toast(errorText(err), true); + } + } + + function openForm(user) { + const side = root.querySelector("#side"); + const editing = user !== null; + const quotaTb = editing && user.quotaBytes > 0 ? +(user.quotaBytes / TB).toFixed(3) : 0; + const renderNode = editing ? (user.permissions & RENDER_NODE) !== 0 : false; + side.hidden = false; + side.innerHTML = ` +
+

+ + +
+ ${ROLES.map((r) => ``).join("")} +
+ + ${editing ? "" : ` + + `} + + +
+ + +
+
`; + applyStrings(side); + const form = side.querySelector("form"); + side.querySelector("#close").addEventListener("click", () => { + side.hidden = true; + }); + side.querySelector("#regenerate")?.addEventListener("click", () => { + form.elements.password.value = generatePassword(); + }); + form.addEventListener("submit", async (e) => { + e.preventDefault(); + const error = form.querySelector(".error"); + error.textContent = ""; + const data = new FormData(form); + const quotaBytes = Math.round(Number(data.get("quota") || 0) * TB); + try { + if (editing) { + await api.patch(`/api/v1/users/${user.id}`, { + displayName: String(data.get("displayName")), role: data.get("role"), quotaBytes, allowRenderNode: data.get("renderNode") === "on", + }); + side.hidden = true; + } else { + const r = await api.post("/api/v1/users", { + username: String(data.get("username")).trim(), displayName: String(data.get("displayName")), role: data.get("role"), quotaBytes, + password: String(data.get("password")), allowRenderNode: data.get("renderNode") === "on", + }); + showPassword(r); + } + await load(); + } catch (err) { + error.textContent = errorText(err); + } + }); + form.querySelector("input:not([disabled])").focus(); + } + + /** The one-time password of a new or reset account, shown once in the side panel. */ + function showPassword(result) { + const side = root.querySelector("#side"); + side.hidden = false; + side.innerHTML = ` +
+

${escapeHtml(result.user.username)}

+ +
+
`; + applyStrings(side); + side.querySelector("#copy").addEventListener("click", async () => { + try { + await navigator.clipboard.writeText(result.temporaryPassword); + toast(t("Admin.Users.Copied")); + } catch { + side.querySelector("input").select(); + } + }); + side.querySelector("#close").addEventListener("click", () => { + side.hidden = true; + }); + } + + await load(); +} + +/** Three groups of four unambiguous characters, like the server's own temporary passwords. */ +function generatePassword() { + const alphabet = "abcdefghjkmnpqrstuvwxyz23456789"; + const chars = []; + while (chars.length < 12) { + // Bytes at or above 248 (8 × 31) are skipped so every character is equally likely. + for (const b of crypto.getRandomValues(new Uint8Array(16))) { + if (b < 248 && chars.length < 12) chars.push(alphabet[b % alphabet.length]); + } + } + return `${chars.slice(0, 4).join("")}-${chars.slice(4, 8).join("")}-${chars.slice(8).join("")}`; +} + +function cap(s) { + return s.charAt(0).toUpperCase() + s.slice(1); +} diff --git a/src/Titano.Server/Admin/web/ui.js b/src/Titano.Server/Admin/web/ui.js new file mode 100644 index 00000000..a9f25e67 --- /dev/null +++ b/src/Titano.Server/Admin/web/ui.js @@ -0,0 +1,146 @@ +// Small DOM and formatting helpers shared by the admin pages. No inline styles: the panel runs under a strict CSP. +import { t, culture } from "./i18n.js"; + +export function escapeHtml(s) { + return String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c])); +} + +/** Escaped attribute value for title="…" tooltips. */ +export const attr = escapeHtml; + +export function formatNumber(n) { + return new Intl.NumberFormat(culture()).format(n ?? 0); +} + +/** 9.4 TB, 610 GB … (decimal units, like disk vendors and the desktop client). */ +export function formatBytes(bytes) { + const units = ["B", "KB", "MB", "GB", "TB", "PB"]; + let value = Number(bytes ?? 0); + let unit = 0; + while (value >= 1000 && unit < units.length - 1) { + value /= 1000; + unit++; + } + const digits = value >= 100 || unit === 0 ? 0 : 1; + return `${new Intl.NumberFormat(culture(), { maximumFractionDigits: digits }).format(value)} ${units[unit]}`; +} + +export function formatTime(iso) { + if (!iso) return "—"; + const date = new Date(iso); + const sameDay = new Date().toDateString() === date.toDateString(); + return new Intl.DateTimeFormat(culture(), sameDay ? { timeStyle: "short" } : { dateStyle: "medium" }).format(date); +} + +export function formatDateTime(iso) { + return iso ? new Intl.DateTimeFormat(culture(), { dateStyle: "short", timeStyle: "medium" }).format(new Date(iso)) : "—"; +} + +export function formatDuration(seconds) { + const s = Math.max(0, Math.round(seconds ?? 0)); + if (s >= 86400) return t("Common.Duration.Days", Math.floor(s / 86400), Math.floor((s % 86400) / 3600)); + if (s >= 3600) return t("Common.Duration.Hours", Math.floor(s / 3600), Math.floor((s % 3600) / 60)); + if (s >= 60) return t("Common.Duration.Minutes", Math.floor(s / 60)); + return t("Common.Duration.Seconds", s); +} + +/** 6 px progress bar drawn as SVG (attributes, not inline styles). */ +export function bar(fraction, muted = false) { + const width = Math.max(0, Math.min(100, Math.round((fraction ?? 0) * 100))); + return ``; +} + +/** + * Column chart: values with labels under each bar and, when given, the value above it. + * Bars use the accent colour, or the sun colour for upload activity. + */ +export function columns(items, { sun = false, valueLabel = null, height = 190 } = {}) { + const width = 400; + const base = height - 30; + const max = Math.max(1, ...items.map((i) => i.value)); + const step = width / Math.max(1, items.length); + const barWidth = Math.max(2, Math.min(32, step * 0.65)); + let svg = ``; + svg += ``; + items.forEach((item, i) => { + const h = item.value > 0 ? Math.max(2, (item.value / max) * (base - 18)) : 0; + const x = i * step + (step - barWidth) / 2; + svg += `${escapeHtml(item.title ?? "")}`; + if (valueLabel) svg += `${escapeHtml(valueLabel(item))}`; + if (item.label) svg += `${escapeHtml(item.label)}`; + }); + return svg + ""; +} + +/** Shows a toast at the bottom right for 8 s (hover pauses). */ +export function toast(message, isError = false) { + const host = document.getElementById("toasts") ?? document.body.appendChild(Object.assign(document.createElement("div"), { id: "toasts" })); + const item = document.createElement("div"); + item.className = `toast${isError ? " error" : ""}`; + item.setAttribute("role", isError ? "alert" : "status"); + item.textContent = message; + host.appendChild(item); + let remaining = 8000; + let started = Date.now(); + let timer = setTimeout(() => item.remove(), remaining); + item.addEventListener("mouseenter", () => { + clearTimeout(timer); + remaining -= Date.now() - started; + }); + item.addEventListener("mouseleave", () => { + started = Date.now(); + timer = setTimeout(() => item.remove(), remaining); + }); +} + +/** + * Two-button confirmation dialog (safe action left and default). Resolves true when the destructive action is chosen; + * with `typed`, that button stays disabled until the text is typed. + */ +export function confirmDialog({ title, body, confirm, confirmTip, cancel, cancelTip, typed }) { + return new Promise((resolve) => { + const dialog = document.createElement("dialog"); + dialog.className = "dialog"; + dialog.innerHTML = ` +
+

${escapeHtml(title)}

+ ${body ? `

${escapeHtml(body)}

` : ""} + ${typed ? `` : ""} +
+ + +
+
`; + document.body.appendChild(dialog); + const ok = dialog.querySelector('button[value="ok"]'); + dialog.querySelector('input[name="typed"]')?.addEventListener("input", (e) => { + ok.disabled = e.target.value !== typed; + }); + dialog.addEventListener("close", () => { + resolve(dialog.returnValue === "ok"); + dialog.remove(); + }); + dialog.showModal(); + }); +} + +/** The localised message of an API failure, or the generic one when the server was not reached. */ +export function errorText(err) { + return err?.message && err.status ? err.message : t("Admin.Error.Unreachable"); +} + +/** Query parameters of the hash route (#/logs?view=signins). */ +export function routeParams() { + return new URLSearchParams(location.hash.split("?")[1] ?? ""); +} + +/** A language's own name ("Italiano"), for language menus. */ +export function nativeName(code) { + try { + const name = new Intl.DisplayNames([code], { type: "language" }).of(code) ?? code; + return name.charAt(0).toLocaleUpperCase(code) + name.slice(1); + } catch { + return code; + } +} diff --git a/src/Titano.Server/Api/AdminEndpoints.cs b/src/Titano.Server/Api/AdminEndpoints.cs new file mode 100644 index 00000000..45826700 --- /dev/null +++ b/src/Titano.Server/Api/AdminEndpoints.cs @@ -0,0 +1,112 @@ +using System.Text; +using Titano.Core.Model; +using Titano.Data; +using Titano.Protocol; +using Titano.Server.Configuration; +using Titano.Server.Jobs; +using Titano.Server.Security; +using Titano.Server.Storage; + +namespace Titano.Server.Api; + +/// Administration (BRIEF §14.8): dashboard figures, server settings, log tail and downloads, audit log. +public static class AdminEndpoints +{ + private const int MaxTailLines = 2000; + private const int TailWindowBytes = 1 << 20; + + public static void Map(RouteGroupBuilder api) + { + api.MapGet("/server/dashboard", DashboardAsync).Requires(Permissions.ManageArchive | Permissions.ManageUsers); + api.MapGet("/server/settings", (ServerSettingsStore store) => Results.Json(store.Current(), ProtocolJson.Default.ServerSettingsDto)) + .Requires(Permissions.ManageArchive); + api.MapPut("/server/settings", SaveSettingsAsync).Requires(Permissions.ManageArchive); + api.MapGet("/audit", AuditAsync).Requires(Permissions.ManageUsers | Permissions.ManageArchive); + + var logs = api.MapGroup("/logs").Requires(Permissions.ManageArchive); + logs.MapGet("", TailAsync); + logs.MapGet("/files", (ServerOptions options) => Results.Json(LogFiles(options).Select(f => new LogFileDto(f.Name, f.Length, f.LastWriteTimeUtc)).ToList(), + ProtocolJson.Default.ListLogFileDto)); + logs.MapGet("/files/{name}", (HttpContext context, string name, ServerOptions options) => + { + // Only names of existing log files are served: the name never becomes part of a path. + if (LogFiles(options).FirstOrDefault(f => f.Name == name) is not { } file) return ApiErrors.NotFound(context, "log"); + context.Response.Headers.CacheControl = "no-store"; + return Results.File(file.FullName, "text/plain; charset=utf-8", file.Name, file.LastWriteTimeUtc, enableRangeProcessing: true); + }); + } + + private static async Task DashboardAsync(HttpContext context, TitanoDatabase db, ServerArchive archive, ServerOptions options, ServerState state, + JobEngineHost jobs, TimeProvider time) + { + var ct = context.RequestAborted; + var now = time.GetUtcNow(); + var today = new DateTimeOffset(now.UtcDateTime.Date, TimeSpan.Zero); + var users = await db.Users.ListAsync(ct); + var names = users.ToDictionary(u => u.Id, u => u.Username); + var counts = await db.Jobs.CountAsync(today, ct); + var running = await db.Jobs.ListAsync(JobState.Running, null, 10, null, ct); + var queued = await db.Jobs.ListAsync(JobState.Queued, null, 10, null, ct); + var nodes = await db.RenderNodes.ListAsync(ct); + var uploads = await db.Uploads.CommittedPerDayAsync(today.AddDays(-29), ct); + var activity = await db.Audit.ListAsync(null, null, 12, null, ct); + return Results.Json(new DashboardDto( + options.ServerName, + SystemEndpoints.Version, + (long)(now - state.StartedAt).TotalSeconds, + state.Database, + options.TlsMode.ToString().ToLowerInvariant(), + options.Discovery, + users.Count, + await ArchiveEndpoints.StatsAsync(db, archive, ct), + new JobCountsDto(counts.Queued, counts.Running, counts.Paused, counts.DoneSince, counts.FailedSince), + jobs.Workers, + [.. nodes.Select(n => Dto.Node(n, names.GetValueOrDefault(n.UserId, string.Empty)))], + [.. uploads.Select(u => new UploadDayDto(u.Day, u.Count, u.Bytes))], + [.. activity.Items.Select(Dto.Audit)], + [.. running.Items.Concat(queued.Items.OrderByDescending(j => j.Priority).ThenBy(j => j.Created)).Take(10).Select(Dto.Job)]), + ProtocolJson.Default.DashboardDto); + } + + private static async Task SaveSettingsAsync(HttpContext context, Caller caller, ServerSettingsDto request, ServerSettingsStore store, AuditLog audit) + { + switch (store.Save(request)) + { + case ("setting_locked", var setting): + return ApiErrors.Conflict(context, "setting_locked", setting); + case (var code, var setting): + return ApiErrors.BadRequest(context, code, setting, setting); + } + await audit.WriteAsync(caller, context, "server_settings", "server", null, request.ServerName); + return Results.Json(store.Current(), ProtocolJson.Default.ServerSettingsDto); + } + + private static async Task AuditAsync(HttpContext context, TitanoDatabase db, Guid? user, string? action, string? cursor, int? limit) + { + var page = await db.Audit.ListAsync(user, string.IsNullOrWhiteSpace(action) ? null : action, limit ?? 100, + string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted); + return Results.Json(new Page([.. page.Items.Select(Dto.Audit)], page.NextCursor, null), ProtocolJson.Default.PageAuditEntryDto); + } + + /// The last lines lines (default 300) of the newest server log. + private static async Task TailAsync(HttpContext context, ServerOptions options, int? lines) + { + var take = Math.Clamp(lines ?? 300, 1, MaxTailLines); + if (LogFiles(options).FirstOrDefault() is not { } file) return Results.Json(new LogTailDto(string.Empty, []), ProtocolJson.Default.LogTailDto); + await using var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); + var start = Math.Max(0, stream.Length - TailWindowBytes); + stream.Position = start; + using var reader = new StreamReader(stream, Encoding.UTF8); + var text = await reader.ReadToEndAsync(context.RequestAborted); + var all = text.Split('\n', StringSplitOptions.RemoveEmptyEntries); + // A window that starts mid-file begins with a partial line. + var complete = start > 0 && all.Length > 0 ? all[1..] : all; + return Results.Json(new LogTailDto(file.Name, [.. complete.TakeLast(take).Select(l => l.TrimEnd('\r'))]), ProtocolJson.Default.LogTailDto); + } + + /// Server log files, newest first. + private static List LogFiles(ServerOptions options) => + Directory.Exists(options.LogPath) + ? [.. new DirectoryInfo(options.LogPath).EnumerateFiles("*.log").OrderByDescending(f => f.LastWriteTimeUtc).ThenByDescending(f => f.Name)] + : []; +} diff --git a/src/Titano.Server/Api/ApiErrors.cs b/src/Titano.Server/Api/ApiErrors.cs new file mode 100644 index 00000000..372962c5 --- /dev/null +++ b/src/Titano.Server/Api/ApiErrors.cs @@ -0,0 +1,90 @@ +using System.Globalization; +using System.Text; +using Microsoft.Net.Http.Headers; +using Titano.Core.Localization; +using Titano.Protocol; +using Titano.Server.Configuration; + +namespace Titano.Server.Api; + +/// +/// Error responses of API v1: { code, message, details } (BRIEF §14.3). code is a stable snake_case +/// identifier for programs; message is one localised sentence (resource Server.Error.<Code>, language +/// from Accept-Language, else the server language); details is optional machine-readable context. +/// +public static class ApiErrors +{ + public static IResult Result(HttpContext context, int status, string code, string? details = null, params object?[] args) => + Results.Json(new ApiError(code, Message(context, code, args), details), ProtocolJson.Default.ApiError, statusCode: status); + + public static Task WriteAsync(HttpContext context, int status, string code, string? details = null) => + Result(context, status, code, details).ExecuteAsync(context); + + public static IResult NotFound(HttpContext context, string? what = null) => Result(context, StatusCodes.Status404NotFound, "not_found", what); + + public static IResult BadRequest(HttpContext context, string code, string? details = null, params object?[] args) => + Result(context, StatusCodes.Status400BadRequest, code, details, args); + + public static IResult Conflict(HttpContext context, string code, string? details = null) => Result(context, StatusCodes.Status409Conflict, code, details); + + public static IResult Forbidden(HttpContext context) => Result(context, StatusCodes.Status403Forbidden, "forbidden"); + + public static string Message(HttpContext context, string code, params object?[] args) + { + var culture = RequestCulture.Of(context); + var template = Loc.Get("Server.Error." + Pascal(code), culture) ?? code; + return args.Length == 0 ? template : string.Format(culture, template, args); + } + + private static string Pascal(string code) + { + var sb = new StringBuilder(code.Length); + var upper = true; + foreach (var c in code) + { + if (c == '_') + { + upper = true; + continue; + } + sb.Append(upper ? char.ToUpperInvariant(c) : c); + upper = false; + } + return sb.ToString(); + } +} + +/// The language of a request's messages: the best shipped match of Accept-Language, else the server language. +public static class RequestCulture +{ + public static CultureInfo Of(HttpContext context) + { + if (context.Items.TryGetValue(typeof(RequestCulture), out var cached) && cached is CultureInfo known) return known; + var fallback = context.RequestServices.GetRequiredService().Language; + var culture = FromHeader(context.Request.Headers.AcceptLanguage.ToString()) ?? Parse(fallback) ?? CultureInfo.GetCultureInfo("en"); + context.Items[typeof(RequestCulture)] = culture; + return culture; + } + + private static CultureInfo? FromHeader(string header) + { + if (string.IsNullOrWhiteSpace(header) || !StringWithQualityHeaderValue.TryParseList([header], out var values)) return null; + foreach (var value in values.OrderByDescending(v => v.Quality ?? 1)) + { + if (value.Value.Value is { } tag && tag != "*" && Parse(tag) is { } culture) return culture; + } + return null; + } + + private static CultureInfo? Parse(string tag) + { + try + { + return Languages.BestMatch(CultureInfo.GetCultureInfo(tag)); + } + catch (CultureNotFoundException) + { + return null; + } + } +} diff --git a/src/Titano.Server/Api/ArchiveEndpoints.cs b/src/Titano.Server/Api/ArchiveEndpoints.cs new file mode 100644 index 00000000..daf097d1 --- /dev/null +++ b/src/Titano.Server/Api/ArchiveEndpoints.cs @@ -0,0 +1,95 @@ +using Titano.Core.Model; +using Titano.Core.Naming; +using Titano.Data; +using Titano.Data.Repositories; +using Titano.Protocol; +using Titano.Server.Jobs; +using Titano.Server.Security; +using Titano.Server.Storage; + +namespace Titano.Server.Api; + +/// /api/v1/archive (admin): templates and proxy policy, scan/rebuild/verify/maintenance jobs, statistics. +public static class ArchiveEndpoints +{ + public static void Map(RouteGroupBuilder api) + { + var archive = api.MapGroup("/archive").Requires(Permissions.ManageArchive); + archive.MapGet("/settings", (ServerArchive a) => Results.Json(Settings(a.Current), ProtocolJson.Default.ArchiveSettingsDto)); + archive.MapPut("/settings", UpdateAsync); + archive.MapGet("/stats", async (HttpContext c, TitanoDatabase db, ServerArchive a) => + Results.Json(await StatsAsync(db, a, c.RequestAborted), ProtocolJson.Default.ArchiveStatsDto)); + foreach (var (path, kind) in new[] { ("scan", JobKind.Scan), ("rebuild", JobKind.Rebuild), ("verify", JobKind.Verify), ("maintenance", JobKind.Maintenance) }) + { + archive.MapPost("/" + path, (HttpContext c, Caller caller, ServerArchive a, JobEngineHost host, AuditLog audit) => EnqueueAsync(c, caller, a, host, audit, kind)); + } + } + + /// Totals, distributions, free space and health of the archive (also shown on the dashboard). + public static async Task StatsAsync(TitanoDatabase db, ServerArchive archive, CancellationToken cancellationToken) + { + var stats = await db.Sequences.GetStatsAsync(archive.Id, cancellationToken); + var withoutProxies = await db.Sequences.CountAsync( + new SequenceQuery { ArchiveId = archive.Id, WithoutStatus = SequenceStatus.ProxiesBuilt }, cancellationToken); + long free = 0, capacity = 0; + try + { + var drive = new DriveInfo(archive.Root); + free = drive.AvailableFreeSpace; + capacity = drive.TotalSize; + } + catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException) + { + // An offline archive volume reports no space figures. + } + return new ArchiveStatsDto( + stats.Sequences, + stats.Photos, + stats.Bytes, + free, + capacity, + stats.ByCamera.Count(c => c.Model is not null), + stats.ByYear.Count > 0 ? stats.ByYear[0].Year : null, + stats.ByYear.Count > 0 ? stats.ByYear[^1].Year : null, + [.. stats.ByYear.Select(y => new KeyValueCount(y.Year.ToString(System.Globalization.CultureInfo.InvariantCulture), y.Photos, y.Bytes))], + [.. stats.ByMonth.Select(m => new KeyValueCount($"{m.Year:D4}-{m.Month:D2}", m.Photos, 0))], + [.. stats.ByCamera.Select(c => new KeyValueCount(string.Join(' ', new[] { c.Make, c.Model }.Where(v => !string.IsNullOrEmpty(v))), c.Photos, c.Bytes))], + [.. stats.ByFormat.Select(f => new KeyValueCount(f.Format.ToString().ToLowerInvariant(), f.Photos, f.Bytes))], + withoutProxies, + stats.Health.Unverified); + } + + private static async Task UpdateAsync(HttpContext context, Caller caller, ArchiveSettingsDto request, TitanoDatabase db, ServerArchive archive, AuditLog audit) + { + var name = request.Name?.Trim(); + if (string.IsNullOrEmpty(name) || name.Length > 100) return ApiErrors.BadRequest(context, "bad_request", "name"); + foreach (var (field, template) in new[] { ("folderTemplate", request.FolderTemplate), ("fileTemplate", request.FileTemplate) }) + { + if (string.IsNullOrWhiteSpace(template) || !PathTemplate.Parse(template).IsValid) return ApiErrors.BadRequest(context, "invalid_template", field); + } + if (request.ProxyLongEdge is not (-1 or 0) and (< 256 or > 8192)) return ApiErrors.BadRequest(context, "bad_request", "proxyLongEdge"); + + var updated = archive.Current with + { + Name = name, + FolderTemplate = request.FolderTemplate, + FileTemplate = request.FileTemplate, + ProxyLongEdge = request.ProxyLongEdge, + }; + await db.Archives.UpsertAsync(updated, context.RequestAborted); + archive.Current = updated; + await audit.WriteAsync(caller, context, "archive_settings", "archive", updated.Id.ToString(), $"{updated.FolderTemplate} · {updated.FileTemplate}"); + return Results.Json(Settings(updated), ProtocolJson.Default.ArchiveSettingsDto); + } + + private static async Task EnqueueAsync(HttpContext context, Caller caller, ServerArchive archive, JobEngineHost host, AuditLog audit, JobKind kind) + { + if (!host.CanRun(kind)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(kind)); + var job = await host.Engine.EnqueueAsync(kind, [archive.Id], archive.Current.Name, priority: JobPriority.User, createdBy: caller.Id, + cancellationToken: context.RequestAborted); + await audit.WriteAsync(caller, context, "job_created", "job", job.Id.ToString(), JobWire.Kind(kind)); + return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted); + } + + private static ArchiveSettingsDto Settings(Archive a) => new(a.Id, a.Name, a.RootPath, a.FolderTemplate, a.FileTemplate, a.ProxyLongEdge); +} diff --git a/src/Titano.Server/Api/AuthEndpoints.cs b/src/Titano.Server/Api/AuthEndpoints.cs new file mode 100644 index 00000000..6f9d30f3 --- /dev/null +++ b/src/Titano.Server/Api/AuthEndpoints.cs @@ -0,0 +1,105 @@ +using System.Globalization; +using Titano.Core.Model; +using Titano.Data; +using Titano.Protocol; +using Titano.Server.Configuration; +using Titano.Server.Security; + +namespace Titano.Server.Api; + +/// /api/v1/auth: sign-in (token or admin cookie), sign-out, password change, current account. +public static class AuthEndpoints +{ + public static void Map(RouteGroupBuilder api) + { + api.MapPost("/auth/login", LoginAsync).AllowAnonymousCaller(); + api.MapPost("/auth/logout", LogoutAsync).AllowDuringPasswordChange(); + api.MapPost("/auth/password", ChangePasswordAsync).AllowDuringPasswordChange(); + api.MapGet("/auth/me", (Caller caller) => Results.Json(Dto.User(caller.User), ProtocolJson.Default.UserDto)).AllowDuringPasswordChange(); + } + + private static async Task LoginAsync(HttpContext context, LoginRequest request, TitanoDatabase db, TokenService tokens, LoginGuard guard, + AuditLog audit, TimeProvider time) + { + var ct = context.RequestAborted; + if (!guard.TryAttempt(context.Connection.RemoteIpAddress?.ToString() ?? "unknown", out var wait)) return TooMany(context, "rate_limited", wait); + var account = Accounts.Key(request.Username); + if (!Accounts.IsValidUsername(account) || string.IsNullOrEmpty(request.Password)) + return ApiErrors.Result(context, StatusCodes.Status401Unauthorized, "invalid_credentials"); + if (guard.IsLocked(account, out wait)) return TooMany(context, "account_locked", wait); + if (request.Cookie && !SessionCookies.IsSecureTransport(context)) return ApiErrors.BadRequest(context, "https_required"); + + var user = await db.Users.GetByUsernameAsync(account, ct); + bool valid; + if (user is null) + { + PasswordHasher.VerifyDummy(request.Password); + valid = false; + } + else + { + valid = PasswordHasher.Verify(request.Password, user.PasswordHash) && !user.Disabled; + } + if (!valid) + { + guard.RecordFailure(account); + await audit.WriteAsync(user?.Id, account, context, "login_failed", "user", user?.Id.ToString()); + return ApiErrors.Result(context, StatusCodes.Status401Unauthorized, "invalid_credentials"); + } + + guard.RecordSuccess(account); + var signedIn = user! with { LastLogin = time.GetUtcNow() }; + if (PasswordHasher.NeedsRehash(signedIn.PasswordHash)) + { + signedIn = signedIn with { PasswordHash = PasswordHasher.Hash(request.Password) }; + await db.Users.UpdateAsync(signedIn, ct); + } + else + { + await db.Users.RecordLoginAsync(signedIn.Id, signedIn.LastLogin.Value, ct); + } + var issued = await tokens.IssueAsync(signedIn, request.ClientName, ct); + await audit.WriteAsync(signedIn.Id, signedIn.Username, context, "login", "user", signedIn.Id.ToString(), issued.Stored.ClientName); + + string? secret = issued.Secret; + if (request.Cookie) + { + // The admin panel never sees the token itself; it travels only in the HttpOnly cookie. + SessionCookies.Issue(context, issued); + secret = null; + } + return Results.Json(new LoginResponse(secret, Dto.User(signedIn), signedIn.MustChangePassword, issued.Stored.Expires), ProtocolJson.Default.LoginResponse); + } + + private static async Task LogoutAsync(HttpContext context, Caller caller, TitanoDatabase db) + { + await db.Tokens.RevokeAsync(caller.TokenHash, context.RequestAborted); + if (caller.Source == CredentialSource.Cookie) SessionCookies.Clear(context); + return Results.NoContent(); + } + + private static async Task ChangePasswordAsync(HttpContext context, Caller caller, ChangePasswordRequest request, TitanoDatabase db, + AuditLog audit, ServerOptions options) + { + var ct = context.RequestAborted; + if (string.IsNullOrEmpty(request.CurrentPassword) || !PasswordHasher.Verify(request.CurrentPassword, caller.User.PasswordHash)) + return ApiErrors.BadRequest(context, "wrong_password"); + if (!PasswordHasher.IsAcceptable(request.NewPassword)) + return ApiErrors.BadRequest(context, "weak_password", PasswordHasher.MinimumLength.ToString(CultureInfo.InvariantCulture), PasswordHasher.MinimumLength); + if (request.NewPassword == request.CurrentPassword) return ApiErrors.BadRequest(context, "same_password"); + + var updated = caller.User with { PasswordHash = PasswordHasher.Hash(request.NewPassword), MustChangePassword = false }; + await db.Users.UpdateAsync(updated, ct); + await db.Tokens.RevokeAllExceptAsync(caller.Id, caller.TokenHash, ct); + await audit.WriteAsync(caller, context, "password_changed", "user", caller.Id.ToString()); + if (caller.User.Role == UserRole.Admin) AdminBootstrap.ForgetInitialPassword(options); + return Results.NoContent(); + } + + private static IResult TooMany(HttpContext context, string code, TimeSpan wait) + { + var seconds = Math.Max(1, (int)Math.Ceiling(wait.TotalSeconds)); + context.Response.Headers.RetryAfter = seconds.ToString(CultureInfo.InvariantCulture); + return ApiErrors.Result(context, StatusCodes.Status429TooManyRequests, code, seconds.ToString(CultureInfo.InvariantCulture)); + } +} diff --git a/src/Titano.Server/Api/Dto.cs b/src/Titano.Server/Api/Dto.cs new file mode 100644 index 00000000..7d02ecd3 --- /dev/null +++ b/src/Titano.Server/Api/Dto.cs @@ -0,0 +1,90 @@ +using System.Text; +using System.Text.Json; +using Titano.Core.Model; +using Titano.Protocol; + +namespace Titano.Server.Api; + +/// Domain entities → API v1 contracts. +public static class Dto +{ + public static UserDto User(User u) => new( + u.Id, u.Username, u.DisplayName, Role(u.Role), (long)u.Permissions, u.QuotaBytes, u.UsedBytes, u.Disabled, u.MustChangePassword, + u.Created, u.LastLogin); + + public static string Role(UserRole role) => role.ToString().ToLowerInvariant(); + + public static SequenceDto Sequence(Sequence s) => new( + s.Id, s.Title, s.DisplayTitle, s.Folder, s.CameraMake, s.CameraModel, s.Lens, s.FirstCapture, s.LastCapture, s.FrameCount, + s.MedianInterval, s.TotalBytes, (long)s.Status, s.RenderedCount, s.HeroFrameId, s.Tags, s.Rating, s.Notes, s.SettingsVersion, + s.FramesVersion, s.WarningCount, s.OwnerUserId, s.UpdatedAt); + + public static FrameDto Frame(Frame f) => new( + f.Id, f.SequenceId, f.Index, f.FileName, f.Size, f.Sha256, f.CapturedAt, f.UtcOffsetMinutes, f.ExposureTime, f.FNumber, f.Iso, + f.FocalLength, f.Orientation, f.Width, f.Height, f.Format.ToString().ToLowerInvariant(), (long)f.Flags); + + public static RenderDto Render(Render r) => new(r.Id, r.PresetName, r.Codec, r.Width, r.Height, r.Fps, r.DurationSeconds, r.Bytes, r.Created, r.ProducedBy); + + public static JobDto Job(Job j) => new( + j.Id, JobWire.Kind(j.Kind), j.TargetIds, j.TargetLabel, JobWire.State(j.State), JobWire.Priority(j.Priority), j.ProgressDone, + j.ProgressTotal, j.ItemsPerSecond, j.Eta?.TotalSeconds, j.LogTail, j.NodeId, j.Attempts, j.Error, j.Created, j.Started, j.Finished); + + public static AuditEntryDto Audit(AuditEntry a) => new(a.Id, a.At, a.UserId, a.Username, a.Action, a.TargetKind, a.TargetId, a.Details, a.RemoteAddress); + + public static RenderNodeDto Node(RenderNode node, string username) => + new(node.Id, node.ClientName, username, Capabilities(node)?.Encoders ?? [], node.LastHeartbeat, node.State); + + /// What a node announced at registration (stored as its capabilities JSON). + public static RegisterNodeRequest? Capabilities(RenderNode node) + { + if (node.CapabilitiesJson is null) return null; + try + { + return JsonSerializer.Deserialize(node.CapabilitiesJson, ProtocolJson.Default.RegisterNodeRequest); + } + catch (JsonException) + { + return null; + } + } +} + +/// Wire names of job kinds, states and priorities: kebab-case (deep-analyse) and lower-case (running). +public static class JobWire +{ + public static string Kind(JobKind kind) + { + var name = kind.ToString(); + var sb = new StringBuilder(name.Length + 4); + for (var i = 0; i < name.Length; i++) + { + if (i > 0 && char.IsUpper(name[i])) sb.Append('-'); + sb.Append(char.ToLowerInvariant(name[i])); + } + return sb.ToString(); + } + + public static bool TryParseKind(string? text, out JobKind kind) => TryParse(text, Kind, out kind); + + public static string State(JobState state) => state.ToString().ToLowerInvariant(); + + public static bool TryParseState(string? text, out JobState state) => TryParse(text, State, out state); + + public static string Priority(JobPriority priority) => priority.ToString().ToLowerInvariant(); + + public static bool TryParsePriority(string? text, out JobPriority priority) => TryParse(text, Priority, out priority); + + private static bool TryParse(string? text, Func name, out T value) where T : struct, Enum + { + foreach (var candidate in Enum.GetValues()) + { + if (string.Equals(name(candidate), text, StringComparison.OrdinalIgnoreCase)) + { + value = candidate; + return true; + } + } + value = default; + return false; + } +} diff --git a/src/Titano.Server/Api/FileEndpoints.cs b/src/Titano.Server/Api/FileEndpoints.cs new file mode 100644 index 00000000..71bfc53e --- /dev/null +++ b/src/Titano.Server/Api/FileEndpoints.cs @@ -0,0 +1,59 @@ +using System.Globalization; +using Titano.Core.Model; +using Titano.Data; +using Titano.Server.Security; +using Titano.Server.Storage; + +namespace Titano.Server.Api; + +/// +/// Streamed archive files with Range support (BRIEF §14.3): originals, proxies, thumbnails and exports. Paths come +/// from the database by id and are resolved under the archive root only. +/// +public static class FileEndpoints +{ + private const string Immutable = "private, max-age=31536000, immutable"; + private const string Regenerable = "private, max-age=3600"; + + public static void Map(RouteGroupBuilder api) + { + // Originals feed editing clients and render nodes; viewers browse proxies, thumbnails and exports. + api.MapGet("/frames/{id:guid}/original", OriginalAsync).Requires(Permissions.Edit | Permissions.RenderNode); + api.MapGet("/frames/{id:guid}/proxy", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Proxy, db, a, l)) + .Requires(Permissions.Read | Permissions.RenderNode); + api.MapGet("/frames/{id:guid}/thumb", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Thumbnail, db, a, l)) + .Requires(Permissions.Read); + api.MapGet("/renders/{id:guid}/file", RenderAsync).Requires(Permissions.Read); + } + + private static async Task OriginalAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive) + { + if (await db.Frames.GetAsync(id, context.RequestAborted) is not { } frame) return ApiErrors.NotFound(context, "frame"); + if (ArchivePaths.ExistingFile(archive.Root, frame.RelativePath) is not { } file) return ApiErrors.NotFound(context, "file_missing"); + return FileResponses.Serve(context, file, 0, file.Length, FileResponses.ContentTypeOf(frame.FileName), Immutable, frame.Sha256, frame.FileName) + ?? ApiErrors.NotFound(context, "file_missing"); + } + + private static async Task AssetAsync(HttpContext context, Guid id, FrameAsset kind, TitanoDatabase db, ServerArchive archive, IFrameAssetLocator locator) + { + var ct = context.RequestAborted; + if (await db.Frames.GetAsync(id, ct) is not { } frame || await db.Sequences.GetAsync(frame.SequenceId, ct) is not { } sequence) + return ApiErrors.NotFound(context, "frame"); + var location = await locator.LocateAsync(archive.Current, sequence, frame, kind, ct); + if (location is null || !ArchivePaths.IsInside(archive.Root, location.Path)) return ApiErrors.NotFound(context, "asset_missing"); + var file = new FileInfo(location.Path); + if (!file.Exists || file.LinkTarget is not null) return ApiErrors.NotFound(context, "asset_missing"); + var etag = string.Create(CultureInfo.InvariantCulture, $"{kind}-{frame.Id:N}-{file.LastWriteTimeUtc.Ticks}-{location.Offset}"); + return FileResponses.Serve(context, file, location.Offset, location.Length, location.ContentType, Regenerable, etag) + ?? ApiErrors.NotFound(context, "asset_missing"); + } + + private static async Task RenderAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive) + { + if (await db.Renders.GetAsync(id, context.RequestAborted) is not { } render) return ApiErrors.NotFound(context, "render"); + if (ArchivePaths.ExistingFile(archive.Root, render.OutputPath) is not { } file) return ApiErrors.NotFound(context, "file_missing"); + var etag = string.Create(CultureInfo.InvariantCulture, $"r-{render.Id:N}-{file.Length}-{file.LastWriteTimeUtc.Ticks}"); + return FileResponses.Serve(context, file, 0, file.Length, FileResponses.ContentTypeOf(file.Name), Immutable, etag, file.Name) + ?? ApiErrors.NotFound(context, "file_missing"); + } +} diff --git a/src/Titano.Server/Api/JobEndpoints.cs b/src/Titano.Server/Api/JobEndpoints.cs new file mode 100644 index 00000000..c07e5f00 --- /dev/null +++ b/src/Titano.Server/Api/JobEndpoints.cs @@ -0,0 +1,155 @@ +using System.Text.Json; +using Titano.Core.Model; +using Titano.Data; +using Titano.Protocol; +using Titano.Server.Jobs; +using Titano.Server.Security; +using Titano.Server.Storage; + +namespace Titano.Server.Api; + +/// +/// /api/v1/jobs (BRIEF §14.6): queue and history, creation, cancel, pause, resume, retry and priority. Changes +/// reach WebSocket clients as jobState and jobProgress events. +/// +public static class JobEndpoints +{ + /// Kinds that work on sequences (targets are sequence ids) and need the edit right. + private static readonly HashSet SequenceKinds = [JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Proxies, JobKind.Thumbs, JobKind.Verify]; + + /// Archive-wide maintenance (target: the archive) for administrators. + private static readonly HashSet ArchiveKinds = [JobKind.Scan, JobKind.Rebuild, JobKind.Maintenance, JobKind.Duplicates, JobKind.Orphans]; + + public static void Map(RouteGroupBuilder api) + { + var jobs = api.MapGroup("/jobs").Requires(Permissions.Read); + jobs.MapGet("", ListAsync); + jobs.MapPost("", CreateAsync); + jobs.MapGet("/{id:guid}", async (HttpContext context, Guid id, TitanoDatabase db) => + await db.Jobs.GetAsync(id, context.RequestAborted) is { } job + ? Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto) + : ApiErrors.NotFound(context, "job")); + jobs.MapPost("/{id:guid}/cancel", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) => + ActAsync(c, caller, id, db, [JobState.Queued, JobState.Running, JobState.Paused], (job, ct) => host.Engine.CancelAsync(job.Id, ct))); + jobs.MapPost("/{id:guid}/pause", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) => + ActAsync(c, caller, id, db, [JobState.Queued, JobState.Running], (job, ct) => host.Engine.PauseAsync(job.Id, ct))); + jobs.MapPost("/{id:guid}/resume", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) => + ActAsync(c, caller, id, db, [JobState.Paused], (job, ct) => host.Engine.ResumeAsync(job.Id, ct))); + jobs.MapPost("/{id:guid}/retry", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) => + ActAsync(c, caller, id, db, [JobState.Failed, JobState.Cancelled], (job, ct) => host.Engine.ResumeAsync(job.Id, ct))); + jobs.MapPost("/{id:guid}/priority", PriorityAsync); + } + + private static async Task ListAsync(HttpContext context, TitanoDatabase db, string? state, string? kind, string? cursor, int? limit) + { + JobState? stateFilter = null; + JobKind? kindFilter = null; + if (!string.IsNullOrEmpty(state)) + { + if (!JobWire.TryParseState(state, out var s)) return ApiErrors.BadRequest(context, "invalid_filter", "state"); + stateFilter = s; + } + if (!string.IsNullOrEmpty(kind)) + { + if (!JobWire.TryParseKind(kind, out var k)) return ApiErrors.BadRequest(context, "invalid_filter", "kind"); + kindFilter = k; + } + var page = await db.Jobs.ListAsync(stateFilter, kindFilter, limit ?? 50, string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted); + return Results.Json(new Page([.. page.Items.Select(Dto.Job)], page.NextCursor, null), ProtocolJson.Default.PageJobDto); + } + + private static async Task CreateAsync(HttpContext context, Caller caller, CreateJobRequest request, TitanoDatabase db, JobEngineHost host, + ServerArchive archive, AuditLog audit) + { + var ct = context.RequestAborted; + if (!JobWire.TryParseKind(request.Kind, out var kind) || !(kind == JobKind.Render || SequenceKinds.Contains(kind) || ArchiveKinds.Contains(kind))) + return ApiErrors.BadRequest(context, "invalid_job_kind"); + var needed = kind == JobKind.Render ? Permissions.Render : SequenceKinds.Contains(kind) ? Permissions.Edit : Permissions.ManageArchive; + if (!caller.Has(needed)) return ApiErrors.Forbidden(context); + if (!host.CanRun(kind)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(kind)); + + IReadOnlyList targets; + string label; + if (ArchiveKinds.Contains(kind)) + { + targets = [archive.Id]; + label = archive.Current.Name; + } + else + { + var ids = request.Target ?? []; + if (ids.Count == 0 || (kind == JobKind.Render && ids.Count != 1)) return ApiErrors.BadRequest(context, "invalid_parameters", "target"); + var titles = new List(ids.Count); + foreach (var id in ids) + { + if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); + titles.Add(sequence.DisplayTitle); + } + targets = ids; + label = titles.Count == 1 ? titles[0] : string.Join(", ", titles.Take(3)) + (titles.Count > 3 ? " …" : string.Empty); + } + if (kind == JobKind.Render) + { + if (ParseRender(request.ParametersJson) is not { } render) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson"); + label = $"{label} → {render.PresetName}"; + } + else if (request.ParametersJson is not null && !IsJsonObject(request.ParametersJson)) + { + return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson"); + } + + var job = await host.Engine.EnqueueAsync(kind, targets, label, request.ParametersJson, JobPriority.User, caller.Id, ct); + await audit.WriteAsync(caller, context, "job_created", "job", job.Id.ToString(), $"{JobWire.Kind(kind)} · {label}"); + return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted); + } + + private static async Task PriorityAsync(HttpContext context, Caller caller, Guid id, JobPriorityRequest request, TitanoDatabase db, JobEngineHost host) + { + if (!JobWire.TryParsePriority(request.Priority, out var priority)) return ApiErrors.BadRequest(context, "invalid_priority"); + return await ActAsync(context, caller, id, db, [JobState.Queued, JobState.Paused], async (job, ct) => + { + await db.Jobs.SetPriorityAsync(job.Id, priority, ct); + host.Publish(job with { Priority = priority }); + }); + } + + /// Applies when the job is in one of states and the caller created it or administers the archive. + private static async Task ActAsync(HttpContext context, Caller caller, Guid id, TitanoDatabase db, JobState[] allowed, Func action) + { + var ct = context.RequestAborted; + if (await db.Jobs.GetAsync(id, ct) is not { } job) return ApiErrors.NotFound(context, "job"); + if (job.CreatedBy != caller.Id && !caller.Has(Permissions.ManageArchive)) return ApiErrors.Forbidden(context); + if (!allowed.Contains(job.State)) return ApiErrors.Conflict(context, "invalid_state", JobWire.State(job.State)); + await action(job, ct); + var updated = await db.Jobs.GetAsync(id, ct) ?? job; + return Results.Json(Dto.Job(updated), ProtocolJson.Default.JobDto); + } + + /// Render parameters, or null when they are missing or incomplete. + public static RenderJobParameters? ParseRender(string? json) + { + if (string.IsNullOrWhiteSpace(json)) return null; + try + { + var p = JsonSerializer.Deserialize(json, ProtocolJson.Default.RenderJobParameters); + return p is { PresetName.Length: > 0, PresetJson.Length: > 0, Codec.Length: > 0, Width: > 0, Height: > 0 } && IsJsonObject(p.PresetJson) ? p : null; + } + catch (JsonException) + { + return null; + } + } + + private static bool IsJsonObject(string json) + { + try + { + using var document = JsonDocument.Parse(json); + return document.RootElement.ValueKind == JsonValueKind.Object; + } + catch (JsonException) + { + return false; + } + } +} diff --git a/src/Titano.Server/Api/NodeEndpoints.cs b/src/Titano.Server/Api/NodeEndpoints.cs new file mode 100644 index 00000000..24594b1d --- /dev/null +++ b/src/Titano.Server/Api/NodeEndpoints.cs @@ -0,0 +1,57 @@ +using Titano.Core.Model; +using Titano.Data; +using Titano.Protocol; +using Titano.Server.Events; +using Titano.Server.Jobs; +using Titano.Server.Security; + +namespace Titano.Server.Api; + +/// /api/v1/nodes (BRIEF §14.7): render node registration, heartbeat, claim, progress, failure and output upload. +public static class NodeEndpoints +{ + public static void Map(RouteGroupBuilder api) + { + api.MapGet("/nodes", ListAsync).Requires(Permissions.Read | Permissions.RenderNode); + api.MapDelete("/nodes/{id:guid}", DeleteAsync).Requires(Permissions.RenderNode | Permissions.ManageArchive); + + var nodes = api.MapGroup("/nodes").Requires(Permissions.RenderNode); + nodes.MapPost("/register", async (HttpContext c, Caller caller, RegisterNodeRequest request, RenderNodeService service) => + (await service.RegisterAsync(caller, request, c.RequestAborted)).ToResult(c, n => Results.Json(n, ProtocolJson.Default.RenderNodeDto))); + nodes.MapPost("/{id:guid}/heartbeat", async (HttpContext c, Caller caller, Guid id, RenderNodeService service) => + (await service.HeartbeatAsync(caller, id, c.RequestAborted)).ToResult(c, _ => Results.NoContent())); + nodes.MapPost("/{id:guid}/claim", async (HttpContext c, Caller caller, Guid id, RenderNodeService service) => + (await service.ClaimAsync(caller, id, c.RequestAborted)).ToResult(c, job => job is null ? Results.NoContent() : Results.Json(job, ProtocolJson.Default.ClaimedJob))); + nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/progress", async (HttpContext c, Caller caller, Guid id, Guid jobId, NodeProgress progress, RenderNodeService service) => + (await service.ProgressAsync(caller, id, jobId, progress, c.RequestAborted)).ToResult(c, _ => Results.NoContent())); + nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/failed", async (HttpContext c, Caller caller, Guid id, Guid jobId, NodeJobFailure failure, RenderNodeService service) => + (await service.FailAsync(caller, id, jobId, failure, c.RequestAborted)).ToResult(c, _ => Results.NoContent())); + nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/result", async (HttpContext c, Caller caller, Guid id, Guid jobId, RenderResultRequest request, RenderNodeService service) => + (await service.StartResultAsync(caller, id, jobId, request, c.RequestAborted)) + .ToResult(c, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse, statusCode: StatusCodes.Status201Created))); + nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/result/complete", async (HttpContext c, Caller caller, Guid id, Guid jobId, RenderNodeService service, + IHostApplicationLifetime lifetime) => + // The commit runs to the end even if the node disconnects meanwhile. + (await service.CompleteResultAsync(caller, id, jobId, lifetime.ApplicationStopping)).ToResult(c, r => Results.Json(r, ProtocolJson.Default.RenderDto))); + } + + private static async Task ListAsync(HttpContext context, TitanoDatabase db) + { + var ct = context.RequestAborted; + var users = (await db.Users.ListAsync(ct)).ToDictionary(u => u.Id, u => u.Username); + var nodes = await db.RenderNodes.ListAsync(ct); + return Results.Json(nodes.Select(n => Dto.Node(n, users.GetValueOrDefault(n.UserId, string.Empty))).ToList(), ProtocolJson.Default.ListRenderNodeDto); + } + + /// Forgets a node (its owner or an archive admin); its running jobs go back to the queue. + private static async Task DeleteAsync(HttpContext context, Caller caller, Guid id, TitanoDatabase db, EventHub events) + { + var ct = context.RequestAborted; + if (await db.RenderNodes.GetAsync(id, ct) is not { } node) return ApiErrors.NotFound(context, "node"); + if (node.UserId != caller.Id && !caller.Has(Permissions.ManageArchive)) return ApiErrors.Forbidden(context); + await db.Jobs.RequeueRunningAsync(RenderNodeService.NodeKey(node), ct); + await db.RenderNodes.DeleteAsync(id, ct); + events.Publish(new NodeStateEvent(node.Id, node.ClientName, RenderNodeService.Offline)); + return Results.NoContent(); + } +} diff --git a/src/Titano.Server/Api/Outcome.cs b/src/Titano.Server/Api/Outcome.cs new file mode 100644 index 00000000..faf8d4df --- /dev/null +++ b/src/Titano.Server/Api/Outcome.cs @@ -0,0 +1,17 @@ +namespace Titano.Server.Api; + +/// Result of a server operation: a value, or an HTTP status with an API error code (and optional details). +public readonly record struct Outcome(T? Value, int Status, string? Code, string? Details) +{ + public bool Succeeded => Code is null; + + public static Outcome Ok(T value) => new(value, StatusCodes.Status200OK, null, null); + + public static Outcome Fail(int status, string code, string? details = null) => new(default, status, code, details); + + /// The API response: for a value, the error body otherwise. + public IResult ToResult(HttpContext context, Func ok) => + Succeeded ? ok(Value!) : ApiErrors.Result(context, Status, Code!, Details); + + public static implicit operator Outcome(T value) => Ok(value); +} diff --git a/src/Titano.Server/Api/SequenceEndpoints.cs b/src/Titano.Server/Api/SequenceEndpoints.cs new file mode 100644 index 00000000..dd687af4 --- /dev/null +++ b/src/Titano.Server/Api/SequenceEndpoints.cs @@ -0,0 +1,313 @@ +using System.Buffers.Text; +using System.Globalization; +using System.Text; +using System.Text.Json; +using Titano.Core.Model; +using Titano.Data; +using Titano.Data.Repositories; +using Titano.Protocol; +using Titano.Server.Configuration; +using Titano.Server.Events; +using Titano.Server.Security; +using Titano.Server.Storage; + +namespace Titano.Server.Api; + +/// +/// /api/v1/sequences: library listing with filters and keyset paging, detail with ETag, edits with +/// settings_version optimistic concurrency, the sync manifest, frames, analysis, statistics and renders. +/// +public static class SequenceEndpoints +{ + private const int MaxTitle = 200; + private const int MaxNotes = 2000; + private const int MaxTags = 50; + + public static void Map(RouteGroupBuilder api) + { + // Uploaders list sequences to pick an upload target. + var sequences = api.MapGroup("/sequences").Requires(Permissions.Read | Permissions.Upload); + sequences.MapGet("", ListAsync); + sequences.MapPost("", CreateAsync).Requires(Permissions.Upload | Permissions.Edit); + sequences.MapGet("/{id:guid}", GetAsync); + sequences.MapPatch("/{id:guid}", UpdateAsync).Requires(Permissions.Edit); + sequences.MapDelete("/{id:guid}", DeleteAsync).Requires(Permissions.Delete); + sequences.MapGet("/{id:guid}/manifest", ManifestAsync).Requires(Permissions.Read); + sequences.MapGet("/{id:guid}/frames", FramesAsync).Requires(Permissions.Read); + sequences.MapGet("/{id:guid}/analysis", AnalysisAsync).Requires(Permissions.Read); + sequences.MapGet("/{id:guid}/stats", StatsAsync).Requires(Permissions.Read); + sequences.MapGet("/{id:guid}/renders", RendersAsync).Requires(Permissions.Read); + } + + private static async Task ListAsync(HttpContext context, TitanoDatabase db, ServerArchive archive, int? year, string? camera, string? status, + string? tag, string? q, string? owner, string? sort, string? cursor, int? limit, Caller caller) + { + if (!TryParseStatus(status, out var with, out var without)) return ApiErrors.BadRequest(context, "invalid_filter", "status"); + if (!TryParseSort(sort, out var order)) return ApiErrors.BadRequest(context, "invalid_filter", "sort"); + Guid? ownerId = null; + if (owner == "me") ownerId = caller.Id; + else if (owner is not null && Guid.TryParse(owner, out var parsed)) ownerId = parsed; + else if (owner is not null) return ApiErrors.BadRequest(context, "invalid_filter", "owner"); + + var query = new SequenceQuery + { + ArchiveId = archive.Id, + Year = year, + Camera = string.IsNullOrWhiteSpace(camera) ? null : camera, + WithStatus = with, + WithoutStatus = without, + Tag = string.IsNullOrWhiteSpace(tag) ? null : tag, + Text = string.IsNullOrWhiteSpace(q) ? null : q, + OwnerUserId = ownerId, + Sort = order, + Limit = limit ?? 100, + Cursor = string.IsNullOrEmpty(cursor) ? null : cursor, + }; + var page = await db.Sequences.ListAsync(query, context.RequestAborted); + long? total = query.Cursor is null ? await db.Sequences.CountAsync(query, context.RequestAborted) : null; + return Results.Json(new Titano.Protocol.Page([.. page.Items.Select(Dto.Sequence)], page.NextCursor, total), ProtocolJson.Default.PageSequenceDto); + } + + private static async Task CreateAsync(HttpContext context, Caller caller, CreateSequenceRequest request, TitanoDatabase db, ServerArchive archive, + AuditLog audit, EventHub events, TimeProvider time) + { + var title = request.Title?.Trim(); + if (title is { Length: > MaxTitle }) return ApiErrors.BadRequest(context, "bad_request", "title"); + var hint = request.CameraHint?.Trim(); + var sequence = new Sequence + { + Id = Guid.CreateVersion7(), + ArchiveId = archive.Id, + Title = string.IsNullOrEmpty(title) ? null : title, + // Placed in the archive by the ingest when its first frame arrives. + Folder = string.Empty, + CameraModel = string.IsNullOrEmpty(hint) ? null : hint[..Math.Min(hint.Length, MaxTitle)], + Origin = SequenceOrigin.Server, + SyncState = SyncState.InSync, + OwnerUserId = caller.Id, + UpdatedAt = time.GetUtcNow(), + }; + await db.Sequences.UpsertAsync(sequence, context.RequestAborted); + await audit.WriteAsync(caller, context, "sequence_created", "sequence", sequence.Id.ToString(), sequence.DisplayTitle); + events.Publish(new SequenceChangedEvent(sequence.Id, "created", sequence.FramesVersion, sequence.SettingsVersion)); + context.Response.Headers.ETag = ETag(sequence); + return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto, statusCode: StatusCodes.Status201Created); + } + + private static async Task GetAsync(HttpContext context, Guid id, TitanoDatabase db) + { + if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); + var etag = ETag(sequence); + if (context.Request.Headers.IfNoneMatch == etag) return Results.StatusCode(StatusCodes.Status304NotModified); + context.Response.Headers.ETag = etag; + return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto); + } + + private static async Task UpdateAsync(HttpContext context, Caller caller, Guid id, UpdateSequenceRequest request, TitanoDatabase db, + AuditLog audit, EventHub events) + { + if (request.Title is { Length: > MaxTitle }) return ApiErrors.BadRequest(context, "bad_request", "title"); + if (request.Notes is { Length: > MaxNotes }) return ApiErrors.BadRequest(context, "bad_request", "notes"); + if (request.Rating is < 0 or > 5) return ApiErrors.BadRequest(context, "bad_request", "rating"); + if (request.Tags is { } tags && (tags.Count > MaxTags || tags.Any(t => t is null || t.Trim().Length > TagRepository.MaxLength))) + return ApiErrors.BadRequest(context, "bad_request", "tags"); + if (request.SettingsJson is not null && !IsJson(request.SettingsJson)) return ApiErrors.BadRequest(context, "invalid_settings"); + + var expected = request.ExpectedSettingsVersion ?? IfMatchVersion(context); + var edit = new SequenceEdit { Title = request.Title, Tags = request.Tags, Rating = request.Rating, Notes = request.Notes, SettingsJson = request.SettingsJson }; + var result = await db.Sequences.EditAsync(id, edit, expected, context.RequestAborted); + switch (result.Outcome) + { + case SequenceEditOutcome.NotFound: + return ApiErrors.NotFound(context, "sequence"); + case SequenceEditOutcome.Conflict: + context.Response.Headers.ETag = ETag(result.Sequence!); + return ApiErrors.Conflict(context, "settings_conflict", result.Sequence!.SettingsVersion.ToString(CultureInfo.InvariantCulture)); + } + var sequence = result.Sequence!; + await audit.WriteAsync(caller, context, "sequence_updated", "sequence", id.ToString(), Changes(request)); + events.Publish(new SequenceChangedEvent(id, "settings", sequence.FramesVersion, sequence.SettingsVersion)); + context.Response.Headers.ETag = ETag(sequence); + return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto); + } + + /// + /// Removes a sequence from the index and moves its folder into the archive's trash (BRIEF §0.2 rule 8: nothing is + /// erased). ?dryRun=true only reports what would happen. Owners and admins only. + /// + private static async Task DeleteAsync(HttpContext context, Caller caller, Guid id, bool? dryRun, TitanoDatabase db, ServerArchive archive, + ServerOptions options, AuditLog audit, EventHub events) + { + if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); + if (!caller.IsAdmin && sequence.OwnerUserId != caller.Id) return ApiErrors.Forbidden(context); + var folder = string.IsNullOrEmpty(sequence.Folder) ? null : ArchivePaths.Resolve(archive.Root, sequence.Folder); + var trash = Path.Combine(options.TrashPath, id.ToString("N")); + var hasFolder = folder is not null && Directory.Exists(folder); + var plan = new SequenceDeletePlan(id, sequence.Folder, hasFolder ? Path.GetRelativePath(archive.Root, trash).Replace('\\', '/') : null, + sequence.FrameCount, sequence.TotalBytes, sequence.RenderedCount, dryRun == true); + if (dryRun == true) return Results.Json(plan, ProtocolJson.Default.SequenceDeletePlan); + + if (hasFolder) + { + Directory.CreateDirectory(options.TrashPath); + Directory.Move(folder!, trash); + } + await db.Sequences.DeleteAsync(id, context.RequestAborted); + await audit.WriteAsync(caller, context, "sequence_deleted", "sequence", id.ToString(), sequence.DisplayTitle); + events.Publish(new SequenceChangedEvent(id, "deleted", sequence.FramesVersion, sequence.SettingsVersion)); + return Results.Json(plan, ProtocolJson.Default.SequenceDeletePlan); + } + + /// Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5); ETag over the version counters. + private static async Task ManifestAsync(HttpContext context, Guid id, TitanoDatabase db) + { + var ct = context.RequestAborted; + if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence"); + var etag = string.Create(CultureInfo.InvariantCulture, $"\"m{sequence.SettingsVersion}-{sequence.FramesVersion}-{sequence.RenderedCount}\""); + if (context.Request.Headers.IfNoneMatch == etag) return Results.StatusCode(StatusCodes.Status304NotModified); + + var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt); + var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt); + var frames = new List(sequence.FrameCount); + var after = -1; + while (true) + { + var batch = await db.Frames.ListBySequenceAsync(id, after, 1000, ct); + frames.AddRange(batch.Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb))); + if (batch.Count < 1000) break; + after = batch[^1].Index; + } + var renders = await db.Renders.ListBySequenceAsync(id, ct); + context.Response.Headers.ETag = etag; + return Results.Json(new SequenceManifest(id, sequence.FramesVersion, sequence.SettingsVersion, sequence.SettingsJson, sequence.Tags, frames, + [.. renders.Select(Dto.Render)]), ProtocolJson.Default.SequenceManifest); + } + + private static async Task FramesAsync(HttpContext context, Guid id, string? cursor, int? limit, TitanoDatabase db) + { + var ct = context.RequestAborted; + if (await db.Sequences.GetAsync(id, ct) is null) return ApiErrors.NotFound(context, "sequence"); + var after = -1; + if (!string.IsNullOrEmpty(cursor) && !FrameCursor.TryDecode(cursor, out after)) return ApiErrors.BadRequest(context, "invalid_cursor"); + var take = Math.Clamp(limit ?? 500, 1, 1000); + var frames = await db.Frames.ListBySequenceAsync(id, after, take, ct); + var next = frames.Count == take ? FrameCursor.Encode(frames[^1].Index) : null; + return Results.Json(new Titano.Protocol.Page([.. frames.Select(Dto.Frame)], next, null), ProtocolJson.Default.PageFrameDto); + } + + private static async Task AnalysisAsync(HttpContext context, Guid id, TitanoDatabase db) => + await db.Sequences.GetAsync(id, context.RequestAborted) switch + { + null => ApiErrors.NotFound(context, "sequence"), + { AnalysisJson: { } json } => Results.Text(json, "application/json; charset=utf-8"), + _ => ApiErrors.NotFound(context, "analysis"), + }; + + private static async Task StatsAsync(HttpContext context, Guid id, TitanoDatabase db) + { + if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } s) return ApiErrors.NotFound(context, "sequence"); + var stats = await db.Frames.GetSequenceStatsAsync(id, context.RequestAborted); + return Results.Json(new SequenceStatsDto( + id, s.FrameCount, s.TotalBytes, s.FirstCapture, s.LastCapture, s.MedianInterval, + [.. stats.ByFormat.Select(f => new KeyValueCount(f.Format.ToString().ToLowerInvariant(), f.Photos, f.Bytes))], + stats.Missing, stats.Corrupt, stats.Duplicate, stats.Excluded), ProtocolJson.Default.SequenceStatsDto); + } + + private static async Task RendersAsync(HttpContext context, Guid id, TitanoDatabase db) + { + if (await db.Sequences.GetAsync(id, context.RequestAborted) is null) return ApiErrors.NotFound(context, "sequence"); + var renders = await db.Renders.ListBySequenceAsync(id, context.RequestAborted); + return Results.Json(renders.Select(Dto.Render).ToList(), ProtocolJson.Default.ListRenderDto); + } + + /// Changes whenever anything the sequence representation shows changes (versions, status, statistics). + private static string ETag(Sequence s) => + string.Create(CultureInfo.InvariantCulture, $"\"{s.SettingsVersion}-{s.FramesVersion}-{s.UpdatedAt.UtcTicks}\""); + + /// The settings version named by an If-Match ETag of , if any. + private static int? IfMatchVersion(HttpContext context) + { + var header = context.Request.Headers.IfMatch.ToString().Trim('"', ' '); + var dash = header.IndexOf('-', StringComparison.Ordinal); + return dash > 0 && int.TryParse(header.AsSpan(0, dash), NumberStyles.None, CultureInfo.InvariantCulture, out var version) ? version : null; + } + + private static bool TryParseStatus(string? text, out SequenceStatus with, out SequenceStatus without) + { + with = without = SequenceStatus.None; + if (string.IsNullOrWhiteSpace(text)) return true; + foreach (var part in text.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) + { + var clear = part.StartsWith('-'); + var name = clear ? part[1..] : part; + var flag = Enum.GetValues().FirstOrDefault(v => v != SequenceStatus.None && v.ToString().Equals(name, StringComparison.OrdinalIgnoreCase)); + if (flag == SequenceStatus.None) return false; + if (clear) without |= flag; + else with |= flag; + } + return true; + } + + private static bool TryParseSort(string? text, out SequenceSort sort) + { + sort = SequenceSort.Newest; + if (string.IsNullOrEmpty(text)) return true; + foreach (var candidate in Enum.GetValues()) + { + if (candidate.ToString().Equals(text, StringComparison.OrdinalIgnoreCase)) + { + sort = candidate; + return true; + } + } + return false; + } + + private static bool IsJson(string text) + { + try + { + using var _ = JsonDocument.Parse(text); + return true; + } + catch (JsonException) + { + return false; + } + } + + private static string Changes(UpdateSequenceRequest r) + { + var fields = new List(); + if (r.Title is not null) fields.Add("title"); + if (r.Tags is not null) fields.Add("tags"); + if (r.Rating is not null) fields.Add("rating"); + if (r.Notes is not null) fields.Add("notes"); + if (r.SettingsJson is not null) fields.Add("settings"); + return string.Join(", ", fields); + } + + /// Opaque cursor of the frame listing: the index of the last frame returned. + private static class FrameCursor + { + private const string Prefix = "frames|"; + + public static string Encode(int index) => + Base64Url.EncodeToString(Encoding.UTF8.GetBytes(Prefix + index.ToString(CultureInfo.InvariantCulture))); + + public static bool TryDecode(string cursor, out int index) + { + index = -1; + try + { + var text = Encoding.UTF8.GetString(Base64Url.DecodeFromChars(cursor)); + return text.StartsWith(Prefix, StringComparison.Ordinal) + && int.TryParse(text.AsSpan(Prefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out index); + } + catch (FormatException) + { + return false; + } + } + } +} diff --git a/src/Titano.Server/Api/SystemEndpoints.cs b/src/Titano.Server/Api/SystemEndpoints.cs index a7da0fe4..d104bde0 100644 --- a/src/Titano.Server/Api/SystemEndpoints.cs +++ b/src/Titano.Server/Api/SystemEndpoints.cs @@ -1,40 +1,48 @@ using System.Globalization; using System.Reflection; using Titano.Core.Localization; +using Titano.Data; using Titano.Protocol; using Titano.Server.Configuration; +using Titano.Server.Security; namespace Titano.Server.Api; -/// /health, /api/v1/server/info and /api/v1/i18n/{culture}. +/// /health, /api/v1/server/info and /api/v1/i18n/{culture} (all anonymous). public static class SystemEndpoints { private static readonly string[] AdminAreas = ["Common", "Admin"]; + /// Languages with translated resources (English plus every satellite of the catalog), for the admin language menu. + private static readonly Lazy AdminLanguages = new(() => string.Join(',', Languages.Shipped.Where(code => + { + if (code == "en") return true; + try + { + typeof(Loc).Assembly.GetSatelliteAssembly(CultureInfo.GetCultureInfo(code)); + return true; + } + catch (FileNotFoundException) + { + return false; + } + }))); + public static string Version { get; } = typeof(SystemEndpoints).Assembly.GetCustomAttribute()?.InformationalVersion ?? "0.0.0"; - public static void Map(WebApplication app, ServerOptions options, ServerState state) + public static void Map(WebApplication app, RouteGroupBuilder api) { - app.MapGet("/health", () => - { - var checks = new Dictionary(StringComparer.Ordinal) - { - ["archive"] = Directory.Exists(options.ArchivePath) ? "ok" : "missing", - ["data"] = Directory.Exists(options.DataPath) ? "ok" : "missing", - }; - return Results.Json(new HealthStatus("ok", Version, checks), ProtocolJson.Default.HealthStatus); - }); + app.MapGet("/health", HealthAsync); - var api = app.MapGroup("/api/v1"); - api.MapGet("/server/info", () => Results.Json(new ServerInfo( + api.MapGet("/server/info", (ServerOptions options, ServerState state) => Results.Json(new ServerInfo( options.ServerName, Version, ApiVersion.Current, state.Capabilities, state.TlsFingerprint, ["jpeg", "png", "tiff", "dng", "cr2", "cr3", "nef", "arw", "raf", "orf", "rw2", "pef", "heic", "webp"], - new ServerLimits(options.MaxUploadBytes, options.ChunkSizeBytes)), ProtocolJson.Default.ServerInfo)); + new ServerLimits(options.MaxUploadBytes, options.ChunkSizeBytes, PasswordHasher.MinimumLength)), ProtocolJson.Default.ServerInfo)).AllowAnonymousCaller(); api.MapGet("/i18n/{culture}", (string culture) => { @@ -52,15 +60,46 @@ public static class SystemEndpoints foreach (var (k, v) in Loc.GetArea(area, ci)) strings[k] = v; strings["$culture"] = ci.Name; strings["$rtl"] = ci.TextInfo.IsRightToLeft ? "true" : "false"; + strings["$languages"] = AdminLanguages.Value; return Results.Json(strings, ProtocolJson.Default.DictionaryStringString); - }); + }).AllowAnonymousCaller(); + } + + /// Liveness for the container health check: 503 while the database cannot be reached. + private static async Task HealthAsync(HttpContext context, ServerOptions options, TitanoDatabase db) + { + var checks = new Dictionary(StringComparer.Ordinal) + { + ["archive"] = Directory.Exists(options.ArchivePath) ? "ok" : "missing", + ["data"] = Directory.Exists(options.DataPath) ? "ok" : "missing", + }; + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted); + timeout.CancelAfter(TimeSpan.FromSeconds(3)); + try + { + await using var session = await db.Database.OpenAsync(timeout.Token); + await session.ExecuteAsync("SELECT 1", [], timeout.Token); + checks["database"] = "ok"; + } + catch (Exception ex) when (ex is DatabaseException or OperationCanceledException or IOException) + { + checks["database"] = "unavailable"; + } + var healthy = checks["database"] == "ok"; + return Results.Json(new HealthStatus(healthy ? "ok" : "unavailable", Version, checks), ProtocolJson.Default.HealthStatus, + statusCode: healthy ? StatusCodes.Status200OK : StatusCodes.Status503ServiceUnavailable); } } /// Process-wide facts computed at startup. public sealed class ServerState { - public string? TlsFingerprint { get; set; } + public string? TlsFingerprint { get; init; } + + /// Engine and version of the database ("PostgreSQL 17.2", "SQLite 3.45.1"). + public string Database { get; init; } = string.Empty; + public List Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events"]; + public DateTimeOffset StartedAt { get; } = DateTimeOffset.UtcNow; } diff --git a/src/Titano.Server/Api/UploadEndpoints.cs b/src/Titano.Server/Api/UploadEndpoints.cs new file mode 100644 index 00000000..92ccfa53 --- /dev/null +++ b/src/Titano.Server/Api/UploadEndpoints.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Http.Features; +using Titano.Core.Model; +using Titano.Protocol; +using Titano.Server.Configuration; +using Titano.Server.Security; +using Titano.Server.Storage; + +namespace Titano.Server.Api; + +/// +/// /api/v1/uploads (BRIEF §14.4). Chunk, state and cancel calls also serve render nodes uploading their output, +/// which start and complete through the node endpoints. +/// +public static class UploadEndpoints +{ + public static void Map(RouteGroupBuilder api) + { + var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode); + uploads.MapPost("", StartAsync).Requires(Permissions.Upload); + uploads.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync); + uploads.MapGet("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) => + (await service.GetStateAsync(caller, id, context.RequestAborted)).ToResult(context, s => Results.Json(s, ProtocolJson.Default.UploadState))); + uploads.MapDelete("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) => + (await service.CancelAsync(caller, id, context.RequestAborted)).ToResult(context, _ => Results.NoContent())); + uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) => + (await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse))) + .Requires(Permissions.Upload); + } + + private static async Task StartAsync(HttpContext context, Caller caller, StartUploadRequest request, UploadService service) + { + var outcome = await service.StartAsync(caller, request, context.RequestAborted); + return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse, + statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created)); + } + + private static async Task ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service, ServerOptions options) + { + // Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. + if (context.Features.Get() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = options.ChunkSizeBytes; + var outcome = await service.ReceiveChunkAsync(caller, id, index, context.Request, context.RequestAborted); + return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.ChunkReceived)); + } +} diff --git a/src/Titano.Server/Api/UserEndpoints.cs b/src/Titano.Server/Api/UserEndpoints.cs new file mode 100644 index 00000000..2a4099fc --- /dev/null +++ b/src/Titano.Server/Api/UserEndpoints.cs @@ -0,0 +1,157 @@ +using System.Globalization; +using Titano.Core.Model; +using Titano.Data; +using Titano.Protocol; +using Titano.Server.Security; + +namespace Titano.Server.Api; + +/// /api/v1/users (admin): accounts, roles, quotas, render-node right, temporary passwords. +public static class UserEndpoints +{ + private const int MaxDisplayName = 100; + + public static void Map(RouteGroupBuilder api) + { + var users = api.MapGroup("/users").Requires(Permissions.ManageUsers); + users.MapGet("", ListAsync); + users.MapPost("", CreateAsync); + users.MapGet("/{id:guid}", GetAsync); + users.MapPatch("/{id:guid}", UpdateAsync); + users.MapDelete("/{id:guid}", DeleteAsync); + users.MapPost("/{id:guid}/reset-password", ResetPasswordAsync); + } + + private static async Task ListAsync(HttpContext context, TitanoDatabase db, TimeProvider time) + { + var ct = context.RequestAborted; + var users = await db.Users.ListAsync(ct); + var latest = await db.Tokens.LatestByUserAsync(ct); + var uploads = await db.Uploads.CommittedCountByUserAsync(time.GetUtcNow().AddDays(-30), ct); + var rows = users.Select(u => + { + var token = latest.GetValueOrDefault(u.Id); + return new UserSummaryDto(Dto.User(u), token?.LastUsed ?? u.LastLogin, token?.ClientName, uploads.GetValueOrDefault(u.Id)); + }).ToList(); + return Results.Json(rows, ProtocolJson.Default.ListUserSummaryDto); + } + + private static async Task GetAsync(HttpContext context, Guid id, TitanoDatabase db) => + await db.Users.GetAsync(id, context.RequestAborted) is { } user + ? Results.Json(Dto.User(user), ProtocolJson.Default.UserDto) + : ApiErrors.NotFound(context, "user"); + + private static async Task CreateAsync(HttpContext context, Caller caller, CreateUserRequest request, TitanoDatabase db, AuditLog audit, TimeProvider time) + { + var username = request.Username?.Trim(); + if (!Accounts.IsValidUsername(username)) return ApiErrors.BadRequest(context, "invalid_username"); + if (!Accounts.TryParseRole(request.Role, out var role)) return ApiErrors.BadRequest(context, "invalid_role"); + if (request.QuotaBytes < 0) return ApiErrors.BadRequest(context, "bad_request", "quotaBytes"); + var password = string.IsNullOrEmpty(request.Password) ? TemporaryPassword.Generate() : request.Password; + if (!PasswordHasher.IsAcceptable(password)) return WeakPassword(context); + + var user = new User + { + Id = Guid.CreateVersion7(), + Username = username!, + DisplayName = Clean(request.DisplayName), + PasswordHash = PasswordHasher.Hash(password), + Role = role, + Permissions = Accounts.PermissionsFor(role, request.AllowRenderNode), + QuotaBytes = request.QuotaBytes, + MustChangePassword = true, + Created = time.GetUtcNow(), + }; + try + { + await db.Users.CreateAsync(user, context.RequestAborted); + } + catch (DatabaseException ex) when (ex.IsUniqueViolation) + { + return ApiErrors.Conflict(context, "username_taken"); + } + await audit.WriteAsync(caller, context, "user_created", "user", user.Id.ToString(), $"{user.Username} · {Dto.Role(role)}"); + return Results.Json(new TemporaryPasswordResponse(Dto.User(user), password), ProtocolJson.Default.TemporaryPasswordResponse, statusCode: StatusCodes.Status201Created); + } + + private static async Task UpdateAsync(HttpContext context, Caller caller, Guid id, UpdateUserRequest request, TitanoDatabase db, AuditLog audit) + { + var ct = context.RequestAborted; + if (await db.Users.GetAsync(id, ct) is not { } user) return ApiErrors.NotFound(context, "user"); + var role = user.Role; + if (request.Role is not null && !Accounts.TryParseRole(request.Role, out role)) return ApiErrors.BadRequest(context, "invalid_role"); + if (request.QuotaBytes < 0) return ApiErrors.BadRequest(context, "bad_request", "quotaBytes"); + var updated = user with + { + DisplayName = request.DisplayName is null ? user.DisplayName : Clean(request.DisplayName), + Role = role, + Permissions = Accounts.PermissionsFor(role, request.AllowRenderNode ?? user.Permissions.HasFlag(Permissions.RenderNode)), + QuotaBytes = request.QuotaBytes ?? user.QuotaBytes, + Disabled = request.Disabled ?? user.Disabled, + }; + var losesAdmin = IsActiveAdmin(user) && !IsActiveAdmin(updated); + if (losesAdmin && user.Id == caller.Id) return ApiErrors.Conflict(context, "self"); + if (losesAdmin && !await HasOtherActiveAdminAsync(db, user.Id, ct)) return ApiErrors.Conflict(context, "last_admin"); + + await db.Users.UpdateAsync(updated, ct); + if (updated.Disabled && !user.Disabled) await db.Tokens.RevokeAllAsync(id, ct); + await audit.WriteAsync(caller, context, "user_updated", "user", id.ToString(), Changes(user, updated)); + return Results.Json(Dto.User(updated), ProtocolJson.Default.UserDto); + } + + private static async Task DeleteAsync(HttpContext context, Caller caller, Guid id, TitanoDatabase db, AuditLog audit) + { + var ct = context.RequestAborted; + if (id == caller.Id) return ApiErrors.Conflict(context, "self"); + if (await db.Users.GetAsync(id, ct) is not { } user) return ApiErrors.NotFound(context, "user"); + if (IsActiveAdmin(user) && !await HasOtherActiveAdminAsync(db, id, ct)) return ApiErrors.Conflict(context, "last_admin"); + + // Render jobs of the user's nodes go back to the queue before the nodes disappear with the account. + foreach (var node in (await db.RenderNodes.ListAsync(ct)).Where(n => n.UserId == id)) + await db.Jobs.RequeueRunningAsync(node.Id.ToString(), ct); + var reassigned = await db.Sequences.ReassignOwnerAsync(id, caller.Id, ct); + await db.Users.DeleteAsync(id, ct); + await audit.WriteAsync(caller, context, "user_deleted", "user", id.ToString(), + $"{user.Username} · {reassigned.ToString(CultureInfo.InvariantCulture)}"); + return Results.NoContent(); + } + + private static async Task ResetPasswordAsync(HttpContext context, Caller caller, Guid id, ResetPasswordRequest? request, TitanoDatabase db, AuditLog audit) + { + var ct = context.RequestAborted; + if (await db.Users.GetAsync(id, ct) is not { } user) return ApiErrors.NotFound(context, "user"); + var password = string.IsNullOrEmpty(request?.Password) ? TemporaryPassword.Generate() : request.Password; + if (!PasswordHasher.IsAcceptable(password)) return WeakPassword(context); + var updated = user with { PasswordHash = PasswordHasher.Hash(password), MustChangePassword = true }; + await db.Users.UpdateAsync(updated, ct); + await db.Tokens.RevokeAllAsync(id, ct); + await audit.WriteAsync(caller, context, "password_reset", "user", id.ToString(), user.Username); + return Results.Json(new TemporaryPasswordResponse(Dto.User(updated), password), ProtocolJson.Default.TemporaryPasswordResponse); + } + + private static bool IsActiveAdmin(User user) => user.Role == UserRole.Admin && !user.Disabled; + + private static async Task HasOtherActiveAdminAsync(TitanoDatabase db, Guid except, CancellationToken ct) => + (await db.Users.ListAsync(ct)).Any(u => u.Id != except && IsActiveAdmin(u)); + + private static IResult WeakPassword(HttpContext context) => + ApiErrors.BadRequest(context, "weak_password", PasswordHasher.MinimumLength.ToString(CultureInfo.InvariantCulture), PasswordHasher.MinimumLength); + + private static string? Clean(string? displayName) + { + var trimmed = displayName?.Trim(); + return string.IsNullOrEmpty(trimmed) ? null : trimmed[..Math.Min(trimmed.Length, MaxDisplayName)]; + } + + /// Audit detail listing the fields an edit changed (never secrets). + private static string Changes(User before, User after) + { + var changes = new List(); + if (before.DisplayName != after.DisplayName) changes.Add("displayName"); + if (before.Role != after.Role) changes.Add("role=" + Dto.Role(after.Role)); + if (before.Permissions != after.Permissions) changes.Add("permissions"); + if (before.QuotaBytes != after.QuotaBytes) changes.Add("quota=" + after.QuotaBytes.ToString(CultureInfo.InvariantCulture)); + if (before.Disabled != after.Disabled) changes.Add(after.Disabled ? "disabled" : "enabled"); + return string.Join(", ", changes); + } +} diff --git a/src/Titano.Server/Configuration/ServerOptions.cs b/src/Titano.Server/Configuration/ServerOptions.cs index d1bcbfa8..b6658bcc 100644 --- a/src/Titano.Server/Configuration/ServerOptions.cs +++ b/src/Titano.Server/Configuration/ServerOptions.cs @@ -1,5 +1,7 @@ +using System.Globalization; using System.Text.Json; using System.Text.Json.Serialization; +using Titano.Data; namespace Titano.Server.Configuration; @@ -23,15 +25,44 @@ public enum DatabaseKind /// /// Server configuration (BRIEF §14.9): environment variables override /data/server.json, which overrides defaults. +/// Settings applied from the environment are listed in ; the admin panel cannot change them. /// public sealed class ServerOptions { + /// Environment variable → setting it fixes, and how its text is applied. + private static readonly (string Variable, string Setting, Action Apply)[] Variables = + [ + ("TITANO_SERVER_NAME", nameof(ServerName), (o, v) => o.ServerName = v), + ("TITANO_ARCHIVE_PATH", nameof(ArchivePath), (o, v) => o.ArchivePath = v), + ("TITANO_ADMIN_USER", nameof(AdminUser), (o, v) => o.AdminUser = v), + ("TITANO_ADMIN_PASSWORD", nameof(AdminPassword), (o, v) => o.AdminPassword = v), + ("TITANO_DB", nameof(Database), (o, v) => o.ApplyDatabase(v)), + ("TITANO_DB_HOST", nameof(DbHost), (o, v) => o.DbHost = v), + ("TITANO_DB_PORT", nameof(DbPort), (o, v) => o.DbPort = Int("TITANO_DB_PORT", v)), + ("TITANO_DB_NAME", nameof(DbName), (o, v) => o.DbName = v), + ("TITANO_DB_USER", nameof(DbUser), (o, v) => o.DbUser = v), + ("TITANO_DB_PASSWORD", nameof(DbPassword), (o, v) => o.DbPassword = v), + ("TITANO_DB_SSLMODE", nameof(DbSslMode), (o, v) => o.DbSslMode = v), + ("TITANO_HTTP_PORT", nameof(HttpPort), (o, v) => o.HttpPort = Int("TITANO_HTTP_PORT", v)), + ("TITANO_HTTPS_PORT", nameof(HttpsPort), (o, v) => o.HttpsPort = Int("TITANO_HTTPS_PORT", v)), + ("TITANO_TLS_MODE", nameof(TlsMode), (o, v) => o.TlsMode = Enum.Parse(v, ignoreCase: true)), + ("TITANO_TLS_CERT", nameof(TlsCert), (o, v) => o.TlsCert = v), + ("TITANO_TLS_KEY", nameof(TlsKey), (o, v) => o.TlsKey = v), + ("TITANO_LANG", nameof(Language), (o, v) => o.Language = v), + ("TITANO_WORKERS", nameof(Workers), (o, v) => o.Workers = Math.Max(1, Int("TITANO_WORKERS", v))), + ("TITANO_DISCOVERY", nameof(Discovery), (o, v) => o.Discovery = bool.Parse(v)), + ]; + public string ServerName { get; set; } = "Titano Server"; public string ArchivePath { get; set; } = OperatingSystem.IsWindows() ? @"C:\TitanoArchive" : "/archive"; public string DataPath { get; set; } = OperatingSystem.IsWindows() ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Titano Server") : "/data"; public string? AdminUser { get; set; } public string? AdminPassword { get; set; } public DatabaseKind Database { get; set; } = OperatingSystem.IsWindows() ? DatabaseKind.Sqlite : DatabaseKind.Postgres; + + /// Complete connection string (from TITANO_DB when it holds one); overrides the Db* parts. + public string? DbConnectionString { get; set; } + public string DbHost { get; set; } = "postgres"; public int DbPort { get; set; } = 5432; public string DbName { get; set; } = "titano"; @@ -51,6 +82,27 @@ public sealed class ServerOptions public int JobHistoryDays { get; set; } = 90; public int TokenLifetimeDays { get; set; } = 30; + /// Uploads untouched for this long are abandoned: their partial files are deleted. + public int UploadRetentionHours { get; set; } = 48; + + /// Sign-in attempts accepted per minute and client address (BRIEF §14.2: 5). + public int LoginAttemptsPerMinute { get; set; } = 5; + + /// Consecutive failed sign-ins that lock an account name for . + public int LockoutThreshold { get; set; } = 10; + + public int LockoutMinutes { get; set; } = 15; + + /// A render node silent for this long is offline and its jobs go back to the queue. + public int NodeTimeoutSeconds { get; set; } = 90; + + /// Chunk uploads one user may run at the same time; more are answered 429. + public int ConcurrentChunksPerUser { get; set; } = 8; + + /// Settings fixed by environment variables (names of the properties above). + [JsonIgnore] + public IReadOnlyCollection Locked { get; private set; } = []; + [JsonIgnore] public string LogPath => Path.Combine(DataPath, "logs"); @@ -60,6 +112,17 @@ public sealed class ServerOptions [JsonIgnore] public string UploadsPath => Path.Combine(DataPath, "uploads"); + [JsonIgnore] + public string SettingsFile => Path.Combine(DataPath, "server.json"); + + /// Verified uploads wait here, on the archive volume, until the archive ingest moves them into place. + [JsonIgnore] + public string StagingPath => Path.Combine(ArchivePath, ".titano", "staging"); + + /// Deleted sequences are moved here (never erased by the server). + [JsonIgnore] + public string TrashPath => Path.Combine(ArchivePath, ".titano", "trash"); + /// Loads {DataPath}/server.json (if present) then applies TITANO_* environment variables. public static ServerOptions Load(IReadOnlyDictionary? environment = null) { @@ -82,35 +145,38 @@ public sealed class ServerOptions } options.DataPath = dataPath; - options.ServerName = Env("TITANO_SERVER_NAME") ?? options.ServerName; - options.ArchivePath = Env("TITANO_ARCHIVE_PATH") ?? options.ArchivePath; - options.AdminUser = Env("TITANO_ADMIN_USER") ?? options.AdminUser; - options.AdminPassword = Env("TITANO_ADMIN_PASSWORD") ?? options.AdminPassword; - if (Env("TITANO_DB") is { } db) options.Database = Enum.Parse(db, ignoreCase: true); - options.DbHost = Env("TITANO_DB_HOST") ?? options.DbHost; - if (Env("TITANO_DB_PORT") is { } port) options.DbPort = int.Parse(port, System.Globalization.CultureInfo.InvariantCulture); - options.DbName = Env("TITANO_DB_NAME") ?? options.DbName; - options.DbUser = Env("TITANO_DB_USER") ?? options.DbUser; - options.DbPassword = Env("TITANO_DB_PASSWORD") ?? options.DbPassword; - options.DbSslMode = Env("TITANO_DB_SSLMODE") ?? options.DbSslMode; - if (Env("TITANO_HTTP_PORT") is { } http) options.HttpPort = int.Parse(http, System.Globalization.CultureInfo.InvariantCulture); - if (Env("TITANO_HTTPS_PORT") is { } https) options.HttpsPort = int.Parse(https, System.Globalization.CultureInfo.InvariantCulture); - if (Env("TITANO_TLS_MODE") is { } tls) options.TlsMode = Enum.Parse(tls, ignoreCase: true); - options.TlsCert = Env("TITANO_TLS_CERT") ?? options.TlsCert; - options.TlsKey = Env("TITANO_TLS_KEY") ?? options.TlsKey; - options.Language = Env("TITANO_LANG") ?? options.Language; - if (Env("TITANO_WORKERS") is { } workers) options.Workers = Math.Max(1, int.Parse(workers, System.Globalization.CultureInfo.InvariantCulture)); - if (Env("TITANO_DISCOVERY") is { } discovery) options.Discovery = bool.Parse(discovery); + var locked = new List(); + foreach (var (variable, setting, apply) in Variables) + { + if (Env(variable) is not { } value) continue; + try + { + apply(options, value); + } + catch (Exception ex) when (ex is FormatException or ArgumentException) + { + throw new InvalidOperationException($"{variable} has an invalid value.", ex); + } + locked.Add(setting); + } + options.Locked = locked; options.Validate(); return options; } - private static Dictionary ReadEnvironment() + /// The database to open: , the SQLite file in the data folder, or PostgreSQL from the parts. + public DatabaseOptions ToDatabaseOptions() { - // Variables may differ only by case on Linux (no_proxy/NO_PROXY); the last one wins. - var result = new Dictionary(StringComparer.OrdinalIgnoreCase); - foreach (System.Collections.DictionaryEntry e in Environment.GetEnvironmentVariables()) result[(string)e.Key] = (string?)e.Value; - return result; + if (DbConnectionString is { Length: > 0 } cs) return DatabaseOptions.Parse(cs); + if (Database == DatabaseKind.Sqlite) return DatabaseOptions.Sqlite(Path.Combine(DataPath, "titano.db")); + var builder = new System.Text.StringBuilder() + .Append("Host=").Append(Quote(DbHost)) + .Append(";Port=").Append(DbPort.ToString(CultureInfo.InvariantCulture)) + .Append(";Database=").Append(Quote(DbName)) + .Append(";Username=").Append(Quote(DbUser)) + .Append(";SslMode=").Append(Quote(DbSslMode)); + if (DbPassword is not null) builder.Append(";Password=").Append(Quote(DbPassword)); + return DatabaseOptions.Postgres(builder.ToString()); } public void Validate() @@ -122,6 +188,35 @@ public sealed class ServerOptions // Port 0 asks the OS for a free port (used by the in-process test host). if (HttpPort is < 0 or > 65535 || HttpsPort is < 0 or > 65535) throw new InvalidOperationException("Ports must be between 0 and 65535."); + if (ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024) + throw new InvalidOperationException("The chunk size must be between 64 KiB and 64 MiB."); + if (MaxUploadBytes < 1 || TokenLifetimeDays < 1 || JobHistoryDays < 1 || UploadRetentionHours < 1) + throw new InvalidOperationException("Upload size, token lifetime, job history and upload retention must be positive."); + } + + /// TITANO_DB is postgres, sqlite or a complete connection string (Host=… / Data Source=…). + private void ApplyDatabase(string value) + { + if (value.Contains('=', StringComparison.Ordinal)) + { + DbConnectionString = value; + Database = DatabaseOptions.Parse(value).Dialect == Dialect.Postgres ? DatabaseKind.Postgres : DatabaseKind.Sqlite; + return; + } + Database = Enum.Parse(value, ignoreCase: true); + } + + private static int Int(string variable, string value) => + int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var n) ? n : throw new FormatException($"{variable} must be a number."); + + private static string Quote(string value) => "'" + value.Replace("'", "''", StringComparison.Ordinal) + "'"; + + private static Dictionary ReadEnvironment() + { + // Variables may differ only by case on Linux (no_proxy/NO_PROXY); the last one wins. + var result = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (System.Collections.DictionaryEntry e in Environment.GetEnvironmentVariables()) result[(string)e.Key] = (string?)e.Value; + return result; } } diff --git a/src/Titano.Server/Configuration/ServerSettingsStore.cs b/src/Titano.Server/Configuration/ServerSettingsStore.cs new file mode 100644 index 00000000..5d830471 --- /dev/null +++ b/src/Titano.Server/Configuration/ServerSettingsStore.cs @@ -0,0 +1,101 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using Titano.Core.IO; +using Titano.Core.Localization; +using Titano.Protocol; + +namespace Titano.Server.Configuration; + +/// +/// The admin panel's view of the configuration (BRIEF §14.8 Settings). Saving writes /data/server.json (only the +/// edited keys; secrets and environment values are never written) and applies what can change while running; ports, +/// TLS, workers, chunk size and job history wait for a restart. +/// +public sealed class ServerSettingsStore(ServerOptions options) +{ + private readonly Lock _gate = new(); + private ServerSettingsDto? _saved; + + public ServerSettingsDto Current() + { + lock (_gate) return _saved ?? Snapshot(options, restartRequired: false); + } + + /// Validates and saves; returns the error code and the offending setting, or null on success. + public (string Code, string Setting)? Save(ServerSettingsDto request) + { + if (Validate(request) is { } invalid) return ("invalid_setting", invalid); + lock (_gate) + { + var current = _saved ?? Snapshot(options, restartRequired: false); + var changes = Changes(current, request); + if (changes.FirstOrDefault(c => options.Locked.Contains(c.Name)) is { Name: { } locked }) return ("setting_locked", locked); + if (changes.Count == 0) return null; + + var file = File.Exists(options.SettingsFile) ? JsonNode.Parse(File.ReadAllText(options.SettingsFile)) as JsonObject ?? [] : []; + foreach (var (name, value) in changes) file[name] = value; + Directory.CreateDirectory(options.DataPath); + AtomicFile.WriteAllText(options.SettingsFile, file.ToJsonString(new JsonSerializerOptions { WriteIndented = true })); + + options.ServerName = request.ServerName.Trim(); + options.Discovery = request.Discovery; + options.Language = request.Language; + options.MaxUploadBytes = request.MaxUploadBytes; + options.TokenLifetimeDays = request.TokenLifetimeDays; + options.UploadRetentionHours = request.UploadRetentionHours; + var restart = request.HttpPort != options.HttpPort || request.HttpsPort != options.HttpsPort + || !string.Equals(request.TlsMode, options.TlsMode.ToString(), StringComparison.OrdinalIgnoreCase) + || request.TlsCert != options.TlsCert || request.TlsKey != options.TlsKey || request.Workers != options.Workers + || request.ChunkSizeBytes != options.ChunkSizeBytes || request.JobHistoryDays != options.JobHistoryDays; + _saved = request with { ServerName = request.ServerName.Trim(), Locked = [.. options.Locked], RestartRequired = restart }; + return null; + } + } + + private static ServerSettingsDto Snapshot(ServerOptions o, bool restartRequired) => new( + o.ServerName, o.HttpPort, o.HttpsPort, o.TlsMode.ToString().ToLowerInvariant(), o.TlsCert, o.TlsKey, o.Language, o.Workers, o.Discovery, + o.MaxUploadBytes, o.ChunkSizeBytes, o.JobHistoryDays, o.TokenLifetimeDays, o.UploadRetentionHours, [.. o.Locked], restartRequired); + + private static string? Validate(ServerSettingsDto r) + { + if (string.IsNullOrWhiteSpace(r.ServerName) || r.ServerName.Trim().Length > 60) return nameof(ServerOptions.ServerName); + if (r.HttpPort is < 1 or > 65535) return nameof(ServerOptions.HttpPort); + if (r.HttpsPort is < 1 or > 65535 || r.HttpsPort == r.HttpPort) return nameof(ServerOptions.HttpsPort); + if (!Enum.TryParse(r.TlsMode, ignoreCase: true, out var tls) || !Enum.IsDefined(tls)) return nameof(ServerOptions.TlsMode); + if (tls == TlsMode.Pem && (string.IsNullOrWhiteSpace(r.TlsCert) || !File.Exists(r.TlsCert))) return nameof(ServerOptions.TlsCert); + if (tls == TlsMode.Pem && (string.IsNullOrWhiteSpace(r.TlsKey) || !File.Exists(r.TlsKey))) return nameof(ServerOptions.TlsKey); + if (!Languages.Shipped.Contains(r.Language, StringComparer.OrdinalIgnoreCase)) return nameof(ServerOptions.Language); + if (r.Workers is < 1 or > 64) return nameof(ServerOptions.Workers); + if (r.MaxUploadBytes < 1024 * 1024) return nameof(ServerOptions.MaxUploadBytes); + if (r.ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024) return nameof(ServerOptions.ChunkSizeBytes); + if (r.JobHistoryDays is < 1 or > 3650) return nameof(ServerOptions.JobHistoryDays); + if (r.TokenLifetimeDays is < 1 or > 365) return nameof(ServerOptions.TokenLifetimeDays); + if (r.UploadRetentionHours is < 1 or > 8760) return nameof(ServerOptions.UploadRetentionHours); + return null; + } + + /// Settings whose value differs, as server.json keys and JSON values. + private static List<(string Name, JsonNode? Value)> Changes(ServerSettingsDto a, ServerSettingsDto b) + { + var changes = new List<(string, JsonNode?)>(); + void Add(string name, T before, T after, Func json) + { + if (!EqualityComparer.Default.Equals(before, after)) changes.Add((name, json(after))); + } + Add(nameof(ServerOptions.ServerName), a.ServerName, b.ServerName.Trim(), v => JsonValue.Create(v)); + Add(nameof(ServerOptions.HttpPort), a.HttpPort, b.HttpPort, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.HttpsPort), a.HttpsPort, b.HttpsPort, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.TlsMode), a.TlsMode.ToLowerInvariant(), b.TlsMode.ToLowerInvariant(), v => JsonValue.Create(v)); + Add(nameof(ServerOptions.TlsCert), a.TlsCert, b.TlsCert, v => v is null ? null : JsonValue.Create(v)); + Add(nameof(ServerOptions.TlsKey), a.TlsKey, b.TlsKey, v => v is null ? null : JsonValue.Create(v)); + Add(nameof(ServerOptions.Language), a.Language, b.Language, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.Workers), a.Workers, b.Workers, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.Discovery), a.Discovery, b.Discovery, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.MaxUploadBytes), a.MaxUploadBytes, b.MaxUploadBytes, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.ChunkSizeBytes), a.ChunkSizeBytes, b.ChunkSizeBytes, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.JobHistoryDays), a.JobHistoryDays, b.JobHistoryDays, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.TokenLifetimeDays), a.TokenLifetimeDays, b.TokenLifetimeDays, v => JsonValue.Create(v)); + Add(nameof(ServerOptions.UploadRetentionHours), a.UploadRetentionHours, b.UploadRetentionHours, v => JsonValue.Create(v)); + return changes; + } +} diff --git a/src/Titano.Server/Events/EventEndpoints.cs b/src/Titano.Server/Events/EventEndpoints.cs new file mode 100644 index 00000000..aa315548 --- /dev/null +++ b/src/Titano.Server/Events/EventEndpoints.cs @@ -0,0 +1,103 @@ +using System.Net.WebSockets; +using System.Text.Json; +using Titano.Core.Model; +using Titano.Protocol; +using Titano.Server.Api; +using Titano.Server.Security; + +namespace Titano.Server.Events; + +/// +/// WS /api/v1/events: server events as one JSON text message each (, discriminated by +/// type). The handshake is authenticated like any API call (bearer header, or the admin cookie from this origin); +/// the token is checked again every minute and the socket closes once it is revoked or expires. +/// +public static class EventEndpoints +{ + private static readonly TimeSpan Revalidate = TimeSpan.FromSeconds(60); + + public static void Map(RouteGroupBuilder api) => api.MapGet("/events", RunAsync).Requires(Permissions.Read); + + private static async Task RunAsync(HttpContext context, Caller caller, EventHub hub, TokenService tokens, IHostApplicationLifetime lifetime) + { + if (!context.WebSockets.IsWebSocketRequest) return ApiErrors.BadRequest(context, "websocket_required"); + using var socket = await context.WebSockets.AcceptWebSocketAsync(); + using var subscription = hub.Subscribe(); + using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted, lifetime.ApplicationStopping); + var receiving = ReceiveUntilClosedAsync(socket, stop); + var status = WebSocketCloseStatus.NormalClosure; + try + { + status = await SendAsync(socket, subscription, caller, tokens, stop.Token); + } + catch (Exception ex) when (ex is OperationCanceledException or WebSocketException) + { + status = lifetime.ApplicationStopping.IsCancellationRequested ? WebSocketCloseStatus.EndpointUnavailable : WebSocketCloseStatus.NormalClosure; + } + finally + { + await stop.CancelAsync(); + await receiving; + } + if (socket.State is WebSocketState.Open or WebSocketState.CloseReceived) + { + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5)); + try + { + await socket.CloseOutputAsync(status, null, timeout.Token); + } + catch (Exception ex) when (ex is OperationCanceledException or WebSocketException) + { + // The peer is gone. + } + } + return Results.Empty; + } + + /// Forwards events until cancelled; returns the close status when the caller's token stops being valid. + private static async Task SendAsync(WebSocket socket, EventHub.Subscription subscription, Caller caller, TokenService tokens, + CancellationToken cancellationToken) + { + while (true) + { + using (var wait = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken)) + { + wait.CancelAfter(Revalidate); + try + { + while (await subscription.Reader.WaitToReadAsync(wait.Token)) + { + while (subscription.Reader.TryRead(out var e)) + { + var payload = JsonSerializer.SerializeToUtf8Bytes(e, ProtocolJson.Default.ServerEvent); + await socket.SendAsync(payload, WebSocketMessageType.Text, endOfMessage: true, cancellationToken); + } + } + return WebSocketCloseStatus.NormalClosure; + } + catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) + { + // Quiet minute: fall through to the token check. + } + } + if (!await tokens.IsStillValidAsync(caller, cancellationToken)) return WebSocketCloseStatus.PolicyViolation; + } + } + + private static async Task ReceiveUntilClosedAsync(WebSocket socket, CancellationTokenSource stop) + { + var buffer = new byte[1024]; + try + { + while (!stop.IsCancellationRequested) + { + var result = await socket.ReceiveAsync(buffer, stop.Token); + if (result.MessageType == WebSocketMessageType.Close) break; + } + } + catch (Exception ex) when (ex is OperationCanceledException or WebSocketException) + { + } + await stop.CancelAsync(); + } +} diff --git a/src/Titano.Server/Events/EventHub.cs b/src/Titano.Server/Events/EventHub.cs new file mode 100644 index 00000000..3240ad4d --- /dev/null +++ b/src/Titano.Server/Events/EventHub.cs @@ -0,0 +1,47 @@ +using System.Threading.Channels; +using Titano.Protocol; + +namespace Titano.Server.Events; + +/// +/// Fan-out of server events to WebSocket subscribers (BRIEF §14.3). Each subscriber has a bounded queue that drops +/// its oldest events when it falls behind, so a slow client never holds up the server. +/// +public sealed class EventHub +{ + private const int QueueLength = 512; + private readonly Lock _gate = new(); + private Channel[] _subscribers = []; + + public int SubscriberCount => Volatile.Read(ref _subscribers).Length; + + public Subscription Subscribe() + { + var channel = Channel.CreateBounded(new BoundedChannelOptions(QueueLength) + { + FullMode = BoundedChannelFullMode.DropOldest, + SingleReader = true, + }); + lock (_gate) _subscribers = [.. _subscribers, channel]; + return new Subscription(this, channel); + } + + public void Publish(ServerEvent e) + { + foreach (var subscriber in Volatile.Read(ref _subscribers)) subscriber.Writer.TryWrite(e); + } + + private void Remove(Channel channel) + { + lock (_gate) _subscribers = [.. _subscribers.Where(c => c != channel)]; + channel.Writer.TryComplete(); + } + + /// One subscriber's queue; dispose to unsubscribe. + public sealed class Subscription(EventHub hub, Channel channel) : IDisposable + { + public ChannelReader Reader => channel.Reader; + + public void Dispose() => hub.Remove(channel); + } +} diff --git a/src/Titano.Server/Hosting/AdminPanel.cs b/src/Titano.Server/Hosting/AdminPanel.cs new file mode 100644 index 00000000..d92fde09 --- /dev/null +++ b/src/Titano.Server/Hosting/AdminPanel.cs @@ -0,0 +1,48 @@ +using System.Net; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; +using Titano.Server.Configuration; + +namespace Titano.Server.Hosting; + +/// +/// Serves the embedded admin panel at /admin. Requests over plain HTTP from another machine are redirected to +/// the HTTPS listener, because the session cookie is Secure (BRIEF §14.2) and would not survive plain HTTP. +/// +public static class AdminPanel +{ + public static void Map(WebApplication app, ServerOptions options) + { + var files = new EmbeddedStaticFiles(typeof(AdminPanel).Assembly, "admin/"); + var server = app.Services.GetRequiredService(); + app.MapGet("/", () => Results.Redirect("/admin/")); + app.MapMethods("/admin/{**path}", ["GET", "HEAD"], async (HttpContext context, string? path) => + { + if (HttpsLocation(context, options, server) is { } secure) + { + context.Response.Redirect(secure); + return; + } + // Relative asset URLs need the trailing slash. + if (!context.Request.Path.Value!.EndsWith('/') && string.IsNullOrEmpty(path)) + { + context.Response.Redirect("/admin/"); + return; + } + if (!await files.TryServeAsync(context, path ?? string.Empty)) context.Response.StatusCode = StatusCodes.Status404NotFound; + }); + } + + private static string? HttpsLocation(HttpContext context, ServerOptions options, IServer server) + { + if (context.Request.IsHttps || options.TlsMode == TlsMode.Proxy) return null; + if (context.Connection.RemoteIpAddress is { } remote && IPAddress.IsLoopback(remote)) return null; + foreach (var address in server.Features.Get()?.Addresses ?? []) + { + if (!address.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) continue; + var port = address[(address.LastIndexOf(':') + 1)..].TrimEnd('/'); + return $"https://{context.Request.Host.Host}:{port}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}"; + } + return null; + } +} diff --git a/src/Titano.Server/Hosting/DiscoveryBeaconService.cs b/src/Titano.Server/Hosting/DiscoveryBeaconService.cs new file mode 100644 index 00000000..3f2e8f29 --- /dev/null +++ b/src/Titano.Server/Hosting/DiscoveryBeaconService.cs @@ -0,0 +1,99 @@ +using System.Net; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Hosting.Server.Features; +using Titano.Protocol; +using Titano.Server.Api; +using Titano.Server.Configuration; + +namespace Titano.Server.Hosting; + +/// +/// LAN discovery (BRIEF §14.9, TITANO_DISCOVERY): every few seconds a is broadcast on +/// UDP port of every IPv4 network, so clients can offer this server on first run. +/// Switching discovery off in the settings stops it without a restart. +/// +public sealed class DiscoveryBeaconService(ServerOptions options, ServerState state, IServer server, ILogger logger) : BackgroundService +{ + private static readonly TimeSpan Interval = TimeSpan.FromSeconds(5); + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + using var udp = new UdpClient(AddressFamily.InterNetwork) { EnableBroadcast = true }; + var warned = false; + while (!stoppingToken.IsCancellationRequested) + { + if (options.Discovery) + { + try + { + await SendOnceAsync(udp, BroadcastTargets(), stoppingToken); + } + catch (SocketException ex) when (!warned) + { + warned = true; + logger.LogInformation("LAN discovery beacon cannot be sent: {Reason}", ex.Message); + } + catch (SocketException) + { + // Already reported once; networks may come and go. + } + } + try + { + await Task.Delay(Interval, stoppingToken); + } + catch (OperationCanceledException) + { + return; + } + } + } + + /// The beacon for the ports Kestrel actually listens on (HTTPS 0 when TLS is terminated elsewhere). + public DiscoveryBeacon CreateBeacon() => new( + options.ServerName, + SystemEndpoints.Version, + BoundPort(Uri.UriSchemeHttp) ?? options.HttpPort, + state.TlsFingerprint is null ? 0 : BoundPort(Uri.UriSchemeHttps) ?? options.HttpsPort, + state.TlsFingerprint); + + public async Task SendOnceAsync(UdpClient udp, IEnumerable targets, CancellationToken cancellationToken) + { + var datagram = CreateBeacon().ToBytes(); + foreach (var target in targets) await udp.SendAsync(datagram, target, cancellationToken); + } + + /// The limited broadcast address plus the directed broadcast of each IPv4 interface that is up. + private static List BroadcastTargets() + { + var targets = new List { new(IPAddress.Broadcast, DiscoveryBeacon.Port) }; + foreach (var nic in NetworkInterface.GetAllNetworkInterfaces()) + { + if (nic.OperationalStatus != OperationalStatus.Up || nic.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue; + foreach (var unicast in nic.GetIPProperties().UnicastAddresses) + { + if (unicast.Address.AddressFamily != AddressFamily.InterNetwork || unicast.IPv4Mask is not { } mask) continue; + var address = unicast.Address.GetAddressBytes(); + var bits = mask.GetAddressBytes(); + for (var i = 0; i < 4; i++) address[i] = (byte)(address[i] | ~bits[i]); + var broadcast = new IPEndPoint(new IPAddress(address), DiscoveryBeacon.Port); + if (!targets.Contains(broadcast)) targets.Add(broadcast); + } + } + return targets; + } + + private int? BoundPort(string scheme) + { + var addresses = server.Features.Get()?.Addresses ?? []; + foreach (var address in addresses) + { + if (Uri.TryCreate(address.Replace("[::]", "localhost", StringComparison.Ordinal).Replace("0.0.0.0", "localhost", StringComparison.Ordinal), + UriKind.Absolute, out var uri) && uri.Scheme == scheme) + return uri.Port; + } + return null; + } +} diff --git a/src/Titano.Server/Hosting/FileLoggerProvider.cs b/src/Titano.Server/Hosting/FileLoggerProvider.cs index 7052813b..8f5c4f63 100644 --- a/src/Titano.Server/Hosting/FileLoggerProvider.cs +++ b/src/Titano.Server/Hosting/FileLoggerProvider.cs @@ -38,7 +38,16 @@ public sealed class LoggerSink(ILoggerFactory factory) : ILogSink { public void Write(CoreLogLevel level, string category, string message, Exception? exception) { - var logger = factory.CreateLogger("Titano." + category); + ILogger logger; + try + { + logger = factory.CreateLogger("Titano." + category); + } + catch (ObjectDisposedException) + { + // The host that installed this sink has shut down (engine code may outlive it); the line is dropped. + return; + } var ms = level switch { CoreLogLevel.Trace => MsLogLevel.Trace, diff --git a/src/Titano.Server/Hosting/ResponseMiddleware.cs b/src/Titano.Server/Hosting/ResponseMiddleware.cs new file mode 100644 index 00000000..5536bf57 --- /dev/null +++ b/src/Titano.Server/Hosting/ResponseMiddleware.cs @@ -0,0 +1,74 @@ +using Titano.Protocol; +using Titano.Server.Api; + +namespace Titano.Server.Hosting; + +/// +/// First middleware of the pipeline: API version and security headers on every response, and the +/// { code, message, details } error body for failures that did not produce one (bad requests, unhandled +/// exceptions, unknown methods). +/// +public sealed class ResponseMiddleware(RequestDelegate next, ILogger logger) +{ + /// The admin panel loads nothing from elsewhere and is never framed. + private const string AdminPolicy = + "default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; connect-src 'self'; " + + "frame-ancestors 'none'; base-uri 'none'; form-action 'self'"; + + public async Task InvokeAsync(HttpContext context) + { + var headers = context.Response.Headers; + headers[ApiVersion.Header] = ApiVersion.Current.ToString(System.Globalization.CultureInfo.InvariantCulture); + headers.XContentTypeOptions = "nosniff"; + headers["Referrer-Policy"] = "no-referrer"; + var api = context.Request.Path.StartsWithSegments("/api"); + if (api) headers.CacheControl = "no-store"; + if (context.Request.Path.StartsWithSegments("/admin")) + { + headers.ContentSecurityPolicy = AdminPolicy; + headers.XFrameOptions = "DENY"; + } + + try + { + await next(context); + } + catch (BadHttpRequestException ex) when (!context.Response.HasStarted) + { + await ApiErrors.WriteAsync(context, ex.StatusCode, CodeFor(ex.StatusCode)); + return; + } + catch (FormatException) when (!context.Response.HasStarted && context.Request.Query.ContainsKey("cursor")) + { + // Repositories reject cursors that do not belong to the listing. + await ApiErrors.WriteAsync(context, StatusCodes.Status400BadRequest, "invalid_cursor"); + return; + } + catch (OperationCanceledException) when (context.RequestAborted.IsCancellationRequested) + { + return; + } + catch (Exception ex) when (!context.Response.HasStarted) + { + logger.LogError(ex, "Unhandled error on {Method} {Path}", context.Request.Method, context.Request.Path); + await ApiErrors.WriteAsync(context, StatusCodes.Status500InternalServerError, "internal"); + return; + } + + var response = context.Response; + if (api && response.StatusCode >= 400 && !response.HasStarted && response.ContentType is null) + await ApiErrors.WriteAsync(context, response.StatusCode, CodeFor(response.StatusCode)); + } + + private static string CodeFor(int status) => status switch + { + StatusCodes.Status400BadRequest => "bad_request", + StatusCodes.Status401Unauthorized => "unauthorized", + StatusCodes.Status403Forbidden => "forbidden", + StatusCodes.Status404NotFound => "not_found", + StatusCodes.Status405MethodNotAllowed => "method_not_allowed", + StatusCodes.Status413PayloadTooLarge => "too_large", + StatusCodes.Status415UnsupportedMediaType => "unsupported_media_type", + _ => "internal", + }; +} diff --git a/src/Titano.Server/Hosting/ServerCommandLine.cs b/src/Titano.Server/Hosting/ServerCommandLine.cs new file mode 100644 index 00000000..a4751919 --- /dev/null +++ b/src/Titano.Server/Hosting/ServerCommandLine.cs @@ -0,0 +1,68 @@ +namespace Titano.Server.Hosting; + +/// What Titano.Server was asked to do. +public enum ServerCommand +{ + /// Run in the console (Docker, Linux, development). + Run, + + /// Run under the Windows Service Control Manager (the command line the installed service uses). + Service, + + InstallService, + UninstallService, + + /// Probe /health of the local server and exit 0/1 (container health check). + HealthCheck, + + Help, +} + +/// Command-line switches of the server; anything else is passed on to the web host. +public sealed record ServerCommandLine(ServerCommand Command, IReadOnlyList HostArguments, string? Error) +{ + public const string Usage = """ + Titano Server — time-lapse archive, uploads, jobs and rendering. + + Usage: Titano.Server [--service | --install-service | --uninstall-service | --healthcheck | --help] + + (no switch) run in the console until Ctrl+C + --service run under the Windows Service Control Manager + --install-service register the "TitanoServer" Windows service (automatic start, LocalSystem) + --uninstall-service stop and remove the Windows service + --healthcheck exit 0 when the local server answers /health, 1 otherwise + + Configuration: /data/server.json (TITANO_DATA_PATH) overridden by environment variables: + TITANO_ARCHIVE_PATH TITANO_DATA_PATH TITANO_SERVER_NAME TITANO_ADMIN_USER TITANO_ADMIN_PASSWORD + TITANO_DB (postgres | sqlite | connection string) TITANO_DB_HOST TITANO_DB_PORT TITANO_DB_NAME TITANO_DB_USER + TITANO_DB_PASSWORD TITANO_DB_SSLMODE TITANO_HTTP_PORT TITANO_HTTPS_PORT TITANO_TLS_MODE (auto | pem | proxy) + TITANO_TLS_CERT TITANO_TLS_KEY TITANO_LANG TITANO_WORKERS TITANO_DISCOVERY + """; + + public static ServerCommandLine Parse(IReadOnlyList args) + { + var command = ServerCommand.Run; + var rest = new List(); + string? error = null; + foreach (var arg in args) + { + ServerCommand? parsed = arg switch + { + "--service" => ServerCommand.Service, + "--install-service" => ServerCommand.InstallService, + "--uninstall-service" => ServerCommand.UninstallService, + "--healthcheck" => ServerCommand.HealthCheck, + "--help" or "-h" or "-?" or "/?" => ServerCommand.Help, + _ => null, + }; + if (parsed is not { } switchCommand) + { + rest.Add(arg); + continue; + } + if (command != ServerCommand.Run && command != switchCommand) error = $"{arg} cannot be combined with another command switch."; + command = switchCommand; + } + return new ServerCommandLine(command, rest, error); + } +} diff --git a/src/Titano.Server/Hosting/ServerDatabase.cs b/src/Titano.Server/Hosting/ServerDatabase.cs new file mode 100644 index 00000000..19db1ce9 --- /dev/null +++ b/src/Titano.Server/Hosting/ServerDatabase.cs @@ -0,0 +1,41 @@ +using Titano.Data; +using Titano.Server.Configuration; + +namespace Titano.Server.Hosting; + +/// Opens and migrates the server database at startup (BRIEF §6.1), waiting for a PostgreSQL container that is still starting. +public static class ServerDatabase +{ + private const int Attempts = 10; + private static readonly TimeSpan RetryDelay = TimeSpan.FromSeconds(3); + + public static async Task OpenAsync(ServerOptions options, ILogger logger, CancellationToken cancellationToken) + { + var settings = options.ToDatabaseOptions(); + for (var attempt = 1; ; attempt++) + { + try + { + var db = await TitanoDatabase.OpenAsync(settings, cancellationToken).ConfigureAwait(false); + logger.LogInformation("Database {Dialect} ready, schema version {Version}", db.Dialect, db.SchemaVersion); + return db; + } + catch (DatabaseConnectionException ex) when (attempt < Attempts) + { + logger.LogWarning("Database not reachable yet ({Reason}); retry {Attempt} of {Attempts} in {Delay} s", + ex.Message, attempt, Attempts - 1, RetryDelay.TotalSeconds); + await Task.Delay(RetryDelay, cancellationToken).ConfigureAwait(false); + } + } + } + + /// Engine and version, e.g. "PostgreSQL 17.2" or "SQLite 3.45.1". + public static async Task DescribeAsync(TitanoDatabase db, CancellationToken cancellationToken) + { + await using var session = await db.Database.OpenAsync(cancellationToken).ConfigureAwait(false); + var postgres = db.Dialect == Dialect.Postgres; + var version = await session.ScalarAsync(postgres ? "SHOW server_version" : "SELECT sqlite_version()", [], cancellationToken).ConfigureAwait(false); + var number = version?.Split(' ', 2)[0] ?? string.Empty; + return (postgres ? "PostgreSQL " : "SQLite ") + number; + } +} diff --git a/src/Titano.Server/Hosting/ServerHost.cs b/src/Titano.Server/Hosting/ServerHost.cs index 592a15d7..2f4cf173 100644 --- a/src/Titano.Server/Hosting/ServerHost.cs +++ b/src/Titano.Server/Hosting/ServerHost.cs @@ -1,48 +1,110 @@ using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.AspNetCore.Server.Kestrel.Core; using Titano.Core.Diagnostics; +using Titano.Data; +using Titano.Data.Archiving; using Titano.Protocol; using Titano.Server.Api; using Titano.Server.Configuration; +using Titano.Server.Events; +using Titano.Server.Jobs; +using Titano.Server.Security; +using Titano.Server.Storage; namespace Titano.Server.Hosting; -/// Builds the Titano Server web application from (used by Program and tests). +/// +/// Builds the Titano Server web application from (used by Program, the Windows service +/// host and tests). The archive module plugs in through the configureServices callback of , +/// which runs before the server's own registrations: it must register and +/// (the server refuses to start without them), may add +/// s, and may resolve , +/// and from DI in its factories. +/// public static class ServerHost { - public static WebApplication Build(ServerOptions options, string[] args) + /// Request bodies other than upload chunks stay small (BRIEF §14.2 limits). + public const long JsonBodyLimit = 1024 * 1024; + + public static WebApplication Build(ServerOptions options, string[] args, Action? configureServices = null) { Directory.CreateDirectory(options.DataPath); Directory.CreateDirectory(options.LogPath); + Directory.CreateDirectory(options.UploadsPath); + Directory.CreateDirectory(options.ArchivePath); - var builder = WebApplication.CreateSlimBuilder(args); + var builder = WebApplication.CreateSlimBuilder(new WebApplicationOptions { Args = args, ContentRootPath = AppContext.BaseDirectory }); builder.Logging.ClearProviders(); builder.Logging.AddSimpleConsole(c => c.SingleLine = true); builder.Logging.AddProvider(new FileLoggerProvider(new FileLogSink(options.LogPath, "server"))); builder.Logging.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning); builder.Logging.AddFilter("Microsoft.Hosting.Lifetime", Microsoft.Extensions.Logging.LogLevel.Information); - using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true)); - var certificate = CertificateStore.Load(options, bootLoggerFactory.CreateLogger("Titano.Server")); - var state = new ServerState { TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate) }; + // The archive module registers first, so a missing piece stops the server before it touches the database. + configureServices?.Invoke(builder.Services); + RequireArchiveModule(builder.Services); - builder.WebHost.ConfigureKestrel(k => + using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true)); + var bootLogger = bootLoggerFactory.CreateLogger("Titano.Server"); + var certificate = CertificateStore.Load(options, bootLogger); + var database = ServerDatabase.OpenAsync(options, bootLogger, CancellationToken.None).GetAwaiter().GetResult(); + try { - k.AddServerHeader = false; - k.Limits.MaxRequestBodySize = options.ChunkSizeBytes + 64 * 1024; - k.ListenAnyIP(options.HttpPort); - if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l => + var archive = ServerArchive.OpenAsync(database, options, TimeProvider.System, CancellationToken.None).GetAwaiter().GetResult(); + AdminBootstrap.RunAsync(database, options, TimeProvider.System, bootLogger, CancellationToken.None).GetAwaiter().GetResult(); + var state = new ServerState { - l.Protocols = HttpProtocols.Http1AndHttp2; - l.UseHttps(certificate); + TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate), + Database = ServerDatabase.DescribeAsync(database, CancellationToken.None).GetAwaiter().GetResult(), + }; + + builder.WebHost.ConfigureKestrel(k => + { + k.AddServerHeader = false; + k.Limits.MaxRequestBodySize = JsonBodyLimit; + k.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30); + k.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); + k.ListenAnyIP(options.HttpPort); + if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l => + { + l.Protocols = HttpProtocols.Http1AndHttp2; + l.UseHttps(certificate); + }); }); - }); - builder.Services.AddSingleton(options); - builder.Services.AddSingleton(state); - builder.Services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default)); + ConfigureServices(builder.Services, options, state, database, archive); + + var app = builder.Build(); + Log.SetSink(new LoggerSink(app.Services.GetRequiredService())); + ConfigurePipeline(app, options); + app.Logger.LogInformation("Titano Server {Version} · archive {Archive} · data {Data} · {Database} · TLS {Tls}{Fingerprint}", + SystemEndpoints.Version, options.ArchivePath, options.DataPath, state.Database, options.TlsMode, + state.TlsFingerprint is null ? string.Empty : " · SHA-256 " + state.TlsFingerprint); + app.Lifetime.ApplicationStopping.Register(() => app.Logger.LogInformation("Titano Server stopping")); + app.Lifetime.ApplicationStopped.Register(() => database.DisposeAsync().AsTask().GetAwaiter().GetResult()); + return app; + } + catch + { + database.DisposeAsync().AsTask().GetAwaiter().GetResult(); + throw; + } + } + + /// The single composition root of the server's services. + public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, TitanoDatabase database, ServerArchive archive) + { + services.AddSingleton(options); + services.AddSingleton(state); + services.AddSingleton(database); + services.AddSingleton(archive); + services.TryAddSingleton(TimeProvider.System); + services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default)); + services.Configure(o => o.ThrowOnBadRequest = true); + services.Configure(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30)); if (options.TlsMode == TlsMode.Proxy) { - builder.Services.Configure(f => + services.Configure(f => { f.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost; f.KnownIPNetworks.Clear(); @@ -50,49 +112,61 @@ public static class ServerHost }); } - var app = builder.Build(); - Log.SetSink(new LoggerSink(app.Services.GetRequiredService())); - var logger = app.Logger; - logger.LogInformation("Titano Server {Version} · archive {Archive} · data {Data} · TLS {Tls}{Fingerprint}", - SystemEndpoints.Version, options.ArchivePath, options.DataPath, options.TlsMode, - state.TlsFingerprint is null ? string.Empty : " · SHA-256 " + state.TlsFingerprint); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddHostedService(sp => sp.GetRequiredService()); + services.AddSingleton(); + services.AddHostedService(sp => sp.GetRequiredService()); + services.AddSingleton(); + services.AddHostedService(sp => sp.GetRequiredService()); + services.AddSingleton(); + services.AddHostedService(sp => sp.GetRequiredService()); + } + private static void ConfigurePipeline(WebApplication app, ServerOptions options) + { if (options.TlsMode == TlsMode.Proxy) app.UseForwardedHeaders(); + app.UseMiddleware(); + app.UseWebSockets(new WebSocketOptions { KeepAliveInterval = TimeSpan.FromSeconds(30) }); + app.UseRouting(); + app.UseMiddleware(); - app.Use(async (context, next) => + AdminPanel.Map(app, options); + var api = app.MapGroup("/api/v1"); + SystemEndpoints.Map(app, api); + AuthEndpoints.Map(api); + UserEndpoints.Map(api); + SequenceEndpoints.Map(api); + FileEndpoints.Map(api); + UploadEndpoints.Map(api); + JobEndpoints.Map(api); + NodeEndpoints.Map(api); + ArchiveEndpoints.Map(api); + AdminEndpoints.Map(api); + EventEndpoints.Map(api); + app.MapFallback("/api/{**path}", (HttpContext context) => ApiErrors.NotFound(context)).AllowAnonymousCaller(); + } + + /// Fails fast when the archive module's services are missing (the server would accept uploads it cannot commit). + private static void RequireArchiveModule(IServiceCollection services) + { + var missing = new List(); + if (!services.Any(d => d.ServiceType == typeof(IArchiveIngest))) missing.Add($"{typeof(IArchiveIngest).FullName} (commits verified uploads into the archive layout)"); + if (!services.Any(d => d.ServiceType == typeof(IFrameAssetLocator))) missing.Add($"{typeof(IFrameAssetLocator).FullName} (finds proxies and thumbnails)"); + if (missing.Count > 0) { - context.Response.Headers[ApiVersion.Header] = ApiVersion.Current.ToString(System.Globalization.CultureInfo.InvariantCulture); - context.Response.Headers.XContentTypeOptions = "nosniff"; - context.Response.Headers["Referrer-Policy"] = "no-referrer"; - try - { - await next(context); - } - catch (Exception ex) when (!context.Response.HasStarted && ex is not OperationCanceledException) - { - logger.LogError(ex, "Unhandled error on {Method} {Path}", context.Request.Method, context.Request.Path); - context.Response.StatusCode = StatusCodes.Status500InternalServerError; - await context.Response.WriteAsJsonAsync(new ApiError("internal", "Unexpected server error."), ProtocolJson.Default.ApiError); - } - }); - - var admin = new EmbeddedStaticFiles(typeof(ServerHost).Assembly, "admin/"); - app.MapGet("/", () => Results.Redirect("/admin/")); - app.MapMethods("/admin/{**path}", ["GET", "HEAD"], async (HttpContext context, string? path) => - { - // Relative asset URLs need the trailing slash. - if (!context.Request.Path.Value!.EndsWith('/') && string.IsNullOrEmpty(path)) - { - context.Response.Redirect("/admin/"); - return; - } - if (!await admin.TryServeAsync(context, path ?? string.Empty)) context.Response.StatusCode = StatusCodes.Status404NotFound; - }); - - SystemEndpoints.Map(app, options, state); - - app.MapFallback("/api/{**path}", () => Results.Json(new ApiError("not_found", "No such endpoint."), ProtocolJson.Default.ApiError, statusCode: 404)); - app.Lifetime.ApplicationStopping.Register(() => logger.LogInformation("Titano Server stopping")); - return app; + throw new InvalidOperationException( + "Titano Server cannot start: no implementation is registered for " + string.Join(" and ", missing) + + ". Register them in the services callback of ServerHost.Build."); + } } } diff --git a/src/Titano.Server/Hosting/Windows/ServiceInstaller.cs b/src/Titano.Server/Hosting/Windows/ServiceInstaller.cs new file mode 100644 index 00000000..7d3adc8f --- /dev/null +++ b/src/Titano.Server/Hosting/Windows/ServiceInstaller.cs @@ -0,0 +1,83 @@ +using System.ComponentModel; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using Titano.Core.Localization; +using static Titano.Server.Hosting.Windows.ServiceNative; + +namespace Titano.Server.Hosting.Windows; + +/// +/// --install-service / --uninstall-service: registers the server with the Service Control Manager +/// (automatic start, LocalSystem, command line "…\Titano.Server.exe" --service) or stops and removes it. +/// Both need an elevated prompt; failures surface as . +/// +[SupportedOSPlatform("windows")] +public static unsafe class ServiceInstaller +{ + private const string DisplayName = "Titano Server"; + + public static void Install() + { + var manager = OpenSCManagerW(null, null, ScManagerConnect | ScManagerCreateService); + if (manager == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); + try + { + var service = CreateServiceW(manager, WindowsServiceHost.ServiceName, DisplayName, ServiceAllAccess, ServiceWin32OwnProcess, ServiceAutoStart, + ServiceErrorNormal, CommandLine(), null, 0, null, null, null); + if (service == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); + try + { + fixed (char* text = Loc.L("Server.Service.Description")) + { + var description = new ServiceDescription { Description = text }; + ChangeServiceConfig2W(service, ServiceConfigDescription, &description); + } + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(manager); + } + } + + public static void Uninstall() + { + var manager = OpenSCManagerW(null, null, ScManagerConnect); + if (manager == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); + try + { + var service = OpenServiceW(manager, WindowsServiceHost.ServiceName, Delete | ServiceStop | ServiceQueryStatus); + if (service == 0) throw new Win32Exception(Marshal.GetLastPInvokeError()); + try + { + ServiceStatus status; + if (!ControlService(service, ControlStop, &status) && Marshal.GetLastPInvokeError() is var stopError && stopError != ErrorServiceNotActive) + throw new Win32Exception(stopError); + if (!DeleteService(service) && Marshal.GetLastPInvokeError() is var deleteError && deleteError != ErrorServiceMarkedForDelete) + throw new Win32Exception(deleteError); + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(manager); + } + } + + /// The service's command line: this executable (or dotnet plus this assembly) with --service. + private static string CommandLine() + { + var process = Environment.ProcessPath ?? throw new InvalidOperationException("The executable path is unknown."); + var assembly = typeof(ServiceInstaller).Assembly.Location; + return Path.GetFileNameWithoutExtension(process).Equals("dotnet", StringComparison.OrdinalIgnoreCase) + ? $"\"{process}\" \"{assembly}\" --service" + : $"\"{process}\" --service"; + } +} diff --git a/src/Titano.Server/Hosting/Windows/ServiceNative.cs b/src/Titano.Server/Hosting/Windows/ServiceNative.cs new file mode 100644 index 00000000..1b89fd1e --- /dev/null +++ b/src/Titano.Server/Hosting/Windows/ServiceNative.cs @@ -0,0 +1,93 @@ +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace Titano.Server.Hosting.Windows; + +/// Service Control Manager functions of advapi32 (no Windows-only package needed). +[SupportedOSPlatform("windows")] +internal static unsafe partial class ServiceNative +{ + public const uint ServiceWin32OwnProcess = 0x10; + public const uint ServiceAutoStart = 2; + public const uint ServiceErrorNormal = 1; + public const uint ScManagerConnect = 0x1; + public const uint ScManagerCreateService = 0x2; + public const uint ServiceAllAccess = 0xF01FF; + public const uint ServiceQueryStatus = 0x4; + public const uint ServiceStop = 0x20; + public const uint Delete = 0x10000; + public const uint ServiceConfigDescription = 1; + + public const uint ControlStop = 1; + public const uint ControlInterrogate = 4; + public const uint ControlShutdown = 5; + + public const int ErrorFailedServiceControllerConnect = 1063; + public const int ErrorServiceNotActive = 1062; + public const int ErrorServiceMarkedForDelete = 1072; + public const uint NoError = 0; + public const uint ErrorCallNotImplemented = 120; + public const uint ErrorExceptionInService = 1064; + + [StructLayout(LayoutKind.Sequential)] + public struct ServiceStatus + { + public uint ServiceType; + public uint CurrentState; + public uint ControlsAccepted; + public uint Win32ExitCode; + public uint ServiceSpecificExitCode; + public uint CheckPoint; + public uint WaitHint; + } + + [StructLayout(LayoutKind.Sequential)] + public struct ServiceTableEntry + { + public char* ServiceName; + public delegate* unmanaged[Stdcall] ServiceProc; + } + + [StructLayout(LayoutKind.Sequential)] + public struct ServiceDescription + { + public char* Description; + } + + [LibraryImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool StartServiceCtrlDispatcherW(ServiceTableEntry* table); + + [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] + public static partial nint RegisterServiceCtrlHandlerExW(string serviceName, delegate* unmanaged[Stdcall] handler, nint context); + + [LibraryImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool SetServiceStatus(nint statusHandle, ServiceStatus* status); + + [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] + public static partial nint OpenSCManagerW(string? machineName, string? databaseName, uint desiredAccess); + + [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] + public static partial nint CreateServiceW(nint manager, string serviceName, string displayName, uint desiredAccess, uint serviceType, uint startType, + uint errorControl, string binaryPathName, string? loadOrderGroup, nint tagId, string? dependencies, string? serviceStartName, string? password); + + [LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] + public static partial nint OpenServiceW(nint manager, string serviceName, uint desiredAccess); + + [LibraryImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool ChangeServiceConfig2W(nint service, uint infoLevel, void* info); + + [LibraryImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool ControlService(nint service, uint control, ServiceStatus* status); + + [LibraryImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool DeleteService(nint service); + + [LibraryImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static partial bool CloseServiceHandle(nint handle); +} diff --git a/src/Titano.Server/Hosting/Windows/ServiceStatusTracker.cs b/src/Titano.Server/Hosting/Windows/ServiceStatusTracker.cs new file mode 100644 index 00000000..01d55ad4 --- /dev/null +++ b/src/Titano.Server/Hosting/Windows/ServiceStatusTracker.cs @@ -0,0 +1,68 @@ +namespace Titano.Server.Hosting.Windows; + +/// SERVICE_STATUS.dwCurrentState values the server reports. +public enum ServiceState : uint +{ + Stopped = 1, + StartPending = 2, + StopPending = 3, + Running = 4, +} + +/// One status report for SetServiceStatus. +public readonly record struct ServiceStatusReport(ServiceState State, uint ControlsAccepted, uint CheckPoint, uint WaitHintMilliseconds, uint ExitCode); + +/// +/// The service's state machine and the rules of SERVICE_STATUS: no controls are accepted while pending, the check point +/// grows with every pending report and is zero otherwise, a stopped service carries its exit code. +/// +public sealed class ServiceStatusTracker +{ + public const uint AcceptStop = 0x1; + public const uint AcceptShutdown = 0x4; + + private readonly Lock _gate = new(); + private uint _checkPoint; + + public ServiceStatusReport Current { get; private set; } = new(ServiceState.Stopped, 0, 0, 0, 0); + + public ServiceStatusReport StartPending(uint waitHintMilliseconds) => Pending(ServiceState.StartPending, waitHintMilliseconds, ServiceState.Stopped, ServiceState.StartPending); + + public ServiceStatusReport Running() + { + lock (_gate) + { + Require(ServiceState.StartPending); + _checkPoint = 0; + return Current = new ServiceStatusReport(ServiceState.Running, AcceptStop | AcceptShutdown, 0, 0, 0); + } + } + + /// Stopping from start-up or from running; repeating it reports progress (a higher check point). + public ServiceStatusReport StopPending(uint waitHintMilliseconds) => + Pending(ServiceState.StopPending, waitHintMilliseconds, ServiceState.StartPending, ServiceState.Running, ServiceState.StopPending); + + public ServiceStatusReport Stopped(uint exitCode) + { + lock (_gate) + { + _checkPoint = 0; + return Current = new ServiceStatusReport(ServiceState.Stopped, 0, 0, 0, exitCode); + } + } + + private ServiceStatusReport Pending(ServiceState state, uint waitHintMilliseconds, params ServiceState[] from) + { + lock (_gate) + { + Require(from); + _checkPoint = Current.State == state ? _checkPoint + 1 : 1; + return Current = new ServiceStatusReport(state, 0, _checkPoint, waitHintMilliseconds, 0); + } + } + + private void Require(params ServiceState[] allowed) + { + if (!allowed.Contains(Current.State)) throw new InvalidOperationException($"A service cannot go from {Current.State} to that state."); + } +} diff --git a/src/Titano.Server/Hosting/Windows/WindowsServiceHost.cs b/src/Titano.Server/Hosting/Windows/WindowsServiceHost.cs new file mode 100644 index 00000000..d08a87e7 --- /dev/null +++ b/src/Titano.Server/Hosting/Windows/WindowsServiceHost.cs @@ -0,0 +1,117 @@ +using System.ComponentModel; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using static Titano.Server.Hosting.Windows.ServiceNative; + +namespace Titano.Server.Hosting.Windows; + +/// +/// Runs the server as a Windows service with the SCM API directly (StartServiceCtrlDispatcherW, +/// RegisterServiceCtrlHandlerExW, SetServiceStatus). Stop and shutdown requests stop the web host gracefully: running +/// jobs keep their checkpoints and resume at the next start. +/// +[SupportedOSPlatform("windows")] +public static class WindowsServiceHost +{ + public const string ServiceName = "TitanoServer"; + private const uint StartWaitHint = 60_000; + private const uint StopWaitHint = 35_000; + + private static readonly ServiceStatusTracker Status = new(); + private static Func? _build; + private static WebApplication? _app; + private static nint _statusHandle; + + /// + /// Hands the process to the Service Control Manager and returns when the service has stopped. False when the + /// process was not started by the SCM (for example from a console). + /// + public static unsafe bool Run(Func build) + { + _build = build; + fixed (char* name = ServiceName) + { + var table = stackalloc ServiceTableEntry[2]; + table[0] = new ServiceTableEntry { ServiceName = name, ServiceProc = &ServiceMain }; + table[1] = default; + if (StartServiceCtrlDispatcherW(table)) return true; + } + var error = Marshal.GetLastPInvokeError(); + if (error == ErrorFailedServiceControllerConnect) return false; + throw new Win32Exception(error); + } + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] + private static unsafe void ServiceMain(uint argc, char** argv) + { + _statusHandle = RegisterServiceCtrlHandlerExW(ServiceName, &Handler, 0); + if (_statusHandle == 0) return; + Report(Status.StartPending(StartWaitHint)); + try + { + _app = _build!(); + _app.StartAsync().GetAwaiter().GetResult(); + Report(Status.Running()); + } + catch (Exception ex) + { + Console.Error.WriteLine(ex); + Report(Status.Stopped(ErrorExceptionInService)); + } + } + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] + private static unsafe uint Handler(uint control, uint eventType, nint eventData, nint context) + { + switch (control) + { + case ControlStop or ControlShutdown: + if (Status.Current.State is ServiceState.Running or ServiceState.StartPending) + { + Report(Status.StopPending(StopWaitHint)); + // The handler must return at once; the host stops on the thread pool. + _ = Task.Run(StopAsync); + } + return NoError; + case ControlInterrogate: + Report(Status.Current); + return NoError; + default: + return ErrorCallNotImplemented; + } + } + + private static async Task StopAsync() + { + uint exitCode = 0; + try + { + if (_app is { } app) + { + await app.StopAsync(); + await app.DisposeAsync(); + } + } + catch (Exception ex) + { + Console.Error.WriteLine(ex); + exitCode = ErrorExceptionInService; + } + Report(Status.Stopped(exitCode)); + } + + private static unsafe void Report(ServiceStatusReport report) + { + var status = new ServiceStatus + { + ServiceType = ServiceWin32OwnProcess, + CurrentState = (uint)report.State, + ControlsAccepted = report.ControlsAccepted, + Win32ExitCode = report.ExitCode, + CheckPoint = report.CheckPoint, + WaitHint = report.WaitHintMilliseconds, + }; + SetServiceStatus(_statusHandle, &status); + } +} diff --git a/src/Titano.Server/Jobs/JobEngineHost.cs b/src/Titano.Server/Jobs/JobEngineHost.cs new file mode 100644 index 00000000..b2870592 --- /dev/null +++ b/src/Titano.Server/Jobs/JobEngineHost.cs @@ -0,0 +1,82 @@ +using System.Diagnostics; +using Titano.Core.Model; +using Titano.Data; +using Titano.Data.Jobs; +using Titano.Protocol; +using Titano.Server.Api; +using Titano.Server.Configuration; +using Titano.Server.Events; + +namespace Titano.Server.Jobs; + +/// +/// Hosts the server's (BRIEF §14.6) with the s registered in DI and +/// relays its state changes and progress as WebSocket events. Render jobs are never run here: render nodes claim them +/// (BRIEF §14.7). On shutdown running jobs stay marked running and are requeued from their checkpoints at the next start. +/// +public sealed class JobEngineHost : IHostedService +{ + private static readonly long ProgressInterval = Stopwatch.Frequency / 4; + + private readonly TitanoDatabase _db; + private readonly EventHub _events; + private readonly HashSet _kinds; + private readonly Dictionary _lastProgress = []; + private readonly Lock _gate = new(); + + public JobEngineHost(TitanoDatabase db, IEnumerable handlers, ServerOptions options, EventHub events) + { + _db = db; + _events = events; + var local = handlers.Where(h => h.Kind != JobKind.Render).ToList(); + _kinds = [.. local.Select(h => h.Kind)]; + // Without handlers no worker starts: an engine with an empty kind list would claim every kind. + Workers = _kinds.Count == 0 ? 0 : options.Workers; + Engine = new JobEngine(db, local, new JobEngineOptions + { + Workers = Workers, + WorkerId = WorkerId, + Kinds = _kinds, + HistoryRetention = TimeSpan.FromDays(options.JobHistoryDays), + }); + Engine.JobChanged += OnJobChanged; + Engine.Progress += OnProgress; + } + + /// Node id stamped on jobs this server runs (its workers use WorkerId#n). + public static string WorkerId { get; } = "server:" + Environment.MachineName; + + public JobEngine Engine { get; } + + public int Workers { get; } + + /// True when a job of has somewhere to run: a local handler, or render nodes for renders. + public bool CanRun(JobKind kind) => kind == JobKind.Render || _kinds.Contains(kind); + + public Task StartAsync(CancellationToken cancellationToken) => Engine.StartAsync(cancellationToken); + + public async Task StopAsync(CancellationToken cancellationToken) => await Engine.DisposeAsync(); + + /// Publishes a state change made outside the engine (render nodes, cancellation of node jobs). + public void Publish(Job job) => OnJobChanged(job); + + private void OnJobChanged(Job job) + { + if (job.State != JobState.Running) + { + lock (_gate) _lastProgress.Remove(job.Id); + } + _events.Publish(new JobStateEvent(job.Id, JobWire.State(job.State), job.Error)); + } + + private void OnProgress(JobProgressInfo progress) + { + var now = Stopwatch.GetTimestamp(); + lock (_gate) + { + if (_lastProgress.TryGetValue(progress.JobId, out var last) && now - last < ProgressInterval && progress.Done < progress.Total) return; + _lastProgress[progress.JobId] = now; + } + _events.Publish(new JobProgressEvent(progress.JobId, progress.Done, progress.Total, progress.ItemsPerSecond, progress.Eta?.TotalSeconds)); + } +} diff --git a/src/Titano.Server/Jobs/RenderNodeMonitor.cs b/src/Titano.Server/Jobs/RenderNodeMonitor.cs new file mode 100644 index 00000000..f8d5b73d --- /dev/null +++ b/src/Titano.Server/Jobs/RenderNodeMonitor.cs @@ -0,0 +1,72 @@ +using Titano.Core.Model; +using Titano.Data; +using Titano.Protocol; +using Titano.Server.Configuration; +using Titano.Server.Events; + +namespace Titano.Server.Jobs; + +/// +/// Marks render nodes offline when they miss heartbeats for and gives +/// their running jobs back to the queue for another node; a job that lost its node times fails. +/// +public sealed class RenderNodeMonitor(TitanoDatabase db, ServerOptions options, JobEngineHost jobs, EventHub events, TimeProvider time, + ILogger logger) : BackgroundService +{ + public const int MaxAttempts = 5; + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + var interval = TimeSpan.FromSeconds(Math.Clamp(options.NodeTimeoutSeconds / 3.0, 1, 30)); + while (!stoppingToken.IsCancellationRequested) + { + try + { + await SweepAsync(stoppingToken); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + logger.LogWarning(ex, "Render node check failed"); + } + try + { + await Task.Delay(interval, time, stoppingToken); + } + catch (OperationCanceledException) + { + return; + } + } + } + + /// One pass: silent nodes go offline and their jobs are requeued (or failed after too many attempts). + public async Task SweepAsync(CancellationToken cancellationToken) + { + var silentSince = time.GetUtcNow() - TimeSpan.FromSeconds(options.NodeTimeoutSeconds); + foreach (var node in await db.RenderNodes.ListSilentAsync(silentSince, RenderNodeService.Offline, cancellationToken)) + { + await db.RenderNodes.SetStateAsync(node.Id, RenderNodeService.Offline, cancellationToken); + events.Publish(new NodeStateEvent(node.Id, node.ClientName, RenderNodeService.Offline)); + var key = RenderNodeService.NodeKey(node); + var lost = await db.Jobs.ListRunningByNodeAsync(key, cancellationToken); + foreach (var job in lost) + { + var line = RenderNodeService.AppendLog(job.LogTail, $"Render node {node.ClientName} stopped responding."); + if (job.Attempts >= MaxAttempts) + { + await db.Jobs.FinishNodeJobAsync(job.Id, key, JobState.Failed, $"Lost its render node {job.Attempts} times.", line, cancellationToken); + } + else + { + await db.Jobs.UpdateNodeProgressAsync(job.Id, key, job.ProgressDone, job.ProgressTotal, null, null, line, cancellationToken); + } + } + var requeued = await db.Jobs.RequeueRunningAsync(key, cancellationToken); + logger.LogWarning("Render node {Node} is offline; {Count} job(s) back in the queue", node.ClientName, requeued); + foreach (var job in lost) + { + if (await db.Jobs.GetAsync(job.Id, cancellationToken) is { } current) jobs.Publish(current); + } + } + } +} diff --git a/src/Titano.Server/Jobs/RenderNodeService.cs b/src/Titano.Server/Jobs/RenderNodeService.cs new file mode 100644 index 00000000..1e6f5e6a --- /dev/null +++ b/src/Titano.Server/Jobs/RenderNodeService.cs @@ -0,0 +1,240 @@ +using System.Globalization; +using System.Text.Json; +using Titano.Core.Model; +using Titano.Data; +using Titano.Data.Repositories; +using Titano.Protocol; +using Titano.Server.Api; +using Titano.Server.Configuration; +using Titano.Server.Events; +using Titano.Server.Security; +using Titano.Server.Storage; +using UploadState = Titano.Data.Repositories.UploadState; + +namespace Titano.Server.Jobs; + +/// +/// Render nodes (BRIEF §14.7): clients with the render-node right register their encoders, heartbeat, claim queued +/// render jobs they can encode, report progress, and upload the output in resumable chunks that become a render of +/// the sequence. A node that stops heart-beating loses its jobs to the queue (). +/// +public sealed class RenderNodeService( + TitanoDatabase db, + ServerOptions options, + UploadService uploads, + UploadStore store, + RenderStore renders, + JobEngineHost jobs, + EventHub events, + TimeProvider time) +{ + public const string Idle = "idle"; + public const string Busy = "busy"; + public const string Offline = "offline"; + private const int LogTailLines = 40; + + public async Task> RegisterAsync(Caller caller, RegisterNodeRequest request, CancellationToken cancellationToken) + { + var name = request.ClientName?.Trim(); + if (string.IsNullOrEmpty(name) || name.Length > 100) return Outcome.Fail(400, "bad_request", "clientName"); + if (request.Encoders is null || request.Encoders.Count > 64 || request.Encoders.Any(e => string.IsNullOrWhiteSpace(e?.Codec))) + return Outcome.Fail(400, "bad_request", "encoders"); + + var existing = (await db.RenderNodes.ListAsync(cancellationToken)).FirstOrDefault(n => n.UserId == caller.Id && n.ClientName == name); + var id = existing?.Id ?? Guid.CreateVersion7(); + var running = await db.Jobs.ListRunningByNodeAsync(id.ToString(), cancellationToken); + var node = new RenderNode + { + Id = id, + UserId = caller.Id, + ClientName = name, + CapabilitiesJson = JsonSerializer.Serialize(request with { ClientName = name }, ProtocolJson.Default.RegisterNodeRequest), + LastHeartbeat = time.GetUtcNow(), + State = running.Count > 0 ? Busy : Idle, + }; + await db.RenderNodes.RegisterAsync(node, cancellationToken); + events.Publish(new NodeStateEvent(node.Id, node.ClientName, node.State)); + return Dto.Node(node, caller.User.Username); + } + + public async Task> HeartbeatAsync(Caller caller, Guid nodeId, CancellationToken cancellationToken) + { + if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome.Fail(404, "not_found", "node"); + await TouchAsync(node, cancellationToken); + return true; + } + + /// The next queued render job this node can encode (highest priority, then oldest), or null when none fits. + public async Task> ClaimAsync(Caller caller, Guid nodeId, CancellationToken cancellationToken) + { + if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome.Fail(404, "not_found", "node"); + var capabilities = Dto.Capabilities(node); + foreach (var queued in await db.Jobs.ListQueuedAsync(JobKind.Render, 50, cancellationToken)) + { + if (JobEndpoints.ParseRender(queued.ParametersJson) is not { } parameters || capabilities is null || !CanEncode(capabilities, parameters)) continue; + if (await db.Jobs.TryClaimAsync(queued.Id, NodeKey(node), cancellationToken) is not { } job) continue; + jobs.Publish(job); + if (job.TargetIds.Count != 1 || await db.Sequences.GetAsync(job.TargetIds[0], cancellationToken) is not { } sequence) + { + await FinishAsync(node, job, JobState.Failed, "The sequence no longer exists.", cancellationToken); + continue; + } + await TouchAsync(node, cancellationToken); + return new ClaimedJob(Dto.Job(job), sequence.Id, parameters.SettingsJson ?? sequence.SettingsJson ?? "{}", parameters.PresetJson, + parameters.UseProxies, await ManifestFramesAsync(sequence, cancellationToken)); + } + await TouchAsync(node, cancellationToken); + return (ClaimedJob?)null; + } + + public async Task> ProgressAsync(Caller caller, Guid nodeId, Guid jobId, NodeProgress progress, CancellationToken cancellationToken) + { + if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome.Fail(404, "not_found", "node"); + if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome.Fail(409, "job_not_assigned"); + double? eta = progress.ItemsPerSecond is > 0 && progress.Total > progress.Done ? (progress.Total - progress.Done) / progress.ItemsPerSecond.Value : null; + var tail = AppendLog(job.LogTail, progress.LogLine); + if (!await db.Jobs.UpdateNodeProgressAsync(jobId, NodeKey(node), progress.Done, progress.Total, progress.ItemsPerSecond, + eta is { } seconds ? TimeSpan.FromSeconds(seconds) : null, tail, cancellationToken)) + return Outcome.Fail(409, "job_not_assigned"); + await TouchAsync(node, cancellationToken); + events.Publish(new JobProgressEvent(jobId, progress.Done, progress.Total, progress.ItemsPerSecond, eta)); + return true; + } + + public async Task> FailAsync(Caller caller, Guid nodeId, Guid jobId, NodeJobFailure failure, CancellationToken cancellationToken) + { + if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome.Fail(404, "not_found", "node"); + if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome.Fail(409, "job_not_assigned"); + var error = string.IsNullOrWhiteSpace(failure.Error) ? "Render failed on the node." : failure.Error.Trim()[..Math.Min(failure.Error.Trim().Length, 500)]; + return await FinishAsync(node, job, JobState.Failed, error, cancellationToken) ? true : Outcome.Fail(409, "job_not_assigned"); + } + + /// Starts (or resumes) the chunked upload of a job's output. + public async Task> StartResultAsync(Caller caller, Guid nodeId, Guid jobId, RenderResultRequest request, CancellationToken cancellationToken) + { + if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome.Fail(404, "not_found", "node"); + if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome.Fail(409, "job_not_assigned"); + if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Outcome.Fail(400, "invalid_file_name"); + if (!UploadRules.IsSha256(request.Sha256)) return Outcome.Fail(400, "invalid_hash"); + if (request.Size <= 0) return Outcome.Fail(400, "bad_request", "size"); + if (request.Size > options.MaxUploadBytes) + return Outcome.Fail(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture)); + if (string.IsNullOrWhiteSpace(request.PresetName)) return Outcome.Fail(400, "bad_request", "presetName"); + + var sha = request.Sha256.ToLowerInvariant(); + if (await db.Uploads.FindByJobAsync(jobId, cancellationToken) is { State: UploadState.Receiving } open && open.Sha256 == sha && open.Size == request.Size + && store.Exists(open.Id)) + return Started(open); + if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size) return Outcome.Fail(507, "insufficient_storage"); + + var now = time.GetUtcNow(); + var upload = await db.Uploads.CreateAsync(new Upload + { + Id = Guid.CreateVersion7(), + UserId = caller.Id, + SequenceId = job.TargetIds.Count == 1 ? job.TargetIds[0] : null, + FileName = fileName, + Size = request.Size, + Sha256 = sha, + ChunkSize = options.ChunkSizeBytes, + State = UploadState.Receiving, + Created = now, + UpdatedAt = now, + JobId = jobId, + RenderJson = JsonSerializer.Serialize(request with { FileName = fileName, Sha256 = sha }, ProtocolJson.Default.RenderResultRequest), + }, cancellationToken); + store.Create(upload.Id, upload.Size); + return Started(upload); + } + + /// Verifies the uploaded output, files it as a render of the sequence and marks the job done. + public async Task> CompleteResultAsync(Caller caller, Guid nodeId, Guid jobId, CancellationToken cancellationToken) + { + if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome.Fail(404, "not_found", "node"); + if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome.Fail(409, "job_not_assigned"); + var upload = await db.Uploads.FindByJobAsync(jobId, cancellationToken); + if (upload is not { State: UploadState.Receiving } || upload.RenderJson is null) return Outcome.Fail(409, "upload_state"); + if (!upload.IsComplete) + return Outcome.Fail(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture)); + if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Committing, cancellationToken: cancellationToken)) + return Outcome.Fail(409, "upload_state"); + + if (await uploads.StageVerifiedAsync(upload, cancellationToken) is not { } staged) return Outcome.Fail(422, "hash_mismatch"); + var output = JsonSerializer.Deserialize(upload.RenderJson, ProtocolJson.Default.RenderResultRequest)!; + var sequence = await db.Sequences.GetAsync(job.TargetIds[0], cancellationToken); + var render = sequence is null ? null : await renders.CommitAsync(sequence, staged, output, node.ClientName, cancellationToken); + if (render is null) + { + UploadStore.TryDelete(staged); + await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Failed, cancellationToken: CancellationToken.None); + store.Delete(upload.Id); + await FinishAsync(node, job, JobState.Failed, "The sequence has no folder in the archive.", CancellationToken.None); + return Outcome.Fail(409, "upload_state", "sequence"); + } + await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Committed, cancellationToken: CancellationToken.None); + store.Delete(upload.Id); + await FinishAsync(node, job, JobState.Done, null, CancellationToken.None); + if (await db.Sequences.GetAsync(sequence!.Id, CancellationToken.None) is { } updated) + events.Publish(new SequenceChangedEvent(updated.Id, "renders", updated.FramesVersion, updated.SettingsVersion)); + return Dto.Render(render); + } + + /// Running-job owner key stored in jobs.node_id. + public static string NodeKey(RenderNode node) => node.Id.ToString(); + + private async Task OwnedNodeAsync(Caller caller, Guid nodeId, CancellationToken cancellationToken) => + await db.RenderNodes.GetAsync(nodeId, cancellationToken) is { } node && node.UserId == caller.Id ? node : null; + + private async Task AssignedJobAsync(RenderNode node, Guid jobId, CancellationToken cancellationToken) => + await db.Jobs.GetAsync(jobId, cancellationToken) is { State: JobState.Running } job && job.NodeId == NodeKey(node) ? job : null; + + private async Task FinishAsync(RenderNode node, Job job, JobState state, string? error, CancellationToken cancellationToken) + { + var tail = error is null ? null : AppendLog(job.LogTail, error); + if (!await db.Jobs.FinishNodeJobAsync(job.Id, NodeKey(node), state, error, tail, cancellationToken)) return false; + if (await db.Jobs.GetAsync(job.Id, cancellationToken) is { } finished) jobs.Publish(finished); + await TouchAsync(node, cancellationToken); + return true; + } + + /// Records a sign of life and publishes the node's state when it changed. + private async Task TouchAsync(RenderNode node, CancellationToken cancellationToken) + { + var state = (await db.Jobs.ListRunningByNodeAsync(NodeKey(node), cancellationToken)).Count > 0 ? Busy : Idle; + await db.RenderNodes.HeartbeatAsync(node.Id, state, time.GetUtcNow(), cancellationToken); + if (state != node.State) events.Publish(new NodeStateEvent(node.Id, node.ClientName, state)); + } + + private async Task> ManifestFramesAsync(Sequence sequence, CancellationToken cancellationToken) + { + var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt); + var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt); + var frames = new List(sequence.FrameCount); + var after = -1; + while (true) + { + var batch = await db.Frames.ListBySequenceAsync(sequence.Id, after, 1000, cancellationToken); + frames.AddRange(batch.Where(f => !f.Flags.HasFlag(FrameFlags.Excluded)) + .Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb))); + if (batch.Count < 1000) return frames; + after = batch[^1].Index; + } + } + + /// The node has an encoder for the job's codec and handles its resolution (0 = no announced limit). + private static bool CanEncode(RegisterNodeRequest node, RenderJobParameters job) => + node.Encoders.Any(e => string.Equals(e.Codec, job.Codec, StringComparison.OrdinalIgnoreCase)) + && (node.MaxWidth <= 0 || job.Width <= node.MaxWidth) + && (node.MaxHeight <= 0 || job.Height <= node.MaxHeight); + + internal static string? AppendLog(string? tail, string? line) + { + if (string.IsNullOrWhiteSpace(line)) return tail; + var lines = (tail ?? string.Empty).Split('\n', StringSplitOptions.RemoveEmptyEntries).ToList(); + lines.Add(string.Create(CultureInfo.InvariantCulture, $"{DateTime.UtcNow:HH:mm:ss} {line.Trim()[..Math.Min(line.Trim().Length, 300)]}")); + return string.Join('\n', lines.TakeLast(LogTailLines)); + } + + private static StartUploadResponse Started(Upload upload) => + new(upload.Id, upload.ChunkSize, UploadRules.ReceivedChunks(upload), AlreadyExists: false, FrameId: null); +} diff --git a/src/Titano.Server/Jobs/SequenceJobs.cs b/src/Titano.Server/Jobs/SequenceJobs.cs new file mode 100644 index 00000000..f53c962f --- /dev/null +++ b/src/Titano.Server/Jobs/SequenceJobs.cs @@ -0,0 +1,32 @@ +using Titano.Core.Model; +using Titano.Data; + +namespace Titano.Server.Jobs; + +/// +/// Follow-up work after frames arrive (BRIEF §14.4): thumbnails, proxies and light analysis of the sequence, queued at +/// background priority once per sequence while one is still waiting, and only for kinds this server can run. +/// +public sealed class SequenceJobs(TitanoDatabase db, JobEngineHost jobs) +{ + private static readonly JobKind[] AfterNewFrames = [JobKind.Thumbs, JobKind.Proxies, JobKind.LightAnalyse]; + + private readonly SemaphoreSlim _gate = new(1, 1); + + public async Task FramesAddedAsync(Sequence sequence, CancellationToken cancellationToken) + { + await _gate.WaitAsync(cancellationToken); + try + { + foreach (var kind in AfterNewFrames) + { + if (!jobs.CanRun(kind) || await db.Jobs.IsQueuedAsync(kind, sequence.Id, cancellationToken)) continue; + await jobs.Engine.EnqueueAsync(kind, [sequence.Id], sequence.DisplayTitle, priority: JobPriority.Background, cancellationToken: cancellationToken); + } + } + finally + { + _gate.Release(); + } + } +} diff --git a/src/Titano.Server/Program.cs b/src/Titano.Server/Program.cs index 9bfebd2c..bb9e439f 100644 --- a/src/Titano.Server/Program.cs +++ b/src/Titano.Server/Program.cs @@ -1,11 +1,53 @@ +using System.ComponentModel; using Titano.Server.Configuration; using Titano.Server.Hosting; +using Titano.Server.Hosting.Windows; -if (args.Contains("--healthcheck")) return await HealthProbe.RunAsync(); +var command = ServerCommandLine.Parse(args); +if (command.Error is not null) +{ + await Console.Error.WriteLineAsync(command.Error); + return 2; +} -var app = ServerHost.Build(ServerOptions.Load(), args); -await app.RunAsync(); -return 0; +switch (command.Command) +{ + case ServerCommand.Help: + Console.WriteLine(ServerCommandLine.Usage); + return 0; + case ServerCommand.HealthCheck: + return await HealthProbe.RunAsync(); + case ServerCommand.Service or ServerCommand.InstallService or ServerCommand.UninstallService when !OperatingSystem.IsWindows(): + await Console.Error.WriteLineAsync("Windows services are available on Windows only."); + return 2; + case ServerCommand.InstallService when OperatingSystem.IsWindows(): + return ServiceAction(ServiceInstaller.Install, $"Installed the {WindowsServiceHost.ServiceName} service."); + case ServerCommand.UninstallService when OperatingSystem.IsWindows(): + return ServiceAction(ServiceInstaller.Uninstall, $"Removed the {WindowsServiceHost.ServiceName} service."); + case ServerCommand.Service when OperatingSystem.IsWindows(): + if (WindowsServiceHost.Run(() => ServerHost.Build(ServerOptions.Load(), [.. command.HostArguments]))) return 0; + await Console.Error.WriteLineAsync("--service is used by the Service Control Manager; start the service with 'sc start TitanoServer'."); + return 1; + default: + var app = ServerHost.Build(ServerOptions.Load(), [.. command.HostArguments]); + await app.RunAsync(); + return 0; +} + +static int ServiceAction(Action action, string done) +{ + try + { + action(); + Console.WriteLine(done); + return 0; + } + catch (Win32Exception ex) + { + Console.Error.WriteLine(ex.Message); + return 1; + } +} /// Entry point type, visible to the in-process test host. public partial class Program; diff --git a/src/Titano.Server/Security/AccessRule.cs b/src/Titano.Server/Security/AccessRule.cs new file mode 100644 index 00000000..923829a7 --- /dev/null +++ b/src/Titano.Server/Security/AccessRule.cs @@ -0,0 +1,38 @@ +using Titano.Core.Model; + +namespace Titano.Server.Security; + +/// +/// Endpoint metadata read by . Every /api endpoint requires a signed-in +/// caller unless it carries ; the most specific rule (endpoint over group) wins whole. +/// +public sealed class AccessRule +{ + public static readonly AccessRule Anonymous = new() { AllowAnonymous = true }; + + public static readonly AccessRule SignedIn = new(); + + public bool AllowAnonymous { get; private init; } + + /// The caller needs at least one of these permissions; = any signed-in caller. + public Permissions AnyOf { get; private init; } + + /// Reachable while the account must still replace its temporary password (me, password, logout). + public bool DuringPasswordChange { get; private init; } + + public static AccessRule Requiring(Permissions anyOf) => new() { AnyOf = anyOf }; + + public static AccessRule PasswordChange { get; } = new() { DuringPasswordChange = true }; +} + +public static class AccessRuleExtensions +{ + public static TBuilder AllowAnonymousCaller(this TBuilder builder) where TBuilder : IEndpointConventionBuilder => + builder.WithMetadata(AccessRule.Anonymous); + + public static TBuilder Requires(this TBuilder builder, Permissions anyOf) where TBuilder : IEndpointConventionBuilder => + builder.WithMetadata(AccessRule.Requiring(anyOf)); + + public static TBuilder AllowDuringPasswordChange(this TBuilder builder) where TBuilder : IEndpointConventionBuilder => + builder.WithMetadata(AccessRule.PasswordChange); +} diff --git a/src/Titano.Server/Security/Accounts.cs b/src/Titano.Server/Security/Accounts.cs new file mode 100644 index 00000000..96ce6969 --- /dev/null +++ b/src/Titano.Server/Security/Accounts.cs @@ -0,0 +1,35 @@ +using Titano.Core.Model; + +namespace Titano.Server.Security; + +/// Account rules shared by bootstrap, sign-in and user management. +public static class Accounts +{ + public const int MaxUsernameLength = 64; + + /// Letters, digits, dot, dash and underscore; 1–64 characters. + public static bool IsValidUsername(string? name) => + name is { Length: > 0 and <= MaxUsernameLength } && name.All(c => char.IsAsciiLetterOrDigit(c) || c is '.' or '-' or '_'); + + /// The case-insensitive key of a user name (lock-out bookkeeping). + public static string Key(string? name) => (name ?? string.Empty).Trim().ToLowerInvariant(); + + /// The role's default permissions with the render-node right granted or withheld explicitly. + public static Permissions PermissionsFor(UserRole role, bool renderNode) => + (RolePermissions.Default(role) & ~Permissions.RenderNode) | (renderNode ? Permissions.RenderNode : Permissions.None); + + /// Parses a role name (admin, editor, uploader, viewer); numbers are not accepted. + public static bool TryParseRole(string? text, out UserRole role) + { + foreach (var candidate in Enum.GetValues()) + { + if (string.Equals(candidate.ToString(), text, StringComparison.OrdinalIgnoreCase)) + { + role = candidate; + return true; + } + } + role = default; + return false; + } +} diff --git a/src/Titano.Server/Security/AdminBootstrap.cs b/src/Titano.Server/Security/AdminBootstrap.cs new file mode 100644 index 00000000..027c94ef --- /dev/null +++ b/src/Titano.Server/Security/AdminBootstrap.cs @@ -0,0 +1,85 @@ +using Titano.Core.Model; +using Titano.Data; +using Titano.Server.Configuration; + +namespace Titano.Server.Security; + +/// +/// First-run administrator (BRIEF §14.9): when no admin account exists, one is created from +/// TITANO_ADMIN_USER/TITANO_ADMIN_PASSWORD and must change its password at first sign-in. Without a +/// configured password a temporary one is generated and written to /data/initial-admin-password.txt (owner-only), +/// never to the log; the file is removed once an administrator changes their password. +/// +public static class AdminBootstrap +{ + public const string DefaultUsername = "admin"; + private const string PasswordFileName = "initial-admin-password.txt"; + + public static async Task RunAsync(TitanoDatabase db, ServerOptions options, TimeProvider time, ILogger logger, CancellationToken cancellationToken) + { + var users = await db.Users.ListAsync(cancellationToken).ConfigureAwait(false); + if (users.Any(u => u.Role == UserRole.Admin)) + { + if (!string.IsNullOrEmpty(options.AdminPassword)) + logger.LogInformation("An administrator exists; TITANO_ADMIN_USER and TITANO_ADMIN_PASSWORD are ignored"); + return; + } + + var username = string.IsNullOrWhiteSpace(options.AdminUser) ? DefaultUsername : options.AdminUser.Trim(); + if (!Accounts.IsValidUsername(username)) + throw new InvalidOperationException("TITANO_ADMIN_USER may only contain letters, digits, dot, dash and underscore (up to 64 characters)."); + if (users.Any(u => Accounts.Key(u.Username) == Accounts.Key(username))) + throw new InvalidOperationException($"No administrator exists and TITANO_ADMIN_USER '{username}' names an existing account; choose another name."); + + var password = options.AdminPassword; + string? passwordFile = null; + if (string.IsNullOrEmpty(password)) + { + password = TemporaryPassword.Generate(); + passwordFile = Path.Combine(options.DataPath, PasswordFileName); + await File.WriteAllTextAsync(passwordFile, password + Environment.NewLine, cancellationToken).ConfigureAwait(false); + if (!OperatingSystem.IsWindows()) File.SetUnixFileMode(passwordFile, UnixFileMode.UserRead | UnixFileMode.UserWrite); + } + + var admin = new User + { + Id = Guid.CreateVersion7(), + Username = username, + DisplayName = null, + PasswordHash = PasswordHasher.Hash(password), + Role = UserRole.Admin, + Permissions = Permissions.All, + MustChangePassword = true, + Created = time.GetUtcNow(), + }; + await db.Users.CreateAsync(admin, cancellationToken).ConfigureAwait(false); + await db.Audit.AppendAsync(new AuditEntry + { + Id = Guid.CreateVersion7(), + At = admin.Created, + Action = "user_created", + TargetKind = "user", + TargetId = admin.Id.ToString(), + Details = "admin", + Username = username, + }, cancellationToken).ConfigureAwait(false); + + if (passwordFile is null) + logger.LogWarning("Created administrator '{User}' from TITANO_ADMIN_USER/TITANO_ADMIN_PASSWORD; the password must be changed at first sign-in", username); + else + logger.LogWarning("Created administrator '{User}'; its temporary password is in {File} and must be changed at first sign-in", username, passwordFile); + } + + /// Removes the generated password file (called when an administrator sets their own password). + public static void ForgetInitialPassword(ServerOptions options) + { + try + { + File.Delete(Path.Combine(options.DataPath, PasswordFileName)); + } + catch (IOException) + { + // Best effort: the file holds a password that no longer works. + } + } +} diff --git a/src/Titano.Server/Security/AuditLog.cs b/src/Titano.Server/Security/AuditLog.cs new file mode 100644 index 00000000..7afadcca --- /dev/null +++ b/src/Titano.Server/Security/AuditLog.cs @@ -0,0 +1,26 @@ +using Titano.Core.Model; +using Titano.Data; + +namespace Titano.Server.Security; + +/// Appends who did what to the audit log (BRIEF §14.2): sign-ins, user, sequence, archive and setting changes. +public sealed class AuditLog(TitanoDatabase db, TimeProvider time) +{ + public Task WriteAsync(Caller caller, HttpContext context, string action, string? targetKind = null, string? targetId = null, string? details = null) => + WriteAsync(caller.Id, caller.User.Username, context, action, targetKind, targetId, details); + + public Task WriteAsync(Guid? userId, string? username, HttpContext? context, string action, string? targetKind = null, string? targetId = null, + string? details = null) => + db.Audit.AppendAsync(new AuditEntry + { + Id = Guid.CreateVersion7(), + At = time.GetUtcNow(), + UserId = userId, + Username = username, + Action = action, + TargetKind = targetKind, + TargetId = targetId, + Details = details, + RemoteAddress = context?.Connection.RemoteIpAddress?.ToString(), + }); +} diff --git a/src/Titano.Server/Security/AuthenticationMiddleware.cs b/src/Titano.Server/Security/AuthenticationMiddleware.cs new file mode 100644 index 00000000..b0e18541 --- /dev/null +++ b/src/Titano.Server/Security/AuthenticationMiddleware.cs @@ -0,0 +1,79 @@ +using Titano.Core.Model; +using Titano.Server.Api; + +namespace Titano.Server.Security; + +/// +/// Resolves the caller of every /api request (bearer token or admin cookie) and enforces the endpoint's +/// : 401 without a valid token, 403 for a missing permission, a missing CSRF header on a +/// cookie request, or a pending password change. Runs after routing, so the endpoint's metadata is known. +/// +public sealed class AuthenticationMiddleware(RequestDelegate next, TokenService tokens) +{ + public async Task InvokeAsync(HttpContext context) + { + var rule = context.GetEndpoint()?.Metadata.GetMetadata(); + if (rule is null && !context.Request.Path.StartsWithSegments("/api")) + { + await next(context); + return; + } + rule ??= AccessRule.SignedIn; + if (rule.AllowAnonymous) + { + await next(context); + return; + } + + var caller = await AuthenticateAsync(context); + if (caller is null) + { + context.Response.Headers.WWWAuthenticate = "Bearer"; + await ApiErrors.WriteAsync(context, StatusCodes.Status401Unauthorized, "unauthorized"); + return; + } + if (caller.Source == CredentialSource.Cookie && !IsSameSiteRequest(context, caller)) + { + await ApiErrors.WriteAsync(context, StatusCodes.Status403Forbidden, "csrf_failed"); + return; + } + if (caller.User.MustChangePassword && !rule.DuringPasswordChange) + { + await ApiErrors.WriteAsync(context, StatusCodes.Status403Forbidden, "password_change_required"); + return; + } + if (rule.AnyOf != Permissions.None && !caller.Has(rule.AnyOf)) + { + await ApiErrors.WriteAsync(context, StatusCodes.Status403Forbidden, "forbidden"); + return; + } + context.Features.Set(caller); + await next(context); + } + + private Task AuthenticateAsync(HttpContext context) + { + var authorization = context.Request.Headers.Authorization.ToString(); + if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) + return tokens.ValidateAsync(authorization[7..].Trim(), CredentialSource.Bearer, context.RequestAborted); + if (context.Request.Cookies.TryGetValue(SessionCookies.Session, out var cookie)) + return tokens.ValidateAsync(cookie, CredentialSource.Cookie, context.RequestAborted); + return Task.FromResult(null); + } + + /// + /// Cookie requests must prove they come from the admin panel: unsafe methods carry the CSRF header, WebSocket + /// upgrades (which cannot) must come from this origin. + /// + private static bool IsSameSiteRequest(HttpContext context, Caller caller) + { + if (context.WebSockets.IsWebSocketRequest) + { + var origin = context.Request.Headers.Origin.ToString(); + return origin.Equals($"{context.Request.Scheme}://{context.Request.Host}", StringComparison.OrdinalIgnoreCase); + } + var method = context.Request.Method; + if (HttpMethods.IsGet(method) || HttpMethods.IsHead(method) || HttpMethods.IsOptions(method)) return true; + return SessionCookies.HasValidCsrf(context, caller); + } +} diff --git a/src/Titano.Server/Security/LoginGuard.cs b/src/Titano.Server/Security/LoginGuard.cs new file mode 100644 index 00000000..8e1e49f9 --- /dev/null +++ b/src/Titano.Server/Security/LoginGuard.cs @@ -0,0 +1,92 @@ +using Titano.Server.Configuration; + +namespace Titano.Server.Security; + +/// +/// Sign-in throttling (BRIEF §14.2): at most attempts per client +/// address and minute, and an account name locked for after +/// consecutive failures. Names without an account are tracked the same +/// way, so a lockout reveals nothing about which names exist. State is per process and resets at restart. +/// +public sealed class LoginGuard(ServerOptions options, TimeProvider time) +{ + private static readonly TimeSpan Window = TimeSpan.FromMinutes(1); + private const int PruneAbove = 10_000; + + private readonly Lock _gate = new(); + private readonly Dictionary> _attempts = new(StringComparer.Ordinal); + private readonly Dictionary _accounts = new(StringComparer.Ordinal); + + /// Counts an attempt from ; false, with the time to wait, once its budget is spent. + public bool TryAttempt(string address, out TimeSpan retryAfter) + { + var now = time.GetUtcNow(); + lock (_gate) + { + if (_attempts.Count > PruneAbove) Prune(now); + if (!_attempts.TryGetValue(address, out var recent)) _attempts[address] = recent = new Queue(); + while (recent.Count > 0 && now - recent.Peek() >= Window) recent.Dequeue(); + if (recent.Count >= options.LoginAttemptsPerMinute) + { + retryAfter = Window - (now - recent.Peek()); + return false; + } + recent.Enqueue(now); + retryAfter = TimeSpan.Zero; + return true; + } + } + + /// True while (a normalised user name) is locked. + public bool IsLocked(string account, out TimeSpan retryAfter) + { + var now = time.GetUtcNow(); + lock (_gate) + { + if (_accounts.TryGetValue(account, out var state) && state.LockedUntil is { } until && until > now) + { + retryAfter = until - now; + return true; + } + retryAfter = TimeSpan.Zero; + return false; + } + } + + public void RecordFailure(string account) + { + var now = time.GetUtcNow(); + var span = TimeSpan.FromMinutes(options.LockoutMinutes); + lock (_gate) + { + if (_accounts.Count > PruneAbove) Prune(now); + var state = _accounts.GetValueOrDefault(account); + if (state?.LockedUntil is { } until && until > now) return; + if (state is null || state.LockedUntil is not null || now - state.FirstFailure > span) state = new AccountState(0, now, null); + var failures = state.Failures + 1; + _accounts[account] = failures >= options.LockoutThreshold + ? new AccountState(0, now, now + span) + : state with { Failures = failures }; + } + } + + public void RecordSuccess(string account) + { + lock (_gate) _accounts.Remove(account); + } + + private void Prune(DateTimeOffset now) + { + foreach (var (address, recent) in _attempts.ToList()) + { + if (recent.Count == 0 || now - recent.Last() >= Window) _attempts.Remove(address); + } + var span = TimeSpan.FromMinutes(options.LockoutMinutes); + foreach (var (account, state) in _accounts.ToList()) + { + if ((state.LockedUntil ?? state.FirstFailure + span) < now) _accounts.Remove(account); + } + } + + private sealed record AccountState(int Failures, DateTimeOffset FirstFailure, DateTimeOffset? LockedUntil); +} diff --git a/src/Titano.Server/Security/PasswordHasher.cs b/src/Titano.Server/Security/PasswordHasher.cs new file mode 100644 index 00000000..5f66b357 --- /dev/null +++ b/src/Titano.Server/Security/PasswordHasher.cs @@ -0,0 +1,69 @@ +using System.Globalization; +using System.Security.Cryptography; +using System.Text; + +namespace Titano.Server.Security; + +/// +/// Password hashes (BRIEF §14.2): PBKDF2-SHA512, 210,000 iterations, 16-byte random salt per hash, stored as +/// pbkdf2-sha512$<iterations>$<salt>$<hash> (base64). The format names its algorithm and cost, so +/// older hashes keep verifying and are upgraded at the next sign-in. +/// +public static class PasswordHasher +{ + public const int MinimumLength = 10; + public const int Iterations = 210_000; + private const string Algorithm = "pbkdf2-sha512"; + private const int SaltBytes = 16; + private const int HashBytes = 64; + + /// Verified against unknown user names so a miss costs as much time as a wrong password. + private static readonly Lazy DummyHash = new(() => Hash(Convert.ToBase64String(RandomNumberGenerator.GetBytes(18)))); + + public static string Hash(string password) + { + var salt = RandomNumberGenerator.GetBytes(SaltBytes); + var hash = Derive(password, salt, Iterations); + return string.Create(CultureInfo.InvariantCulture, $"{Algorithm}${Iterations}${Convert.ToBase64String(salt)}${Convert.ToBase64String(hash)}"); + } + + /// Constant-time check of against a stored hash; malformed hashes never match. + public static bool Verify(string password, string storedHash) + { + var parts = storedHash.Split('$'); + if (parts.Length != 4 || parts[0] != Algorithm + || !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var iterations) || iterations < 1) + return false; + byte[] salt, expected; + try + { + salt = Convert.FromBase64String(parts[2]); + expected = Convert.FromBase64String(parts[3]); + } + catch (FormatException) + { + return false; + } + if (salt.Length == 0 || expected.Length == 0) return false; + var actual = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, HashAlgorithmName.SHA512, expected.Length); + return CryptographicOperations.FixedTimeEquals(actual, expected); + } + + /// Spends the same time as for a name that has no account. + public static void VerifyDummy(string password) => Verify(password, DummyHash.Value); + + /// True when the hash was made with fewer iterations than the current policy. + public static bool NeedsRehash(string storedHash) + { + var parts = storedHash.Split('$'); + return parts.Length != 4 || parts[0] != Algorithm + || !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var iterations) || iterations < Iterations; + } + + /// Policy check for passwords users choose: at least characters, not only spaces. + public static bool IsAcceptable(string? password) => + password is not null && password.Length >= MinimumLength && !string.IsNullOrWhiteSpace(password); + + private static byte[] Derive(string password, byte[] salt, int iterations) => + Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, HashAlgorithmName.SHA512, HashBytes); +} diff --git a/src/Titano.Server/Security/SessionCookies.cs b/src/Titano.Server/Security/SessionCookies.cs new file mode 100644 index 00000000..8e49a4df --- /dev/null +++ b/src/Titano.Server/Security/SessionCookies.cs @@ -0,0 +1,55 @@ +using System.Buffers.Text; +using System.Net; +using System.Security.Cryptography; +using System.Text; + +namespace Titano.Server.Security; + +/// +/// The web admin's cookie session (BRIEF §14.2): the token in an HttpOnly; Secure; SameSite=Strict cookie, plus a +/// script-readable CSRF cookie whose value must come back in on every unsafe request. The CSRF +/// value is derived from the session token, so a planted cookie pair cannot forge it. +/// +public static class SessionCookies +{ + public const string Session = "titano_session"; + public const string Csrf = "titano_csrf"; + public const string CsrfHeader = "X-CSRF-Token"; + + public static void Issue(HttpContext context, IssuedToken token) + { + context.Response.Cookies.Append(Session, token.Secret, Options(httpOnly: true)); + context.Response.Cookies.Append(Csrf, CsrfFor(token.Stored.TokenHash), Options(httpOnly: false)); + } + + public static void Clear(HttpContext context) + { + context.Response.Cookies.Delete(Session, Options(httpOnly: true)); + context.Response.Cookies.Delete(Csrf, Options(httpOnly: false)); + } + + /// Constant-time check of the request's CSRF header against the caller's session. + public static bool HasValidCsrf(HttpContext context, Caller caller) + { + var header = context.Request.Headers[CsrfHeader].ToString(); + var expected = CsrfFor(caller.TokenHash); + return header.Length == expected.Length + && CryptographicOperations.FixedTimeEquals(Encoding.ASCII.GetBytes(header), Encoding.ASCII.GetBytes(expected)); + } + + /// Secure cookies only travel over HTTPS (or to the machine itself, which browsers treat as secure). + public static bool IsSecureTransport(HttpContext context) => + context.Request.IsHttps || (context.Connection.RemoteIpAddress is { } remote && IPAddress.IsLoopback(remote)); + + private static string CsrfFor(string tokenHash) => + Base64Url.EncodeToString(SHA256.HashData(Encoding.UTF8.GetBytes("titano-csrf|" + tokenHash))); + + private static CookieOptions Options(bool httpOnly) => new() + { + HttpOnly = httpOnly, + Secure = true, + SameSite = SameSiteMode.Strict, + Path = "/", + IsEssential = true, + }; +} diff --git a/src/Titano.Server/Security/TemporaryPassword.cs b/src/Titano.Server/Security/TemporaryPassword.cs new file mode 100644 index 00000000..bf6874f1 --- /dev/null +++ b/src/Titano.Server/Security/TemporaryPassword.cs @@ -0,0 +1,15 @@ +using System.Security.Cryptography; + +namespace Titano.Server.Security; + +/// One-time passwords for new accounts and resets: three groups of four unambiguous characters (≈ 70 bits). +public static class TemporaryPassword +{ + private const string Alphabet = "abcdefghjkmnpqrstuvwxyz23456789"; + + public static string Generate() + { + var chars = RandomNumberGenerator.GetItems(Alphabet, 12); + return $"{new string(chars, 0, 4)}-{new string(chars, 4, 4)}-{new string(chars, 8, 4)}"; + } +} diff --git a/src/Titano.Server/Security/TokenService.cs b/src/Titano.Server/Security/TokenService.cs new file mode 100644 index 00000000..d364f51d --- /dev/null +++ b/src/Titano.Server/Security/TokenService.cs @@ -0,0 +1,91 @@ +using System.Buffers.Text; +using System.Security.Cryptography; +using System.Text; +using Titano.Core.Model; +using Titano.Data; +using Titano.Server.Configuration; + +namespace Titano.Server.Security; + +/// How a request presented its token. +public enum CredentialSource +{ + /// Authorization: Bearer (desktop client, uploader, render node). + Bearer, + + /// HttpOnly session cookie of the web admin; unsafe methods also need the CSRF header. + Cookie, +} + +/// The signed-in account behind a request. +public sealed record Caller(User User, string TokenHash, CredentialSource Source, string? ClientName) +{ + public Guid Id => User.Id; + + public bool IsAdmin => User.Role == UserRole.Admin; + + /// True when the account holds at least one of . + public bool Has(Permissions any) => (User.Permissions & any) != 0; + + /// Minimal-API binding: endpoint handlers take the caller resolved by . + public static ValueTask BindAsync(HttpContext context) => ValueTask.FromResult(context.Features.Get()); +} + +/// A freshly issued token: the secret (returned once) and what is stored (its hash). +public sealed record IssuedToken(string Secret, SessionToken Stored); + +/// +/// Opaque bearer tokens (BRIEF §14.2): 32 random bytes, stored only as their SHA-256, sliding expiry, revocation. +/// The same tokens back the admin panel's cookie session. +/// +public sealed class TokenService(TitanoDatabase db, ServerOptions options, TimeProvider time) +{ + /// Sliding expiry is written at most this often per token, not on every request. + private static readonly TimeSpan TouchInterval = TimeSpan.FromMinutes(1); + + private TimeSpan Lifetime => TimeSpan.FromDays(options.TokenLifetimeDays); + + public async Task IssueAsync(User user, string? clientName, CancellationToken cancellationToken) + { + var secret = Base64Url.EncodeToString(RandomNumberGenerator.GetBytes(32)); + var now = time.GetUtcNow(); + var name = clientName?.Trim(); + var token = new SessionToken + { + TokenHash = HashOf(secret), + UserId = user.Id, + ClientName = string.IsNullOrEmpty(name) ? null : name[..Math.Min(name.Length, 100)], + Created = now, + Expires = now + Lifetime, + LastUsed = now, + }; + await db.Tokens.InsertAsync(token, cancellationToken).ConfigureAwait(false); + return new IssuedToken(secret, token); + } + + /// The caller a token belongs to, or null when it is unknown, revoked, expired or its account is disabled. + public async Task ValidateAsync(string secret, CredentialSource source, CancellationToken cancellationToken) + { + if (secret.Length is 0 or > 128) return null; + var hash = HashOf(secret); + var token = await db.Tokens.GetAsync(hash, cancellationToken).ConfigureAwait(false); + var now = time.GetUtcNow(); + if (token is null || token.Revoked || token.Expires <= now) return null; + var user = await db.Users.GetAsync(token.UserId, cancellationToken).ConfigureAwait(false); + if (user is null || user.Disabled) return null; + if (now - token.LastUsed >= TouchInterval && !await db.Tokens.TouchAsync(hash, now, now + Lifetime, cancellationToken).ConfigureAwait(false)) + return null; + return new Caller(user, hash, source, token.ClientName); + } + + /// True while the caller's token is still live and its account enabled (long-lived WebSocket sessions). + public async Task IsStillValidAsync(Caller caller, CancellationToken cancellationToken) + { + var token = await db.Tokens.GetAsync(caller.TokenHash, cancellationToken).ConfigureAwait(false); + if (token is null || token.Revoked || token.Expires <= time.GetUtcNow()) return false; + return await db.Users.GetAsync(caller.Id, cancellationToken).ConfigureAwait(false) is { Disabled: false }; + } + + /// Stored form of a token: lower-case hex SHA-256 of its text. + public static string HashOf(string secret) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(secret))); +} diff --git a/src/Titano.Server/Storage/ArchivePaths.cs b/src/Titano.Server/Storage/ArchivePaths.cs new file mode 100644 index 00000000..bb8e16f2 --- /dev/null +++ b/src/Titano.Server/Storage/ArchivePaths.cs @@ -0,0 +1,45 @@ +namespace Titano.Server.Storage; + +/// +/// Archive-relative paths from the database → absolute paths, refusing anything that would leave the archive root +/// (BRIEF §14.2: path traversal impossible). Clients never send paths; only ids that the database maps to these. +/// +public static class ArchivePaths +{ + private static readonly StringComparison PathComparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + + /// The absolute path of under , or null when it escapes the root. + public static string? Resolve(string root, string? relative) + { + if (string.IsNullOrWhiteSpace(relative) || relative.Contains('\0', StringComparison.Ordinal)) return null; + var portable = relative.Replace('\\', '/'); + if (Path.IsPathRooted(portable) || portable.StartsWith('/')) return null; + string full; + try + { + full = Path.GetFullPath(Path.Combine(Root(root), portable)); + } + catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException) + { + return null; + } + return IsInside(root, full) ? full : null; + } + + /// True when lies strictly inside . + public static bool IsInside(string root, string path) + { + var full = Path.GetFullPath(path); + return full.StartsWith(Root(root) + Path.DirectorySeparatorChar, PathComparison); + } + + /// A file of the archive that may be served: inside the root, existing, and not a symbolic link. + public static FileInfo? ExistingFile(string root, string? relative) + { + if (Resolve(root, relative) is not { } full) return null; + var file = new FileInfo(full); + return file.Exists && file.LinkTarget is null ? file : null; + } + + private static string Root(string root) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(root)); +} diff --git a/src/Titano.Server/Storage/FileResponses.cs b/src/Titano.Server/Storage/FileResponses.cs new file mode 100644 index 00000000..c105d428 --- /dev/null +++ b/src/Titano.Server/Storage/FileResponses.cs @@ -0,0 +1,123 @@ +using System.Collections.Frozen; +using Microsoft.Net.Http.Headers; + +namespace Titano.Server.Storage; + +/// Streams archive files (originals, proxies, thumbnails, exports) with Range, If-Range and conditional GET support. +public static class FileResponses +{ + private static readonly FrozenDictionary Types = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + [".jpg"] = "image/jpeg", + [".jpeg"] = "image/jpeg", + [".png"] = "image/png", + [".gif"] = "image/gif", + [".tif"] = "image/tiff", + [".tiff"] = "image/tiff", + [".dng"] = "image/x-adobe-dng", + [".heic"] = "image/heic", + [".heif"] = "image/heif", + [".webp"] = "image/webp", + [".avif"] = "image/avif", + [".mp4"] = "video/mp4", + [".mov"] = "video/quicktime", + [".avi"] = "video/x-msvideo", + }.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase); + + public static string ContentTypeOf(string fileName) => + Types.TryGetValue(Path.GetExtension(fileName), out var type) ? type : "application/octet-stream"; + + /// + /// The byte range [, offset + ) of as a + /// range-capable response; null when the range does not fit the file (the asset changed under the index). + /// + public static IResult? Serve(HttpContext context, FileInfo file, long offset, long length, string contentType, string cacheControl, + string? etag = null, string? downloadName = null) + { + if (offset < 0 || length < 0 || offset > file.Length - length) return null; + context.Response.Headers.CacheControl = cacheControl; + return Results.Stream(new FileSlice(file.FullName, offset, length), contentType, downloadName, file.LastWriteTimeUtc, + etag is null ? null : new EntityTagHeaderValue("\"" + etag + "\""), enableRangeProcessing: true); + } + + /// Read-only, seekable view of part of a file (one thumbnail inside a pack, or the whole file). + private sealed class FileSlice : Stream + { + private readonly FileStream _file; + private readonly long _offset; + private readonly long _length; + private long _position; + + public FileSlice(string path, long offset, long length) + { + _file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 1 << 16, FileOptions.Asynchronous | FileOptions.SequentialScan); + _offset = offset; + _length = length; + } + + public override bool CanRead => true; + public override bool CanSeek => true; + public override bool CanWrite => false; + public override long Length => _length; + + public override long Position + { + get => _position; + set => _position = Math.Clamp(value, 0, _length); + } + + public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count)); + + public override int Read(Span buffer) + { + var count = Available(buffer.Length); + if (count == 0) return 0; + _file.Position = _offset + _position; + var read = _file.Read(buffer[..count]); + _position += read; + return read; + } + + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => + ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); + + public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) + { + var count = Available(buffer.Length); + if (count == 0) return 0; + _file.Position = _offset + _position; + var read = await _file.ReadAsync(buffer[..count], cancellationToken).ConfigureAwait(false); + _position += read; + return read; + } + + public override long Seek(long offset, SeekOrigin origin) => Position = origin switch + { + SeekOrigin.Begin => offset, + SeekOrigin.Current => _position + offset, + _ => _length + offset, + }; + + public override void Flush() + { + } + + public override void SetLength(long value) => throw new NotSupportedException(); + + public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); + + protected override void Dispose(bool disposing) + { + if (disposing) _file.Dispose(); + base.Dispose(disposing); + } + + public override async ValueTask DisposeAsync() + { + await _file.DisposeAsync().ConfigureAwait(false); + await base.DisposeAsync().ConfigureAwait(false); + } + + private int Available(int requested) => (int)Math.Min(requested, _length - _position); + } +} diff --git a/src/Titano.Server/Storage/IFrameAssetLocator.cs b/src/Titano.Server/Storage/IFrameAssetLocator.cs new file mode 100644 index 00000000..3148f286 --- /dev/null +++ b/src/Titano.Server/Storage/IFrameAssetLocator.cs @@ -0,0 +1,26 @@ +using Titano.Core.Model; + +namespace Titano.Server.Storage; + +/// Regenerable images derived from an original (BRIEF §8.5). +public enum FrameAsset +{ + /// JPEG proxy under the sequence's proxies/ folder. + Proxy, + + /// Thumbnail inside the sequence's packed thumbnail file. + Thumbnail, +} + +/// Where a derived image lives: a byte range of a file inside the archive (a whole proxy file or one thumbnail of a pack). +public sealed record AssetLocation(string Path, long Offset, long Length, string ContentType); + +/// +/// Finds proxies and thumbnails in the archive layout, which the archive module owns (BRIEF §7). The server streams +/// what it returns with Range support after checking the path stays inside the archive root; null means not built yet. +/// Registered by the host composition alongside . +/// +public interface IFrameAssetLocator +{ + ValueTask LocateAsync(Archive archive, Sequence sequence, Frame frame, FrameAsset asset, CancellationToken cancellationToken); +} diff --git a/src/Titano.Server/Storage/RenderStore.cs b/src/Titano.Server/Storage/RenderStore.cs new file mode 100644 index 00000000..9748accd --- /dev/null +++ b/src/Titano.Server/Storage/RenderStore.cs @@ -0,0 +1,49 @@ +using Titano.Core.IO; +using Titano.Core.Model; +using Titano.Data; +using Titano.Protocol; + +namespace Titano.Server.Storage; + +/// Files a verified render output under the sequence's exports/ folder (BRIEF §7) and records it. +public sealed class RenderStore(TitanoDatabase db, ServerArchive archive, TimeProvider time) +{ + /// + /// Moves (on the archive volume) to <sequence folder>/exports/, never + /// replacing an existing export, and inserts the render row. Null when the sequence has no folder in the archive yet. + /// + public async Task CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, CancellationToken cancellationToken) + { + if (string.IsNullOrEmpty(sequence.Folder) || ArchivePaths.Resolve(archive.Root, sequence.Folder.TrimEnd('/') + "/exports") is not { } exports) + return null; + Directory.CreateDirectory(exports); + var target = Unique(exports, output.FileName); + File.Move(stagedFile, target); + var render = new Render + { + Id = Guid.CreateVersion7(), + SequenceId = sequence.Id, + PresetName = output.PresetName, + OutputPath = LongPath.ToPortableRelative(archive.Root, target), + Codec = output.Codec, + Width = output.Width, + Height = output.Height, + Fps = output.Fps, + DurationSeconds = output.DurationSeconds, + Bytes = output.Size, + Created = time.GetUtcNow(), + ProducedBy = producedBy, + }; + await db.Renders.InsertAsync(render, cancellationToken); + return render; + } + + private static string Unique(string folder, string fileName) + { + var candidate = Path.Combine(folder, fileName); + var stem = Path.GetFileNameWithoutExtension(fileName); + var extension = Path.GetExtension(fileName); + for (var n = 2; File.Exists(candidate); n++) candidate = Path.Combine(folder, $"{stem} ({n}){extension}"); + return candidate; + } +} diff --git a/src/Titano.Server/Storage/ServerArchive.cs b/src/Titano.Server/Storage/ServerArchive.cs new file mode 100644 index 00000000..d7e1ae8d --- /dev/null +++ b/src/Titano.Server/Storage/ServerArchive.cs @@ -0,0 +1,37 @@ +using Titano.Core.Model; +using Titano.Data; +using Titano.Server.Configuration; + +namespace Titano.Server.Storage; + +/// +/// The archive this server serves () and its database row, created at first +/// start. The admin panel can change its name, templates and proxy policy at runtime. +/// +public sealed class ServerArchive(Archive archive) +{ + private volatile Archive _current = archive; + + public Archive Current + { + get => _current; + set => _current = value; + } + + public Guid Id => _current.Id; + + public string Root => _current.RootPath; + + /// Finds the archive row for the configured root, or creates it with the default templates. + public static async Task OpenAsync(TitanoDatabase db, ServerOptions options, TimeProvider time, CancellationToken cancellationToken) + { + var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(options.ArchivePath)); + var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + var existing = (await db.Archives.ListAsync(cancellationToken).ConfigureAwait(false)) + .FirstOrDefault(a => string.Equals(Path.TrimEndingDirectorySeparator(a.RootPath), root, comparison)); + if (existing is not null) return new ServerArchive(existing); + var created = new Archive { Id = Guid.CreateVersion7(), Name = options.ServerName, RootPath = root, Created = time.GetUtcNow() }; + await db.Archives.UpsertAsync(created, cancellationToken).ConfigureAwait(false); + return new ServerArchive(created); + } +} diff --git a/src/Titano.Server/Storage/UploadMaintenance.cs b/src/Titano.Server/Storage/UploadMaintenance.cs new file mode 100644 index 00000000..3abe04c1 --- /dev/null +++ b/src/Titano.Server/Storage/UploadMaintenance.cs @@ -0,0 +1,102 @@ +using Titano.Data; +using Titano.Data.Repositories; +using Titano.Server.Configuration; + +namespace Titano.Server.Storage; + +/// +/// Upload housekeeping (BRIEF §14.4): at start, uploads a crash left half-committed are settled; then every half hour +/// abandoned uploads expire, orphaned partial and staging files are removed, old upload records and dead tokens purged. +/// +public sealed class UploadMaintenance(TitanoDatabase db, ServerOptions options, UploadStore store, TimeProvider time, ILogger logger) + : BackgroundService +{ + private static readonly TimeSpan Interval = TimeSpan.FromMinutes(30); + private static readonly TimeSpan FinishedRetention = TimeSpan.FromDays(90); + private static readonly TimeSpan StagingRetention = TimeSpan.FromDays(1); + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + try + { + await RecoverAsync(stoppingToken); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + logger.LogError(ex, "Upload recovery failed"); + } + while (!stoppingToken.IsCancellationRequested) + { + try + { + await SweepAsync(stoppingToken); + await Task.Delay(Interval, time, stoppingToken); + } + catch (OperationCanceledException) + { + return; + } + catch (Exception ex) + { + logger.LogWarning(ex, "Upload cleanup failed"); + } + } + } + + /// + /// Uploads left committing by a crash: committed when the frame with their hash exists, otherwise back to receiving + /// (the partial file is intact, so the client simply completes again). + /// + public async Task RecoverAsync(CancellationToken cancellationToken) + { + foreach (var upload in await db.Uploads.ListByStateAsync(UploadState.Committing, cancellationToken)) + { + var frame = upload.JobId is null ? (await db.Frames.FindBySha256Async(upload.Sha256, cancellationToken)).FirstOrDefault() : null; + if (frame is not null) + { + await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Committed, frame.Id, cancellationToken); + store.Delete(upload.Id); + } + else + { + await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Receiving, cancellationToken: cancellationToken); + } + logger.LogInformation("Recovered interrupted upload {Upload} ({File})", upload.Id, upload.FileName); + } + } + + public async Task SweepAsync(CancellationToken cancellationToken) + { + var now = time.GetUtcNow(); + var abandonedBefore = now - TimeSpan.FromHours(options.UploadRetentionHours); + var expired = 0; + foreach (var upload in await db.Uploads.ListStaleAsync(abandonedBefore, cancellationToken)) + { + if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Cancelled, cancellationToken: cancellationToken)) continue; + store.Delete(upload.Id); + expired++; + } + + var active = new HashSet(); + foreach (var state in new[] { UploadState.Receiving, UploadState.Committing }) + active.UnionWith((await db.Uploads.ListByStateAsync(state, cancellationToken)).Select(u => u.Id)); + foreach (var (id, _) in store.List().ToList()) + { + if (!active.Contains(id)) store.Delete(id); + } + if (Directory.Exists(options.StagingPath)) + { + // Only this server's staging files (named by upload id) are touched; clients may share the archive. + foreach (var file in new DirectoryInfo(options.StagingPath).EnumerateFiles()) + { + if (Guid.TryParseExact(Path.GetFileNameWithoutExtension(file.Name), "N", out var id) && !active.Contains(id) + && file.LastWriteTimeUtc < now.UtcDateTime - StagingRetention) + UploadStore.TryDelete(file.FullName); + } + } + + await db.Uploads.PurgeFinishedAsync(now - FinishedRetention, cancellationToken); + await db.Tokens.PurgeAsync(now, cancellationToken); + if (expired > 0) logger.LogInformation("Expired {Count} abandoned upload(s)", expired); + } +} diff --git a/src/Titano.Server/Storage/UploadRules.cs b/src/Titano.Server/Storage/UploadRules.cs new file mode 100644 index 00000000..9dd31eef --- /dev/null +++ b/src/Titano.Server/Storage/UploadRules.cs @@ -0,0 +1,45 @@ +namespace Titano.Server.Storage; + +/// Validation shared by frame and render uploads. +public static class UploadRules +{ + /// A plain file name (no folders, no characters Windows or Linux refuse), or null. + public static string? SafeFileName(string? name) + { + if (string.IsNullOrWhiteSpace(name)) return null; + var trimmed = name.Trim(); + if (trimmed.Length > 255 || trimmed is "." or ".." || trimmed.EndsWith('.')) return null; + foreach (var c in trimmed) + { + if (char.IsControl(c) || c is '/' or '\\' or '<' or '>' or ':' or '"' or '|' or '?' or '*') return null; + } + return trimmed; + } + + /// 64 hexadecimal characters. + public static bool IsSha256(string? value) => value is { Length: 64 } && value.All(char.IsAsciiHexDigit); + + /// Indices of the chunks set in a received-chunk bitmap. + public static List ReceivedChunks(Titano.Data.Repositories.Upload upload) + { + var received = new List(upload.ReceivedCount); + for (var i = 0; i < upload.ChunkCount; i++) + { + if (upload.HasChunk(i)) received.Add(i); + } + return received; + } + + /// Free bytes on the volume holding , or null when the platform cannot tell. + public static long? FreeBytes(string path) + { + try + { + return new DriveInfo(Path.GetFullPath(path)).AvailableFreeSpace; + } + catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException) + { + return null; + } + } +} diff --git a/src/Titano.Server/Storage/UploadService.cs b/src/Titano.Server/Storage/UploadService.cs new file mode 100644 index 00000000..cef9f2ff --- /dev/null +++ b/src/Titano.Server/Storage/UploadService.cs @@ -0,0 +1,242 @@ +using System.Collections.Concurrent; +using System.Globalization; +using Titano.Data; +using Titano.Data.Archiving; +using Titano.Data.Repositories; +using Titano.Protocol; +using Titano.Server.Api; +using Titano.Server.Configuration; +using Titano.Server.Events; +using Titano.Server.Jobs; +using Titano.Server.Security; +using UploadState = Titano.Data.Repositories.UploadState; +using UploadStateDto = Titano.Protocol.UploadState; + +namespace Titano.Server.Storage; + +/// +/// Chunked, resumable, hash-verified frame uploads (BRIEF §14.4): start (short-circuit on a known SHA-256, resume an +/// unfinished upload, quota and disk checks), idempotent chunks, and completion that stages the file on the archive +/// volume, verifies its hash and hands it to . +/// +public sealed class UploadService( + TitanoDatabase db, + ServerOptions options, + ServerArchive archive, + UploadStore store, + IArchiveIngest ingest, + SequenceJobs sequenceJobs, + EventHub events, + IHostApplicationLifetime lifetime, + TimeProvider time, + ILogger logger) +{ + /// Space kept free on the upload volume beyond the announced size. + private const long DiskReserve = 64L * 1024 * 1024; + + private readonly ConcurrentDictionary _chunksInFlight = new(); + + public async Task> StartAsync(Caller caller, StartUploadRequest request, CancellationToken cancellationToken) + { + if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Fail(400, "invalid_file_name"); + if (!UploadRules.IsSha256(request.Sha256)) return Fail(400, "invalid_hash"); + if (request.Size <= 0) return Fail(400, "bad_request", "size"); + if (request.Size > options.MaxUploadBytes) return Fail(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture)); + if (request.SessionKey is { Length: > 200 }) return Fail(400, "bad_request", "sessionKey"); + if (request.SequenceId is { } target && await db.Sequences.GetAsync(target, cancellationToken) is null) + return Fail(404, "not_found", "sequence"); + + var sha = request.Sha256.ToLowerInvariant(); + var known = await db.Frames.FindBySha256Async(sha, cancellationToken); + if (known.Count > 0) + { + var frame = known.FirstOrDefault(f => f.SequenceId == request.SequenceId) ?? known[0]; + return new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, frame.Id); + } + + var resumable = await db.Uploads.FindReceivingAsync(caller.Id, sha, cancellationToken); + if (resumable is not null && resumable.Size == request.Size && resumable.SequenceId == request.SequenceId && store.Exists(resumable.Id)) + return Started(resumable); + + if (caller.User.QuotaBytes > 0) + { + var pending = await db.Uploads.PendingBytesAsync(caller.Id, cancellationToken); + if (caller.User.UsedBytes + pending + request.Size > caller.User.QuotaBytes) + return Fail(403, "quota_exceeded", caller.User.QuotaBytes.ToString(CultureInfo.InvariantCulture)); + } + if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size + DiskReserve) + return Fail(507, "insufficient_storage"); + + var now = time.GetUtcNow(); + var upload = await db.Uploads.CreateAsync(new Upload + { + Id = Guid.CreateVersion7(), + UserId = caller.Id, + SequenceId = request.SequenceId, + FileName = fileName, + Size = request.Size, + Sha256 = sha, + CapturedAt = request.CapturedAt, + ChunkSize = options.ChunkSizeBytes, + State = UploadState.Receiving, + Created = now, + UpdatedAt = now, + SessionKey = string.IsNullOrWhiteSpace(request.SessionKey) ? null : request.SessionKey.Trim(), + }, cancellationToken); + store.Create(upload.Id, upload.Size); + return Started(upload); + } + + /// Stores chunk of an upload the caller owns; repeating a received chunk changes nothing. + public async Task> ReceiveChunkAsync(Caller caller, Guid id, int index, HttpRequest request, CancellationToken cancellationToken) + { + var upload = await db.Uploads.GetAsync(id, cancellationToken); + if (upload is null || upload.UserId != caller.Id) return Fail(404, "not_found", "upload"); + if (upload.State != UploadState.Receiving) return Fail(409, "upload_state"); + if (index < 0 || index >= upload.ChunkCount) return Fail(400, "chunk_out_of_range"); + var expected = Math.Min(upload.ChunkSize, upload.Size - (long)index * upload.ChunkSize); + if (request.ContentLength is { } announced && announced != expected) return Fail(400, "chunk_size", expected.ToString(CultureInfo.InvariantCulture)); + if (upload.HasChunk(index)) return new ChunkReceived(index, UploadRules.ReceivedChunks(upload)); + + if (_chunksInFlight.AddOrUpdate(caller.Id, 1, (_, n) => n + 1) > options.ConcurrentChunksPerUser) + { + Release(caller.Id); + return Fail(429, "too_many_uploads"); + } + try + { + if (!await store.WriteChunkAsync(id, (long)index * upload.ChunkSize, request.Body, expected, cancellationToken)) + return Fail(400, "chunk_size", expected.ToString(CultureInfo.InvariantCulture)); + } + finally + { + Release(caller.Id); + } + var updated = await db.Uploads.MarkChunkReceivedAsync(id, index, cancellationToken); + return updated is null ? Fail(404, "not_found", "upload") : new ChunkReceived(index, UploadRules.ReceivedChunks(updated)); + } + + public async Task> GetStateAsync(Caller caller, Guid id, CancellationToken cancellationToken) + { + var upload = await db.Uploads.GetAsync(id, cancellationToken); + if (upload is null || upload.UserId != caller.Id) return Fail(404, "not_found", "upload"); + return new UploadStateDto(upload.Id, upload.FileName, upload.Size, upload.Sha256, upload.ChunkSize, UploadRules.ReceivedChunks(upload), + upload.State.ToString().ToLowerInvariant()); + } + + /// Abandons an upload that is still receiving and deletes its partial file. + public async Task> CancelAsync(Caller caller, Guid id, CancellationToken cancellationToken) + { + var upload = await db.Uploads.GetAsync(id, cancellationToken); + if (upload is null || upload.UserId != caller.Id) return Fail(404, "not_found", "upload"); + if (!await db.Uploads.TryTransitionAsync(id, UploadState.Receiving, UploadState.Cancelled, cancellationToken: cancellationToken)) + return Fail(409, "upload_state"); + store.Delete(id); + return true; + } + + /// + /// Verifies and commits a frame upload. Completing an already committed upload answers its frame again, so a client + /// may retry a completion whose response it lost. Runs to the end even if the client disconnects. + /// + public async Task> CompleteAsync(Caller caller, Guid id) + { + var stopping = lifetime.ApplicationStopping; + var upload = await db.Uploads.GetAsync(id, stopping); + if (upload is null || upload.UserId != caller.Id) return Fail(404, "not_found", "upload"); + if (upload.JobId is not null) return Fail(409, "wrong_upload_kind"); + if (upload is { State: UploadState.Committed, FrameId: { } committed } && await db.Frames.GetAsync(committed, stopping) is { } done) + return new CompleteUploadResponse(done.Id, done.SequenceId, done.Index); + if (upload.State != UploadState.Receiving) return Fail(409, "upload_state"); + if (!upload.IsComplete) + return Fail(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture)); + if (!await db.Uploads.TryTransitionAsync(id, UploadState.Receiving, UploadState.Committing, cancellationToken: stopping)) + return Fail(409, "upload_state"); + + var user = await db.Users.GetAsync(caller.Id, stopping); + if (user is { QuotaBytes: > 0 } && user.UsedBytes + upload.Size > user.QuotaBytes) + { + await FailAsync(upload); + return Fail(403, "quota_exceeded", user.QuotaBytes.ToString(CultureInfo.InvariantCulture)); + } + + var staged = await StageVerifiedAsync(upload, stopping); + if (staged is null) return Fail(422, "hash_mismatch"); + + IngestResult result; + try + { + result = await ingest.CommitAsync(new IngestRequest( + archive.Id, + upload.SequenceId, + NewSequenceTitle: null, + upload.SequenceId is null ? upload.SessionKey ?? "user-" + caller.Id.ToString("N") : null, + staged, + upload.FileName, + upload.Size, + upload.Sha256, + upload.CapturedAt, + caller.Id), stopping); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + // The part file is intact: the client may complete again. + logger.LogError(ex, "Archive ingest of upload {Upload} ({File}) failed", id, upload.FileName); + UploadStore.TryDelete(staged); + await db.Uploads.TryTransitionAsync(id, UploadState.Committing, UploadState.Receiving, cancellationToken: CancellationToken.None); + return Fail(503, "commit_failed"); + } + + await db.Uploads.TryTransitionAsync(id, UploadState.Committing, UploadState.Committed, result.FrameId, CancellationToken.None); + store.Delete(id); + if (File.Exists(staged)) UploadStore.TryDelete(staged); + if (!result.Duplicate) await db.Users.AddUsedBytesAsync(caller.Id, upload.Size, CancellationToken.None); + if (await db.Sequences.GetAsync(result.SequenceId, CancellationToken.None) is { } sequence) + { + await sequenceJobs.FramesAddedAsync(sequence, CancellationToken.None); + events.Publish(new SequenceChangedEvent(sequence.Id, "frames", sequence.FramesVersion, sequence.SettingsVersion)); + } + return new CompleteUploadResponse(result.FrameId, result.SequenceId, result.Index); + } + + /// + /// Copies a fully received upload to the archive staging folder and checks its SHA-256. On a mismatch the upload + /// fails, its files are deleted and null is returned; the client starts over. + /// + public async Task StageVerifiedAsync(Upload upload, CancellationToken cancellationToken) + { + var staged = Path.Combine(options.StagingPath, upload.Id.ToString("N") + Path.GetExtension(upload.FileName).ToLowerInvariant()); + string hash; + try + { + hash = await store.StageAsync(upload.Id, staged, cancellationToken); + } + catch + { + UploadStore.TryDelete(staged); + await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Receiving, cancellationToken: CancellationToken.None); + throw; + } + if (hash == upload.Sha256) return staged; + logger.LogWarning("Upload {Upload} ({File}) arrived damaged: SHA-256 {Actual} instead of {Expected}", upload.Id, upload.FileName, hash, upload.Sha256); + UploadStore.TryDelete(staged); + await FailAsync(upload); + return null; + } + + private async Task FailAsync(Upload upload) + { + await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Failed, cancellationToken: CancellationToken.None); + store.Delete(upload.Id); + } + + private StartUploadResponse Started(Upload upload) => + new(upload.Id, upload.ChunkSize, UploadRules.ReceivedChunks(upload), AlreadyExists: false, FrameId: null); + + private void Release(Guid userId) + { + if (_chunksInFlight.AddOrUpdate(userId, 0, (_, n) => n - 1) <= 0) _chunksInFlight.TryRemove(new KeyValuePair(userId, 0)); + } + + private static Outcome Fail(int status, string code, string? details = null) => Outcome.Fail(status, code, details); +} diff --git a/src/Titano.Server/Storage/UploadStore.cs b/src/Titano.Server/Storage/UploadStore.cs new file mode 100644 index 00000000..7e5c447b --- /dev/null +++ b/src/Titano.Server/Storage/UploadStore.cs @@ -0,0 +1,109 @@ +using System.Buffers; +using System.Security.Cryptography; +using Titano.Server.Configuration; + +namespace Titano.Server.Storage; + +/// +/// Files behind resumable uploads (BRIEF §14.4): /data/uploads/{id}.part receives chunks at their offsets in any +/// order; at completion it is copied, hashing on the way, to a staging file on the archive volume. +/// +public sealed class UploadStore(ServerOptions options) +{ + private const string Extension = ".part"; + + public string PartPath(Guid id) => Path.Combine(options.UploadsPath, id.ToString("N") + Extension); + + public bool Exists(Guid id) => File.Exists(PartPath(id)); + + /// Creates the part file at its final size, so chunks can land at their offsets. + public void Create(Guid id, long size) + { + Directory.CreateDirectory(options.UploadsPath); + using var file = new FileStream(PartPath(id), FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite); + file.SetLength(size); + } + + /// + /// Writes exactly bytes of at and flushes + /// them to disk; false when the body is shorter or longer (nothing is marked received then). + /// + public async Task WriteChunkAsync(Guid id, long offset, Stream body, long length, CancellationToken cancellationToken) + { + var buffer = ArrayPool.Shared.Rent(1 << 16); + try + { + await using var file = new FileStream(PartPath(id), FileMode.Open, FileAccess.Write, FileShare.ReadWrite, 1 << 16, FileOptions.Asynchronous); + file.Position = offset; + long total = 0; + int read; + while ((read = await body.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0) + { + total += read; + if (total > length) return false; + await file.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false); + } + if (total != length) return false; + await file.FlushAsync(cancellationToken).ConfigureAwait(false); + file.Flush(flushToDisk: true); + return true; + } + finally + { + ArrayPool.Shared.Return(buffer); + } + } + + /// Copies the assembled upload to (synced to disk) and returns its lower-case hex SHA-256. + public async Task StageAsync(Guid id, string stagingPath, CancellationToken cancellationToken) + { + Directory.CreateDirectory(Path.GetDirectoryName(stagingPath)!); + using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); + var buffer = ArrayPool.Shared.Rent(1 << 20); + try + { + await using var source = new FileStream(PartPath(id), FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 16, FileOptions.Asynchronous | FileOptions.SequentialScan); + await using var target = new FileStream(stagingPath, FileMode.Create, FileAccess.Write, FileShare.None, 1 << 16, FileOptions.Asynchronous); + int read; + while ((read = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0) + { + hash.AppendData(buffer, 0, read); + await target.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false); + } + await target.FlushAsync(cancellationToken).ConfigureAwait(false); + target.Flush(flushToDisk: true); + } + finally + { + ArrayPool.Shared.Return(buffer); + } + return Convert.ToHexStringLower(hash.GetHashAndReset()); + } + + public void Delete(Guid id) => TryDelete(PartPath(id)); + + /// Every part file with its last write time (cleanup of uploads the database no longer knows). + public IEnumerable<(Guid Id, DateTime LastWriteUtc)> List() + { + if (!Directory.Exists(options.UploadsPath)) yield break; + foreach (var path in Directory.EnumerateFiles(options.UploadsPath, "*" + Extension)) + { + if (Guid.TryParseExact(Path.GetFileNameWithoutExtension(path), "N", out var id)) yield return (id, File.GetLastWriteTimeUtc(path)); + } + } + + public static void TryDelete(string path) + { + try + { + File.Delete(path); + } + catch (IOException) + { + // Retried by the next cleanup pass. + } + catch (UnauthorizedAccessException) + { + } + } +} diff --git a/tests/Titano.Server.Tests/Api/AdminTests.cs b/tests/Titano.Server.Tests/Api/AdminTests.cs new file mode 100644 index 00000000..bdae948e --- /dev/null +++ b/tests/Titano.Server.Tests/Api/AdminTests.cs @@ -0,0 +1,90 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json.Nodes; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.Server.Tests.Api; + +[Category("server")] +public sealed class AdminTests(TestContext context) +{ + [Test] + public async Task TheDashboardSummarisesTheServer() + { + await using var server = await TestServer.StartAsync(context, o => o.ServerName = "Studio NAS"); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + await Uploads.UploadAsync(editor, Uploads.Photo(4000, 61)); + var admin = await server.AdminAsync(); + using var response = await admin.GetAsync("/api/v1/server/dashboard"); + await response.ExpectAsync(HttpStatusCode.OK); + var dashboard = await response.ReadAsync(ProtocolJson.Default.DashboardDto); + Assert.Equal(("Studio NAS", 2, 1L, 1L), (dashboard.ServerName, dashboard.Users, dashboard.Archive.Sequences, dashboard.Archive.Photos)); + Assert.True(dashboard.Database.StartsWith("SQLite ", StringComparison.Ordinal), dashboard.Database); + Assert.Equal(1L, dashboard.Uploads.Single().Frames); + Assert.Contains(dashboard.Activity, a => a.Action == "user_created"); + Assert.True(dashboard.Archive.CapacityBytes > 0); + } + + [Test] + public async Task SettingsAreSavedAndRestartOnlyValuesFlagged() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using var read = await admin.GetAsync("/api/v1/server/settings"); + var settings = await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto); + Assert.False(settings.RestartRequired); + + var edited = settings with { ServerName = "Rooftop", Discovery = true, HttpPort = 18080, HttpsPort = 18443 }; + using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto)) + Assert.True((await saved.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).RestartRequired); + // Name and discovery apply at once; ports wait for the restart. + Assert.Equal(("Rooftop", true, 0), (server.Options.ServerName, server.Options.Discovery, server.Options.HttpPort)); + using (var again = await admin.GetAsync("/api/v1/server/settings")) + Assert.Equal(18080, (await again.ReadAsync(ProtocolJson.Default.ServerSettingsDto)).HttpPort); + var file = JsonNode.Parse(await File.ReadAllTextAsync(server.Options.SettingsFile))!.AsObject(); + Assert.Equal(("Rooftop", 18080), (file["ServerName"]!.GetValue(), file["HttpPort"]!.GetValue())); + Assert.False(file.ContainsKey("AdminPassword")); + + using var invalid = await admin.PutAsJsonAsync("/api/v1/server/settings", edited with { Language = "tlh" }, ProtocolJson.Default.ServerSettingsDto); + Assert.Equal("Language", (await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details); + } + + [Test] + public async Task LogsAreReadableButOnlyByName() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using var tail = await admin.GetAsync("/api/v1/logs?lines=20"); + var lines = await tail.ReadAsync(ProtocolJson.Default.LogTailDto); + Assert.NotEmpty(lines.Lines); + Assert.True(lines.Lines.Count <= 20); + using var files = await admin.GetAsync("/api/v1/logs/files"); + var name = (await files.ReadAsync(ProtocolJson.Default.ListLogFileDto)).First().Name; + using (var download = await admin.GetAsync($"/api/v1/logs/files/{name}")) await download.ExpectAsync(HttpStatusCode.OK); + using var traversal = await admin.GetAsync("/api/v1/logs/files/..%2Fserver.json"); + await traversal.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + } + + [Test] + public async Task ArchiveSettingsValidateTemplatesAndJobsNeedHandlers() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using var read = await admin.GetAsync("/api/v1/archive/settings"); + var archive = await read.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto); + Assert.Equal(Path.GetFullPath(server.Options.ArchivePath), archive.RootPath); + + using (var bad = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { FolderTemplate = "{year/{date" }, ProtocolJson.Default.ArchiveSettingsDto)) + Assert.Equal("folderTemplate", (await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_template")).Details); + using (var good = await admin.PutAsJsonAsync("/api/v1/archive/settings", archive with { Name = "Studio", ProxyLongEdge = 2560 }, ProtocolJson.Default.ArchiveSettingsDto)) + { + var saved = await good.ReadAsync(ProtocolJson.Default.ArchiveSettingsDto); + Assert.Equal(("Studio", 2560), (saved.Name, saved.ProxyLongEdge)); + } + using var scan = await admin.PostAsync("/api/v1/archive/scan", null); + await scan.ExpectErrorAsync(HttpStatusCode.Conflict, "job_unavailable"); + using var stats = await admin.GetAsync("/api/v1/archive/stats"); + await stats.ExpectAsync(HttpStatusCode.OK); + } +} diff --git a/tests/Titano.Server.Tests/Api/SequenceTests.cs b/tests/Titano.Server.Tests/Api/SequenceTests.cs new file mode 100644 index 00000000..abcddbac --- /dev/null +++ b/tests/Titano.Server.Tests/Api/SequenceTests.cs @@ -0,0 +1,173 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.Server.Tests.Api; + +[Category("server")] +public sealed class SequenceTests(TestContext context) +{ + [Test] + public async Task ConcurrentSettingsEditsConflict() + { + await using var server = await TestServer.StartAsync(context); + var (alberto, _, _) = await server.UserAsync("alberto", "editor"); + var (giulia, _, _) = await server.UserAsync("giulia", "editor"); + using var created = await alberto.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest("Harbour sunset", null), ProtocolJson.Default.CreateSequenceRequest); + await created.ExpectAsync(HttpStatusCode.Created); + var sequence = await created.ReadAsync(ProtocolJson.Default.SequenceDto); + Assert.Equal(0, sequence.SettingsVersion); + + using (var first = await alberto.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}", + new UpdateSequenceRequest(null, ["sunset", "harbour"], 4, null, """{"deflicker":{"strength":0.8}}""", 0), ProtocolJson.Default.UpdateSequenceRequest)) + { + await first.ExpectAsync(HttpStatusCode.OK); + var updated = await first.ReadAsync(ProtocolJson.Default.SequenceDto); + Assert.Equal(1, updated.SettingsVersion); + Assert.SequenceEqual(["harbour", "sunset"], updated.Tags); + Assert.Equal(4, updated.Rating); + } + + using (var stale = await giulia.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}", + new UpdateSequenceRequest(null, null, null, null, """{"deflicker":{"strength":0.2}}""", 0), ProtocolJson.Default.UpdateSequenceRequest)) + { + var error = await stale.ExpectErrorAsync(HttpStatusCode.Conflict, "settings_conflict"); + Assert.Equal("1", error.Details); + } + + // The ETag of a fresh read works as If-Match. + using var read = await giulia.GetAsync($"/api/v1/sequences/{sequence.Id}"); + var etag = Assert.NotNull(read.Headers.ETag); + using var request = new HttpRequestMessage(HttpMethod.Patch, $"/api/v1/sequences/{sequence.Id}") + { + Content = JsonContent.Create(new UpdateSequenceRequest(null, null, null, null, """{"deflicker":{"strength":0.2}}""", null), ProtocolJson.Default.UpdateSequenceRequest), + }; + request.Headers.IfMatch.Add(etag); + using (var retried = await giulia.SendAsync(request)) + { + await retried.ExpectAsync(HttpStatusCode.OK); + Assert.Equal(2, (await retried.ReadAsync(ProtocolJson.Default.SequenceDto)).SettingsVersion); + } + + using var notModified = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/sequences/{sequence.Id}"); + notModified.Headers.IfNoneMatch.Add(etag); + using (var changed = await giulia.SendAsync(notModified)) await changed.ExpectAsync(HttpStatusCode.OK); + + using var invalid = await giulia.PatchAsJsonAsync($"/api/v1/sequences/{sequence.Id}", + new UpdateSequenceRequest(null, null, null, null, "{ broken", null), ProtocolJson.Default.UpdateSequenceRequest); + await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_settings"); + } + + [Test] + public async Task TheManifestCarriesHashesAndAnEtag() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var a = Uploads.Photo(2000, 31); + var b = Uploads.Photo(2000, 32); + var first = await Uploads.UploadAsync(editor, a, "a.CR3", sessionKey: "k"); + await Uploads.UploadAsync(editor, b, "b.CR3", sessionKey: "k"); + + using var response = await editor.GetAsync($"/api/v1/sequences/{first.SequenceId}/manifest"); + await response.ExpectAsync(HttpStatusCode.OK); + var manifest = await response.ReadAsync(ProtocolJson.Default.SequenceManifest); + Assert.SequenceEqual([Uploads.Sha(a), Uploads.Sha(b)], manifest.Frames.Select(f => f.Sha256)); + Assert.Equal(2, manifest.FramesVersion); + + using var cached = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/sequences/{first.SequenceId}/manifest"); + cached.Headers.IfNoneMatch.Add(response.Headers.ETag!); + using (var unchanged = await editor.SendAsync(cached)) await unchanged.ExpectAsync(HttpStatusCode.NotModified); + + await Uploads.UploadAsync(editor, Uploads.Photo(2000, 33), "c.CR3", sessionKey: "k"); + using var again = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/sequences/{first.SequenceId}/manifest"); + again.Headers.IfNoneMatch.Add(response.Headers.ETag!); + using var changed = await editor.SendAsync(again); + await changed.ExpectAsync(HttpStatusCode.OK); + Assert.Count(3, (await changed.ReadAsync(ProtocolJson.Default.SequenceManifest)).Frames); + } + + [Test] + public async Task ListingFiltersAndPagesWithACursor() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + for (var i = 0; i < 5; i++) + { + using var created = await editor.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest($"Shoot {i}", null), ProtocolJson.Default.CreateSequenceRequest); + await created.ExpectAsync(HttpStatusCode.Created); + } + var seen = new List(); + string? cursor = null; + long? total = null; + do + { + using var page = await editor.GetAsync($"/api/v1/sequences?sort=title&limit=2{(cursor is null ? string.Empty : "&cursor=" + Uri.EscapeDataString(cursor))}"); + await page.ExpectAsync(HttpStatusCode.OK); + var items = await page.ReadAsync(ProtocolJson.Default.PageSequenceDto); + total ??= items.Total; + seen.AddRange(items.Items.Select(s => s.DisplayTitle)); + cursor = items.NextCursor; + } + while (cursor is not null); + Assert.Equal(5L, total); + Assert.SequenceEqual(["Shoot 0", "Shoot 1", "Shoot 2", "Shoot 3", "Shoot 4"], seen); + + using (var search = await editor.GetAsync("/api/v1/sequences?q=shoot%203")) Assert.Count(1, (await search.ReadAsync(ProtocolJson.Default.PageSequenceDto)).Items); + using (var badCursor = await editor.GetAsync("/api/v1/sequences?cursor=bm9wZQ")) await badCursor.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_cursor"); + using var badStatus = await editor.GetAsync("/api/v1/sequences?status=shiny"); + await badStatus.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_filter"); + } + + [Test] + public async Task DeletingPlansFirstAndMovesTheFolderToTrash() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var (other, _, _) = await server.UserAsync("giulia", "editor"); + var frame = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 34)); + var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!; + var folder = Path.Combine(server.Options.ArchivePath, sequence.Folder); + + using (var notOwner = await other.DeleteAsync($"/api/v1/sequences/{sequence.Id}")) await notOwner.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var plan = await editor.DeleteAsync($"/api/v1/sequences/{sequence.Id}?dryRun=true")) + { + var dryRun = await plan.ReadAsync(ProtocolJson.Default.SequenceDeletePlan); + Assert.True(dryRun.DryRun); + Assert.Equal(1, dryRun.FrameCount); + Assert.Equal($".titano/trash/{sequence.Id:N}", dryRun.TrashFolder); + } + Assert.True(Directory.Exists(folder), "a dry run changes nothing"); + Assert.NotNull(await server.Database.Sequences.GetAsync(sequence.Id)); + + using (var deleted = await editor.DeleteAsync($"/api/v1/sequences/{sequence.Id}")) await deleted.ExpectAsync(HttpStatusCode.OK); + Assert.False(Directory.Exists(folder)); + Assert.True(Directory.Exists(Path.Combine(server.Options.TrashPath, sequence.Id.ToString("N"))), "the files are kept in the trash"); + Assert.Null(await server.Database.Sequences.GetAsync(sequence.Id)); + using var gone = await editor.GetAsync($"/api/v1/sequences/{sequence.Id}"); + await gone.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + } + + [Test] + public async Task FramesAndStatsArePaged() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var first = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 40), "f0.jpg", sessionKey: "p"); + for (var i = 1; i < 4; i++) await Uploads.UploadAsync(editor, Uploads.Photo(1000 + i, 40 + i), $"f{i}.jpg", sessionKey: "p"); + var id = first.SequenceId; + using var page1 = await editor.GetAsync($"/api/v1/sequences/{id}/frames?limit=2"); + var frames = await page1.ReadAsync(ProtocolJson.Default.PageFrameDto); + Assert.SequenceEqual([0, 1], frames.Items.Select(f => f.Index)); + using var page2 = await editor.GetAsync($"/api/v1/sequences/{id}/frames?limit=2&cursor={Uri.EscapeDataString(frames.NextCursor!)}"); + Assert.SequenceEqual([2, 3], (await page2.ReadAsync(ProtocolJson.Default.PageFrameDto)).Items.Select(f => f.Index)); + + using var stats = await editor.GetAsync($"/api/v1/sequences/{id}/stats"); + var dto = await stats.ReadAsync(ProtocolJson.Default.SequenceStatsDto); + Assert.Equal(4, dto.FrameCount); + Assert.Equal(4L, dto.ByFormat.Sum(f => f.Count)); + using var analysis = await editor.GetAsync($"/api/v1/sequences/{id}/analysis"); + Assert.Equal("analysis", (await analysis.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details); + } +} diff --git a/tests/Titano.Server.Tests/Api/UserTests.cs b/tests/Titano.Server.Tests/Api/UserTests.cs new file mode 100644 index 00000000..d64d7d2f --- /dev/null +++ b/tests/Titano.Server.Tests/Api/UserTests.cs @@ -0,0 +1,78 @@ +using System.Net; +using System.Net.Http.Json; +using Titano.Core.Model; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.Server.Tests.Api; + +[Category("server")] +public sealed class UserTests(TestContext context) +{ + [Test] + public async Task AdminsManageAccountsAndTemporaryPasswords() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using var created = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("lucia", "Lucia", "editor", 2_000_000_000_000, null, AllowRenderNode: false), + ProtocolJson.Default.CreateUserRequest); + await created.ExpectAsync(HttpStatusCode.Created); + var account = await created.ReadAsync(ProtocolJson.Default.TemporaryPasswordResponse); + Assert.True(account.User.MustChangePassword); + Assert.Equal(14, account.TemporaryPassword.Length); + Assert.Equal(0L, account.User.Permissions & (long)Permissions.RenderNode); + + using (var taken = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("LUCIA", null, "viewer", 0, null, false), ProtocolJson.Default.CreateUserRequest)) + await taken.ExpectErrorAsync(HttpStatusCode.Conflict, "username_taken"); + using (var badName = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("lu cia", null, "viewer", 0, null, false), ProtocolJson.Default.CreateUserRequest)) + await badName.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_username"); + using (var badRole = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest("lucio", null, "owner", 0, null, false), ProtocolJson.Default.CreateUserRequest)) + await badRole.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_role"); + + using (var promoted = await admin.PatchAsJsonAsync($"/api/v1/users/{account.User.Id}", new UpdateUserRequest(null, "uploader", 5, null, true), ProtocolJson.Default.UpdateUserRequest)) + { + var user = await promoted.ReadAsync(ProtocolJson.Default.UserDto); + Assert.Equal(("uploader", 5L), (user.Role, user.QuotaBytes)); + Assert.Equal((long)(RolePermissions.Default(UserRole.Uploader) | Permissions.RenderNode), user.Permissions); + } + + using var reset = await admin.PostAsJsonAsync($"/api/v1/users/{account.User.Id}/reset-password", new ResetPasswordRequest(null), ProtocolJson.Default.ResetPasswordRequest); + var temporary = await reset.ReadAsync(ProtocolJson.Default.TemporaryPasswordResponse); + var login = await server.LoginAsync("lucia", temporary.TemporaryPassword); + Assert.True(login.MustChangePassword); + + using var list = await admin.GetAsync("/api/v1/users"); + var rows = await list.ReadAsync(ProtocolJson.Default.ListUserSummaryDto); + var lucia = rows.Single(r => r.User.Username == "lucia"); + Assert.Equal("tests", lucia.LastClient); + Assert.NotNull(lucia.LastSeen); + } + + [Test] + public async Task TheLastAdministratorStays() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using var me = await admin.GetAsync("/api/v1/auth/me"); + var self = await me.ReadAsync(ProtocolJson.Default.UserDto); + using (var demote = await admin.PatchAsJsonAsync($"/api/v1/users/{self.Id}", new UpdateUserRequest(null, "editor", null, null, null), ProtocolJson.Default.UpdateUserRequest)) + await demote.ExpectErrorAsync(HttpStatusCode.Conflict, "self"); + using (var delete = await admin.DeleteAsync($"/api/v1/users/{self.Id}")) await delete.ExpectErrorAsync(HttpStatusCode.Conflict, "self"); + } + + [Test] + public async Task DeletingAUserHandsTheirSequencesToTheAdmin() + { + await using var server = await TestServer.StartAsync(context); + var (editor, user, _) = await server.UserAsync("simone", "editor"); + using var created = await editor.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest("Comet", null), ProtocolJson.Default.CreateSequenceRequest); + var sequence = await created.ReadAsync(ProtocolJson.Default.SequenceDto); + var admin = await server.AdminAsync(); + using (var deleted = await admin.DeleteAsync($"/api/v1/users/{user.Id}")) await deleted.ExpectAsync(HttpStatusCode.NoContent); + using var me = await admin.GetAsync("/api/v1/auth/me"); + var adminId = (await me.ReadAsync(ProtocolJson.Default.UserDto)).Id; + Assert.Equal(adminId, (await server.Database.Sequences.GetAsync(sequence.Id))!.OwnerUserId); + using var audit = await admin.GetAsync("/api/v1/audit?action=user_deleted"); + Assert.Count(1, (await audit.ReadAsync(ProtocolJson.Default.PageAuditEntryDto)).Items); + } +} diff --git a/tests/Titano.Server.Tests/Events/EventTests.cs b/tests/Titano.Server.Tests/Events/EventTests.cs new file mode 100644 index 00000000..820c0678 --- /dev/null +++ b/tests/Titano.Server.Tests/Events/EventTests.cs @@ -0,0 +1,83 @@ +using System.Net; +using System.Net.WebSockets; +using Microsoft.Extensions.DependencyInjection; +using Titano.Core.Model; +using Titano.Data.Jobs; +using Titano.Protocol; +using Titano.Protocol.Client; +using Titano.Server.Tests.Jobs; +using Titano.Testing; + +namespace Titano.Server.Tests.Events; + +[Category("server")] +public sealed class EventTests(TestContext context) +{ + [Test] + public async Task AWebSocketClientReceivesJobProgressAndStates() + { + var handler = new BlockingJobHandler(JobKind.DeepAnalyse); + await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton(handler)); + var (editor, _, token) = await server.UserAsync("alberto", "editor"); + var sequence = await JobApiTests.CreateSequenceAsync(editor); + + using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "tests") { Token = token }; + using var listening = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation); + listening.CancelAfter(TimeSpan.FromSeconds(20)); + var received = new List(); + var connected = new TaskCompletionSource(); + var reader = Task.Run(async () => + { + var events = client.EventsAsync(listening.Token).GetAsyncEnumerator(listening.Token); + var first = events.MoveNextAsync(); + connected.TrySetResult(); + try + { + if (!await first) return; + do + { + received.Add(events.Current); + if (events.Current is JobStateEvent { State: "done" }) return; + } + while (await events.MoveNextAsync()); + } + finally + { + await events.DisposeAsync(); + } + }); + await connected.Task; + await WaitForSubscriberAsync(server); + + var job = await JobApiTests.CreateAsync(editor, "deep-analyse", sequence); + await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10)); + handler.Release(); + await reader.WaitAsync(TimeSpan.FromSeconds(20)); + + Assert.Contains(received, e => e is JobProgressEvent p && p.JobId == job.Id && p.Total == 4); + Assert.Contains(received, e => e is JobStateEvent { State: "running" } s && s.JobId == job.Id); + Assert.Contains(received, e => e is JobStateEvent { State: "done" } s && s.JobId == job.Id); + } + + [Test] + public async Task TheEventSocketNeedsAValidToken() + { + await using var server = await TestServer.StartAsync(context); + var ws = new UriBuilder(new Uri(server.HttpBase, "/api/v1/events")) { Scheme = "ws" }.Uri; + using (var anonymous = new ClientWebSocket()) + { + var error = await Assert.ThrowsAsync(() => anonymous.ConnectAsync(ws, context.Cancellation)); + Assert.Contains("401", error.Message); + } + var admin = await server.AdminAsync(); + using var plain = await admin.GetAsync("/api/v1/events"); + await plain.ExpectErrorAsync(HttpStatusCode.BadRequest, "websocket_required"); + } + + private static async Task WaitForSubscriberAsync(TestServer server) + { + var hub = server.Services.GetRequiredService(); + for (var i = 0; i < 200 && hub.SubscriberCount == 0; i++) await Task.Delay(25); + Assert.Equal(1, hub.SubscriberCount); + } +} diff --git a/tests/Titano.Server.Tests/Fakes.cs b/tests/Titano.Server.Tests/Fakes.cs new file mode 100644 index 00000000..d4f3d080 --- /dev/null +++ b/tests/Titano.Server.Tests/Fakes.cs @@ -0,0 +1,147 @@ +using System.Collections.Concurrent; +using Titano.Core.Model; +using Titano.Data; +using Titano.Data.Archiving; +using Titano.Data.Jobs; +using Titano.Server.Storage; + +namespace Titano.Server.Tests; + +/// +/// Stand-in for the archive module's ingest: files staged uploads as <folder>/originals/<name>, one +/// sequence per session key, and records every request it received. +/// +public sealed class FakeArchiveIngest(TitanoDatabase db) : IArchiveIngest +{ + private readonly ConcurrentDictionary _sessions = new(StringComparer.Ordinal); + private readonly SemaphoreSlim _gate = new(1, 1); + + public ConcurrentQueue Requests { get; } = new(); + + public async Task CommitAsync(IngestRequest request, CancellationToken cancellationToken = default) + { + Requests.Enqueue(request); + await _gate.WaitAsync(cancellationToken); + try + { + var archive = (await db.Archives.GetAsync(request.ArchiveId, cancellationToken))!; + var sequence = await SequenceForAsync(request, archive, cancellationToken); + var existing = (await db.Frames.FindBySha256Async(request.Sha256, cancellationToken)).FirstOrDefault(f => f.SequenceId == sequence.Id); + if (existing is not null) + { + File.Delete(request.StagedPath); + return new IngestResult(sequence.Id, existing.Id, existing.Index, existing.RelativePath, Duplicate: true); + } + var index = sequence.FrameCount; + var relative = $"{sequence.Folder}/originals/{index:D4}_{request.OriginalFileName}"; + var target = Path.Combine(archive.RootPath, relative); + Directory.CreateDirectory(Path.GetDirectoryName(target)!); + File.Move(request.StagedPath, target); + var frame = new Frame + { + Id = Guid.CreateVersion7(), + SequenceId = sequence.Id, + Index = index, + RelativePath = relative, + FileName = Path.GetFileName(relative), + OriginalFileName = request.OriginalFileName, + Size = request.Size, + Sha256 = request.Sha256, + CapturedAt = request.ClientCapturedAt, + }; + await db.Frames.InsertAsync(frame, cancellationToken); + await db.Sequences.RefreshStatsAsync(sequence.Id, cancellationToken); + return new IngestResult(sequence.Id, frame.Id, index, relative, Duplicate: false); + } + finally + { + _gate.Release(); + } + } + + private async Task SequenceForAsync(IngestRequest request, Archive archive, CancellationToken cancellationToken) + { + Sequence? sequence = null; + if (request.SequenceId is { } id) sequence = await db.Sequences.GetAsync(id, cancellationToken); + else if (request.SessionKey is { } key && _sessions.TryGetValue(key, out var known)) sequence = await db.Sequences.GetAsync(known, cancellationToken); + if (sequence is null) + { + sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = archive.Id, Folder = string.Empty, Title = request.NewSequenceTitle, OwnerUserId = request.UserId }; + if (request.SessionKey is { } key) _sessions[key] = sequence.Id; + } + if (string.IsNullOrEmpty(sequence.Folder)) + { + sequence = sequence with { Folder = $"2026/{sequence.Id:N}", UpdatedAt = DateTimeOffset.UtcNow }; + await db.Sequences.UpsertAsync(sequence, cancellationToken); + } + return sequence; + } +} + +/// Proxies at <folder>/proxies/NNNN.jpg; thumbnails as 64-byte slots of <folder>/.titano/thumbs.pack. +public sealed class FakeAssetLocator : IFrameAssetLocator +{ + public const int ThumbnailSize = 64; + + /// When set, every lookup answers this location (path traversal tests). + public AssetLocation? Override { get; set; } + + public ValueTask LocateAsync(Archive archive, Sequence sequence, Frame frame, FrameAsset asset, CancellationToken cancellationToken) + { + if (Override is not null) return ValueTask.FromResult(Override); + var folder = Path.Combine(archive.RootPath, sequence.Folder); + AssetLocation? location = asset == FrameAsset.Proxy + ? new AssetLocation(Path.Combine(folder, "proxies", $"{frame.Index:D4}.jpg"), 0, -1, "image/jpeg") + : new AssetLocation(Path.Combine(folder, ".titano", "thumbs.pack"), (long)frame.Index * ThumbnailSize, ThumbnailSize, "image/jpeg"); + if (!File.Exists(location.Path)) return ValueTask.FromResult(null); + if (location.Length < 0) location = location with { Length = new FileInfo(location.Path).Length }; + return ValueTask.FromResult(location); + } +} + +/// A clock tests move by hand (token expiry, heartbeat timeouts). +public sealed class ManualTimeProvider : TimeProvider +{ + private long _ticks = DateTimeOffset.UtcNow.UtcTicks; + + public override DateTimeOffset GetUtcNow() => new(Interlocked.Read(ref _ticks), TimeSpan.Zero); + + public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks); +} + +/// +/// A job handler that reports progress and then waits until (or cancellation), so tests can +/// observe running, paused and cancelled states. +/// +public sealed class BlockingJobHandler(JobKind kind) : IJobHandler +{ + private TaskCompletionSource _release = new(TaskCreationOptions.RunContinuationsAsynchronously); + + public JobKind Kind => kind; + + public TaskCompletionSource Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + + /// When set, the next run fails with this message. + public string? FailWith { get; set; } + + public void Release() => _release.TrySetResult(); + + public async Task RunAsync(JobContext context) + { + await context.ReportAsync(1, 4, "started"); + Started.TrySetResult(); + if (FailWith is { } message) + { + FailWith = null; + throw new InvalidOperationException(message); + } + for (var i = 2; i <= 3; i++) + { + await Task.Delay(50, context.Cancellation); + await context.ReportAsync(i, 4); + } + await _release.Task.WaitAsync(context.Cancellation); + _release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + await context.ReportAsync(4, 4); + } +} diff --git a/tests/Titano.Server.Tests/Hosting/DiscoveryTests.cs b/tests/Titano.Server.Tests/Hosting/DiscoveryTests.cs new file mode 100644 index 00000000..f2e5dc3a --- /dev/null +++ b/tests/Titano.Server.Tests/Hosting/DiscoveryTests.cs @@ -0,0 +1,41 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; +using Microsoft.Extensions.DependencyInjection; +using Titano.Protocol; +using Titano.Server.Api; +using Titano.Server.Hosting; +using Titano.Testing; + +namespace Titano.Server.Tests.Hosting; + +[Category("server")] +public sealed class DiscoveryTests(TestContext context) +{ + [Test] + public async Task TheBeaconTravelsOverUdpAndParses() + { + await using var server = await TestServer.StartAsync(context, o => o.ServerName = "Studio NAS"); + var service = server.Services.GetRequiredService(); + using var listener = new UdpClient(new IPEndPoint(IPAddress.Loopback, 0)); + using var sender = new UdpClient(AddressFamily.InterNetwork); + await service.SendOnceAsync(sender, [(IPEndPoint)listener.Client.LocalEndPoint!], context.Cancellation); + + var datagram = await listener.ReceiveAsync(context.Cancellation).AsTask().WaitAsync(TimeSpan.FromSeconds(5)); + var beacon = Assert.NotNull(DiscoveryBeacon.TryParse(datagram.Buffer)); + Assert.Equal("Studio NAS", beacon.Name); + Assert.Equal(SystemEndpoints.Version, beacon.Version); + Assert.Equal(server.HttpBase.Port, beacon.HttpPort); + Assert.Equal(server.HttpsBase!.Port, beacon.HttpsPort); + using var info = await server.Client.GetAsync("/api/v1/server/info"); + Assert.Equal((await info.ReadAsync(ProtocolJson.Default.ServerInfo)).TlsFingerprint, beacon.TlsFingerprint); + } + + [Test] + public void ForeignDatagramsAreIgnored() + { + Assert.Null(DiscoveryBeacon.TryParse(Encoding.UTF8.GetBytes("HELLO {}"))); + Assert.Null(DiscoveryBeacon.TryParse(Encoding.UTF8.GetBytes("TITANO1 {not json"))); + Assert.Null(DiscoveryBeacon.TryParse(new byte[4096])); + } +} diff --git a/tests/Titano.Server.Tests/Hosting/StartupTests.cs b/tests/Titano.Server.Tests/Hosting/StartupTests.cs new file mode 100644 index 00000000..c375aaef --- /dev/null +++ b/tests/Titano.Server.Tests/Hosting/StartupTests.cs @@ -0,0 +1,67 @@ +using Titano.Data; +using Titano.Server.Configuration; +using Titano.Server.Hosting; +using Titano.Testing; + +namespace Titano.Server.Tests.Hosting; + +[Category("server")] +public sealed class StartupTests(TestContext context) +{ + [Test] + public void TheServerRefusesToStartWithoutTheArchiveModule() + { + var root = context.CreateTempDirectory("bare"); + var options = new ServerOptions + { + DataPath = Path.Combine(root, "data"), + ArchivePath = Path.Combine(root, "archive"), + HttpPort = 0, + HttpsPort = 0, + Database = DatabaseKind.Sqlite, + Discovery = false, + }; + var error = Assert.Throws(() => ServerHost.Build(options, [])); + Assert.Contains("IArchiveIngest", error.Message); + Assert.Contains("IFrameAssetLocator", error.Message); + } + + [Test] + public void TitanoDbAcceptsAConnectionString() + { + var root = context.CreateTempDirectory("env"); + var options = ServerOptions.Load(new Dictionary + { + ["TITANO_DATA_PATH"] = root, + ["TITANO_DB"] = "Host=db.lan;Port=6543;Database=archive;Username=titano;Password=secret", + }); + Assert.Equal(DatabaseKind.Postgres, options.Database); + var database = options.ToDatabaseOptions(); + Assert.Equal(Dialect.Postgres, database.Dialect); + Assert.Equal(6543, Titano.Data.Postgres.PostgresSettings.Parse(database.ConnectionString).Port); + } + + [Test] + public void PartsBuildAQuotedPostgresConnectionString() + { + var options = new ServerOptions { DbHost = "postgres", DbName = "titano", DbUser = "titano", DbPassword = "it's;secret", DbSslMode = "disable" }; + var settings = Titano.Data.Postgres.PostgresSettings.Parse(options.ToDatabaseOptions().ConnectionString); + Assert.Equal(("postgres", "titano", "it's;secret"), (settings.Host, settings.Database, settings.Password)); + Assert.Equal(Titano.Data.Postgres.SslMode.Disable, settings.SslMode); + } + + [Test] + public void EnvironmentVariablesLockTheirSettings() + { + var root = context.CreateTempDirectory("locked"); + var options = ServerOptions.Load(new Dictionary + { + ["TITANO_DATA_PATH"] = root, + ["TITANO_DB"] = "postgres", + ["TITANO_HTTP_PORT"] = "9090", + ["TITANO_DISCOVERY"] = "false", + }); + Assert.SequenceEqual(["Database", "HttpPort", "Discovery"], options.Locked); + Assert.Throws(() => ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "many" })); + } +} diff --git a/tests/Titano.Server.Tests/Hosting/WindowsServiceTests.cs b/tests/Titano.Server.Tests/Hosting/WindowsServiceTests.cs new file mode 100644 index 00000000..d09c7c7e --- /dev/null +++ b/tests/Titano.Server.Tests/Hosting/WindowsServiceTests.cs @@ -0,0 +1,55 @@ +using Titano.Server.Hosting; +using Titano.Server.Hosting.Windows; +using Titano.Testing; + +namespace Titano.Server.Tests.Hosting; + +public sealed class WindowsServiceTests +{ + [Test] + public void CommandSwitchesAreParsedAndTheRestPassedOn() + { + Assert.Equal(ServerCommand.Run, ServerCommandLine.Parse([]).Command); + var service = ServerCommandLine.Parse(["--service", "--urls", "x"]); + Assert.Equal(ServerCommand.Service, service.Command); + Assert.SequenceEqual(["--urls", "x"], service.HostArguments); + Assert.Equal(ServerCommand.InstallService, ServerCommandLine.Parse(["--install-service"]).Command); + Assert.Equal(ServerCommand.UninstallService, ServerCommandLine.Parse(["--uninstall-service"]).Command); + Assert.Equal(ServerCommand.HealthCheck, ServerCommandLine.Parse(["--healthcheck"]).Command); + Assert.Equal(ServerCommand.Help, ServerCommandLine.Parse(["/?"]).Command); + Assert.NotNull(ServerCommandLine.Parse(["--install-service", "--uninstall-service"]).Error); + Assert.Null(ServerCommandLine.Parse(["--service", "--service"]).Error); + } + + [Test] + public void StatusReportsFollowTheServiceControlManagerRules() + { + var tracker = new ServiceStatusTracker(); + var start = tracker.StartPending(60_000); + Assert.Equal((ServiceState.StartPending, 0u, 1u, 60_000u), (start.State, start.ControlsAccepted, start.CheckPoint, start.WaitHintMilliseconds)); + Assert.Equal(2u, tracker.StartPending(60_000).CheckPoint); + var running = tracker.Running(); + Assert.Equal((ServiceState.Running, ServiceStatusTracker.AcceptStop | ServiceStatusTracker.AcceptShutdown, 0u), (running.State, running.ControlsAccepted, running.CheckPoint)); + Assert.Throws(() => tracker.Running()); + Assert.Equal(1u, tracker.StopPending(35_000).CheckPoint); + Assert.Equal(2u, tracker.StopPending(35_000).CheckPoint); + var stopped = tracker.Stopped(0); + Assert.Equal((ServiceState.Stopped, 0u, 0u), (stopped.State, stopped.ControlsAccepted, stopped.CheckPoint)); + Assert.Throws(() => tracker.StopPending(1)); + Assert.Equal(1064u, new ServiceStatusTracker().Stopped(1064).ExitCode); + } + + [Test] + [WindowsOnly] + public void TheDispatcherDeclinesAConsoleProcess() + { + if (!OperatingSystem.IsWindows()) return; + var built = false; + Assert.False(WindowsServiceHost.Run(() => + { + built = true; + throw new InvalidOperationException("never built outside the SCM"); + })); + Assert.False(built); + } +} diff --git a/tests/Titano.Server.Tests/Http.cs b/tests/Titano.Server.Tests/Http.cs new file mode 100644 index 00000000..6f5b38f1 --- /dev/null +++ b/tests/Titano.Server.Tests/Http.cs @@ -0,0 +1,41 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json.Serialization.Metadata; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.Server.Tests; + +/// Assertions and JSON helpers for API calls. +internal static class Http +{ + /// The body of a successful response (an error response fails the test with its body). + public static async Task ReadAsync(this HttpResponseMessage response, JsonTypeInfo type) where T : class + { + if (!response.IsSuccessStatusCode) Assert.Fail($"Expected success, got {(int)response.StatusCode}: {await response.Content.ReadAsStringAsync()}"); + return Assert.NotNull(await response.Content.ReadFromJsonAsync(type)); + } + + /// Asserts the status, the { code, message, details } body with , and the API header. + public static async Task ExpectErrorAsync(this HttpResponseMessage response, HttpStatusCode status, string code) + { + var body = await response.Content.ReadAsStringAsync(); + Assert.Equal(status, response.StatusCode, body); + Assert.Equal("1", response.Headers.GetValues(ApiVersion.Header).Single()); + var error = Assert.NotNull(System.Text.Json.JsonSerializer.Deserialize(body, ProtocolJson.Default.ApiError)); + Assert.Equal(code, error.Code, body); + Assert.False(string.IsNullOrWhiteSpace(error.Message), "message is empty"); + return error; + } + + public static async Task BytesEqualAsync(byte[] expected, Task actual) + { + var bytes = await actual; + Assert.BytesEqual(expected, bytes); + } + + public static async Task ExpectAsync(this HttpResponseMessage response, HttpStatusCode status) + { + if (response.StatusCode != status) Assert.Fail($"Expected {(int)status}, got {(int)response.StatusCode}: {await response.Content.ReadAsStringAsync()}"); + } +} diff --git a/tests/Titano.Server.Tests/Jobs/JobApiTests.cs b/tests/Titano.Server.Tests/Jobs/JobApiTests.cs new file mode 100644 index 00000000..2957d42c --- /dev/null +++ b/tests/Titano.Server.Tests/Jobs/JobApiTests.cs @@ -0,0 +1,139 @@ +using System.Net; +using System.Net.Http.Json; +using Microsoft.Extensions.DependencyInjection; +using Titano.Core.Model; +using Titano.Data.Jobs; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.Server.Tests.Jobs; + +[Category("server")] +public sealed class JobApiTests(TestContext context) +{ + [Test] + public async Task JobsMoveThroughPauseResumeAndDone() + { + var handler = new BlockingJobHandler(JobKind.DeepAnalyse); + await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton(handler)); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var sequence = await CreateSequenceAsync(editor); + + var job = await CreateAsync(editor, "deep-analyse", sequence); + Assert.Equal(("queued", "user", "Harbour sunset"), (job.State, job.Priority, job.TargetLabel)); + await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10)); + await WaitForAsync(editor, job.Id, "running"); + + Assert.Equal("paused", (await ActAsync(editor, job.Id, "pause", HttpStatusCode.OK)).State); + await WaitForAsync(editor, job.Id, "paused"); + await ActAsync(editor, job.Id, "pause", HttpStatusCode.Conflict); + await ActAsync(editor, job.Id, "retry", HttpStatusCode.Conflict); + + await ActAsync(editor, job.Id, "resume", HttpStatusCode.OK); + await WaitForAsync(editor, job.Id, "running"); + handler.Release(); + var done = await WaitForAsync(editor, job.Id, "done"); + Assert.Equal((4L, 4L), (done.ProgressDone, done.ProgressTotal)); + await ActAsync(editor, job.Id, "cancel", HttpStatusCode.Conflict); + + using var list = await editor.GetAsync("/api/v1/jobs?state=done&kind=deep-analyse"); + Assert.Equal(job.Id, (await list.ReadAsync(ProtocolJson.Default.PageJobDto)).Items.Single().Id); + } + + [Test] + public async Task FailedJobsRetryAndRunningJobsCancel() + { + var handler = new BlockingJobHandler(JobKind.Proxies) { FailWith = "decoder exploded" }; + await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton(handler)); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var sequence = await CreateSequenceAsync(editor); + + var job = await CreateAsync(editor, "proxies", sequence); + var failed = await WaitForAsync(editor, job.Id, "failed"); + Assert.Equal("decoder exploded", failed.Error); + Assert.Contains("decoder exploded", failed.LogTail); + + await ActAsync(editor, job.Id, "retry", HttpStatusCode.OK); + await WaitForAsync(editor, job.Id, "running"); + Assert.Equal("cancelled", (await ActAsync(editor, job.Id, "cancel", HttpStatusCode.OK)).State); + await Task.Delay(200); + Assert.Equal("cancelled", (await GetAsync(editor, job.Id)).State); + } + + [Test] + public async Task WaitingJobsChangePriorityAndRightsAreChecked() + { + var handler = new BlockingJobHandler(JobKind.Thumbs); + await using var server = await TestServer.StartAsync(context, o => o.Workers = 1, s => s.AddSingleton(handler)); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var (viewer, _, _) = await server.UserAsync("marco", "viewer"); + var sequence = await CreateSequenceAsync(editor); + + var first = await CreateAsync(editor, "thumbs", sequence); + await WaitForAsync(editor, first.Id, "running"); + var second = await CreateAsync(editor, "thumbs", sequence); + using (var lower = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("background"), ProtocolJson.Default.JobPriorityRequest)) + Assert.Equal("background", (await lower.ReadAsync(ProtocolJson.Default.JobDto)).Priority); + using (var bad = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("urgent"), ProtocolJson.Default.JobPriorityRequest)) + await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_priority"); + + using (var foreign = await viewer.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await foreign.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var create = await viewer.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("thumbs", [sequence], null), ProtocolJson.Default.CreateJobRequest)) + await create.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var unknown = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("teleport", [sequence], null), ProtocolJson.Default.CreateJobRequest)) + await unknown.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_job_kind"); + using (var unavailable = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("deep-analyse", [sequence], null), ProtocolJson.Default.CreateJobRequest)) + await unavailable.ExpectErrorAsync(HttpStatusCode.Conflict, "job_unavailable"); + using (var render = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("render", [sequence], "{}"), ProtocolJson.Default.CreateJobRequest)) + await render.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_parameters"); + + var admin = await server.AdminAsync(); + using (var removed = await admin.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await removed.ExpectAsync(HttpStatusCode.OK); + handler.Release(); + await WaitForAsync(editor, first.Id, "done"); + } + + public static async Task CreateSequenceAsync(HttpClient client, string title = "Harbour sunset") + { + using var created = await client.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest(title, null), ProtocolJson.Default.CreateSequenceRequest); + await created.ExpectAsync(HttpStatusCode.Created); + return (await created.ReadAsync(ProtocolJson.Default.SequenceDto)).Id; + } + + public static async Task CreateAsync(HttpClient client, string kind, Guid target, string? parameters = null) + { + using var response = await client.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest(kind, [target], parameters), ProtocolJson.Default.CreateJobRequest); + await response.ExpectAsync(HttpStatusCode.Accepted); + return await response.ReadAsync(ProtocolJson.Default.JobDto); + } + + public static async Task GetAsync(HttpClient client, Guid id) + { + using var response = await client.GetAsync($"/api/v1/jobs/{id}"); + return await response.ReadAsync(ProtocolJson.Default.JobDto); + } + + public static async Task WaitForAsync(HttpClient client, Guid id, string state) + { + var deadline = DateTime.UtcNow.AddSeconds(15); + while (true) + { + var job = await GetAsync(client, id); + if (job.State == state) return job; + if (DateTime.UtcNow > deadline) Assert.Fail($"Job {id} is {job.State}, expected {state}"); + await Task.Delay(50); + } + } + + private static async Task ActAsync(HttpClient client, Guid id, string action, HttpStatusCode expected) + { + using var response = await client.PostAsync($"/api/v1/jobs/{id}/{action}", null); + if (expected != HttpStatusCode.OK) + { + await response.ExpectErrorAsync(expected, "invalid_state"); + return await GetAsync(client, id); + } + await response.ExpectAsync(expected); + return await response.ReadAsync(ProtocolJson.Default.JobDto); + } +} diff --git a/tests/Titano.Server.Tests/Jobs/RenderNodeTests.cs b/tests/Titano.Server.Tests/Jobs/RenderNodeTests.cs new file mode 100644 index 00000000..0b7610fc --- /dev/null +++ b/tests/Titano.Server.Tests/Jobs/RenderNodeTests.cs @@ -0,0 +1,133 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json; +using Microsoft.Extensions.DependencyInjection; +using Titano.Protocol; +using Titano.Protocol.Client; +using Titano.Server.Jobs; +using Titano.Testing; + +namespace Titano.Server.Tests.Jobs; + +[Category("server")] +public sealed class RenderNodeTests(TestContext context) +{ + private static readonly string HevcJob = JsonSerializer.Serialize( + new RenderJobParameters("4K HEVC", """{"codec":"hevc","bitrate":60}""", "hevc", 3840, 2160, UseProxies: false, null), ProtocolJson.Default.RenderJobParameters); + + [Test] + public async Task NodesClaimWhatTheyCanEncodeAndLoseJobsWhenSilent() + { + var clock = new ManualTimeProvider(); + await using var server = await TestServer.StartAsync(context, o => o.NodeTimeoutSeconds = 60, s => s.AddSingleton(clock)); + var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true); + var frame = await Uploads.UploadAsync(editor, Uploads.Photo(5000, 51), "IMG_0001.CR3"); + var job = await JobApiTests.CreateAsync(editor, "render", frame.SequenceId, HevcJob); + + var studio = await RegisterAsync(editor, "studio-pc", "hevc", 8192); + var laptop = await RegisterAsync(editor, "laptop", "h264", 4096); + var spare = await RegisterAsync(editor, "spare-pc", "HEVC", 0); + using (var none = await editor.PostAsync($"/api/v1/nodes/{laptop.Id}/claim", null)) await none.ExpectAsync(HttpStatusCode.NoContent); + + ClaimedJob claimed; + using (var claim = await editor.PostAsync($"/api/v1/nodes/{studio.Id}/claim", null)) + { + await claim.ExpectAsync(HttpStatusCode.OK); + claimed = await claim.ReadAsync(ProtocolJson.Default.ClaimedJob); + } + Assert.Equal((job.Id, frame.SequenceId, "running"), (claimed.Job.Id, claimed.SequenceId, claimed.Job.State)); + Assert.Equal(frame.FrameId, claimed.Frames.Single().Id); + Assert.Equal(studio.Id.ToString(), claimed.Job.NodeId); + using (var second = await editor.PostAsync($"/api/v1/nodes/{spare.Id}/claim", null)) await second.ExpectAsync(HttpStatusCode.NoContent); + + using (var progress = await editor.PostAsJsonAsync($"/api/v1/nodes/{studio.Id}/jobs/{job.Id}/progress", new NodeProgress(120, 3600, 40, "encoding"), + ProtocolJson.Default.NodeProgress)) + await progress.ExpectAsync(HttpStatusCode.NoContent); + Assert.Equal(120L, (await JobApiTests.GetAsync(editor, job.Id)).ProgressDone); + + // studio-pc goes silent; the others keep heart-beating. + clock.Advance(TimeSpan.FromSeconds(90)); + foreach (var alive in new[] { laptop, spare }) + using (var beat = await editor.PostAsync($"/api/v1/nodes/{alive.Id}/heartbeat", null)) await beat.ExpectAsync(HttpStatusCode.NoContent); + await server.Services.GetRequiredService().SweepAsync(context.Cancellation); + + var requeued = await JobApiTests.GetAsync(editor, job.Id); + Assert.Equal("queued", requeued.State); + Assert.Contains("studio-pc stopped responding", requeued.LogTail); + using (var nodes = await editor.GetAsync("/api/v1/nodes")) + { + var states = (await nodes.ReadAsync(ProtocolJson.Default.ListRenderNodeDto)).ToDictionary(n => n.ClientName, n => n.State); + Assert.Equal(("offline", "idle"), (states["studio-pc"], states["laptop"])); + } + using (var late = await editor.PostAsJsonAsync($"/api/v1/nodes/{studio.Id}/jobs/{job.Id}/progress", new NodeProgress(200, 3600, 40, null), + ProtocolJson.Default.NodeProgress)) + await late.ExpectErrorAsync(HttpStatusCode.Conflict, "job_not_assigned"); + + using (var reassigned = await editor.PostAsync($"/api/v1/nodes/{spare.Id}/claim", null)) + { + var again = await reassigned.ReadAsync(ProtocolJson.Default.ClaimedJob); + Assert.Equal((job.Id, 2), (again.Job.Id, again.Job.Attempts)); + } + } + + [Test] + public async Task TheOutputUploadBecomesARenderAndFinishesTheJob() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = 64 * 1024); + var (editor, _, token) = await server.UserAsync("alberto", "editor", renderNode: true); + var frame = await Uploads.UploadAsync(editor, Uploads.Photo(5000, 52), "IMG_0001.CR3"); + var job = await JobApiTests.CreateAsync(editor, "render", frame.SequenceId, HevcJob); + + using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "studio-pc") { Token = token }; + var node = await client.RegisterNodeAsync(new RegisterNodeRequest("studio-pc", [new NodeEncoder("hevc", "NVENC HEVC", true, true)], 8192, 8192)); + var claimed = Assert.NotNull(await client.ClaimAsync(node.Id)); + await client.ReportNodeProgressAsync(node.Id, claimed.Job.Id, new NodeProgress(3600, 3600, 50, "muxing")); + + var output = Path.Combine(context.CreateTempDirectory("render"), "Harbour sunset - 4K HEVC.mp4"); + var video = Uploads.Photo(150_000, 53); + await File.WriteAllBytesAsync(output, video); + var render = await client.UploadRenderResultAsync(node.Id, job.Id, output, + new RenderResultRequest(Path.GetFileName(output), video.Length, Uploads.Sha(video), "4K HEVC", "hevc", 3840, 2160, 25, 144)); + Assert.Equal(("4K HEVC", "studio-pc", (long)video.Length), (render.PresetName, render.ProducedBy, render.Bytes)); + + var done = await JobApiTests.GetAsync(editor, job.Id); + Assert.Equal("done", done.State); + var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!; + Assert.Equal(1, sequence.RenderedCount); + var file = Path.Combine(server.Options.ArchivePath, sequence.Folder, "exports", "Harbour sunset - 4K HEVC.mp4"); + await Http.BytesEqualAsync(video, File.ReadAllBytesAsync(file)); + using var renders = await editor.GetAsync($"/api/v1/sequences/{sequence.Id}/renders"); + Assert.Equal(render.Id, (await renders.ReadAsync(ProtocolJson.Default.ListRenderDto)).Single().Id); + } + + [Test] + public async Task NodesReportFailuresAndNeedTheRight() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor", renderNode: true); + var (plain, _, _) = await server.UserAsync("giulia", "editor"); + var sequence = await JobApiTests.CreateSequenceAsync(editor); + var job = await JobApiTests.CreateAsync(editor, "render", sequence, HevcJob); + var node = await RegisterAsync(editor, "studio-pc", "hevc", 8192); + using (var claim = await editor.PostAsync($"/api/v1/nodes/{node.Id}/claim", null)) await claim.ExpectAsync(HttpStatusCode.OK); + using (var failed = await editor.PostAsJsonAsync($"/api/v1/nodes/{node.Id}/jobs/{job.Id}/failed", new NodeJobFailure("NVENC session limit reached"), + ProtocolJson.Default.NodeJobFailure)) + await failed.ExpectAsync(HttpStatusCode.NoContent); + var state = await JobApiTests.GetAsync(editor, job.Id); + Assert.Equal(("failed", "NVENC session limit reached"), (state.State, state.Error)); + + using var refused = await plain.PostAsJsonAsync("/api/v1/nodes/register", new RegisterNodeRequest("giulia-pc", [new NodeEncoder("hevc", "QSV", true, false)], 0, 0), + ProtocolJson.Default.RegisterNodeRequest); + await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using var foreign = await plain.PostAsync($"/api/v1/nodes/{node.Id}/heartbeat", null); + await foreign.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + } + + private static async Task RegisterAsync(HttpClient client, string name, string codec, int maxWidth) + { + using var response = await client.PostAsJsonAsync("/api/v1/nodes/register", + new RegisterNodeRequest(name, [new NodeEncoder(codec, codec.ToUpperInvariant() + " encoder", true, false)], maxWidth, maxWidth), ProtocolJson.Default.RegisterNodeRequest); + await response.ExpectAsync(HttpStatusCode.OK); + return await response.ReadAsync(ProtocolJson.Default.RenderNodeDto); + } +} diff --git a/tests/Titano.Server.Tests/Postgres/PostgresServerTests.cs b/tests/Titano.Server.Tests/Postgres/PostgresServerTests.cs new file mode 100644 index 00000000..511be137 --- /dev/null +++ b/tests/Titano.Server.Tests/Postgres/PostgresServerTests.cs @@ -0,0 +1,89 @@ +using System.Net; +using System.Net.Http.Json; +using Microsoft.Extensions.DependencyInjection; +using Titano.Core.Model; +using Titano.Data; +using Titano.Data.Jobs; +using Titano.Protocol; +using Titano.Server.Configuration; +using Titano.Server.Tests.Jobs; +using Titano.Testing; + +namespace Titano.Server.Tests.Postgres; + +/// The server on PostgreSQL (TITANO_TEST_PG), each test in a private schema. +[Category("server")] +[RequiresEnvironment("TITANO_TEST_PG")] +public sealed class PostgresServerTests(TestContext context) +{ + [Test] + public async Task SignInUploadSyncAndJobsWorkOnPostgres() + { + await using var schema = await Schema.CreateAsync(context); + var handler = new BlockingJobHandler(JobKind.Thumbs); + await using var server = await TestServer.StartAsync(context, o => + { + o.Database = DatabaseKind.Postgres; + o.DbConnectionString = schema.ConnectionString; + o.ChunkSizeBytes = 64 * 1024; + }, s => s.AddSingleton(handler)); + Assert.Equal(Dialect.Postgres, server.Database.Dialect); + + var (editor, _, _) = await server.UserAsync("alberto", "editor", quotaBytes: 10_000_000); + var data = Uploads.Photo(150_000, 71); + var frame = await Uploads.UploadAsync(editor, data, sessionKey: "pg"); + using (var manifest = await editor.GetAsync($"/api/v1/sequences/{frame.SequenceId}/manifest")) + Assert.Equal(Uploads.Sha(data), (await manifest.ReadAsync(ProtocolJson.Default.SequenceManifest)).Frames.Single().Sha256); + using (var me = await editor.GetAsync("/api/v1/auth/me")) Assert.Equal(data.Length, (await me.ReadAsync(ProtocolJson.Default.UserDto)).UsedBytes); + + using (var stale = await editor.PatchAsJsonAsync($"/api/v1/sequences/{frame.SequenceId}", new UpdateSequenceRequest(null, ["pg"], null, null, "{}", 99), + ProtocolJson.Default.UpdateSequenceRequest)) + await stale.ExpectErrorAsync(HttpStatusCode.Conflict, "settings_conflict"); + + // Uploading queued a background thumbnail job for the sequence; it runs on the server's engine. + using var queued = await editor.GetAsync("/api/v1/jobs?kind=thumbs"); + var job = (await queued.ReadAsync(ProtocolJson.Default.PageJobDto)).Items.Single(); + await JobApiTests.WaitForAsync(editor, job.Id, "running"); + handler.Release(); + await JobApiTests.WaitForAsync(editor, job.Id, "done"); + + var admin = await server.AdminAsync(); + using var dashboard = await admin.GetAsync("/api/v1/server/dashboard"); + Assert.True((await dashboard.ReadAsync(ProtocolJson.Default.DashboardDto)).Database.StartsWith("PostgreSQL ", StringComparison.Ordinal)); + using var health = await server.Client.GetAsync("/health"); + Assert.Equal("ok", (await health.ReadAsync(ProtocolJson.Default.HealthStatus)).Checks["database"]); + } + + /// A throw-away schema on the test server, selected through Search Path. + private sealed class Schema : IAsyncDisposable + { + private readonly IDatabase _admin; + private readonly string _name; + + private Schema(IDatabase admin, string name, string connectionString) + { + _admin = admin; + _name = name; + ConnectionString = connectionString; + } + + public string ConnectionString { get; } + + public static async Task CreateAsync(TestContext context) + { + var baseConnection = Environment.GetEnvironmentVariable("TITANO_TEST_PG")!; + var admin = DatabaseFactory.Open(DatabaseOptions.Postgres(baseConnection)); + var name = "titano_s_" + Guid.NewGuid().ToString("N")[..16]; + await using (var session = await admin.OpenAsync(context.Cancellation)) + await session.ExecuteScriptAsync($"CREATE SCHEMA {name}", context.Cancellation); + return new Schema(admin, name, baseConnection.TrimEnd(';', ' ') + $";Search Path={name}"); + } + + public async ValueTask DisposeAsync() + { + await using (var session = await _admin.OpenAsync(CancellationToken.None)) + await session.ExecuteScriptAsync($"DROP SCHEMA IF EXISTS {_name} CASCADE", CancellationToken.None); + await _admin.DisposeAsync(); + } + } +} diff --git a/tests/Titano.Server.Tests/Security/AuthTests.cs b/tests/Titano.Server.Tests/Security/AuthTests.cs new file mode 100644 index 00000000..7bebadd8 --- /dev/null +++ b/tests/Titano.Server.Tests/Security/AuthTests.cs @@ -0,0 +1,227 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using Microsoft.Extensions.DependencyInjection; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.Server.Tests.Security; + +[Category("server")] +public sealed class AuthTests(TestContext context) +{ + [Test] + public async Task BootstrapAdminMustChangePasswordBeforeAnythingElse() + { + await using var server = await TestServer.StartAsync(context); + var login = await server.LoginAsync("admin", TestServer.AdminPassword); + Assert.True(login.MustChangePassword); + Assert.Equal("admin", login.User.Role); + var client = server.NewClient(login.Token); + + using (var blocked = await client.GetAsync("/api/v1/users")) await blocked.ExpectErrorAsync(HttpStatusCode.Forbidden, "password_change_required"); + using (var me = await client.GetAsync("/api/v1/auth/me")) await me.ExpectAsync(HttpStatusCode.OK); + using (var weak = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(TestServer.AdminPassword, "short"), + ProtocolJson.Default.ChangePasswordRequest)) + await weak.ExpectErrorAsync(HttpStatusCode.BadRequest, "weak_password"); + using (var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(TestServer.AdminPassword, TestServer.Password), + ProtocolJson.Default.ChangePasswordRequest)) + await changed.ExpectAsync(HttpStatusCode.NoContent); + using var users = await client.GetAsync("/api/v1/users"); + await users.ExpectAsync(HttpStatusCode.OK); + Assert.Count(1, await users.ReadAsync(ProtocolJson.Default.ListUserSummaryDto)); + } + + [Test] + public async Task BootstrapWithoutPasswordWritesItToAFileNeverToTheLog() + { + await using var server = await TestServer.StartAsync(context, o => o.AdminPassword = null); + var file = Path.Combine(server.Options.DataPath, "initial-admin-password.txt"); + Assert.FileExists(file); + var password = File.ReadAllText(file).Trim(); + if (!OperatingSystem.IsWindows()) Assert.Equal(UnixFileMode.UserRead | UnixFileMode.UserWrite, File.GetUnixFileMode(file)); + var login = await server.LoginAsync("admin", password); + Assert.True(login.MustChangePassword); + foreach (var log in Directory.EnumerateFiles(server.Options.LogPath)) + { + using var reader = new StreamReader(new FileStream(log, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); + Assert.False((await reader.ReadToEndAsync()).Contains(password, StringComparison.Ordinal), "the password reached the log"); + } + + var client = server.NewClient(login.Token); + using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(password, TestServer.Password), + ProtocolJson.Default.ChangePasswordRequest); + await changed.ExpectAsync(HttpStatusCode.NoContent); + Assert.FileMissing(file); + } + + [Test] + public async Task WrongPasswordAndUnknownUserFailAlike() + { + await using var server = await TestServer.StartAsync(context); + using var wrong = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", "not-the-password", "tests"), ProtocolJson.Default.LoginRequest); + var a = await wrong.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials"); + using var unknown = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("nobody", "not-the-password", "tests"), ProtocolJson.Default.LoginRequest); + var b = await unknown.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials"); + Assert.Equal(a.Message, b.Message); + } + + [Test] + public async Task RepeatedFailuresLockTheAccountNameEvenWithTheRightPassword() + { + await using var server = await TestServer.StartAsync(context, o => + { + o.LoginAttemptsPerMinute = 100; + o.LockoutThreshold = 3; + }); + for (var i = 0; i < 3; i++) + { + using var failed = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest); + await failed.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials"); + } + using var locked = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.AdminPassword, "tests"), ProtocolJson.Default.LoginRequest); + await locked.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "account_locked"); + Assert.True(locked.Headers.RetryAfter?.Delta > TimeSpan.Zero); + + for (var i = 0; i < 3; i++) + (await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("ghost", "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest)).Dispose(); + using var ghost = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("ghost", "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest); + await ghost.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "account_locked"); + } + + [Test] + public async Task SignInsAreRateLimitedPerAddress() + { + await using var server = await TestServer.StartAsync(context, o => o.LoginAttemptsPerMinute = 5); + for (var i = 0; i < 5; i++) + (await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("user" + i, "wrong-password-x", "tests"), ProtocolJson.Default.LoginRequest)).Dispose(); + using var limited = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.AdminPassword, "tests"), ProtocolJson.Default.LoginRequest); + await limited.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "rate_limited"); + } + + [Test] + public async Task LogoutRevokesTheToken() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using (var me = await admin.GetAsync("/api/v1/auth/me")) await me.ExpectAsync(HttpStatusCode.OK); + using (var logout = await admin.PostAsync("/api/v1/auth/logout", null)) await logout.ExpectAsync(HttpStatusCode.NoContent); + using var after = await admin.GetAsync("/api/v1/auth/me"); + await after.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized"); + Assert.Equal("Bearer", after.Headers.WwwAuthenticate.Single().Scheme); + } + + [Test] + public async Task TokensExpireUnlessUsedWithinTheirLifetime() + { + var clock = new ManualTimeProvider(); + await using var server = await TestServer.StartAsync(context, o => o.TokenLifetimeDays = 30, s => s.AddSingleton(clock)); + var admin = await server.AdminAsync(); + clock.Advance(TimeSpan.FromDays(20)); + using (var used = await admin.GetAsync("/api/v1/auth/me")) await used.ExpectAsync(HttpStatusCode.OK); + clock.Advance(TimeSpan.FromDays(20)); + using (var slid = await admin.GetAsync("/api/v1/auth/me")) await slid.ExpectAsync(HttpStatusCode.OK); + clock.Advance(TimeSpan.FromDays(31)); + using var expired = await admin.GetAsync("/api/v1/auth/me"); + await expired.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized"); + } + + [Test] + public async Task TokensAreStoredOnlyAsHashes() + { + await using var server = await TestServer.StartAsync(context); + var login = await server.LoginAsync("admin", TestServer.AdminPassword); + await using var session = await server.Database.Database.OpenAsync(context.Cancellation); + var hashes = new List(); + await foreach (var row in session.QueryAsync("SELECT token_hash FROM tokens", [], context.Cancellation)) hashes.Add(row.GetString(0)); + Assert.Contains(Titano.Server.Security.TokenService.HashOf(login.Token!), hashes); + Assert.DoesNotContain(login.Token!, hashes); + } + + [Test] + public async Task DisablingAUserEndsTheirSessions() + { + await using var server = await TestServer.StartAsync(context); + var (editor, user, _) = await server.UserAsync("giulia", "editor"); + var admin = await server.AdminAsync(); + using (var disable = await admin.PatchAsJsonAsync($"/api/v1/users/{user.Id}", new UpdateUserRequest(null, null, null, true, null), ProtocolJson.Default.UpdateUserRequest)) + await disable.ExpectAsync(HttpStatusCode.OK); + using var me = await editor.GetAsync("/api/v1/auth/me"); + await me.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized"); + using var login = await server.Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("giulia", TestServer.Password, "tests"), ProtocolJson.Default.LoginRequest); + await login.ExpectErrorAsync(HttpStatusCode.Unauthorized, "invalid_credentials"); + } + + [Test] + public async Task PermissionsSeparateMissingSignInFromMissingRights() + { + await using var server = await TestServer.StartAsync(context); + using (var anonymous = await server.Client.GetAsync("/api/v1/users")) await anonymous.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized"); + using (var anonymousSequences = await server.Client.GetAsync("/api/v1/sequences")) await anonymousSequences.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized"); + + var (viewer, _, _) = await server.UserAsync("marco", "viewer"); + using (var users = await viewer.GetAsync("/api/v1/users")) await users.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var sequences = await viewer.GetAsync("/api/v1/sequences")) await sequences.ExpectAsync(HttpStatusCode.OK); + using (var upload = await viewer.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "a.jpg", 10, new string('a', 64), null, null), + ProtocolJson.Default.StartUploadRequest)) + await upload.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + using (var settings = await viewer.GetAsync("/api/v1/server/settings")) await settings.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + using (var start = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "a.jpg", 10, new string('a', 64), null, null), + ProtocolJson.Default.StartUploadRequest)) + await start.ExpectAsync(HttpStatusCode.Created); + using var edit = await uploader.PatchAsJsonAsync($"/api/v1/sequences/{Guid.NewGuid()}", new UpdateSequenceRequest("x", null, null, null, null, null), + ProtocolJson.Default.UpdateSequenceRequest); + await edit.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + } + + [Test] + public async Task CookieSessionsNeedTheCsrfHeaderForChanges() + { + await using var server = await TestServer.StartAsync(context); + await server.AdminAsync(); + var cookies = new CookieContainer(); + var browser = server.NewCookieClient(cookies); + using (var login = await browser.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest("admin", TestServer.Password, "admin-web", Cookie: true), + ProtocolJson.Default.LoginRequest)) + { + await login.ExpectAsync(HttpStatusCode.OK); + Assert.Null((await login.ReadAsync(ProtocolJson.Default.LoginResponse)).Token); + var session = login.Headers.GetValues("Set-Cookie").Single(c => c.StartsWith("titano_session=", StringComparison.Ordinal)); + Assert.Contains("httponly", session.ToLowerInvariant()); + Assert.Contains("secure", session.ToLowerInvariant()); + Assert.Contains("samesite=strict", session.ToLowerInvariant()); + } + using (var me = await browser.GetAsync("/api/v1/auth/me")) await me.ExpectAsync(HttpStatusCode.OK); + using (var forged = await browser.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest("x", null), ProtocolJson.Default.CreateSequenceRequest)) + await forged.ExpectErrorAsync(HttpStatusCode.Forbidden, "csrf_failed"); + using (var wrong = new HttpRequestMessage(HttpMethod.Post, "/api/v1/sequences") { Content = JsonContent.Create(new CreateSequenceRequest("x", null), ProtocolJson.Default.CreateSequenceRequest) }) + { + wrong.Headers.Add("X-CSRF-Token", "not-the-token"); + using var response = await browser.SendAsync(wrong); + await response.ExpectErrorAsync(HttpStatusCode.Forbidden, "csrf_failed"); + } + var csrf = cookies.GetCookies(server.HttpsBase!)["titano_csrf"]!.Value; + using var request = new HttpRequestMessage(HttpMethod.Post, "/api/v1/sequences") { Content = JsonContent.Create(new CreateSequenceRequest("x", null), ProtocolJson.Default.CreateSequenceRequest) }; + request.Headers.Add("X-CSRF-Token", csrf); + using var created = await browser.SendAsync(request); + await created.ExpectAsync(HttpStatusCode.Created); + } + + [Test] + public async Task ErrorsHaveTheApiShapeAndHeader() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using (var missing = await server.Client.GetAsync("/api/v1/nothing-here")) await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + using (var badJson = await admin.PostAsync("/api/v1/sequences", new StringContent("{ not json", MediaTypeHeaderValue.Parse("application/json")))) + await badJson.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request"); + using (var badGuid = await admin.GetAsync($"/api/v1/sequences/{Guid.NewGuid()}")) await badGuid.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + using var italian = new HttpRequestMessage(HttpMethod.Get, "/api/v1/users"); + italian.Headers.AcceptLanguage.ParseAdd("it-IT"); + using var response = await server.Client.SendAsync(italian); + var error = await response.ExpectErrorAsync(HttpStatusCode.Unauthorized, "unauthorized"); + Assert.Equal("Accedi per continuare.", error.Message); + } +} diff --git a/tests/Titano.Server.Tests/Storage/FileTests.cs b/tests/Titano.Server.Tests/Storage/FileTests.cs new file mode 100644 index 00000000..b76d4fb0 --- /dev/null +++ b/tests/Titano.Server.Tests/Storage/FileTests.cs @@ -0,0 +1,144 @@ +using System.Net; +using System.Net.Http.Headers; +using Microsoft.Extensions.DependencyInjection; +using Titano.Core.Model; +using Titano.Server.Storage; +using Titano.Testing; + +namespace Titano.Server.Tests.Storage; + +[Category("server")] +public sealed class FileTests(TestContext context) +{ + [Test] + public async Task OriginalsAreStreamedWithRangesAndEtags() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var data = Uploads.Photo(10_000, 21); + var frame = await Uploads.UploadAsync(editor, data); + + using (var full = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original")) + { + await full.ExpectAsync(HttpStatusCode.OK); + Assert.Equal("bytes", full.Headers.AcceptRanges.Single()); + Assert.Equal($"\"{Uploads.Sha(data)}\"", full.Headers.ETag!.Tag); + await Http.BytesEqualAsync(data, full.Content.ReadAsByteArrayAsync()); + } + + using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original"); + range.Headers.Range = new RangeHeaderValue(100, 199); + using (var partial = await editor.SendAsync(range)) + { + await partial.ExpectAsync(HttpStatusCode.PartialContent); + Assert.Equal((100L, 199L, 10_000L), (partial.Content.Headers.ContentRange!.From!.Value, partial.Content.Headers.ContentRange.To!.Value, + partial.Content.Headers.ContentRange.Length!.Value)); + await Http.BytesEqualAsync(data[100..200], partial.Content.ReadAsByteArrayAsync()); + } + + using var tail = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original"); + tail.Headers.Range = new RangeHeaderValue(9_990, null); + using (var end = await editor.SendAsync(tail)) await Http.BytesEqualAsync(data[9_990..], end.Content.ReadAsByteArrayAsync()); + + using var cached = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{frame.FrameId}/original"); + cached.Headers.IfNoneMatch.Add(new EntityTagHeaderValue($"\"{Uploads.Sha(data)}\"")); + using (var notModified = await editor.SendAsync(cached)) await notModified.ExpectAsync(HttpStatusCode.NotModified); + + var (viewer, _, _) = await server.UserAsync("marco", "viewer"); + using var refused = await viewer.GetAsync($"/api/v1/frames/{frame.FrameId}/original"); + await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden"); + } + + [Test] + public async Task ThumbnailsAndProxiesComeFromTheLocatedFiles() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var first = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 22), "a.jpg", sessionKey: "s"); + var second = await Uploads.UploadAsync(editor, Uploads.Photo(3000, 23), "b.jpg", sessionKey: "s"); + var sequence = (await server.Database.Sequences.GetAsync(first.SequenceId))!; + var folder = Path.Combine(server.Options.ArchivePath, sequence.Folder); + var pack = Uploads.Photo(FakeAssetLocator.ThumbnailSize * 2, 24); + Directory.CreateDirectory(Path.Combine(folder, ".titano")); + await File.WriteAllBytesAsync(Path.Combine(folder, ".titano", "thumbs.pack"), pack); + + using (var thumb = await editor.GetAsync($"/api/v1/frames/{second.FrameId}/thumb")) + { + await thumb.ExpectAsync(HttpStatusCode.OK); + Assert.Equal("image/jpeg", thumb.Content.Headers.ContentType!.MediaType); + await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(2 * FakeAssetLocator.ThumbnailSize)], thumb.Content.ReadAsByteArrayAsync()); + } + using var range = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/frames/{second.FrameId}/thumb"); + range.Headers.Range = new RangeHeaderValue(0, 9); + using (var partial = await editor.SendAsync(range)) + { + await partial.ExpectAsync(HttpStatusCode.PartialContent); + await Http.BytesEqualAsync(pack[FakeAssetLocator.ThumbnailSize..(FakeAssetLocator.ThumbnailSize + 10)], partial.Content.ReadAsByteArrayAsync()); + } + + using (var missing = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy")) + Assert.Equal("asset_missing", (await missing.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found")).Details); + var proxy = Uploads.Photo(500, 25); + Directory.CreateDirectory(Path.Combine(folder, "proxies")); + await File.WriteAllBytesAsync(Path.Combine(folder, "proxies", "0000.jpg"), proxy); + using var found = await editor.GetAsync($"/api/v1/frames/{first.FrameId}/proxy"); + await Http.BytesEqualAsync(proxy, found.Content.ReadAsByteArrayAsync()); + } + + [Test] + public async Task PathsOutsideTheArchiveAreNeverServed() + { + await using var server = await TestServer.StartAsync(context); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 26)); + var secret = Path.Combine(Path.GetDirectoryName(server.Options.ArchivePath)!, "secret.txt"); + await File.WriteAllTextAsync(secret, "not for clients"); + + foreach (var hostile in new[] { "../secret.txt", "2026/../../secret.txt", "..\\secret.txt", secret, "/etc/passwd" }) + { + await using (var session = await server.Database.Database.OpenAsync(context.Cancellation)) + await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", [hostile, frame.FrameId], context.Cancellation); + using var response = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original"); + await response.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + } + + var locator = server.Services.GetRequiredService(); + locator.Override = new AssetLocation(secret, 0, 5, "text/plain"); + using (var proxy = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/proxy")) await proxy.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + locator.Override = new AssetLocation(Path.Combine(server.Options.ArchivePath, "..", "secret.txt"), 0, 5, "text/plain"); + using (var dotted = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/thumb")) await dotted.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + + if (!OperatingSystem.IsWindows()) + { + var link = Path.Combine(server.Options.ArchivePath, "link.jpg"); + File.CreateSymbolicLink(link, secret); + await using (var session = await server.Database.Database.OpenAsync(context.Cancellation)) + await session.ExecuteAsync("UPDATE frames SET relative_path = $1 WHERE id = $2", ["link.jpg", frame.FrameId], context.Cancellation); + using var linked = await editor.GetAsync($"/api/v1/frames/{frame.FrameId}/original"); + await linked.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + } + } + + [Test] + public async Task ExportsAreDownloadableWithRanges() + { + await using var server = await TestServer.StartAsync(context); + var (viewer, _, _) = await server.UserAsync("marco", "viewer"); + var (editor, _, _) = await server.UserAsync("alberto", "editor"); + var frame = await Uploads.UploadAsync(editor, Uploads.Photo(1000, 27)); + var sequence = (await server.Database.Sequences.GetAsync(frame.SequenceId))!; + var video = Uploads.Photo(4096, 28); + var relative = $"{sequence.Folder}/exports/Harbour sunset - 4K.mp4"; + Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(server.Options.ArchivePath, relative))!); + await File.WriteAllBytesAsync(Path.Combine(server.Options.ArchivePath, relative), video); + var render = new Render { Id = Guid.CreateVersion7(), SequenceId = sequence.Id, PresetName = "4K HEVC", OutputPath = relative, Bytes = video.Length, Created = DateTimeOffset.UtcNow }; + await server.Database.Renders.InsertAsync(render); + + using var request = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/renders/{render.Id}/file"); + request.Headers.Range = new RangeHeaderValue(1000, 1999); + using var response = await viewer.SendAsync(request); + await response.ExpectAsync(HttpStatusCode.PartialContent); + Assert.Equal("video/mp4", response.Content.Headers.ContentType!.MediaType); + await Http.BytesEqualAsync(video[1000..2000], response.Content.ReadAsByteArrayAsync()); + } +} diff --git a/tests/Titano.Server.Tests/Storage/UploadTests.cs b/tests/Titano.Server.Tests/Storage/UploadTests.cs new file mode 100644 index 00000000..bb072e69 --- /dev/null +++ b/tests/Titano.Server.Tests/Storage/UploadTests.cs @@ -0,0 +1,227 @@ +using System.Net; +using System.Net.Http.Json; +using Microsoft.Extensions.DependencyInjection; +using Titano.Protocol; +using Titano.Protocol.Client; +using Titano.Server.Storage; +using Titano.Testing; + +namespace Titano.Server.Tests.Storage; + +[Category("server")] +public sealed class UploadTests(TestContext context) +{ + private const int Chunk = 64 * 1024; + + [Test] + public async Task ChunksInAnyOrderCommitAVerifiedFrame() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var data = Uploads.Photo(Chunk * 3 + 1234, 1); + var start = await Uploads.StartAsync(uploader, data, sessionKey: "roof-2026-09-28"); + var id = Assert.NotNull(start.UploadId); + Assert.Equal(Chunk, start.ChunkSize); + Assert.Empty(start.Received); + + foreach (var index in new[] { 3, 0, 2, 1, 0 }) + { + using var put = await Uploads.PutChunkAsync(uploader, id, index, Uploads.Chunk(data, Chunk, index)); + await put.ExpectAsync(HttpStatusCode.OK); + Assert.Equal(index, (await put.ReadAsync(ProtocolJson.Default.ChunkReceived)).Index); + } + using (var state = await uploader.GetAsync($"/api/v1/uploads/{id}")) + { + var resume = await state.ReadAsync(ProtocolJson.Default.UploadState); + Assert.SequenceEqual([0, 1, 2, 3], resume.Received); + Assert.Equal("receiving", resume.State); + } + + using var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null); + await complete.ExpectAsync(HttpStatusCode.OK); + var frame = await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse); + var ingest = server.Services.GetRequiredService(); + var request = Assert.NotNull(ingest.Requests.LastOrDefault()); + Assert.Equal("roof-2026-09-28", request.SessionKey); + Assert.Null(request.SequenceId); + Assert.Equal(Uploads.Sha(data), request.Sha256); + Assert.True(request.StagedPath.StartsWith(server.Options.StagingPath, StringComparison.Ordinal), request.StagedPath); + + var stored = Assert.NotNull(await server.Database.Frames.GetAsync(frame.FrameId)); + await Http.BytesEqualAsync(data, File.ReadAllBytesAsync(Path.Combine(server.Options.ArchivePath, stored.RelativePath))); + Assert.False(server.Services.GetRequiredService().Exists(id), "the partial file is gone"); + Assert.Empty(Directory.EnumerateFiles(server.Options.StagingPath)); + using (var me = await uploader.GetAsync("/api/v1/auth/me")) Assert.Equal(data.Length, (await me.ReadAsync(ProtocolJson.Default.UserDto)).UsedBytes); + + // A completion whose response was lost can be repeated. + using var again = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null); + await again.ExpectAsync(HttpStatusCode.OK); + Assert.Equal(frame.FrameId, (await again.ReadAsync(ProtocolJson.Default.CompleteUploadResponse)).FrameId); + } + + [Test] + public async Task AnInterruptedUploadResumesWithItsReceivedChunks() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var data = Uploads.Photo(Chunk * 4, 2); + var first = await Uploads.StartAsync(uploader, data); + foreach (var index in new[] { 0, 2 }) + { + using var put = await Uploads.PutChunkAsync(uploader, first.UploadId!.Value, index, Uploads.Chunk(data, Chunk, index)); + await put.ExpectAsync(HttpStatusCode.OK); + } + var resumed = await Uploads.StartAsync(uploader, data); + Assert.Equal(first.UploadId, resumed.UploadId); + Assert.SequenceEqual([0, 2], resumed.Received); + foreach (var index in new[] { 1, 3 }) + { + using var put = await Uploads.PutChunkAsync(uploader, first.UploadId!.Value, index, Uploads.Chunk(data, Chunk, index)); + await put.ExpectAsync(HttpStatusCode.OK); + } + using var complete = await uploader.PostAsync($"/api/v1/uploads/{first.UploadId}/complete", null); + await complete.ExpectAsync(HttpStatusCode.OK); + } + + [Test] + public async Task AKnownHashShortCircuitsTheUpload() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var data = Uploads.Photo(5000, 3); + var frame = await Uploads.UploadAsync(uploader, data); + var again = await Uploads.StartAsync(uploader, data, expected: HttpStatusCode.OK); + Assert.True(again.AlreadyExists); + Assert.Null(again.UploadId); + Assert.Equal(frame.FrameId, again.FrameId); + } + + [Test] + public async Task TheProtocolClientUploadsAndDownloadsLikeTheUploader() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (_, _, token) = await server.UserAsync("alberto", "editor"); + var folder = context.CreateTempDirectory("source"); + var path = Path.Combine(folder, "2026-09-28_18-02-11_0413.CR3"); + var data = Uploads.Photo(Chunk * 2 + 77, 4); + await File.WriteAllBytesAsync(path, data); + + using var client = new TitanoClient(server.HttpBase, new CertificatePinning([]), "Titano Uploader (tests)") { Token = token }; + var sequence = await client.CreateSequenceAsync(new CreateSequenceRequest("Harbour sunset", null)); + var progress = new List(); + var result = Assert.NotNull(await client.UploadFileAsync(path, sequence.Id, new DateTime(2026, 9, 28), progress: new SyncProgress(progress.Add))); + Assert.Equal(sequence.Id, result.SequenceId); + Assert.Equal(0, result.Index); + Assert.Equal(data.Length, progress[^1].BytesSent); + + var duplicate = Assert.NotNull(await client.UploadFileAsync(path, sequence.Id, null)); + Assert.Equal((result.FrameId, -1), (duplicate.FrameId, duplicate.Index)); + + using var downloaded = new MemoryStream(); + await client.DownloadFrameAsync(result.FrameId, "original", downloaded); + Assert.BytesEqual(data, downloaded.ToArray()); + var manifest = await client.GetManifestAsync(sequence.Id); + Assert.Equal(Uploads.Sha(data), manifest.Frames.Single().Sha256); + } + + [Test] + public async Task ADamagedChunkFailsTheHashCheck() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var data = Uploads.Photo(Chunk * 2, 5); + var start = await Uploads.StartAsync(uploader, data); + var id = start.UploadId!.Value; + var damaged = Uploads.Chunk(data, Chunk, 1).ToArray(); + damaged[100] ^= 0xFF; + using (var a = await Uploads.PutChunkAsync(uploader, id, 0, Uploads.Chunk(data, Chunk, 0))) await a.ExpectAsync(HttpStatusCode.OK); + using (var b = await Uploads.PutChunkAsync(uploader, id, 1, damaged)) await b.ExpectAsync(HttpStatusCode.OK); + + using var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null); + await complete.ExpectErrorAsync(HttpStatusCode.UnprocessableEntity, "hash_mismatch"); + using (var state = await uploader.GetAsync($"/api/v1/uploads/{id}")) Assert.Equal("failed", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State); + Assert.False(server.Services.GetRequiredService().Exists(id)); + Assert.Empty(await server.Database.Frames.FindBySha256Async(Uploads.Sha(data))); + Assert.Empty(server.Services.GetRequiredService().Requests); + + var retry = await Uploads.StartAsync(uploader, data); + Assert.NotEqual(id, retry.UploadId!.Value); + } + + [Test] + public async Task ChunksAndCompletionsAreValidated() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var data = Uploads.Photo(Chunk + 10, 6); + var id = (await Uploads.StartAsync(uploader, data)).UploadId!.Value; + using (var small = await Uploads.PutChunkAsync(uploader, id, 0, new byte[10])) await small.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_size"); + using (var range = await Uploads.PutChunkAsync(uploader, id, 2, new byte[10])) await range.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_out_of_range"); + using (var early = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null)) await early.ExpectErrorAsync(HttpStatusCode.Conflict, "incomplete"); + + using (var name = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "../../etc/passwd", 10, Uploads.Sha(data), null, null), + ProtocolJson.Default.StartUploadRequest)) + await name.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_file_name"); + using (var hash = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "a.jpg", 10, "abc", null, null), ProtocolJson.Default.StartUploadRequest)) + await hash.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_hash"); + + var (other, _, _) = await server.UserAsync("intruder", "uploader"); + using (var foreign = await Uploads.PutChunkAsync(other, id, 0, Uploads.Chunk(data, Chunk, 0))) await foreign.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found"); + using (var cancel = await uploader.DeleteAsync($"/api/v1/uploads/{id}")) await cancel.ExpectAsync(HttpStatusCode.NoContent); + Assert.False(server.Services.GetRequiredService().Exists(id)); + using var late = await Uploads.PutChunkAsync(uploader, id, 0, Uploads.Chunk(data, Chunk, 0)); + await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state"); + } + + [Test] + public async Task QuotaCountsCommittedAndPendingUploads() + { + await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = Chunk); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader", quotaBytes: 100_000); + var first = Uploads.Photo(80_000, 7); + var pending = await Uploads.StartAsync(uploader, first); + using (var over = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "b.jpg", 40_000, Uploads.Sha(Uploads.Photo(40_000, 8)), null, null), + ProtocolJson.Default.StartUploadRequest)) + await over.ExpectErrorAsync(HttpStatusCode.Forbidden, "quota_exceeded"); + using (var cancel = await uploader.DeleteAsync($"/api/v1/uploads/{pending.UploadId}")) await cancel.ExpectAsync(HttpStatusCode.NoContent); + await Uploads.UploadAsync(uploader, first); + using var after = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "c.jpg", 30_000, Uploads.Sha(Uploads.Photo(30_000, 9)), null, null), + ProtocolJson.Default.StartUploadRequest); + await after.ExpectErrorAsync(HttpStatusCode.Forbidden, "quota_exceeded"); + } + + [Test] + public async Task FilesAboveTheLimitAreRefused() + { + await using var server = await TestServer.StartAsync(context, o => o.MaxUploadBytes = 1000); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + using var response = await uploader.PostAsJsonAsync("/api/v1/uploads", new StartUploadRequest(null, "big.jpg", 1001, new string('0', 64), null, null), + ProtocolJson.Default.StartUploadRequest); + var error = await response.ExpectErrorAsync(HttpStatusCode.RequestEntityTooLarge, "too_large"); + Assert.Equal("1000", error.Details); + } + + [Test] + public async Task AbandonedUploadsExpireAndLoseTheirFiles() + { + var clock = new ManualTimeProvider(); + await using var server = await TestServer.StartAsync(context, o => o.UploadRetentionHours = 48, s => s.AddSingleton(clock)); + var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader"); + var id = (await Uploads.StartAsync(uploader, Uploads.Photo(1000, 10))).UploadId!.Value; + var store = server.Services.GetRequiredService(); + Assert.True(store.Exists(id)); + + // Upload rows carry real timestamps; the sweep looks 48 h past the moved clock. + clock.Advance(TimeSpan.FromHours(49)); + await server.Services.GetRequiredService().SweepAsync(context.Cancellation); + Assert.False(store.Exists(id)); + using var state = await uploader.GetAsync($"/api/v1/uploads/{id}"); + Assert.Equal("cancelled", (await state.ReadAsync(ProtocolJson.Default.UploadState)).State); + } + + /// Reports synchronously (Progress<T> posts to the thread pool and may arrive late). + private sealed class SyncProgress(Action report) : IProgress + { + public void Report(T value) => report(value); + } +} diff --git a/tests/Titano.Server.Tests/TestServer.cs b/tests/Titano.Server.Tests/TestServer.cs index 2e099360..d08cacbd 100644 --- a/tests/Titano.Server.Tests/TestServer.cs +++ b/tests/Titano.Server.Tests/TestServer.cs @@ -1,17 +1,33 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Titano.Data; +using Titano.Data.Archiving; +using Titano.Protocol; using Titano.Server.Configuration; using Titano.Server.Hosting; +using Titano.Server.Storage; using Titano.Testing; namespace Titano.Server.Tests; -/// In-process Kestrel on ephemeral ports with temporary archive/data folders (BRIEF §20.1). +/// +/// In-process Kestrel on ephemeral ports with temporary archive/data folders and a SQLite database (BRIEF §20.1). +/// The archive module is replaced by and . +/// public sealed class TestServer : IAsyncDisposable { + public const string AdminPassword = "first-admin-pass"; + public const string Password = "a-good-password-1"; + private readonly WebApplication _app; + private readonly List _clients = []; + private string _adminPassword = AdminPassword; private TestServer(WebApplication app, ServerOptions options, Uri http, Uri? https) { @@ -19,16 +35,21 @@ public sealed class TestServer : IAsyncDisposable Options = options; HttpBase = http; HttpsBase = https; - Client = new HttpClient { BaseAddress = http, Timeout = TimeSpan.FromSeconds(30) }; + Client = NewClient(); } public ServerOptions Options { get; } public Uri HttpBase { get; } public Uri? HttpsBase { get; } + + /// An anonymous client. public HttpClient Client { get; } + public IServiceProvider Services => _app.Services; - public static async Task StartAsync(TestContext context, Action? configure = null) + public TitanoDatabase Database => Services.GetRequiredService(); + + public static async Task StartAsync(TestContext context, Action? configure = null, Action? services = null) { var root = context.CreateTempDirectory("server"); var options = new ServerOptions @@ -39,10 +60,23 @@ public sealed class TestServer : IAsyncDisposable HttpsPort = 0, TlsMode = TlsMode.Auto, Database = DatabaseKind.Sqlite, + Discovery = false, + Workers = 2, + AdminUser = "admin", + AdminPassword = AdminPassword, + // Tests sign in many times from one address; the rate-limit test sets the production value. + LoginAttemptsPerMinute = 1000, }; Directory.CreateDirectory(options.ArchivePath); configure?.Invoke(options); - var app = ServerHost.Build(options, []); + var app = ServerHost.Build(options, [], s => + { + s.AddSingleton(); + s.TryAddSingleton(sp => sp.GetRequiredService()); + s.AddSingleton(); + s.TryAddSingleton(sp => sp.GetRequiredService()); + services?.Invoke(s); + }); await app.StartAsync(context.Cancellation); var addresses = app.Services.GetRequiredService().Features.Get()!.Addresses .Select(a => new Uri(a.Replace("[::]", "127.0.0.1", StringComparison.Ordinal).Replace("0.0.0.0", "127.0.0.1", StringComparison.Ordinal))) @@ -52,9 +86,65 @@ public sealed class TestServer : IAsyncDisposable return new TestServer(app, options, http, https); } + /// A plain HTTP client (owned by the server, disposed with it). + public HttpClient NewClient(string? token = null) + { + var client = new HttpClient { BaseAddress = HttpBase, Timeout = TimeSpan.FromSeconds(60) }; + if (token is not null) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + _clients.Add(client); + return client; + } + + /// An HTTPS client with a cookie jar that trusts the self-signed certificate (admin panel sessions). + public HttpClient NewCookieClient(CookieContainer cookies) + { + var handler = new HttpClientHandler { CookieContainer = cookies, ServerCertificateCustomValidationCallback = (_, _, _, _) => true }; + var client = new HttpClient(handler) { BaseAddress = HttpsBase, Timeout = TimeSpan.FromSeconds(60) }; + _clients.Add(client); + return client; + } + + public async Task LoginAsync(string username, string password, string clientName = "tests") + { + using var response = await Client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest(username, password, clientName), ProtocolJson.Default.LoginRequest); + if (!response.IsSuccessStatusCode) throw new InvalidOperationException($"Login of {username} failed: {(int)response.StatusCode} {await response.Content.ReadAsStringAsync()}"); + return (await response.Content.ReadFromJsonAsync(ProtocolJson.Default.LoginResponse))!; + } + + /// The bootstrap administrator, signed in; its first sign-in replaces the bootstrap password with . + public async Task AdminAsync() + { + var login = await LoginAsync("admin", _adminPassword); + var client = NewClient(login.Token); + if (login.MustChangePassword) + { + using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(_adminPassword, Password), + ProtocolJson.Default.ChangePasswordRequest); + changed.EnsureSuccessStatusCode(); + _adminPassword = Password; + } + return client; + } + + /// Creates a user (through the admin API) who has set , and returns a client signed in as them. + public async Task<(HttpClient Client, UserDto User, string Token)> UserAsync(string username, string role, long quotaBytes = 0, bool renderNode = false) + { + var admin = await AdminAsync(); + using var created = await admin.PostAsJsonAsync("/api/v1/users", new CreateUserRequest(username, null, role, quotaBytes, null, renderNode), + ProtocolJson.Default.CreateUserRequest); + created.EnsureSuccessStatusCode(); + var temporary = (await created.Content.ReadFromJsonAsync(ProtocolJson.Default.TemporaryPasswordResponse))!; + var first = await LoginAsync(username, temporary.TemporaryPassword); + var client = NewClient(first.Token); + using var changed = await client.PostAsJsonAsync("/api/v1/auth/password", new ChangePasswordRequest(temporary.TemporaryPassword, Password), + ProtocolJson.Default.ChangePasswordRequest); + changed.EnsureSuccessStatusCode(); + return (client, temporary.User, first.Token!); + } + public async ValueTask DisposeAsync() { - Client.Dispose(); + foreach (var client in _clients) client.Dispose(); await _app.StopAsync(); await _app.DisposeAsync(); } diff --git a/tests/Titano.Server.Tests/Uploads.cs b/tests/Titano.Server.Tests/Uploads.cs new file mode 100644 index 00000000..c1479ace --- /dev/null +++ b/tests/Titano.Server.Tests/Uploads.cs @@ -0,0 +1,57 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Security.Cryptography; +using Titano.Protocol; +using Titano.Testing; + +namespace Titano.Server.Tests; + +/// Raw-HTTP upload helpers (the protocol client is exercised separately). +internal static class Uploads +{ + public static byte[] Photo(int size, int seed) + { + var bytes = new byte[size]; + new Random(seed).NextBytes(bytes); + return bytes; + } + + public static string Sha(byte[] data) => Convert.ToHexStringLower(SHA256.HashData(data)); + + public static async Task StartAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null, + string? sessionKey = null, HttpStatusCode expected = HttpStatusCode.Created) + { + using var response = await client.PostAsJsonAsync("/api/v1/uploads", + new StartUploadRequest(sequenceId, fileName, data.Length, Sha(data), new DateTime(2026, 9, 28, 18, 2, 11), sessionKey), ProtocolJson.Default.StartUploadRequest); + await response.ExpectAsync(expected); + return await response.ReadAsync(ProtocolJson.Default.StartUploadResponse); + } + + public static async Task PutChunkAsync(HttpClient client, Guid uploadId, int index, ReadOnlyMemory data) + { + var content = new ByteArrayContent(data.ToArray()); + content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); + return await client.PutAsync($"/api/v1/uploads/{uploadId}/chunks/{index}", content); + } + + public static ReadOnlyMemory Chunk(byte[] data, int chunkSize, int index) => + data.AsMemory(index * chunkSize, Math.Min(chunkSize, data.Length - index * chunkSize)); + + /// Start, every chunk, complete; returns the committed frame. + public static async Task UploadAsync(HttpClient client, byte[] data, string fileName = "IMG_0001.CR3", Guid? sequenceId = null, + string? sessionKey = null) + { + var start = await StartAsync(client, data, fileName, sequenceId, sessionKey); + var id = Assert.NotNull(start.UploadId); + var chunks = (data.Length + start.ChunkSize - 1) / start.ChunkSize; + for (var i = 0; i < chunks; i++) + { + using var put = await PutChunkAsync(client, id, i, Chunk(data, start.ChunkSize, i)); + await put.ExpectAsync(HttpStatusCode.OK); + } + using var complete = await client.PostAsync($"/api/v1/uploads/{id}/complete", null); + await complete.ExpectAsync(HttpStatusCode.OK); + return await complete.ReadAsync(ProtocolJson.Default.CompleteUploadResponse); + } +}