Resolve WebUI static assets
这个提交存在于:
父节点
100367c989
当前提交
7306649f49
共有 2 个文件被更改,包括 73 次插入 和 1 次删除
|
|
@ -7,6 +7,9 @@ without starting a standalone server.
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ErrorCode, error_response
|
||||
from backend.services.web_hosting_service import WebHostingService
|
||||
from backend.route_registry import route_manifest
|
||||
|
||||
|
|
@ -15,3 +18,39 @@ def get_web_mount_manifest() -> dict:
|
|||
manifest = WebHostingService().manifest()
|
||||
manifest["api_routes"] = route_manifest()
|
||||
return manifest
|
||||
|
||||
|
||||
def resolve_web_asset(path: str, service: WebHostingService | None = None) -> dict[str, Any]:
|
||||
web_service = service or WebHostingService()
|
||||
relative_path = _standalone_relative_path(path)
|
||||
if relative_path is None:
|
||||
return error_response(
|
||||
error_code=ErrorCode.RESOURCE_NOT_FOUND,
|
||||
message="web asset route not found",
|
||||
details={"path": path},
|
||||
)
|
||||
try:
|
||||
metadata = web_service.resolve_standalone_asset(relative_path)
|
||||
except Exception as exc:
|
||||
from backend.dto.common import response_from_exception
|
||||
|
||||
return response_from_exception(exc)
|
||||
return {
|
||||
"ok": True,
|
||||
"data": {
|
||||
**metadata,
|
||||
"relative_path": relative_path,
|
||||
},
|
||||
"warnings": [],
|
||||
"request_id": None,
|
||||
}
|
||||
|
||||
|
||||
def _standalone_relative_path(path: str) -> str | None:
|
||||
normalized = str(path or "").split("?", 1)[0].replace("\\", "/").strip("/")
|
||||
mount = "api/v1/plugins/extensions/astrbot_plugin_imagestudio/web"
|
||||
if normalized == mount:
|
||||
return "index.html"
|
||||
if normalized.startswith(f"{mount}/"):
|
||||
return normalized[len(mount) + 1 :] or "index.html"
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from adapters.astrbot_adapter import (
|
||||
dispatch_extension_request,
|
||||
|
|
@ -9,8 +11,9 @@ from adapters.astrbot_adapter import (
|
|||
resolve_route,
|
||||
runtime_route_table,
|
||||
)
|
||||
from adapters.webui_adapter import get_web_mount_manifest
|
||||
from adapters.webui_adapter import get_web_mount_manifest, resolve_web_asset
|
||||
from backend.route_registry import ROUTES, assert_unique_routes, route_manifest
|
||||
from backend.services.web_hosting_service import WebHostingService
|
||||
from core.identity import RequestContext
|
||||
|
||||
|
||||
|
|
@ -58,6 +61,36 @@ class RouteRegistryTests(unittest.TestCase):
|
|||
self.assertIn("standalone_web", manifest)
|
||||
self.assertTrue(any(route["endpoint"] == "dashboard/summary" for route in manifest["api_routes"]))
|
||||
|
||||
def test_webui_adapter_resolves_standalone_assets(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
web = root / "web"
|
||||
assets = web / "assets"
|
||||
assets.mkdir(parents=True)
|
||||
(web / "index.html").write_text("<html></html>", encoding="utf-8")
|
||||
(assets / "app.js").write_text("console.log('ok')", encoding="utf-8")
|
||||
service = WebHostingService(plugin_page_dir=root / "plugin", standalone_web_dir=web)
|
||||
|
||||
index = resolve_web_asset(
|
||||
"/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web",
|
||||
service,
|
||||
)
|
||||
script = resolve_web_asset(
|
||||
"/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/assets/app.js?cache=1",
|
||||
service,
|
||||
)
|
||||
escaped = resolve_web_asset(
|
||||
"/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/../config.yaml",
|
||||
service,
|
||||
)
|
||||
|
||||
self.assertTrue(index["ok"])
|
||||
self.assertEqual(index["data"]["relative_path"], "index.html")
|
||||
self.assertTrue(script["ok"])
|
||||
self.assertEqual(script["data"]["content_type"], "text/javascript")
|
||||
self.assertFalse(escaped["ok"])
|
||||
self.assertEqual(escaped["error_code"], "FORBIDDEN")
|
||||
|
||||
def test_route_handlers_are_importable_callables(self):
|
||||
for route in ROUTES:
|
||||
module_name, function_name = route.handler.rsplit(".", 1)
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用