Consolidate file response methods
This commit is contained in:
parent
77913c2dea
commit
603da83705
@ -318,38 +318,12 @@ class DocumentViewSet(
|
|||||||
doc,
|
doc,
|
||||||
):
|
):
|
||||||
return HttpResponseForbidden("Insufficient permissions")
|
return HttpResponseForbidden("Insufficient permissions")
|
||||||
if not self.original_requested(request) and doc.has_archive_version:
|
return serve_file(
|
||||||
file_handle = doc.archive_file
|
doc=doc,
|
||||||
filename = doc.get_public_filename(archive=True)
|
use_archive=not self.original_requested(request)
|
||||||
mime_type = "application/pdf"
|
and doc.has_archive_version,
|
||||||
else:
|
disposition=disposition,
|
||||||
file_handle = doc.source_file
|
|
||||||
filename = doc.get_public_filename()
|
|
||||||
mime_type = doc.mime_type
|
|
||||||
# Support browser previewing csv files by using text mime type
|
|
||||||
if mime_type in {"application/csv", "text/csv"} and disposition == "inline":
|
|
||||||
mime_type = "text/plain"
|
|
||||||
|
|
||||||
if doc.storage_type == Document.STORAGE_TYPE_GPG:
|
|
||||||
file_handle = GnuPG.decrypted(file_handle)
|
|
||||||
|
|
||||||
response = HttpResponse(file_handle, content_type=mime_type)
|
|
||||||
# Firefox is not able to handle unicode characters in filename field
|
|
||||||
# RFC 5987 addresses this issue
|
|
||||||
# see https://datatracker.ietf.org/doc/html/rfc5987#section-4.2
|
|
||||||
# Chromium cannot handle commas in the filename
|
|
||||||
filename_normalized = normalize("NFKD", filename.replace(",", "_")).encode(
|
|
||||||
"ascii",
|
|
||||||
"ignore",
|
|
||||||
)
|
)
|
||||||
filename_encoded = quote(filename)
|
|
||||||
content_disposition = (
|
|
||||||
f"{disposition}; "
|
|
||||||
f'filename="{filename_normalized}"; '
|
|
||||||
f"filename*=utf-8''{filename_encoded}"
|
|
||||||
)
|
|
||||||
response["Content-Disposition"] = content_disposition
|
|
||||||
return response
|
|
||||||
|
|
||||||
def get_metadata(self, file, mime_type):
|
def get_metadata(self, file, mime_type):
|
||||||
if not os.path.isfile(file):
|
if not os.path.isfile(file):
|
||||||
@ -595,7 +569,6 @@ class DocumentViewSet(
|
|||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
if request.method == "GET":
|
if request.method == "GET":
|
||||||
try:
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
links = [
|
links = [
|
||||||
{
|
{
|
||||||
@ -609,19 +582,6 @@ class DocumentViewSet(
|
|||||||
.order_by("-created")
|
.order_by("-created")
|
||||||
]
|
]
|
||||||
return Response(links)
|
return Response(links)
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"An error occurred retrieving share links: {e!s}")
|
|
||||||
return Response(
|
|
||||||
{
|
|
||||||
"error": "Error retreiving share links, see logs for details.",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
return Response(
|
|
||||||
{
|
|
||||||
"error": "error",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class SearchResultSerializer(DocumentSerializer, PassUserMixin):
|
class SearchResultSerializer(DocumentSerializer, PassUserMixin):
|
||||||
@ -1205,9 +1165,15 @@ class SharedLinkView(View):
|
|||||||
return HttpResponseRedirect("/accounts/login/?sharelink_notfound=1")
|
return HttpResponseRedirect("/accounts/login/?sharelink_notfound=1")
|
||||||
if share_link.expiration is not None and share_link.expiration < timezone.now():
|
if share_link.expiration is not None and share_link.expiration < timezone.now():
|
||||||
return HttpResponseRedirect("/accounts/login/?sharelink_expired=1")
|
return HttpResponseRedirect("/accounts/login/?sharelink_expired=1")
|
||||||
try:
|
return serve_file(
|
||||||
doc = share_link.document
|
doc=share_link.document,
|
||||||
if share_link.document_version == "archive":
|
use_archive=share_link.document_version == "archive",
|
||||||
|
disposition="inline",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def serve_file(doc: Document, use_archive: bool, disposition: str):
|
||||||
|
if use_archive:
|
||||||
file_handle = doc.archive_file
|
file_handle = doc.archive_file
|
||||||
filename = doc.get_public_filename(archive=True)
|
filename = doc.get_public_filename(archive=True)
|
||||||
mime_type = "application/pdf"
|
mime_type = "application/pdf"
|
||||||
@ -1216,7 +1182,7 @@ class SharedLinkView(View):
|
|||||||
filename = doc.get_public_filename()
|
filename = doc.get_public_filename()
|
||||||
mime_type = doc.mime_type
|
mime_type = doc.mime_type
|
||||||
# Support browser previewing csv files by using text mime type
|
# Support browser previewing csv files by using text mime type
|
||||||
if mime_type in {"application/csv", "text/csv"}:
|
if mime_type in {"application/csv", "text/csv"} and disposition == "inline":
|
||||||
mime_type = "text/plain"
|
mime_type = "text/plain"
|
||||||
|
|
||||||
if doc.storage_type == Document.STORAGE_TYPE_GPG:
|
if doc.storage_type == Document.STORAGE_TYPE_GPG:
|
||||||
@ -1233,11 +1199,9 @@ class SharedLinkView(View):
|
|||||||
)
|
)
|
||||||
filename_encoded = quote(filename)
|
filename_encoded = quote(filename)
|
||||||
content_disposition = (
|
content_disposition = (
|
||||||
"inline; "
|
f"{disposition}; "
|
||||||
f'filename="{filename_normalized}"; '
|
f'filename="{filename_normalized}"; '
|
||||||
f"filename*=utf-8''{filename_encoded}"
|
f"filename*=utf-8''{filename_encoded}"
|
||||||
)
|
)
|
||||||
response["Content-Disposition"] = content_disposition
|
response["Content-Disposition"] = content_disposition
|
||||||
return response
|
return response
|
||||||
except (FileNotFoundError, Document.DoesNotExist):
|
|
||||||
raise Http404
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user