"""Opaque storage port. The adapter stores bytes and does not decide who may read them. Cleartext for a reader goes through require_cleartext. Locked profiles stay bytes on disk and are not decrypted here. No new cryptography is implemented. """ from __future__ import annotations import hashlib from pathlib import Path from uuid import UUID from app.core.errors import Problem, storage_unavailable, unprocessable from app.media.storage import read_bytes, sha256, write_bytes LEGACY_GENERATION = "legacy-local-1" LEGACY_PROFILE = "legacy-plaintext" LOCKED_PROFILES = frozenset({"managed-encrypted", "client-encrypted"}) PROFILES = frozenset({LEGACY_PROFILE, *LOCKED_PROFILES}) def require_cleartext(profile: str | None) -> None: name = profile or LEGACY_PROFILE if name == LEGACY_PROFILE: return if name in LOCKED_PROFILES: raise Problem( 422, "Geschützter Inhalt", "Dieser Inhalt ist geschützt. Ein Klartextabruf ist in dieser Lieferung nicht möglich.", "unsupported", ) raise Problem( 422, "Nicht unterstützt", "Dieses Schutzprofil wird nicht unterstützt.", "unsupported", ) def check_object_key(object_key: str) -> str: key = (object_key or "").replace("\\", "/").strip() parts = key.split("/") if not key or key.startswith("/") or any(part in {"", ".", ".."} for part in parts): raise unprocessable("Ungültiger Speicherpfad.") try: if len(parts) == 1: UUID(parts[0]) elif len(parts) == 2 and parts[0] == "dispatch": UUID(parts[1]) else: raise ValueError except ValueError: raise unprocessable("Ungültiger Speicherpfad.") from None return key def object_path(root: Path, object_key: str) -> Path: key = check_object_key(object_key) base = Path(root).resolve() base.mkdir(parents=True, exist_ok=True) target = (base / key).resolve() if base != target and base not in target.parents: raise unprocessable("Ungültiger Speicherpfad.") return target class LocalAdapter: """Files stay under one recorded root. A client path is not an address.""" def __init__(self, root: str | Path) -> None: self.root = Path(root) def put(self, object_key: str, data: bytes) -> None: path = object_path(self.root, object_key) path.parent.mkdir(parents=True, exist_ok=True) write_bytes(path, data) def open(self, object_key: str) -> bytes: return read_bytes(object_path(self.root, object_key)) def delete(self, object_key: str) -> None: path = object_path(self.root, object_key) try: path.unlink(missing_ok=True) except OSError as exc: raise storage_unavailable() from exc def describe(self, object_key: str) -> dict: path = object_path(self.root, object_key) try: if not path.is_file(): raise storage_unavailable() return {"byte_size": path.stat().st_size, "object_key": object_key} except OSError as exc: raise storage_unavailable() from exc class MemoryAdapter: """In-memory stand-in for the same port. It is not a second product store.""" def __init__(self) -> None: self._items: dict[str, bytes] = {} self.fail_put = False def put(self, object_key: str, data: bytes) -> None: if self.fail_put: raise storage_unavailable() key = check_object_key(object_key) self._items[key] = bytes(data) def open(self, object_key: str) -> bytes: key = check_object_key(object_key) try: return self._items[key] except KeyError as exc: raise storage_unavailable() from exc def delete(self, object_key: str) -> None: key = check_object_key(object_key) self._items.pop(key, None) def describe(self, object_key: str) -> dict: data = self.open(object_key) return {"byte_size": len(data), "object_key": object_key} def publish(adapter, object_key: str, data: bytes) -> None: """Make bytes readable only after a checked roundtrip. A failed put leaves nothing published.""" adapter.put(object_key, data) try: opened = adapter.open(object_key) except Problem: adapter.delete(object_key) raise if opened != data: adapter.delete(object_key) raise storage_unavailable() def ensure_legacy_instance(conn, media_root: str) -> dict: """Bind the legacy generation to the current root once. Later roots do not retarget it.""" root = str(Path(media_root).resolve()) conn.execute( """ INSERT INTO storage_instances (kind, generation, root_path) VALUES ('local', %s, %s) ON CONFLICT (generation) DO NOTHING """, (LEGACY_GENERATION, root), ) conn.execute( """ UPDATE storage_instances SET root_path = %s WHERE generation = %s AND root_path IS NULL """, (root, LEGACY_GENERATION), ) row = conn.execute( """ SELECT id, kind, generation, root_path FROM storage_instances WHERE generation = %s """, (LEGACY_GENERATION,), ).fetchone() conn.execute( """ UPDATE media_assets SET storage_instance_id = %s WHERE storage_generation = %s AND storage_instance_id IS NULL """, (row["id"], LEGACY_GENERATION), ) conn.execute( """ UPDATE dispatch_snapshots SET storage_instance_id = %s WHERE storage_generation = %s AND storage_instance_id IS NULL """, (row["id"], LEGACY_GENERATION), ) return row def instance_for_write(conn, media_root: str) -> dict: legacy = ensure_legacy_instance(conn, media_root) root = str(Path(media_root).resolve()) if legacy["root_path"] == root: return legacy generation = "local-" + hashlib.sha256(root.encode("utf-8")).hexdigest()[:16] conn.execute( """ INSERT INTO storage_instances (kind, generation, root_path) VALUES ('local', %s, %s) ON CONFLICT (generation) DO NOTHING """, (generation, root), ) return conn.execute( """ SELECT id, kind, generation, root_path FROM storage_instances WHERE generation = %s """, (generation,), ).fetchone() class StoredHandle: """Rollback and delete go through the adapter that owns the bytes.""" def __init__(self, adapter: StoragePort, object_key: str): self._adapter = adapter self._key = object_key def discard(self) -> None: self._adapter.delete(self._key) _FACTORIES: dict[str, callable] = {} def _local_factory(row: dict) -> StoragePort: return LocalAdapter(row["root_path"]) _FACTORIES["local"] = _local_factory def adapter_for(row: dict) -> StoragePort: factory = _FACTORIES.get(row["kind"]) if factory is None or not row.get("root_path") and row["kind"] == "local": raise storage_unavailable() return factory(row) def removal_for(conn, instance_id, generation: str | None, object_key: str, media_root: str) -> StoredHandle: row = None if instance_id is not None: row = conn.execute( "SELECT id, kind, generation, root_path FROM storage_instances WHERE id = %s", (instance_id,), ).fetchone() if row is None: root = resolve_root(conn, instance_id, generation, media_root) return StoredHandle(LocalAdapter(root), object_key) return StoredHandle(adapter_for(row), object_key) def resolve_root(conn, instance_id, generation: str | None, media_root: str) -> str: if instance_id is not None: row = conn.execute( "SELECT root_path FROM storage_instances WHERE id = %s", (instance_id,), ).fetchone() if row is not None and row["root_path"]: return row["root_path"] if generation in {None, LEGACY_GENERATION}: return ensure_legacy_instance(conn, media_root)["root_path"] return str(Path(media_root).resolve()) def stored_path(conn, instance_id, generation: str | None, object_key: str, media_root: str) -> Path: return object_path(Path(resolve_root(conn, instance_id, generation, media_root)), object_key) def read_clear_bytes( conn, *, instance_id, generation: str | None, object_key: str, media_root: str, protection_profile: str | None, expected_sha256: str | None, ) -> bytes: require_cleartext(protection_profile) row = None if instance_id is not None: row = conn.execute( "SELECT id, kind, generation, root_path FROM storage_instances WHERE id = %s", (instance_id,), ).fetchone() adapter = adapter_for(row) if row is not None else LocalAdapter(resolve_root(conn, instance_id, generation, media_root)) data = adapter.open(object_key) if expected_sha256 and sha256(data) != expected_sha256: raise Problem( 422, "Inhalt nicht geprüft", "Die gespeicherte Datei stimmt nicht mit dem erfassten Stand überein.", "processing", ) return data