Changeset View
Changeset View
Standalone View
Standalone View
common/upload_paths.py
| """Implements utilities related to media uploads.""" | """Implements utilities related to media uploads.""" | ||||
| from pathlib import Path | |||||
| from time import time | |||||
| import hashlib | import hashlib | ||||
| import logging | |||||
| import typing | import typing | ||||
| import uuid | import uuid | ||||
| from pathlib import Path | |||||
| import logging | |||||
| if typing.TYPE_CHECKING: | if typing.TYPE_CHECKING: | ||||
| import static_assets.models.StaticAsset | import static_assets.models.StaticAsset | ||||
| import films | import films | ||||
| import training | import training | ||||
| DISALLOWED_MEDIA_PREFIXES = ['ad'] | DISALLOWED_MEDIA_PREFIXES = ['ad'] | ||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
| Show All 33 Lines | def get_upload_to_hashed_path(_: ModelWithFile, filename: str) -> Path: | ||||
| MEDIA_ROOT/bd/bd2b5b1cd81333ed2d8db03971f91200/bd2b5b1cd81333ed2d8db03971f91200.mp4, | MEDIA_ROOT/bd/bd2b5b1cd81333ed2d8db03971f91200/bd2b5b1cd81333ed2d8db03971f91200.mp4, | ||||
| where `bd` is the first two characters of the hashed filename. | where `bd` is the first two characters of the hashed filename. | ||||
| """ | """ | ||||
| extension = Path(filename).suffix | extension = Path(filename).suffix | ||||
| hashed = generate_hash_from_filename(filename) | hashed = generate_hash_from_filename(filename) | ||||
| path = Path(hashed[:2], hashed, hashed).with_suffix(extension) | path = Path(hashed[:2], hashed, hashed).with_suffix(extension) | ||||
| return path | return path | ||||
| def shortuid() -> str: | |||||
| """Generate a 14-characters long string ID based on time.""" | |||||
| return hex(int(time() * 10000000))[2:] | |||||