Skip to content

Commit

Permalink
Customize @@display-file to allow to download files with proper filen…
Browse files Browse the repository at this point in the history
…ame (#113)

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

* add missing file
  • Loading branch information
cekk authored Sep 3, 2024
1 parent cd1bc29 commit 11f2f26
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Changelog
[lucabel]
- remove newsitem template override, use default dexterity view for newsitem in backend
[mamico]

- Customize @@display-file to allow to download files with proper filename.
[cekk]

5.5.1 (2024-07-22)
------------------
Expand Down
8 changes: 8 additions & 0 deletions src/redturtle/volto/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@
layer="redturtle.volto.interfaces.IRedturtleVoltoLayer"
/>

<!-- custom display-file -->
<browser:page
name="display-file"
for="*"
class=".display_file.DisplayFile"
permission="zope2.View"
layer="redturtle.volto.interfaces.IRedturtleVoltoLayer"
/>
</configure>
27 changes: 27 additions & 0 deletions src/redturtle/volto/browser/display_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from plone.namedfile.browser import DisplayFile as BaseView
from urllib.parse import quote


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

def set_headers(self, file):
"""
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}"
)

0 comments on commit 11f2f26

Please sign in to comment.