Provide utf-8 encoded content-disposition field to address the issues in the firefox when downloading files

Fixes #56
This commit is contained in:
Marcin Gordziejewski 2022-02-15 22:37:09 +01:00
parent a10d331296
commit 940d056657

View File

@ -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):