From 940d056657a046aac9a68eb2f2e6ad11d0a131bf Mon Sep 17 00:00:00 2001 From: Marcin Gordziejewski Date: Tue, 15 Feb 2022 22:37:09 +0100 Subject: [PATCH] Provide utf-8 encoded content-disposition field to address the issues in the firefox when downloading files Fixes #56 --- src/documents/views.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/documents/views.py b/src/documents/views.py index d8fcca2a9..6b4d02e49 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -5,6 +5,8 @@ import uuid import zipfile from datetime import datetime from time import mktime +from urllib.parse import quote_plus +from unicodedata import normalize from django.conf import settings from django.db.models import Count, Max, Case, When, IntegerField @@ -62,7 +64,6 @@ from .serialisers import ( BulkDownloadSerializer ) - logger = logging.getLogger("paperless.api") @@ -220,8 +221,16 @@ class DocumentViewSet(RetrieveModelMixin, file_handle = GnuPG.decrypted(file_handle) response = HttpResponse(file_handle, content_type=mime_type) - response["Content-Disposition"] = '{}; filename="{}"'.format( - disposition, filename) + # 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 + filename_normalized = normalize("NFKD", filename)\ + .encode('ascii', 'ignore') + filename_encoded = quote_plus(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):