"""Content-addressed file storage for driver packages.""" import hashlib from pathlib import Path class DriverStore: """SHA256 content-addressed file storage for printer driver packages. Files are stored as DRIVERS_DIR/{sha256} so identical uploads are deduplicated automatically. """ def __init__(self, base_dir: str) -> None: self._base = Path(base_dir) self._base.mkdir(parents=True, exist_ok=True) def save(self, data: bytes) -> str: """Persist *data* and return its SHA256 hex digest. If the file already exists the write is skipped (deduplication). """ digest = hashlib.sha256(data).hexdigest() dest = self._base / digest if not dest.exists(): dest.write_bytes(data) return digest def get_path(self, sha256: str) -> Path: """Return the filesystem path for a given SHA256 digest.""" return self._base / sha256 def exists(self, sha256: str) -> bool: """Return True if the file for *sha256* exists on disk.""" return (self._base / sha256).exists()