Spaces:
Running

Bot / storage.py
ddosvn's picture
Initial deploy of Panel ThTuanIT full stack and Lighthouse-optimized frontend (part 2)
c06c76a verified
Raw History Blame Contribute Delete
2.8 kB
"""
Shared storage layer — zone metadata and path utilities.
Single Responsibility: only handles metadata persistence and path resolution.
Dependency Inversion: routers depend on these abstractions instead of importing each other.
"""
import json
import os
from pathlib import Path
from config import DATA_DIR, ZONES_META, ZONE_NAME_PATTERN
_meta_cache: dict = {}
_meta_mtime: float = 0.0
def load_meta() -> dict:
"""Load zones metadata from JSON file with fast in-memory mtime cache."""
global _meta_cache, _meta_mtime
if ZONES_META.exists():
try:
mtime = os.path.getmtime(ZONES_META)
if mtime != _meta_mtime:
_meta_cache = json.loads(ZONES_META.read_text(encoding="utf-8"))
_meta_mtime = mtime
return _meta_cache
except Exception:
return _meta_cache
return {}
def save_meta(meta: dict):
"""Save zones metadata to JSON file atomically and update memory cache."""
global _meta_cache, _meta_mtime
temp_file = ZONES_META.with_suffix(".tmp")
temp_file.write_text(json.dumps(meta, indent=2, default=str), encoding="utf-8")
temp_file.replace(ZONES_META)
_meta_cache = meta
try:
_meta_mtime = os.path.getmtime(ZONES_META)
except Exception:
pass
def validate_zone_name(name: str):
"""Validate zone name format. Raises ValueError if invalid."""
if not ZONE_NAME_PATTERN.match(name):
raise ValueError("Tên zone chỉ chứa a-z, A-Z, 0-9, _, - (tối đa 50 ký tự)")
def get_zone_path(name: str) -> Path:
"""Get the filesystem path for a zone, validating it exists."""
validate_zone_name(name)
zone_path = DATA_DIR / name
if not zone_path.is_dir():
raise ValueError(f"Zone '{name}' không tồn tại")
return zone_path
def safe_path(zone_path: Path, rel_path: str) -> Path:
"""Resolve a relative path within a zone, preventing path traversal."""
cleaned = rel_path.lstrip("/\\")
target = (zone_path / cleaned).resolve()
zone_resolved = zone_path.resolve()
if target != zone_resolved and not str(target).startswith(str(zone_resolved) + os.sep):
raise ValueError("Truy cập ngoài zone không được phép")
return target
def check_zone_owner(zone_name: str, user_sub: str, user_role: str):
"""Check that the user owns the zone. Admins can access all zones."""
if user_role == "admin":
return
meta = load_meta()
info = meta.get(zone_name)
if not info:
raise ValueError(f"Zone '{zone_name}' không tồn tại")
if info.get("owner_id") != user_sub:
raise ValueError("Bạn không có quyền truy cập zone này")