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

feat: set canonical header for file #163

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions news/163.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Set `Link` header with `rel="canonical"` for file downloads. @mamico
13 changes: 11 additions & 2 deletions plone/namedfile/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ def handle_request_range(self, file):
except ValueError:
return default

def get_canonical(self, file):
filename = getattr(file, "filename", None)
if filename is None:
return f"{self.context.absolute_url()}/@@download/{self.fieldname}"
else:
return f"{self.context.absolute_url()}/@@download/{self.fieldname}/{filename}"

def set_headers(self, file):
# With filename None, set_headers will not add the download headers.
if not self.filename:
Expand All @@ -135,7 +142,8 @@ def set_headers(self, file):
self.filename = self.fieldname
if self.filename is None:
self.filename = "file.ext"
set_headers(file, self.request.response, filename=self.filename)
canonical = self.get_canonical(file)
set_headers(file, self.request.response, filename=self.filename, canonical=canonical)

def _getFile(self):
if not self.fieldname:
Expand Down Expand Up @@ -185,4 +193,5 @@ def set_headers(self, file):
if mimetype not in self.allowed_inline_mimetypes:
# Let the Download view handle this.
return super().set_headers(file)
set_headers(file, self.request.response)
canonical = self.get_canonical(file)
set_headers(file, self.request.response, canonical=canonical)
11 changes: 11 additions & 0 deletions plone/namedfile/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ These store data with the following types::
... self.image = namedfile.NamedImage()
... self.blob = namedfile.NamedBlobFile()
... self.blobimage = namedfile.NamedBlobImage()
...
... def absolute_url(self):
... return "http://foo/bar"


File data and content type
Expand Down Expand Up @@ -226,6 +229,8 @@ We will test this with a dummy request, faking traversal::
'text/plain'
>>> request.response.getHeader('Content-Disposition')
"attachment; filename*=UTF-8''test.txt"
>>> request.response.getHeader('Link')
'<http://foo/bar/@@download/simple/test.txt>; rel="canonical"'

>>> request = TestRequest()
>>> download = Download(container, request).publishTraverse(request, 'blob')
Expand All @@ -238,6 +243,8 @@ We will test this with a dummy request, faking traversal::
'text/plain'
>>> request.response.getHeader('Content-Disposition')
"attachment; filename*=UTF-8''test.txt"
>>> request.response.getHeader('Link')
'<http://foo/bar/@@download/blob/test.txt>; rel="canonical"'

>>> request = TestRequest()
>>> download = Download(container, request).publishTraverse(request, 'image')
Expand All @@ -250,6 +257,8 @@ We will test this with a dummy request, faking traversal::
'image/foo'
>>> request.response.getHeader('Content-Disposition')
"attachment; filename*=UTF-8''zpt.gif"
>>> request.response.getHeader('Link')
'<http://foo/bar/@@download/image/zpt.gif>; rel="canonical"'

>>> request = TestRequest()
>>> download = Download(container, request).publishTraverse(request, 'blobimage')
Expand All @@ -262,6 +271,8 @@ We will test this with a dummy request, faking traversal::
'image/foo'
>>> request.response.getHeader('Content-Disposition')
"attachment; filename*=UTF-8''zpt.gif"
>>> request.response.getHeader('Link')
'<http://foo/bar/@@download/blobimage/zpt.gif>; rel="canonical"'

Range support
-------------
Expand Down
4 changes: 3 additions & 1 deletion plone/namedfile/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def get_contenttype(file=None, filename=None, default="application/octet-stream"
return default


def set_headers(file, response, filename=None):
def set_headers(file, response, filename=None, canonical=None):
"""Set response headers for the given file. If filename is given, set
the Content-Disposition to attachment.
"""
Expand All @@ -149,6 +149,8 @@ def set_headers(file, response, filename=None):
"Content-Disposition", f"attachment; filename*=UTF-8''{filename}"
)

if canonical is not None:
response.setHeader("Link", f'<{canonical}>; rel="canonical"')

def stream_data(file, start=0, end=None):
"""Return the given file as a stream if possible."""
Expand Down
Loading