From 35b4ab542575c9b75940b200791fd8ce519edae8 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Mon, 6 Jul 2026 03:18:33 +0800 Subject: [PATCH] =?UTF-8?q?S97=20=E5=AA=92=E4=BD=93=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/route_registry.py | 1 + backend/routes/media_routes.py | 33 ++++++++++++ backend/services/media_index_service.py | 15 ++++++ core/storage.py | 36 +++++++++++++ tests/test_media_index_service.py | 68 +++++++++++++++++++++++++ tests/test_route_registry.py | 1 + 6 files changed, 154 insertions(+) create mode 100644 backend/services/media_index_service.py create mode 100644 tests/test_media_index_service.py diff --git a/backend/route_registry.py b/backend/route_registry.py index c56afa1..00e046e 100644 --- a/backend/route_registry.py +++ b/backend/route_registry.py @@ -92,6 +92,7 @@ ROUTES: tuple[RouteSpec, ...] = ( RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"), RouteSpec("POST", "storage/migrate", "backend.routes.storage_routes.migrate_storage", "config"), RouteSpec("GET", "media/sign", "backend.routes.media_routes.sign_media", "read"), + RouteSpec("GET", "media/index", "backend.routes.media_routes.index_media", "read"), RouteSpec("GET", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"), RouteSpec("GET", "material-categories", "backend.routes.material_routes.list_material_categories", "read"), RouteSpec("POST", "material-categories", "backend.routes.material_routes.create_material_category", "materials"), diff --git a/backend/routes/media_routes.py b/backend/routes/media_routes.py index 9237030..dd4aff3 100644 --- a/backend/routes/media_routes.py +++ b/backend/routes/media_routes.py @@ -6,6 +6,7 @@ from typing import Any from backend.dto.common import ApiError, ErrorCode from backend.routes.common import handle_request +from backend.services.media_index_service import MediaIndexService from backend.services.media_service import MediaService from core.identity import RequestContext from core.security import require_authenticated @@ -42,3 +43,35 @@ def fetch_media( return media_service.fetch_media(token) return handle_request(ctx, action) + + +def index_media( + ctx: RequestContext, + query: dict[str, Any] | None = None, + service: MediaIndexService | None = None, +) -> dict: + params = query or {} + index_service = service or MediaIndexService(MediaService()) + + def action() -> dict[str, Any]: + require_authenticated(ctx) + return index_service.index_files( + root=_optional_str(params.get("root")), + limit=_int_or_default(params.get("limit"), 500), + ) + + return handle_request(ctx, action) + + +def _optional_str(value: Any) -> str | None: + if value is None: + return None + text = str(value).strip() + return text or None + + +def _int_or_default(value: Any, default: int) -> int: + try: + return int(value) + except (TypeError, ValueError): + return default diff --git a/backend/services/media_index_service.py b/backend/services/media_index_service.py new file mode 100644 index 0000000..d8d80d8 --- /dev/null +++ b/backend/services/media_index_service.py @@ -0,0 +1,15 @@ +"""Media file index service.""" + +from __future__ import annotations + +from typing import Any + +from backend.services.media_service import MediaService + + +class MediaIndexService: + def __init__(self, media_service: MediaService): + self.media_service = media_service + + def index_files(self, *, root: str | None = None, limit: int = 500) -> dict[str, Any]: + return self.media_service.storage.index_files(root=root, limit=limit) diff --git a/core/storage.py b/core/storage.py index adb4f92..0381f90 100644 --- a/core/storage.py +++ b/core/storage.py @@ -4,6 +4,7 @@ from __future__ import annotations import shutil from dataclasses import dataclass +from datetime import datetime, timezone from pathlib import Path from typing import Any @@ -61,6 +62,41 @@ class VfsStorage: "execution_enabled": True, } + def index_files(self, *, root: str | None = None, limit: int = 500) -> dict[str, Any]: + roots = self._root_paths() + if root and root not in roots: + raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "unknown storage root", {"root": root}) + selected_roots = [root] if root else sorted(roots) + normalized_limit = min(max(int(limit or 500), 1), 5000) + items: list[dict[str, Any]] = [] + total = 0 + for root_name in selected_roots: + root_path = _resolve_data_path(roots[root_name]).resolve() + if not root_path.exists(): + continue + for path in sorted(root_path.rglob("*")): + if not path.is_file(): + continue + total += 1 + if len(items) >= normalized_limit: + continue + relative = path.relative_to(root_path).as_posix() + items.append( + { + "root": root_name, + "uri": f"storage://{root_name}/{relative}", + "bytes": path.stat().st_size, + "modified_at": datetime.fromtimestamp(path.stat().st_mtime, timezone.utc).isoformat(), + } + ) + return { + "root": root, + "limit": normalized_limit, + "total": total, + "truncated": total > len(items), + "items": items, + } + def resolve_uri(self, uri: str) -> Path: root, relative = self.split_uri(uri) root_path = self._root_paths().get(root) diff --git a/tests/test_media_index_service.py b/tests/test_media_index_service.py new file mode 100644 index 0000000..a8be6ec --- /dev/null +++ b/tests/test_media_index_service.py @@ -0,0 +1,68 @@ +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path + +from backend.routes.media_routes import index_media +from backend.services.media_index_service import MediaIndexService +from backend.services.media_service import MediaService +from core.config_service import ConfigService +from core.identity import RequestContext + + +class MediaIndexServiceTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.root = Path(self.tmp.name) + self.config_path = self.root / "config.yaml" + self.config_path.write_text( + f""" +storage: + images_path: "{(self.root / "images").as_posix()}" + thumbnails_path: "{(self.root / "thumbnails").as_posix()}" + materials_path: "{(self.root / "materials").as_posix()}" + styles_path: "{(self.root / "styles").as_posix()}" + archives_path: "{(self.root / "archives").as_posix()}" + temp_path: "{(self.root / "temp").as_posix()}" +""", + encoding="utf-8", + ) + for folder in ("images", "thumbnails", "materials", "styles", "archives", "temp"): + (self.root / folder).mkdir(parents=True, exist_ok=True) + (self.root / "images" / "a.png").write_bytes(b"aaa") + (self.root / "images" / "nested").mkdir() + (self.root / "images" / "nested" / "b.png").write_bytes(b"bb") + (self.root / "materials" / "mat.png").write_bytes(b"m") + self.media_service = MediaService(ConfigService(self.config_path)) + self.index_service = MediaIndexService(self.media_service) + self.ctx = RequestContext.from_astrbot(username="admin", user_id="user_001") + + def tearDown(self): + self.tmp.cleanup() + + def test_index_files_returns_storage_uris_without_local_paths(self): + index = self.index_service.index_files(root="images") + + self.assertEqual(index["total"], 2) + self.assertFalse(index["truncated"]) + self.assertEqual( + [item["uri"] for item in index["items"]], + ["storage://images/a.png", "storage://images/nested/b.png"], + ) + self.assertEqual(index["items"][0]["bytes"], 3) + self.assertNotIn(str(self.root), str(index)) + + def test_index_media_route_requires_authentication_and_honors_limit(self): + response = index_media(self.ctx, {"limit": "1"}, self.index_service) + anonymous = index_media(RequestContext(), {}, self.index_service) + + self.assertTrue(response["ok"]) + self.assertEqual(len(response["data"]["items"]), 1) + self.assertTrue(response["data"]["truncated"]) + self.assertFalse(anonymous["ok"]) + self.assertEqual(anonymous["error_code"], "UNAUTHORIZED") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_route_registry.py b/tests/test_route_registry.py index 884714b..195c9e3 100644 --- a/tests/test_route_registry.py +++ b/tests/test_route_registry.py @@ -57,6 +57,7 @@ class RouteRegistryTests(unittest.TestCase): self.assertIn(("GET", "settings/snapshots/{snapshot_id}"), keys) self.assertIn(("POST", "settings/rollback"), keys) self.assertIn(("POST", "storage/migrate"), keys) + self.assertIn(("GET", "media/index"), keys) self.assertIn(("GET", "events/stream"), keys) self.assertIn(("GET", "costs/usage"), keys) self.assertIn(("POST", "costs/guard"), keys)