Files
DEMO-AGENT/review_agent/regulatory_review/storage.py

36 lines
1.0 KiB
Python

from __future__ import annotations
import hashlib
from pathlib import Path
from django.conf import settings
from review_agent.models import RegulatoryArtifact, RegulatoryReviewBatch
def save_artifact(
batch: RegulatoryReviewBatch,
*,
name: str,
content: str | bytes,
artifact_type: str,
metadata: dict | None = None,
) -> RegulatoryArtifact:
root = Path(batch.work_dir) if batch.work_dir else Path(settings.MEDIA_ROOT) / "regulatory_review" / "work" / batch.batch_no
root.mkdir(parents=True, exist_ok=True)
path = root / Path(name).name
if isinstance(content, bytes):
path.write_bytes(content)
digest = hashlib.sha256(content).hexdigest()
else:
path.write_text(content, encoding="utf-8")
digest = hashlib.sha256(content.encode("utf-8")).hexdigest()
return RegulatoryArtifact.objects.create(
batch=batch,
artifact_type=artifact_type,
name=path.name,
storage_path=str(path),
content_hash=digest,
metadata=metadata or {},
)