|
| 1 | +import logging |
1 | 2 | import unicodedata
|
2 | 3 | from urllib.parse import quote
|
3 | 4 |
|
|
36 | 37 | to_filename,
|
37 | 38 | )
|
38 | 39 |
|
| 40 | +logger = logging.getLogger(__name__) |
| 41 | + |
39 | 42 |
|
40 | 43 | def error_response(message, http_status=400):
|
41 | 44 | return {"job": {"status": 4, "error": message}}, http_status
|
@@ -124,19 +127,25 @@ def get_download_filename(query_result, query, filetype):
|
124 | 127 |
|
125 | 128 |
|
126 | 129 | def content_disposition_filenames(attachment_filename):
|
| 130 | + logger.info( |
| 131 | + f"content_disposition_filenames called with: {attachment_filename} (type: {type(attachment_filename)})" |
| 132 | + ) |
127 | 133 | if not isinstance(attachment_filename, str):
|
128 | 134 | attachment_filename = attachment_filename.decode("utf-8")
|
129 | 135 |
|
130 | 136 | 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} |
132 | 141 | except UnicodeEncodeError:
|
| 142 | + # If we can't encode as ASCII, provide both filename and filename* for RFC 6266 compliance |
133 | 143 | filenames = {
|
134 |
| - "filename": unicodedata.normalize("NFKD", attachment_filename).encode("ascii", "ignore"), |
| 144 | + "filename": unicodedata.normalize("NFKD", attachment_filename).encode("ascii", "ignore").decode("ascii"), |
135 | 145 | "filename*": "UTF-8''%s" % quote(attachment_filename, safe=b""),
|
136 | 146 | }
|
137 |
| - else: |
138 |
| - filenames = {"filename": attachment_filename} |
139 | 147 |
|
| 148 | + logger.info(f"content_disposition_filenames returning: {filenames}") |
140 | 149 | return filenames
|
141 | 150 |
|
142 | 151 |
|
|
0 commit comments