"""Dry-run planning for ImageStudio asset pack imports.""" from __future__ import annotations from pathlib import Path from typing import Any from backend.dto.common import ApiError, ErrorCode from core.config_service import ConfigService from core.storage import VfsStorage _MANIFEST_FILENAMES = {"manifest.json", "imagestudio-pack.json", "pack.json"} class AssetImportService: def __init__(self, config_service: ConfigService | None = None): self.storage = VfsStorage(config_service) def plan_import_pack(self, package_uri: str, *, dry_run: bool = True) -> dict[str, Any]: if not dry_run: raise ApiError( ErrorCode.VALIDATION_FAILED, "asset pack import execution is not enabled; run with dry_run=true", {"field": "dry_run"}, ) root, relative = self.storage.split_uri(package_uri) package_path = self.storage.resolve_uri(package_uri) if not package_path.exists(): raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "asset pack not found", {"uri": package_uri}) files, total_bytes = _path_file_stats(package_path) return { "dry_run": True, "status": "planned", "package_uri": package_uri, "files": files, "bytes": total_bytes, "manifest_candidates": _manifest_candidates(root, relative, package_path), "import_enabled": False, "execution_enabled": False, } def _manifest_candidates(root: str, relative: Path, package_path: Path) -> list[str]: if package_path.is_file(): return [_storage_uri(root, relative)] if package_path.name in _MANIFEST_FILENAMES else [] candidates: list[str] = [] for child in package_path.rglob("*"): if child.is_file() and child.name in _MANIFEST_FILENAMES: child_relative = relative / child.relative_to(package_path) candidates.append(_storage_uri(root, child_relative)) return sorted(candidates) def _storage_uri(root: str, relative: Path) -> str: return f"storage://{root}/{relative.as_posix()}" def _path_file_stats(path: Path) -> tuple[int, int]: if path.is_file(): return 1, path.stat().st_size files = 0 total = 0 for child in path.rglob("*"): if child.is_file(): files += 1 total += child.stat().st_size return files, total