"""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}.zip so identical uploads are
    deduplicated automatically. The .zip suffix lets operators identify
    stored driver packages by type when browsing the volume directly.
    """

    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:
        digest = hashlib.sha256(data).hexdigest()
        dest = self.get_path(digest)
        if not dest.exists():
            dest.write_bytes(data)
        return digest

    def get_path(self, sha256: str) -> Path:
        return self._base / f"{sha256}.zip"

    def exists(self, sha256: str) -> bool:
        return self.get_path(sha256).exists()
