S97 媒体文件索引

这个提交存在于:
YokiiroBW 2026-07-06 03:18:33 +08:00
父节点 9a23d89eb7
当前提交 35b4ab5425
共有 6 个文件被更改,包括 154 次插入 和 0 次删除

查看文件

@ -92,6 +92,7 @@ ROUTES: tuple[RouteSpec, ...] = (
RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"), RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"),
RouteSpec("POST", "storage/migrate", "backend.routes.storage_routes.migrate_storage", "config"), 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/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", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"),
RouteSpec("GET", "material-categories", "backend.routes.material_routes.list_material_categories", "read"), RouteSpec("GET", "material-categories", "backend.routes.material_routes.list_material_categories", "read"),
RouteSpec("POST", "material-categories", "backend.routes.material_routes.create_material_category", "materials"), RouteSpec("POST", "material-categories", "backend.routes.material_routes.create_material_category", "materials"),

查看文件

@ -6,6 +6,7 @@ from typing import Any
from backend.dto.common import ApiError, ErrorCode from backend.dto.common import ApiError, ErrorCode
from backend.routes.common import handle_request from backend.routes.common import handle_request
from backend.services.media_index_service import MediaIndexService
from backend.services.media_service import MediaService from backend.services.media_service import MediaService
from core.identity import RequestContext from core.identity import RequestContext
from core.security import require_authenticated from core.security import require_authenticated
@ -42,3 +43,35 @@ def fetch_media(
return media_service.fetch_media(token) return media_service.fetch_media(token)
return handle_request(ctx, action) 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

查看文件

@ -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)

查看文件

@ -4,6 +4,7 @@ from __future__ import annotations
import shutil import shutil
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
@ -61,6 +62,41 @@ class VfsStorage:
"execution_enabled": True, "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: def resolve_uri(self, uri: str) -> Path:
root, relative = self.split_uri(uri) root, relative = self.split_uri(uri)
root_path = self._root_paths().get(root) root_path = self._root_paths().get(root)

查看文件

@ -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()

查看文件

@ -57,6 +57,7 @@ class RouteRegistryTests(unittest.TestCase):
self.assertIn(("GET", "settings/snapshots/{snapshot_id}"), keys) self.assertIn(("GET", "settings/snapshots/{snapshot_id}"), keys)
self.assertIn(("POST", "settings/rollback"), keys) self.assertIn(("POST", "settings/rollback"), keys)
self.assertIn(("POST", "storage/migrate"), keys) self.assertIn(("POST", "storage/migrate"), keys)
self.assertIn(("GET", "media/index"), keys)
self.assertIn(("GET", "events/stream"), keys) self.assertIn(("GET", "events/stream"), keys)
self.assertIn(("GET", "costs/usage"), keys) self.assertIn(("GET", "costs/usage"), keys)
self.assertIn(("POST", "costs/guard"), keys) self.assertIn(("POST", "costs/guard"), keys)