Download apps/documents/models/evidence_storage.py from jun2024/fc: direct link, hf CLI and curl.
- Browser
- Download file 1.84 kB
-
https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/documents/models/evidence_storage.py
- Command line
-
hf download hf://spaces/jun2024/fc/apps/documents/models/evidence_storage.py
-
curl -L -o evidence_storage.py https://huggingface.co/spaces/jun2024/fc/resolve/main/apps/documents/models/evidence_storage.py
1.84 kB
| """ | |
| 证据文件存储配置 | |
| 此模块解决 models 和 services 之间的循环导入问题。 | |
| """ | |
| import os | |
| import re | |
| from django.conf import settings | |
| from django.core.files.storage import FileSystemStorage | |
| from django.utils.deconstruct import deconstructible | |
| class EvidenceFileStorage(FileSystemStorage): | |
| def deconstruct(self) -> tuple[str, list[object], dict[str, object]]: | |
| """不序列化 location/base_url,避免本机路径写入 migration。""" | |
| return (f"{self.__class__.__module__}.{self.__class__.__qualname__}", [], {}) | |
| """ | |
| 证据文件存储类 | |
| 保留中文文件名中的特殊字符(如括号), | |
| 只移除真正危险的字符. | |
| """ | |
| def get_valid_name(self, name: str) -> str: | |
| from pathlib import Path | |
| original_name = name | |
| name = super().get_valid_name(name) | |
| if not name: | |
| return original_name | |
| path = Path(name) | |
| stem = path.stem | |
| ext = path.suffix | |
| stem = re.sub(r'[\\/*?:"<>|\x00-\x1f]', "", stem) | |
| if not stem: | |
| stem = "file" | |
| return stem + ext | |
| def generate_filename(self, name: str) -> str: | |
| from pathlib import Path | |
| original_name = name | |
| name = super().generate_filename(name) | |
| if not name: | |
| return original_name | |
| path = Path(name) | |
| stem = path.stem | |
| ext = path.suffix | |
| stem = re.sub(r'[\\/*?:"<>|\x00-\x1f]', "", stem) | |
| if not stem: | |
| stem = "file" | |
| return str(path.parent / (stem + ext)) | |
| def get_evidence_storage() -> EvidenceFileStorage: | |
| """获取证据文件存储实例""" | |
| return EvidenceFileStorage(location=os.path.join(settings.MEDIA_ROOT, "evidence"), base_url="/media/evidence/") | |
| evidence_file_storage = get_evidence_storage() | |