Skip to content

Commit

Permalink
Report actual filetype when serving files for download
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Rusakov authored and lanseg committed Nov 6, 2024
1 parent 616b1de commit 7167676
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mimetypes
from pathlib import Path

from django.conf import settings
Expand Down Expand Up @@ -607,10 +608,13 @@ def get(self, request, guid):
if instance.extract_result:
file = Path(settings.MEDIA_ROOT, instance.extract_result.name)
if file.is_file():
(actualType, unused) = mimetypes.guess_type(file)
with open(file, 'rb') as result:
response = Response(
headers={'Content-Disposition': 'attachment; filename="report.zip"'},
content_type='application/zip'
headers={
'Content-Disposition': f'attachment; filename="${file.name}"',
'Content-Type': actualType if actualType else 'application/octet-stream'
},
)
response.content = result.read()
return response
Expand Down

0 comments on commit 7167676

Please sign in to comment.