Open vmspike opened 8 years ago
For catch FileNotFoundError exception you may change BaseDownloadView get() method to
class BaseDownloadView(DownloadMixin, View):
"""A base :class:`DownloadMixin` that implements :meth:`get`."""
def get(self, request, *args, **kwargs):
"""Handle GET requests: stream a file."""
try:
return self.render_to_response()
except FileNotFoundError:
return self.file_not_found_response()
As for quoting unicode issue temporary solution can be modifying XAccelRedirectResponse class this way:
from django.utils.encoding import uri_to_iri
class XAccelRedirectResponse(ProxiedDownloadResponse):
...
# self['X-Accel-Redirect'] = redirect_url
self['X-Accel-Redirect'] = bytes(uri_to_iri(redirect_url), 'utf-8')
This hack allow download files with non-ACSII name, but proposing filename for saving is %-quoted which is ugly.
For make request for non US-ACSII filename correct need to change content_disposition()
function to use filename instead of utf8_filename:
def content_disposition(filename):
# Fix incorrect filename propose for download
if not filename:
return 'attachment'
ascii_filename = encode_basename_ascii(filename)
utf8_filename = encode_basename_utf8(filename)
if ascii_filename == utf8_filename: # ASCII only.
return "attachment; filename=\"{ascii}\"".format(ascii=ascii_filename)
else:
return "attachment; filename=\"{ascii}\"; filename*=UTF-8''{utf8}".format(
ascii=ascii_filename,
# utf8=utf8_filename)
utf8=filename)
I'm tried extremely simple configuration based on examples from manual. Files with latin filenames served well but for files like ИП_ТУ.odt (Cyrillic) code 404 returned. Some useful logs:
So the problem is quoted %XX-symbols, nginx trying to find files with exactly path (with %XX-symbols). Probably conversion is required before passing filename to web-server.
Also FileNotFoundError exception doesn't catch up if file not found.