from __future__ import annotations from datetime import datetime from app.models.entities import Blob def build_blob_storage_key(blob: Blob) -> str: content_hash = blob.content_hash prefix_one = content_hash[:2] if len(content_hash) >= 2 else "xx" prefix_two = content_hash[2:4] if len(content_hash) >= 4 else "xx" kind = blob.kind or "blob" return f"objects/{kind}/sha256/{prefix_one}/{prefix_two}/{content_hash}.blob" def build_preview_artifact_storage_key(*, artifact_type: str, blob: Blob) -> str: content_hash = blob.content_hash prefix_one = content_hash[:2] if len(content_hash) >= 2 else "xx" prefix_two = content_hash[2:4] if len(content_hash) >= 4 else "xx" artifact_segment = _normalize_segment(artifact_type) return f"objects/preview/{artifact_segment}/sha256/{prefix_one}/{prefix_two}/{content_hash}.blob" def build_export_snapshot_storage_key(*, exported_at: datetime, digest: str) -> str: year = f"{exported_at.year:04d}" month = f"{exported_at.month:02d}" day = f"{exported_at.day:02d}" normalized_digest = _normalize_segment(digest) or "snapshot" return f"objects/export/metadata/{year}/{month}/{day}/{normalized_digest}.json" def build_manifest_storage_key(*, scope: str, identifier: str) -> str: normalized_scope = _normalize_segment(scope) or "general" normalized_identifier = _normalize_segment(identifier) or "manifest" return f"objects/manifest/{normalized_scope}/{normalized_identifier}.json" def _normalize_segment(value: str) -> str: cleaned = "".join(char if char.isalnum() or char in {"-", "_"} else "-" for char in value.strip().lower()) while "--" in cleaned: cleaned = cleaned.replace("--", "-") return cleaned.strip("-_")