Files
arc53--docsgpt/application/stt/upload_limits.py
T
wehub-resource-sync fed8b2eed7
Backend release / release (push) Waiting to run
Bandit Security Scan / bandit_scan (push) Waiting to run
Build and push multi-arch DocsGPT Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Waiting to run
Build and push multi-arch DocsGPT Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Waiting to run
Build and push multi-arch DocsGPT Docker image / manifest (push) Blocked by required conditions
Build and push DocsGPT FE Docker image for development / build (linux/amd64, ubuntu-latest, amd64) (push) Waiting to run
Build and push DocsGPT FE Docker image for development / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Waiting to run
Build and push DocsGPT FE Docker image for development / manifest (push) Blocked by required conditions
Python linting / ruff (push) Waiting to run
Run python tests with pytest / Run tests and count coverage (3.12) (push) Waiting to run
React Widget Build / build (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:28:29 +08:00

44 lines
1.4 KiB
Python

from pathlib import Path
from application.core.settings import settings
from application.stt.constants import SUPPORTED_AUDIO_EXTENSIONS
from application.utils import safe_filename
STT_REQUEST_SIZE_OVERHEAD_BYTES = 1024 * 1024
STT_SIZE_LIMITED_PATHS = frozenset(("/api/stt", "/api/stt/live/chunk"))
class AudioFileTooLargeError(ValueError):
pass
def get_stt_max_file_size_bytes() -> int:
return max(0, settings.STT_MAX_FILE_SIZE_MB) * 1024 * 1024
def build_stt_file_size_limit_message() -> str:
return f"Audio file exceeds {settings.STT_MAX_FILE_SIZE_MB}MB limit"
def is_audio_filename(filename: str | Path | None) -> bool:
if not filename:
return False
safe_name = safe_filename(Path(str(filename)).name)
return Path(safe_name).suffix.lower() in SUPPORTED_AUDIO_EXTENSIONS
def enforce_audio_file_size_limit(size_bytes: int) -> None:
max_size_bytes = get_stt_max_file_size_bytes()
if max_size_bytes and size_bytes > max_size_bytes:
raise AudioFileTooLargeError(build_stt_file_size_limit_message())
def should_reject_stt_request(path: str, content_length: int | None) -> bool:
if path not in STT_SIZE_LIMITED_PATHS or content_length is None:
return False
max_request_size_bytes = (
get_stt_max_file_size_bytes() + STT_REQUEST_SIZE_OVERHEAD_BYTES
)
return content_length > max_request_size_bytes