better pdf ux

This commit is contained in:
Mateusz Gruszczyński
2026-05-21 12:34:18 +02:00
parent 9142590c79
commit cb48735178
4 changed files with 81 additions and 81 deletions

View File

@@ -145,11 +145,12 @@ def torrent_folder_priority(torrent_hash: str):
return ok(result), status
def _attachment_headers(download_name: str, content_type: str = "application/octet-stream") -> dict:
def _attachment_headers(download_name: str, content_type: str = "application/octet-stream", disposition: str = "attachment") -> dict:
safe = Path(download_name or "download.bin").name or "download.bin"
safe_disposition = "inline" if disposition == "inline" else "attachment"
return {
"Content-Type": content_type,
"Content-Disposition": f"attachment; filename*=UTF-8''{quote(safe)}",
"Content-Disposition": f"{safe_disposition}; filename*=UTF-8''{quote(safe)}",
"X-Content-Type-Options": "nosniff",
}
@@ -206,7 +207,10 @@ def torrent_file_download(torrent_hash: str, file_index: int):
try:
item = rtorrent.torrent_download_file_info(profile, torrent_hash, file_index)
size = int(item.get("size") or 0)
headers = _attachment_headers(item.get("download_name") or "file.bin")
download_name = item.get("download_name") or "file.bin"
inline_pdf = str(request.args.get("disposition") or "").lower() == "inline" and Path(download_name).suffix.lower() == ".pdf"
# Note: Inline mode is limited to PDFs so the existing download behavior remains unchanged for every other file type.
headers = _attachment_headers(download_name, "application/pdf" if inline_pdf else "application/octet-stream", "inline" if inline_pdf else "attachment")
if size > 0:
headers["Content-Length"] = str(size)
def generate():