Skip to content

Commit 4dffaa5

Browse files
committed
Fix filename handling for Unicode
1 parent 5756f21 commit 4dffaa5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

redash/handlers/query_results.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import unicodedata
23
from urllib.parse import quote
34

@@ -36,6 +37,8 @@
3637
to_filename,
3738
)
3839

40+
logger = logging.getLogger(__name__)
41+
3942

4043
def error_response(message, http_status=400):
4144
return {"job": {"status": 4, "error": message}}, http_status
@@ -124,19 +127,25 @@ def get_download_filename(query_result, query, filetype):
124127

125128

126129
def content_disposition_filenames(attachment_filename):
130+
logger.info(
131+
f"content_disposition_filenames called with: {attachment_filename} (type: {type(attachment_filename)})"
132+
)
127133
if not isinstance(attachment_filename, str):
128134
attachment_filename = attachment_filename.decode("utf-8")
129135

130136
try:
131-
attachment_filename = attachment_filename.encode("ascii")
137+
# Test if we can encode as ASCII, but don't actually encode
138+
attachment_filename.encode("ascii")
139+
# If we can encode as ASCII, use the original string
140+
filenames = {"filename": attachment_filename}
132141
except UnicodeEncodeError:
142+
# If we can't encode as ASCII, provide both filename and filename* for RFC 6266 compliance
133143
filenames = {
134-
"filename": unicodedata.normalize("NFKD", attachment_filename).encode("ascii", "ignore"),
144+
"filename": unicodedata.normalize("NFKD", attachment_filename).encode("ascii", "ignore").decode("ascii"),
135145
"filename*": "UTF-8''%s" % quote(attachment_filename, safe=b""),
136146
}
137-
else:
138-
filenames = {"filename": attachment_filename}
139147

148+
logger.info(f"content_disposition_filenames returning: {filenames}")
140149
return filenames
141150

142151

0 commit comments

Comments
 (0)