mirror of
https://github.com/deunix-educ/mediadeck.git
synced 2026-11-25 04:39:42 +01:00
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/<version>/ (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 <noreply@anthropic.com>
This commit is contained in:
@@ -143,6 +143,58 @@ class AdminApiTest(AdminApiCase):
|
||||
self.assertEqual(unknown.status, 400)
|
||||
|
||||
|
||||
class OpenersApiTest(AdminApiCase):
|
||||
async def test_token_required(self) -> None:
|
||||
for method, path in (("GET", "/api/media/openers"), ("PUT", "/api/media/openers/audio")):
|
||||
for headers in ({}, {"Authorization": "Bearer nope"}, {"Authorization": TOKEN}):
|
||||
response = await self.client.request(method, path, headers=headers, json={"kind": "default"})
|
||||
self.assertEqual(response.status, 401, (method, headers))
|
||||
|
||||
async def test_list_defaults_to_the_system_application(self) -> None:
|
||||
data = await (await self.client.get("/api/media/openers", headers=AUTH)).json()
|
||||
self.assertEqual([opener["type"] for opener in data["openers"]], ["audio", "image", "video"])
|
||||
self.assertTrue(all(opener["kind"] == "default" for opener in data["openers"]))
|
||||
self.assertEqual(data["kinds"], ["default", "app", "command"])
|
||||
self.assertFalse(data["allow_command"])
|
||||
|
||||
async def test_set_and_read_back(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
(Path(tmp) / "vlc.desktop").write_text("[Desktop Entry]\nType=Application\nName=VLC\nExec=vlc\n")
|
||||
with mock.patch("mediadeck.launchers.desktop_dirs", return_value=[Path(tmp)]):
|
||||
response = await self.client.put(
|
||||
"/api/media/openers/video", headers=AUTH, json={"kind": "app", "spec": {"desktop_id": "vlc.desktop"}}
|
||||
)
|
||||
self.assertEqual(response.status, 200)
|
||||
self.assertEqual(await response.json(), {"type": "video", "kind": "app", "spec": {"desktop_id": "vlc.desktop"}})
|
||||
data = await (await self.client.get("/api/media/openers", headers=AUTH)).json()
|
||||
self.assertEqual({o["type"]: o["kind"] for o in data["openers"]}, {"audio": "default", "image": "default", "video": "app"})
|
||||
back = await self.client.put("/api/media/openers/video", headers=AUTH, json={"kind": "default"})
|
||||
self.assertEqual((await back.json())["kind"], "default")
|
||||
|
||||
async def test_errors(self) -> None:
|
||||
unknown_type = await self.client.put("/api/media/openers/document", headers=AUTH, json={"kind": "default"})
|
||||
self.assertEqual(unknown_type.status, 404)
|
||||
for body in ({"kind": "web"}, {"kind": "app", "spec": {"desktop_id": "nope.desktop"}}, {}):
|
||||
response = await self.client.put("/api/media/openers/audio", headers=AUTH, json=body)
|
||||
self.assertEqual(response.status, 400, body)
|
||||
forbidden = await self.client.put(
|
||||
"/api/media/openers/audio", headers=AUTH, json={"kind": "command", "spec": {"argv": ["mpv"]}}
|
||||
)
|
||||
self.assertEqual(forbidden.status, 403)
|
||||
self.assertTrue(all(opener.kind == "default" for opener in self.store.openers().values()))
|
||||
|
||||
|
||||
class OpenersApiCommandAllowedTest(AdminApiCase):
|
||||
allow_command = True
|
||||
|
||||
async def test_command_opener(self) -> None:
|
||||
response = await self.client.put(
|
||||
"/api/media/openers/video", headers=AUTH, json={"kind": "command", "spec": {"argv": ["mpv", "--fs"]}}
|
||||
)
|
||||
self.assertEqual(response.status, 200)
|
||||
self.assertEqual(self.store.openers()["video"].spec, {"argv": ["mpv", "--fs"]})
|
||||
|
||||
|
||||
class AdminApiCommandAllowedTest(AdminApiCase):
|
||||
allow_command = True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user