Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Unicode characters are not formatted properly in Content-Disposition field, causes issues in firefox
<!---
=> Before opening an issue, please check the documentation and see if it helps you resolve your issue: https://paperless-ng.readthedocs.io/en/latest/troubleshooting.html
=> Please also make sure that you followed the installation instructions.
=> Please search the issues and look for similar issues before opening a bug report.

=> If you would like to submit a feature request please submit one under https://github.com/jonaswinkler/paperless-ng/discussions/categories/feature-requests

=> If you encounter issues while installing of configuring Paperless-ng, please post that in the "Support" section of the discussions. Remember that Paperless successfully runs on a variety of different systems. If paperless does not start, it's probably an issue with your system, and not an issue of paperless.

=> Don't remove the [BUG] prefix from the title.
-->

**Describe the bug**
When the filename of the file that you try to download from paperless contains UTF-8 characters after formatting, firefox will not able to properly format the resulting filename, causing issues when saving and trying to preview it. 

Related issue in original repository https://github.com/jonaswinkler/paperless-ng/issues/838

**To Reproduce**
Steps to reproduce the behavior:
0. Use Firefox
1. Create a new document in paperless
2. Create correspondent with UTF-8 characters like "Urząd Gorzów Document"
3. Assign the correspondent to the newly created document
4. Attempt to download the file
5. File will be saved as `.pdf` instead of proper name

**Expected behavior**
File should save as `Urząd Gorzów Document.pd`

**Screenshots**
![image](https://user-images.githubusercontent.com/655590/154153312-5dca96e6-5ea6-4c40-a971-5165f1a7fce2.png)

**Webserver logs**
There is no error in logs.

**Relevant information**
 - Linux - Manjaro
 - Firefox
 - 96.0.3 (64-bit)
 - Installation method: docker
 - Default configuration



Discussion:

Resolving patch:
diff --git a/src/documents/views.py b/src/documents/views.py
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -5,6 +5,8 @@
 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 @@
     BulkDownloadSerializer
 )
 
-
 logger = logging.getLogger("paperless.api")
 
 
@@ -220,8 +221,16 @@ def file_response(self, pk, request, disposition):
             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):


Test patch: