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

/source redirects to /source.pdf for PDF source files #1407

Merged
merged 1 commit into from
Jul 26, 2023
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: 2 additions & 2 deletions peachjam/models/core_document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,9 +731,9 @@ def as_pdf(self):
def filename_extension(self):
return os.path.splitext(self.filename)[1][1:]

def filename_for_download(self):
def filename_for_download(self, ext=None):
"""Return a generated filename appropriate for use when downloading this source file."""
ext = os.path.splitext(self.filename)[1]
ext = ext or os.path.splitext(self.filename)[1]
title = re.sub(r"[^a-zA-Z0-9() ]", "", self.document.title)
return title + ext

Expand Down
42 changes: 30 additions & 12 deletions peachjam/views/documents.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from cobalt import FrbrUri
from django.conf import settings
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.shortcuts import get_object_or_404, redirect, reverse
from django.utils.decorators import method_decorator
from django.utils.translation import get_language
from django.views.generic import DetailView, View
Expand Down Expand Up @@ -160,31 +160,49 @@ def render_to_response(self, context, **response_kwargs):
if hasattr(self.object, "source_file") and self.object.source_file.file:
source_file = self.object.source_file

if source_file.mimetype == "application/pdf":
# If the source file is a PDF, redirect to the source.pdf URL
# This avoids providing an identical file from two different URLs, which is bad for caching,
# bad for Google, and bad for PocketLaw.
return redirect(
reverse(
"document_source_pdf",
kwargs={"frbr_uri": self.object.expression_frbr_uri[1:]},
)
)

if source_file.source_url:
return redirect(source_file.source_url)

file = source_file.file.open()
file_bytes = file.read()
response = HttpResponse(file_bytes, content_type=source_file.mimetype)
response[
"Content-Disposition"
] = f"inline; filename={source_file.filename_for_download()}"
response["Content-Length"] = str(len(file_bytes))
return response
return self.make_response(
source_file.file.open(),
source_file.mimetype,
source_file.filename_for_download(),
)
raise Http404

def make_response(self, f, content_type, fname):
file_bytes = f.read()
response = HttpResponse(file_bytes, content_type=content_type)
response["Content-Disposition"] = f"inline; filename={fname}"
response["Content-Length"] = str(len(file_bytes))
return response


class DocumentSourcePDFView(DocumentSourceView):
def render_to_response(self, context, **response_kwargs):
if hasattr(self.object, "source_file"):
source_file = self.object.source_file

# if the source file is remote and a pdf, just redirect there
if source_file.source_url and source_file.mimetype == "application/pdf":
return redirect(source_file.source_url)

file = source_file.as_pdf()
return HttpResponse(file.read(), content_type="application/pdf")

return self.make_response(
source_file.as_pdf(),
"application/pdf",
source_file.filename_for_download(".pdf"),
)
raise Http404()


Expand Down
Loading