72 行
2.5 KiB
Python
72 行
2.5 KiB
Python
"""Storage route handlers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.routes.params import as_bool
|
|
from backend.services.media_service import MediaService
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
from core.audit_service import AuditService
|
|
from core.identity import RequestContext
|
|
from core.security import require_authenticated, require_config_manager
|
|
|
|
|
|
def get_storage_status(
|
|
ctx: RequestContext,
|
|
service: MediaService | None = None,
|
|
) -> dict:
|
|
media_service = service or MediaService()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
return media_service.storage_status()
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def migrate_storage(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any],
|
|
service: MediaService | None = None,
|
|
audit_service: AuditService | None = None,
|
|
) -> dict:
|
|
media_service = service or MediaService()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_config_manager(ctx)
|
|
source_uri = str(payload.get("source_uri") or "").strip()
|
|
target_uri = str(payload.get("target_uri") or "").strip()
|
|
if not source_uri:
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "source_uri is required", {"field": "source_uri"})
|
|
if not target_uri:
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "target_uri is required", {"field": "target_uri"})
|
|
dry_run = as_bool(payload.get("dry_run"), default=True)
|
|
allow_overwrite = as_bool(payload.get("allow_overwrite"), default=False)
|
|
result = media_service.plan_storage_migration(
|
|
source_uri,
|
|
target_uri,
|
|
dry_run=dry_run,
|
|
allow_overwrite=allow_overwrite,
|
|
)
|
|
# A non-dry-run migrate copies arbitrary trees between storage roots and
|
|
# can overwrite existing data; it was previously the only destructive
|
|
# config-tier operation with no audit trail at all.
|
|
if not dry_run:
|
|
(audit_service or AuditService()).record(
|
|
ctx=ctx,
|
|
action="storage.migrate",
|
|
target_type="storage",
|
|
target_id=target_uri,
|
|
metadata={
|
|
"source_uri": source_uri,
|
|
"target_uri": target_uri,
|
|
"files": result.get("files"),
|
|
"bytes": result.get("bytes"),
|
|
"overwrote": bool(result.get("will_overwrite")),
|
|
},
|
|
)
|
|
return result
|
|
|
|
return handle_request(ctx, action)
|