astrbot_plugin_imagestudio/core/storage.py

176 行
6.1 KiB
Python

2026-07-05 09:14:32 +08:00
"""VFS storage helpers."""
from __future__ import annotations
2026-07-05 21:46:07 +08:00
import shutil
2026-07-05 09:14:32 +08:00
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from backend.dto.common import ApiError, ErrorCode
from core.config_service import ConfigService, ImageStudioConfig
from database.db_helper import REPO_ROOT
@dataclass(frozen=True)
class VfsEntry:
root: str
uri_prefix: str
exists: bool
writable: bool
bytes: int
def to_dict(self) -> dict[str, Any]:
return {
"root": self.root,
"uri_prefix": self.uri_prefix,
"exists": self.exists,
"writable": self.writable,
"bytes": self.bytes,
}
class VfsStorage:
def __init__(self, config_service: ConfigService | None = None):
self.config_service = config_service or ConfigService()
def status(self) -> dict[str, Any]:
roots = [self._entry(root, path) for root, path in self._root_paths().items()]
return {
"roots": [entry.to_dict() for entry in roots],
"total_bytes": sum(entry.bytes for entry in roots),
}
2026-07-05 18:54:05 +08:00
def migration_plan(self, source_uri: str, target_uri: str, *, dry_run: bool = True) -> dict[str, Any]:
source_path = self.resolve_uri(source_uri)
target_path = self.resolve_uri(target_uri)
2026-07-05 18:54:05 +08:00
if not source_path.exists():
raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "source media not found", {"uri": source_uri})
files, total_bytes = _path_file_stats(source_path)
2026-07-05 21:46:07 +08:00
will_overwrite = target_path.exists()
if not dry_run:
_copy_path(source_path, target_path)
2026-07-05 18:54:05 +08:00
return {
2026-07-05 21:46:07 +08:00
"dry_run": dry_run,
"status": "planned" if dry_run else "completed",
2026-07-05 18:54:05 +08:00
"source_uri": source_uri,
"target_uri": target_uri,
"files": files,
"bytes": total_bytes,
2026-07-05 21:46:07 +08:00
"will_overwrite": will_overwrite,
"execution_enabled": True,
2026-07-05 18:54:05 +08:00
}
2026-07-05 09:14:32 +08:00
def resolve_uri(self, uri: str) -> Path:
root, relative = self.split_uri(uri)
root_path = self._root_paths().get(root)
if root_path is None:
raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "unknown storage root", {"uri": uri, "root": root})
resolved_root = _resolve_data_path(root_path).resolve()
resolved_path = (resolved_root / relative).resolve()
if resolved_path != resolved_root and resolved_root not in resolved_path.parents:
raise ApiError(ErrorCode.FORBIDDEN, "storage uri escapes its root", {"uri": uri})
return resolved_path
def ensure_file(self, uri: str) -> Path:
path = self.resolve_uri(uri)
if not path.exists() or not path.is_file():
raise ApiError(ErrorCode.MEDIA_NOT_FOUND, "media file not found", {"uri": uri})
return path
def split_uri(self, uri: str) -> tuple[str, Path]:
if not uri.startswith("storage://"):
raise ApiError(ErrorCode.VALIDATION_FAILED, "uri must use storage://", {"field": "uri"})
rest = uri[len("storage://") :]
if "/" not in rest:
raise ApiError(ErrorCode.VALIDATION_FAILED, "storage uri must include a root and path", {"uri": uri})
root, relative = rest.split("/", 1)
if not root or not relative:
raise ApiError(ErrorCode.VALIDATION_FAILED, "storage uri must include a root and path", {"uri": uri})
relative_path = Path(relative)
if relative_path.is_absolute() or ".." in relative_path.parts:
raise ApiError(ErrorCode.FORBIDDEN, "storage uri contains an unsafe path", {"uri": uri})
return root, relative_path
def _entry(self, root: str, path: str) -> VfsEntry:
resolved = _resolve_data_path(path)
return VfsEntry(
root=root,
uri_prefix=f"storage://{root}/",
exists=resolved.exists(),
writable=_is_writable(resolved),
bytes=_directory_size(resolved),
)
def _root_paths(self) -> dict[str, str]:
config = self.config_service.load()
return _storage_roots(config)
def _storage_roots(config: ImageStudioConfig) -> dict[str, str]:
return {
"images": config.storage.images_path,
"thumbnails": config.storage.thumbnails_path,
"materials": config.storage.materials_path,
"styles": config.storage.styles_path,
"archives": config.storage.archives_path,
"temp": config.storage.temp_path,
}
def _resolve_data_path(path: str) -> Path:
resolved = Path(path)
if not resolved.is_absolute():
resolved = REPO_ROOT / resolved
return resolved
def _directory_size(path: Path) -> int:
if not path.exists():
return 0
total = 0
for child in path.rglob("*"):
if child.is_file():
total += child.stat().st_size
return total
2026-07-05 18:54:05 +08:00
def _path_file_stats(path: Path) -> tuple[int, int]:
if path.is_file():
return 1, path.stat().st_size
if path.is_dir():
files = 0
total = 0
for child in path.rglob("*"):
if child.is_file():
files += 1
total += child.stat().st_size
return files, total
return 0, 0
2026-07-05 21:46:07 +08:00
def _copy_path(source_path: Path, target_path: Path) -> None:
if source_path.is_file():
target_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source_path, target_path)
return
source_root = source_path.resolve()
target_root = target_path.resolve()
if target_root == source_root or source_root in target_root.parents:
raise ApiError(ErrorCode.FORBIDDEN, "target path cannot be inside source directory")
target_root.mkdir(parents=True, exist_ok=True)
for child in source_root.rglob("*"):
if not child.is_file():
continue
relative = child.relative_to(source_root)
destination = target_root / relative
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(child, destination)
2026-07-05 09:14:32 +08:00
def _is_writable(path: Path) -> bool:
target = path if path.exists() else path.parent
return target.exists() and target.is_dir()