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

Customize @@display-file to allow to download files with proper filename #70

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Changelog
4.0.11 (unreleased)
-------------------

- Nothing changed yet.
- Customize @@display-file to allow to download files with proper filename.
(backport of https://github.com/RedTurtle/redturtle.volto/pull/113).
[cekk]


4.0.10 (2024-07-12)
Expand Down
8 changes: 8 additions & 0 deletions src/design/plone/policy/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@
permission="zope2.View"
/>

<!-- backport of https://github.com/RedTurtle/redturtle.volto/pull/113 -->
<browser:page
name="display-file"
for="*"
class=".display_file.DisplayFile"
permission="zope2.View"
layer="design.plone.policy.interfaces.IDesignPlonePolicyLayer"
/>

</configure>
29 changes: 29 additions & 0 deletions src/design/plone/policy/browser/display_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from plone.namedfile.browser import DisplayFile as BaseView
from urllib.parse import quote


class DisplayFile(BaseView):
"""
Custom view
"""

def set_headers(self, file):
"""
backport of https://github.com/RedTurtle/redturtle.volto/pull/113

We need to add filename to the reponse because otherwise the browser
use the field name as filename (last path element).

content-disposition should be "inline" to allow to display the file in the browser
without forcing download (with "attachment" the browser will download it).
"""
super().set_headers(file=file)

filename = getattr(file, "filename", "")
if filename is not None:
if not isinstance(filename, str):
filename = str(filename, "utf-8", errors="ignore")
filename = quote(filename.encode("utf8"))
self.request.response.setHeader(
"Content-Disposition", f"inline; filename*=UTF-8''{filename}"
)
Loading