Spaces:
Running
Running
| """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='')}" | |
| ) | |