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

Download miniatures with original filename #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
5.5.6 (unreleased)
------------------

- Nothing changed yet.
- Customize @download view to allow downloading miniature images with their original filename and not with miniature as filename.
[cekk]


5.5.5 (2024-09-23)
Expand Down
10 changes: 10 additions & 0 deletions src/redturtle/volto/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@
permission="zope2.View"
layer="redturtle.volto.interfaces.IRedturtleVoltoLayer"
/>

<!-- override @@download view -->
<browser:page
name="download"
for="*"
class=".download.Download"
permission="zope2.View"
layer="redturtle.volto.interfaces.IRedturtleVoltoLayer"
/>

</configure>
34 changes: 34 additions & 0 deletions src/redturtle/volto/browser/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from plone import api
from plone.namedfile.browser import Download as BaseDownload
from plone.namedfile.utils import set_headers


class Download(BaseDownload):
"""
Customization to allow downloading miniature images with their original
filename and not with miniature as filename.
"""

def set_headers(self, file):
"""
in self.filename we have the miniature name
"""
filename = getattr(file, "filename", None)
if self.filename:
# we are downloading a miniature
filename = f"{self.filename}_{filename}"
set_headers(file, self.request.response, filename=filename)

def _getFile(self):
"""
in self.filename we have the miniature name
"""
if self.filename:
# we are downloading a miniature
view = api.content.get_view(
name="images", context=self.context, request=self.request
)
scale = view.scale(scale=self.filename, fieldname=self.fieldname)
if scale:
return scale.data
return super()._getFile()
Loading