Download apps/documents/services/generation/output_storage.py from jun2024/fc: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/documents/services/generation/output_storage.py
- Command line
-
hf download hf://spaces/jun2024/fc/apps/documents/services/generation/output_storage.py
-
curl -L -o output_storage.py https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/documents/services/generation/output_storage.py
1.19 kB
| """Business logic services.""" | |
| from __future__ import annotations | |
| from apps.core.utils.path import Path | |
| class GeneratedDocumentStorage: | |
| def __init__(self, media_root: str | None = None) -> None: | |
| self._media_root = media_root | |
| def media_root(self) -> Path: | |
| if self._media_root: | |
| return Path(self._media_root) | |
| from apps.core.config import get_config | |
| value = get_config("django.media_root", None) | |
| if not value: | |
| raise RuntimeError("GeneratedDocumentStorage.media_root 未配置") | |
| return Path(str(value)) | |
| def save_bytes(self, *, relative_dir: str, filename: str, content: bytes) -> str: | |
| media_root = self.media_root | |
| target_dir = media_root / relative_dir | |
| target_dir.mkdir(parents=True, exist_ok=True) | |
| file_path = target_dir / filename | |
| with file_path.open("wb") as f: | |
| f.write(content) | |
| return str(file_path.relative_to(media_root)) | |
| def save_for_case(self, *, case_id: int, filename: str, content: bytes) -> str: | |
| return self.save_bytes(relative_dir=f"generated_documents/case_{case_id}", filename=filename, content=content) | |