Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

1.1 KB
Raw Blame
"""Content-Disposition headers for in-memory downloads."""

import re
import unicodedata
from urllib.parse import quote


def build_content_disposition(filename: str, *, inline: bool = False) -> str:
    """Keep Unicode names and offer a safe ASCII name to older clients.

    The fallback is deliberately conservative: punctuation that needs quoting
    or escaping, percent escapes and control characters become underscores.
    The extended parameter retains the original name using RFC 5987 encoding.
    """
    ascii_name = (
        unicodedata.normalize("NFKD", filename)
        .encode("ascii", "ignore")
        .decode("ascii")
    )
    ascii_name = ascii_name.replace("\\", "/").rsplit("/", 1)[-1]
    fallback = re.sub(r"[^a-zA-Z0-9._() -]", "_", ascii_name).strip()
    if not fallback.strip("."):
        fallback = "download"
    elif fallback.startswith("."):
        # An entirely non-Latin stem can leave only the file extension.
        fallback = "download" + fallback

    disposition = "inline" if inline else "attachment"
    return (
        f'{disposition}; filename="{fallback}"; '
        f"filename*=UTF-8''{quote(filename, safe='')}"
    )