2026-07-05 19:28:30 +08:00
|
|
|
"""Asset import route handlers."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
|
|
|
from backend.routes.common import handle_request
|
2026-07-11 21:50:34 +08:00
|
|
|
from backend.routes.params import as_bool
|
2026-07-05 19:28:30 +08:00
|
|
|
from backend.services.asset_import_service import AssetImportService
|
2026-07-05 21:25:50 +08:00
|
|
|
from backend.services.material_service import MaterialService
|
|
|
|
|
from core.audit_service import AuditService
|
2026-07-05 19:28:30 +08:00
|
|
|
from core.identity import RequestContext
|
|
|
|
|
from core.security import require_permission
|
2026-07-05 21:25:50 +08:00
|
|
|
from database.db_helper import Database, load_database_settings
|
|
|
|
|
from database.repositories.material_repository import MaterialRepository
|
2026-07-05 19:28:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def import_asset_pack(
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
payload: dict[str, Any],
|
|
|
|
|
service: AssetImportService | None = None,
|
|
|
|
|
) -> dict:
|
2026-07-05 21:25:50 +08:00
|
|
|
asset_service = service or _create_asset_import_service()
|
2026-07-05 19:28:30 +08:00
|
|
|
|
|
|
|
|
def action() -> dict[str, Any]:
|
|
|
|
|
require_permission(ctx, "can_manage_materials")
|
|
|
|
|
package_uri = str(payload.get("package_uri") or "").strip()
|
|
|
|
|
if not package_uri:
|
|
|
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "package_uri is required", {"field": "package_uri"})
|
2026-07-05 21:25:50 +08:00
|
|
|
return asset_service.plan_import_pack(
|
|
|
|
|
package_uri,
|
2026-07-11 21:50:34 +08:00
|
|
|
dry_run=as_bool(payload.get("dry_run"), default=True),
|
2026-07-05 21:25:50 +08:00
|
|
|
ctx=ctx,
|
|
|
|
|
)
|
2026-07-05 19:28:30 +08:00
|
|
|
|
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
|
|
|
|
|
2026-07-05 21:25:50 +08:00
|
|
|
def _create_asset_import_service() -> AssetImportService:
|
|
|
|
|
database = Database(load_database_settings())
|
|
|
|
|
return AssetImportService(
|
|
|
|
|
material_service=MaterialService(
|
|
|
|
|
repository=MaterialRepository(database),
|
|
|
|
|
audit_service=AuditService(database),
|
|
|
|
|
)
|
|
|
|
|
)
|