Skip to content
Snippets Groups Projects
Commit 7bb1e586 authored by Brian Moe's avatar Brian Moe
Browse files

Mimetypes for file downloads. Issue #713

parent 007406da
No related branches found
No related tags found
No related merge requests found
......@@ -698,8 +698,12 @@ class Files(APIView):
response = HttpResponseNotFound("File not readable")
elif os.path.isfile(filepath):
# get an actual file.
response = HttpResponse(open(filepath, "r"), content_type="application/octet-stream")
response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(filename)
content_type, encoding = VersionedFile.guess_mimetype(filepath)
content_type = content_type or "application/octet-stream"
# XXX encoding should probably not be ignored.
response = HttpResponse(open(filepath, "r"), content_type=content_type)
if content_type == "application/octet-stream":
response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(filename)
elif not filename:
# Get list of files w/urls.
rv = {}
......
......@@ -4,7 +4,7 @@ import tempfile
import logging
import errno
import shutil
import mimetypes
class VersionedFile(file):
"""Open a versioned file.
......@@ -165,3 +165,21 @@ class VersionedFile(file):
# XXX file does not have a __del__ method. Should we?
if not self.closed:
self.close()
@staticmethod
def guess_mimetype(filename):
TEXT_EXTENSIONS = ['.log']
filename = VersionedFile.basename(filename)
content_type, encoding = mimetypes.guess_type(filename)
if content_type is None and '.' in filename:
for ext in TEXT_EXTENSIONS:
if filename.endswith(ext):
content_type = 'text/plain'
break
return content_type, encoding
@staticmethod
def basename(filename):
if ',' in filename:
filename = filename.split(',')[0]
return os.path.basename(filename)
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