Skip to content
Snippets Groups Projects
Unverified Commit 8d9d112a authored by Fredrik Jonsson's avatar Fredrik Jonsson Committed by GitHub
Browse files

Merge pull request #1676 from OpenTechFund/private-media-use-file-response

Use FileResponse as in Django 2.1 it tries to set the Content-Type, Content-Length headers
parents 0c63dcb7 47e658fc
No related branches found
No related tags found
No related merge requests found
import mimetypes
import os
from wsgiref.util import FileWrapper
from django.conf import settings
from django.core.files.storage import get_storage_class
from django.http import StreamingHttpResponse
from django.http import FileResponse
from django.views.generic import View
......@@ -21,21 +17,4 @@ class PrivateMediaView(View):
def get(self, *args, **kwargs):
file_to_serve = self.get_media(*args, **kwargs)
wrapper = FileWrapper(file_to_serve)
encoding_map = {
'bzip2': 'application/x-bzip',
'gzip': 'application/gzip',
'xz': 'application/x-xz',
}
file_name = os.path.basename(file_to_serve.name)
content_type, encoding = mimetypes.guess_type(file_name)
# Encoding isn't set to prevent browsers from automatically uncompressing files.
content_type = encoding_map.get(encoding, content_type)
content_type = content_type or 'application/octet-stream'
# From Django 2.1, we can use FileResponse instead of StreamingHttpResponse
response = StreamingHttpResponse(wrapper, content_type=content_type)
response['Content-Disposition'] = f'filename={file_name}'
response['Content-Length'] = file_to_serve.size
return response
return FileResponse(file_to_serve)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment