Always set the filename in Content-Disposition

Relates to original fix for #1158
pull/8024/head
John-Scott Atlakson 2022-02-17 09:20:57 -08:00 zatwierdzone przez LB Johnston
rodzic aaee9b8c81
commit 4a7fb00d35
4 zmienionych plików z 24 dodań i 20 usunięć

Wyświetl plik

@ -15,6 +15,7 @@ Changelog
* Remove IE11 warnings (Gianluca De Cola)
* Fix: When using `simple_translations` ensure that the user is redirected to the page edit view when submitting for a single locale (Mitchel Cabuloy)
* Fix: When previewing unsaved changes to `Form` pages, ensure that all added fields are correctly shown in the preview (Joshua Munn)
* Fix: When Documents (e.g. PDFs) have been configured to be served inline via `WAGTAILDOCS_CONTENT_TYPES` & `WAGTAILDOCS_INLINE_CONTENT_TYPES` ensure that the filename is correctly set in the `Content-Disposition` header so that saving the files will use the correct filename (John-Scott Atlakson)
2.16.2 (xx.xx.xxxx) - IN DEVELOPMENT

Wyświetl plik

@ -31,6 +31,7 @@ Here are other changes related to the redesign:
* Update django-treebeard dependency to 4.5.1 or above (Serafeim Papastefanos)
* When using `simple_translations` ensure that the user is redirected to the page edit view when submitting for a single locale (Mitchel Cabuloy)
* When previewing unsaved changes to `Form` pages, ensure that all added fields are correctly shown in the preview (Joshua Munn)
* When Documents (e.g. PDFs) have been configured to be served inline via `WAGTAILDOCS_CONTENT_TYPES` & `WAGTAILDOCS_INLINE_CONTENT_TYPES` ensure that the filename is correctly set in the `Content-Disposition` header so that saving the files will use the correct filename (John-Scott Atlakson)
## Upgrade considerations

Wyświetl plik

@ -53,7 +53,10 @@ class TestServeView(TestCase):
)
def test_inline_content_disposition_header(self):
self.assertEqual(self.get(self.pdf_document)["Content-Disposition"], "inline")
self.assertEqual(
self.get(self.pdf_document)["Content-Disposition"],
'inline; filename="{}"'.format(self.pdf_document.filename),
)
@mock.patch("wagtail.documents.views.serve.hooks")
@mock.patch("wagtail.documents.views.serve.get_object_or_404")

Wyświetl plik

@ -78,28 +78,27 @@ def sendfile(
response = _sendfile(request, filename, mimetype=mimetype)
if attachment:
if attachment_filename is None:
attachment_filename = os.path.basename(filename)
parts = ["attachment"]
if attachment_filename:
from django.utils.encoding import force_str
from wagtail.core.utils import string_to_ascii
attachment_filename = force_str(attachment_filename)
ascii_filename = string_to_ascii(attachment_filename)
parts.append('filename="%s"' % ascii_filename)
if ascii_filename != attachment_filename:
from urllib.parse import quote
quoted_filename = quote(attachment_filename)
parts.append("filename*=UTF-8''%s" % quoted_filename)
response["Content-Disposition"] = "; ".join(parts)
else:
response["Content-Disposition"] = "inline"
parts = ["inline"]
if attachment_filename is None:
attachment_filename = os.path.basename(filename)
if attachment_filename:
from django.utils.encoding import force_str
from wagtail.core.utils import string_to_ascii
attachment_filename = force_str(attachment_filename)
ascii_filename = string_to_ascii(attachment_filename)
parts.append('filename="%s"' % ascii_filename)
if ascii_filename != attachment_filename:
from urllib.parse import quote
quoted_filename = quote(attachment_filename)
parts.append("filename*=UTF-8''%s" % quoted_filename)
response["Content-Disposition"] = "; ".join(parts)
response["Content-length"] = os.path.getsize(filename)
response["Content-Type"] = mimetype
response["Content-Encoding"] = encoding or guessed_encoding