"""Shared upload validation. Enforces size + MIME type prefixes from settings."""
from django.conf import settings
from rest_framework.exceptions import ValidationError


def validate_upload(uploaded_file) -> None:
    max_size = getattr(settings, "ATTACHMENT_MAX_FILE_SIZE", 50 * 1024 * 1024)
    if uploaded_file.size > max_size:
        raise ValidationError(
            f"file too large ({uploaded_file.size} bytes); max {max_size}"
        )
    allowed = getattr(settings, "ATTACHMENT_ALLOWED_MIME_PREFIXES", ())
    if allowed:
        mime = (getattr(uploaded_file, "content_type", "") or "").lower()
        if not any(mime.startswith(p) for p in allowed):
            raise ValidationError(f"mime type '{mime}' not allowed")
