diff --git a/backend/route_registry.py b/backend/route_registry.py index a78dc24..6d3df5f 100644 --- a/backend/route_registry.py +++ b/backend/route_registry.py @@ -80,6 +80,7 @@ ROUTES: tuple[RouteSpec, ...] = ( RouteSpec("GET", "settings/snapshots/{snapshot_id}", "backend.routes.settings_routes.get_settings_snapshot", "config"), RouteSpec("POST", "settings/rollback", "backend.routes.settings_routes.rollback_settings", "config"), RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"), + RouteSpec("POST", "storage/migrate", "backend.routes.storage_routes.migrate_storage", "config"), RouteSpec("GET", "media/sign", "backend.routes.media_routes.sign_media", "read"), RouteSpec("GET", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"), RouteSpec("GET", "material-categories", "backend.routes.material_routes.list_material_categories", "read"), diff --git a/backend/routes/storage_routes.py b/backend/routes/storage_routes.py index f2db0cf..0ff3e6a 100644 --- a/backend/routes/storage_routes.py +++ b/backend/routes/storage_routes.py @@ -6,8 +6,9 @@ from typing import Any from backend.routes.common import handle_request from backend.services.media_service import MediaService +from backend.dto.common import ApiError, ErrorCode from core.identity import RequestContext -from core.security import require_authenticated +from core.security import require_authenticated, require_config_manager def get_storage_status( @@ -21,3 +22,35 @@ def get_storage_status( return media_service.storage_status() return handle_request(ctx, action) + + +def migrate_storage( + ctx: RequestContext, + payload: dict[str, Any], + service: MediaService | 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"}) + return media_service.plan_storage_migration( + source_uri, + target_uri, + dry_run=_as_bool(payload.get("dry_run"), default=True), + ) + + return handle_request(ctx, action) + + +def _as_bool(value: Any, *, default: bool = False) -> bool: + if value is None: + return default + if isinstance(value, bool): + return value + return str(value).strip().lower() in {"1", "true", "yes", "on"} diff --git a/backend/services/media_service.py b/backend/services/media_service.py index b80d8e3..cba98eb 100644 --- a/backend/services/media_service.py +++ b/backend/services/media_service.py @@ -21,6 +21,9 @@ class MediaService: def storage_status(self) -> dict[str, Any]: return self.storage.status() + def plan_storage_migration(self, source_uri: str, target_uri: str, *, dry_run: bool = True) -> dict[str, Any]: + return self.storage.migration_plan(source_uri, target_uri, dry_run=dry_run) + def sign_media(self, uri: str) -> dict[str, Any]: return self.media_proxy.sign(uri) diff --git a/core/storage.py b/core/storage.py index 9c9ee81..32d55a9 100644 --- a/core/storage.py +++ b/core/storage.py @@ -40,6 +40,29 @@ class VfsStorage: "total_bytes": sum(entry.bytes for entry in roots), } + def migration_plan(self, source_uri: str, target_uri: str, *, dry_run: bool = True) -> dict[str, Any]: + if not dry_run: + raise ApiError( + ErrorCode.VALIDATION_FAILED, + "storage migration execution is not enabled; run with dry_run=true", + {"field": "dry_run"}, + ) + source_path = self.resolve_uri(source_uri) + self.resolve_uri(target_uri) + 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) + return { + "dry_run": True, + "status": "planned", + "source_uri": source_uri, + "target_uri": target_uri, + "files": files, + "bytes": total_bytes, + "will_overwrite": False, + "execution_enabled": False, + } + def resolve_uri(self, uri: str) -> Path: root, relative = self.split_uri(uri) root_path = self._root_paths().get(root) @@ -114,6 +137,20 @@ def _directory_size(path: Path) -> int: return total +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 + + def _is_writable(path: Path) -> bool: target = path if path.exists() else path.parent return target.exists() and target.is_dir() diff --git a/tests/test_route_registry.py b/tests/test_route_registry.py index d81912d..7827531 100644 --- a/tests/test_route_registry.py +++ b/tests/test_route_registry.py @@ -36,6 +36,7 @@ class RouteRegistryTests(unittest.TestCase): self.assertIn(("GET", "settings/snapshots"), keys) self.assertIn(("GET", "settings/snapshots/{snapshot_id}"), keys) self.assertIn(("POST", "settings/rollback"), keys) + self.assertIn(("POST", "storage/migrate"), keys) self.assertIn(("GET", "events/stream"), keys) self.assertEqual(len(routes), len(ROUTES)) diff --git a/tests/test_s2_routes.py b/tests/test_s2_routes.py index fd7322c..da7ca43 100644 --- a/tests/test_s2_routes.py +++ b/tests/test_s2_routes.py @@ -52,6 +52,7 @@ from backend.routes.settings_routes import ( validate_settings, ) from backend.routes.storage_routes import get_storage_status +from backend.routes.storage_routes import migrate_storage from backend.routes.ui_routes import get_ui_actions, get_ui_bootstrap from backend.routes.web_routes import get_web_manifest from backend.routes.workflow_routes import ( @@ -622,6 +623,33 @@ database: self.assertEqual(fetched["data"]["size_bytes"], 5) self.assertNotIn(str(self.root), str(fetched["data"])) + planned = migrate_storage( + self.ctx, + { + "source_uri": "storage://images/sample.bin", + "target_uri": "storage://archives/sample.bin", + "dry_run": True, + }, + self.media_service, + ) + self.assertTrue(planned["ok"]) + self.assertTrue(planned["data"]["dry_run"]) + self.assertEqual(planned["data"]["files"], 1) + self.assertEqual(planned["data"]["bytes"], 5) + self.assertNotIn(str(self.root), str(planned["data"])) + + execution = migrate_storage( + self.ctx, + { + "source_uri": "storage://images/sample.bin", + "target_uri": "storage://archives/sample.bin", + "dry_run": False, + }, + self.media_service, + ) + self.assertFalse(execution["ok"]) + self.assertEqual(execution["error_code"], "VALIDATION_FAILED") + def test_media_signing_rejects_unsafe_or_missing_uri(self): unsafe = sign_media( self.ctx,