From 71676763763858bf36fd267cd2002523ab4b1216 Mon Sep 17 00:00:00 2001 From: Andrey Rusakov Date: Wed, 6 Nov 2024 14:44:23 +0100 Subject: [PATCH] Report actual filetype when serving files for download --- api/views.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api/views.py b/api/views.py index a4215fe..1d4ccf8 100644 --- a/api/views.py +++ b/api/views.py @@ -1,3 +1,4 @@ +import mimetypes from pathlib import Path from django.conf import settings @@ -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