grantforge-api / backend /core /download_headers.py
GrantForge Bot
Deploy sha-9a5957fcdef15b7e2623f8b147cda6026475aee0 — source build (no GHCR)
3a3734f
Raw
History Blame Contribute Delete
818 Bytes
"""HTTP helpers for file downloads (latin-1 safe Content-Disposition)."""
from __future__ import annotations
import unicodedata
from urllib.parse import quote
def attachment_content_disposition(filename: str) -> str:
"""RFC 5987 filename* plus ASCII fallback for Starlette/HTTP latin-1 headers."""
clean = (
(filename or "download")
.replace('"', "")
.replace("\n", "")
.replace("\r", "")
.replace("/", "-")
.replace("\\", "-")
)
ascii_fallback = (
unicodedata.normalize("NFKD", clean)
.encode("ascii", "ignore")
.decode("ascii")
.strip()
or "download"
)
return (
f'attachment; filename="{ascii_fallback}"; '
f"filename*=UTF-8''{quote(clean, safe='')}"
)