image file info toegevoegd bij CMD+ENTER

This commit is contained in:
kodi
2026-03-13 11:37:27 +01:00
parent 05569576a7
commit 018c3dcd94
18 changed files with 726 additions and 0 deletions
@@ -0,0 +1,102 @@
from __future__ import annotations
import asyncio
import sys
import tempfile
import unittest
from pathlib import Path
import httpx
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
from backend.app.dependencies import get_file_ops_service
from backend.app.fs.filesystem_adapter import FilesystemAdapter
from backend.app.main import app
from backend.app.security.path_guard import PathGuard
from backend.app.services.file_ops_service import FileOpsService
PNG_1X1 = (
b"\x89PNG\r\n\x1a\n"
b"\x00\x00\x00\rIHDR"
b"\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00"
b"\x90wS\xde"
b"\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00"
b"\xc9\xfe\x92\xef"
b"\x00\x00\x00\x00IEND\xaeB`\x82"
)
class ImageApiGoldenTest(unittest.TestCase):
def setUp(self) -> None:
self.temp_dir = tempfile.TemporaryDirectory()
self.root = Path(self.temp_dir.name) / "root"
self.root.mkdir(parents=True, exist_ok=True)
path_guard = PathGuard({"storage1": str(self.root)})
service = FileOpsService(path_guard=path_guard, filesystem=FilesystemAdapter())
async def _override_file_ops_service() -> FileOpsService:
return service
app.dependency_overrides[get_file_ops_service] = _override_file_ops_service
def tearDown(self) -> None:
app.dependency_overrides.clear()
self.temp_dir.cleanup()
def _request(self, path: str) -> httpx.Response:
async def _run() -> httpx.Response:
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client:
return await client.get("/api/files/image", params={"path": path})
return asyncio.run(_run())
def test_image_endpoint_success(self) -> None:
(self.root / "sample.png").write_bytes(PNG_1X1)
response = self._request("storage1/sample.png")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["content-type"], "image/png")
self.assertEqual(response.headers["content-length"], str(len(PNG_1X1)))
self.assertEqual(response.content, PNG_1X1)
def test_image_directory_type_conflict(self) -> None:
(self.root / "images").mkdir()
response = self._request("storage1/images")
self.assertEqual(response.status_code, 409)
self.assertEqual(response.json()["error"]["code"], "type_conflict")
def test_image_path_not_found(self) -> None:
response = self._request("storage1/missing.png")
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json()["error"]["code"], "path_not_found")
def test_image_traversal_blocked(self) -> None:
response = self._request("storage1/../etc/passwd")
self.assertEqual(response.status_code, 403)
self.assertEqual(response.json()["error"]["code"], "path_traversal_detected")
def test_image_invalid_root_alias(self) -> None:
response = self._request("unknown/sample.png")
self.assertEqual(response.status_code, 403)
self.assertEqual(response.json()["error"]["code"], "invalid_root_alias")
def test_image_non_image_blocked(self) -> None:
(self.root / "notes.txt").write_text("hello", encoding="utf-8")
response = self._request("storage1/notes.txt")
self.assertEqual(response.status_code, 409)
self.assertEqual(response.json()["error"]["code"], "unsupported_type")
if __name__ == "__main__":
unittest.main()
@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
import base64
import sys
import tempfile
import unittest
@@ -17,6 +18,11 @@ from backend.app.security.path_guard import PathGuard
from backend.app.services.file_ops_service import FileOpsService
PNG_1X1 = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"
)
class FileInfoApiGoldenTest(unittest.TestCase):
def setUp(self) -> None:
self.temp_dir = tempfile.TemporaryDirectory()
@@ -59,6 +65,8 @@ class FileInfoApiGoldenTest(unittest.TestCase):
self.assertIn("modified", payload)
self.assertIn("owner", payload)
self.assertIn("group", payload)
self.assertIsNone(payload["width"])
self.assertIsNone(payload["height"])
def test_directory_info_success(self) -> None:
directory = self.root / "Media"
@@ -74,6 +82,20 @@ class FileInfoApiGoldenTest(unittest.TestCase):
self.assertIsNone(payload["size"])
self.assertEqual(payload["root"], "storage1")
self.assertIsNone(payload["extension"])
self.assertIsNone(payload["width"])
self.assertIsNone(payload["height"])
def test_image_info_has_width_and_height(self) -> None:
file_path = self.root / "pixel.png"
file_path.write_bytes(PNG_1X1)
response = self._request("storage1/pixel.png")
self.assertEqual(response.status_code, 200)
payload = response.json()
self.assertEqual(payload["width"], 1)
self.assertEqual(payload["height"], 1)
self.assertEqual(payload["content_type"], "image/png")
def test_info_path_not_found(self) -> None:
response = self._request("storage1/missing.txt")
@@ -113,6 +135,8 @@ class FileInfoApiGoldenTest(unittest.TestCase):
"content_type",
"owner",
"group",
"width",
"height",
},
)
@@ -62,6 +62,11 @@ class UiSmokeGoldenTest(unittest.TestCase):
self.assertIn('id="viewer-modal"', body)
self.assertIn('id="video-modal"', body)
self.assertIn('id="pdf-modal"', body)
self.assertIn('id="image-modal"', body)
self.assertIn('id="image-viewer-img"', body)
self.assertIn('id="image-zoom-in-btn"', body)
self.assertIn('id="image-zoom-out-btn"', body)
self.assertIn('id="image-reset-btn"', body)
self.assertIn('id="pdf-frame"', body)
self.assertIn('id="pdf-close-btn"', body)
self.assertIn('id="video-player"', body)
@@ -192,6 +197,12 @@ class UiSmokeGoldenTest(unittest.TestCase):
self.assertIn('function openSearch()', app_js)
self.assertIn('async function submitSearch()', app_js)
self.assertIn('async function openInfo()', app_js)
self.assertIn('function imageElements()', app_js)
self.assertIn('function isImageSelection(item)', app_js)
self.assertIn('async function openImageViewer()', app_js)
self.assertIn('function isImageOpen()', app_js)
self.assertIn("`/api/files/image?", app_js)
self.assertIn('if (isImageSelection(selected)) {', app_js)
self.assertIn('document.getElementById("info-modal")', app_js)
self.assertIn("`/api/files/info?", app_js)
self.assertIn('document.getElementById("search-input")', app_js)