Merge pull request #24246 from akhilnarang/fix-file-download

fix(response): fixup non-ASCII character filenames
This commit is contained in:
Akhil Narang 2024-01-11 16:15:23 +05:30 committed by GitHub
commit 5c90e151ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,7 @@ import json
import mimetypes
import os
import sys
from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING
from urllib.parse import quote
import werkzeug.utils
@ -92,6 +92,7 @@ def as_csv():
response = Response()
response.mimetype = "text/csv"
filename = f"{frappe.response['doctype']}.csv"
filename = filename.encode("utf-8").decode("unicode-escape", "ignore")
response.headers.add("Content-Disposition", "attachment", filename=filename)
response.data = frappe.response["result"]
return response
@ -101,6 +102,7 @@ def as_txt():
response = Response()
response.mimetype = "text"
filename = f"{frappe.response['doctype']}.txt"
filename = filename.encode("utf-8").decode("unicode-escape", "ignore")
response.headers.add("Content-Disposition", "attachment", filename=filename)
response.data = frappe.response["result"]
return response
@ -113,10 +115,11 @@ def as_raw():
or mimetypes.guess_type(frappe.response["filename"])[0]
or "application/unknown"
)
filename = frappe.response["filename"].encode("utf-8").decode("unicode-escape", "ignore")
response.headers.add(
"Content-Disposition",
frappe.response.get("display_content_as", "attachment"),
filename=frappe.response["filename"],
filename=filename,
)
response.data = frappe.response["filecontent"]
return response
@ -138,7 +141,8 @@ def as_json():
def as_pdf():
response = Response()
response.mimetype = "application/pdf"
response.headers.add("Content-Disposition", None, filename=frappe.response["filename"])
filename = frappe.response["filename"].encode("utf-8").decode("unicode-escape", "ignore")
response.headers.add("Content-Disposition", None, filename=filename)
response.data = frappe.response["filecontent"]
return response