From 5921b7c4141734e197cd6178678ae6d1894ff26a Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 26 Oct 2026 13:32:37 +0100 Subject: [PATCH] Add the Media tab: browse sound, image and video files and open them on the PC Media tab (needs at least one folder, hidden otherwise): - Folders from [media] roots in config.toml, default = the user's Music, Videos and Pictures (XDG, never $HOME); [] disables the tab - Icons per type, subfolders, breadcrumb, back, refresh, All/Sounds/Images/ Videos filter; a tap opens the file on the PC (WebSocket system/open_media) - Three special launchers (audio, image, video), editable on /admin: system default (gio open), installed application (gio launch) or a raw command (allow_custom_command); the file path is appended as the last argument - The client never sends an absolute path: (root, relative path) is resolved server side; refuses "..", hidden entries, symlinks leaving the root, and anything but known media extensions (a script renamed .mp3 is refused) - GET /api/media (Bearer token), listing runs in a thread, 1000 entries max Interface cache: pages are revalidated (Cache-Control: no-cache) and static files are served under /static// (immutable cache), so a tablet can no longer keep an old interface after an update. Reproduced and verified: opening the same link serves the cached page, one refresh fixes it for good. Refactors: restapi.py (shared REST helpers), AppPicker (shared by the launcher and opener forms on /admin). Tests: 159 (library escapes, openers, admin API, config, cache headers). Also checked end to end with a real server (fake gio recording its arguments) and headless Chromium. Not tried on the real tablet. Co-Authored-By: Claude Sonnet 5.5 --- CLAUDE.md | 35 +++- README-EN.md | 33 ++++ README.md | 34 ++++ config.example.toml | 7 + mediadeck/admin.py | 64 ++++--- mediadeck/config.py | 22 ++- mediadeck/launchers.py | 81 +++++++++ mediadeck/media.py | 245 ++++++++++++++++++++++++++ mediadeck/restapi.py | 27 +++ mediadeck/server.py | 53 +++++- mediadeck/system.py | 25 ++- mediadeck/web/admin.html | 43 +++++ mediadeck/web/index.html | 15 ++ mediadeck/web/static/admin.css | 8 + mediadeck/web/static/admin.js | 302 ++++++++++++++++++++++++++++----- mediadeck/web/static/app.js | 296 ++++++++++++++++++++++++++++++++ mediadeck/web/static/style.css | 107 ++++++++++++ tests/test_admin.py | 52 ++++++ tests/test_config.py | 13 ++ tests/test_headers.py | 122 +++++++++++++ tests/test_launchers.py | 109 ++++++++++++ tests/test_media.py | 265 +++++++++++++++++++++++++++++ 22 files changed, 1876 insertions(+), 82 deletions(-) create mode 100644 mediadeck/media.py create mode 100644 mediadeck/restapi.py create mode 100644 tests/test_headers.py create mode 100644 tests/test_media.py diff --git a/CLAUDE.md b/CLAUDE.md index d85a12e..c361df2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -47,16 +47,18 @@ mediadeck/ ├── text.py TextTyper : texte -> frappes uinput, verrou, relâchement garanti, 500 caractères max ├── system.py SystemControl : logind (suspend/reboot/poweroff), ScreenSaver.Lock, lancement des lanceurs ├── launchers.py LauncherStore (SQLite), validation, build_argv(), applications .desktop, navigateurs -├── admin.py API REST /api/* des lanceurs (jeton en en-tête Authorization: Bearer) +├── admin.py API REST /api/* des lanceurs et des lecteurs de fichiers (jeton en en-tête Authorization: Bearer) +├── media.py MediaLibrary : dossiers autorisés, parcours sûr (.., liens, cachés, extensions), MediaApi (GET /api/media) +├── restapi.py json_response / error_response / bearer_authorized, partagés par admin.py et media.py ├── qrpage.py page HTML locale des QR codes (`make qr`) : qrencode -t SVG, images en data: URI, écrite en 600 dans $XDG_RUNTIME_DIR ├── tls.py HTTPS local : CA privée + certificat serveur (CLI openssl), contexte SSL, enable_tls() └── web/ - ├── index.html sprite SVG + 4 panneaux (player + audio, remote, launchers, system) + ├── index.html sprite SVG + 5 panneaux (player + audio, media, remote, launchers, system) ├── admin.html page /admin : liste + dialogue d'édition des lanceurs └── static/common.js Storage, I18n(messages), $ : partagés par app.js et admin.js - static/admin.js Api, LauncherList, LauncherForm, AdminPage ; static/admin.css + static/admin.js Api, AppPicker, LauncherList, LauncherForm, OpenerList, OpenerForm, AdminPage ; static/admin.css static/app.js Throttle, Connection, PlayerPanel, AudioPanel, - Touchpad, RemotePanel, HoldButton, LauncherPanel, SystemPanel, Toast, Tabs, ScreenKeeper, App + Touchpad, RemotePanel, HoldButton, LauncherPanel, MediaPanel, SystemPanel, Toast, Tabs, ScreenKeeper, App ``` Tout tourne dans **une seule boucle asyncio**. Le service doit s'exécuter **dans la session graphique** @@ -77,6 +79,7 @@ Client → serveur : {"domain": "input", "action": "text", "text": "<1 à 500 caractères>"} // features.text_input ; erreur renvoyée pour les caractères impossibles {"domain": "system", "action": "power", "kind": "suspend|reboot|poweroff"} {"domain": "system", "action": "lock|launch", "id": ""} +{"domain": "system", "action": "open_media", "root": "", "path": ""} ``` - `seek` est relatif, en µs. Le client l'utilise de préférence à `set_position`, qui exige un trackid valide. @@ -86,6 +89,7 @@ Serveur → client : - `{"type":"state", "mpris":{players, active, available}, "audio":{…}, "input":{available, reason, keys, text:{available, reason}}, "system":{…}}` - `{"type":"error","message":"…"}` +- `system.media = {roots:[{id, name}]}` : dossiers de l'onglet Médias (vide = onglet masqué côté client). ### API REST des lanceurs (`admin.enabled`) @@ -105,6 +109,17 @@ GET /admin page HTML (publique comme /, l'API exige le jeton 403 pour une commande brute si `admin.allow_custom_command` est faux (sauf commande existante inchangée : renommage OK). Chaque modification déclenche `notify()` : les tablettes reçoivent la nouvelle liste (`system.launchers`, `system.admin`) sans recharger. +### API REST des médias + +``` +GET /api/media?root=&path= {root, path, parent (null au sommet d'une racine), entries:[{name, kind: dir|audio|image|video}], truncated} + 400 chemin invalide / hors racine / pas un dossier ; 404 racine ou dossier inconnu ; route absente si aucun dossier +GET /api/media/openers {openers:[{type, kind, spec}] (audio, image, video), kinds:[default,app,command], allow_command} (admin.enabled) +PUT /api/media/openers/{type} {kind, spec} → 200 ; 404 type inconnu ; 403 commande brute sans allow_custom_command (admin.enabled) +``` + +`kind`/`spec` des lecteurs : `default` → `{}` (`gio open `) ; `app` → `{desktop_id}` (`gio launch `) ; `command` → `{argv}` (le chemin est ajouté en **dernier argument**). + La pochette locale (`file://`) n'est jamais transmise : elle est remplacée par `/art?player=…&v=`, auquel le client ajoute le jeton. ## Décisions et raisons @@ -127,6 +142,9 @@ La pochette locale (`file://`) n'est jamais transmise : elle est remplacée par - Les proxies dbus-next sont typés `Any`, car leurs méthodes `call_*` / `on_*` sont générées dynamiquement. - **Page des QR codes = fichier local, jamais une route du serveur** (`make qr`, `--qr`, module `qrpage.py`) : les QR codes contiennent le jeton, une route `/qr` le donnerait à tout le LAN (même limitée à la boucle locale, elle serait fragile derrière un proxy). Page autonome écrite en 600 dans `$XDG_RUNTIME_DIR/mediadeck/access.html` (tmpfs) et ouverte en `file://` : pas de CSP à contourner (`