Skip to content

Commit

Permalink
files: fix filename encoding issues for downloads
Browse files Browse the repository at this point in the history
* See relevant fix applied in invenio-files-rest at inveniosoftware/invenio-files-rest@a419808
  • Loading branch information
slint committed Feb 12, 2025
1 parent 92385d2 commit 1d38c1f
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions site/zenodo_rdm/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,17 @@ def send_file(
# Content-Type sniffing.
# (from invenio-files-rest)
if as_attachment or mimetype == "application/octet-stream":
# See https://github.com/pallets/flask/commit/0049922f2e690a6d
# see https://github.com/pallets/werkzeug/blob/main/src/werkzeug/utils.py#L456-L465
try:
filenames = {"filename": filename.encode("latin-1")}
filename.encode("ascii")
except UnicodeEncodeError:
simple = unicodedata.normalize("NFKD", filename)
simple = simple.encode("ascii", "ignore").decode("ascii")
# safe = RFC 5987 attr-char
quoted = quote(filename, safe="!#$&+-.^_`|~")

filenames = {"filename*": "UTF-8''%s" % quoted}
encoded_filename = unicodedata.normalize("NFKD", filename).encode(
"latin-1", "ignore"
)
if encoded_filename:
filenames["filename"] = encoded_filename
filenames = {"filename": simple, "filename*": f"UTF-8''{quoted}"}
else:
filenames = {"filename": filename}
response.headers.set("Content-Disposition", "attachment", **filenames)
else:
response.headers.set("Content-Disposition", "inline")
Expand Down

0 comments on commit 1d38c1f

Please sign in to comment.