astrbot_plugin_imagestudio/core/runtime_paths.py

143 行
4.3 KiB
Python

"""Resolve writable ImageStudio paths for local and AstrBot runtimes."""
from __future__ import annotations
import os
from pathlib import Path
from typing import Callable
PLUGIN_NAME = "astrbot_plugin_imagestudio"
REPO_ROOT = Path(__file__).resolve().parents[1]
# Opt-in escape hatch for deployments that genuinely mount storage outside the
# runtime data root (e.g. a NAS volume). Deliberately an environment variable
# rather than a config key: ``settings/save`` is reachable by any authenticated
# user, so a config-based flag could be flipped by the same actor it defends
# against.
EXTERNAL_ROOTS_ENV_VAR = "IMAGESTUDIO_ALLOW_EXTERNAL_STORAGE_ROOTS"
def external_storage_roots_allowed() -> bool:
"""Whether configured storage roots may resolve outside the data root."""
return str(os.environ.get(EXTERNAL_ROOTS_ENV_VAR, "")).strip().lower() in {
"1",
"true",
"yes",
"on",
}
def is_within_runtime_data_root(path: str | Path) -> bool:
"""Whether ``path`` resolves inside this runtime's writable data root."""
root = get_runtime_data_root().resolve()
try:
candidate = resolve_data_path(path).resolve()
except OSError:
return False
return candidate == root or root in candidate.parents
def get_astrbot_runtime_data_root() -> Path | None:
"""Return this plugin's AstrBot-managed data directory when available."""
getter = _astrbot_plugin_data_path_getter()
if getter is None:
return None
try:
root = getter()
except Exception:
return None
if root is None or not str(root).strip():
return None
return Path(root).expanduser() / PLUGIN_NAME
def get_runtime_data_root() -> Path:
"""Return the root used for mutable data in the current runtime."""
return get_astrbot_runtime_data_root() or (REPO_ROOT / "data")
def resolve_data_path(path: str | Path) -> Path:
"""Resolve a configured mutable-data path without changing absolute paths.
The repository configuration historically uses ``data/...``. Under
AstrBot, the leading ``data`` component is replaced by AstrBot's
plugin-specific writable directory. Local development keeps the existing
``REPO_ROOT/data/...`` layout.
"""
resolved = Path(path).expanduser()
if resolved.is_absolute():
return resolved
astrbot_root = get_astrbot_runtime_data_root()
if astrbot_root is None:
return REPO_ROOT / resolved
parts = resolved.parts
if parts and parts[0].lower() == "data":
resolved = Path(*parts[1:]) if len(parts) > 1 else Path()
return astrbot_root / resolved
def get_default_config_path() -> Path:
"""Return the writable default configuration path for this runtime."""
astrbot_root = get_astrbot_runtime_data_root()
if astrbot_root is not None:
return astrbot_root / "config.yaml"
return REPO_ROOT / "config.yaml"
def resolve_config_path(path: str | Path = "config.yaml") -> Path:
"""Resolve default config to writable storage while preserving explicit paths."""
resolved = Path(path).expanduser()
if resolved.is_absolute():
return resolved
if _is_default_config_reference(resolved):
return get_default_config_path()
return REPO_ROOT / resolved
def is_default_config_path(path: str | Path) -> bool:
"""Whether a resolved path is the runtime's default writable config."""
return _same_path(Path(path), get_default_config_path())
def get_packaged_config_path() -> Path:
"""Return the read-only configuration defaults shipped with the plugin."""
return REPO_ROOT / "config.yaml"
def resolve_plugin_path(path: str | Path) -> Path:
"""Resolve a plugin resource path relative to the installed package."""
resolved = Path(path).expanduser()
return resolved if resolved.is_absolute() else REPO_ROOT / resolved
def _astrbot_plugin_data_path_getter() -> Callable[[], str | Path] | None:
try:
from astrbot.core.utils.astrbot_path import get_astrbot_plugin_data_path
except Exception:
return None
return get_astrbot_plugin_data_path
def _is_default_config_reference(path: Path) -> bool:
return path == Path("config.yaml")
def _same_path(left: Path, right: Path) -> bool:
try:
return left.resolve() == right.resolve()
except OSError:
return left.absolute() == right.absolute()