Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix send_file's attachment_filename to work with non-ascii filenames #2223

Merged
merged 6 commits into from
Apr 8, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion flask/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .globals import session, _request_ctx_stack, _app_ctx_stack, \
current_app, request
from ._compat import string_types, text_type
from unidecode import unidecode


# sentinel
Expand Down Expand Up @@ -534,8 +535,11 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
if attachment_filename is None:
raise TypeError('filename unavailable, required for '
'sending as attachment')
filename_dict = {
'filename': unidecode(text_type(attachment_filename)),
'filename*': "UTF-8''%s" % url_quote(attachment_filename)}

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

headers.add('Content-Disposition', 'attachment',
filename=attachment_filename)
**filename_dict)

if current_app.use_x_sendfile and filename:
if file is not None:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def hello():
'Jinja2>=2.4',
'itsdangerous>=0.21',
'click>=2.0',
'unidecode',
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
12 changes: 12 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,18 @@ def test_attachment(self):
assert options['filename'] == 'index.txt'
rv.close()

def test_attachment_with_utf8_filename(self):
app = flask.Flask(__name__)
with app.test_request_context():
with open(os.path.join(app.root_path, 'static/index.html')) as f:
rv = flask.send_file(f, as_attachment=True,
attachment_filename=u'Ñandú/pingüino.txt')
content_disposition = set(rv.headers['Content-Disposition'].split(';'))

This comment was marked as off-topic.

assert content_disposition == set(['attachment',
' filename="Nandu/pinguino.txt"',
" filename*=UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt"])
rv.close()

def test_static_file(self):
app = flask.Flask(__name__)
# default cache timeout is 12 hours
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ deps=
devel: git+https://github.com/pallets/itsdangerous.git
devel: git+https://github.com/jek/blinker.git
simplejson: simplejson
unidecode

[testenv:docs]
deps = sphinx
Expand Down