diff --git a/src/documents/views.py b/src/documents/views.py index 7298391f2..c8062f9ca 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -32,6 +32,7 @@ from django.db.models import When from django.db.models.functions import Length from django.db.models.functions import Lower from django.db.models.manager import Manager +from django.http import FileResponse from django.http import Http404 from django.http import HttpResponse from django.http import HttpResponseBadRequest @@ -2015,10 +2016,11 @@ class BulkDownloadView(GenericAPIView): return HttpResponseForbidden("Insufficient permissions") settings.SCRATCH_DIR.mkdir(parents=True, exist_ok=True) - temp = tempfile.NamedTemporaryFile( # noqa: SIM115 - dir=settings.SCRATCH_DIR, - suffix="-compressed-archive", - delete=False, + temp_dir = Path( + tempfile.TemporaryDirectory( + dir=settings.SCRATCH_DIR, + delete=False, + ), ) if content == "both": @@ -2028,20 +2030,14 @@ class BulkDownloadView(GenericAPIView): else: strategy_class = ArchiveOnlyStrategy - with zipfile.ZipFile(temp.name, "w", compression) as zipf: + zip_file = temp_dir / "documents.zip" + + with zipfile.ZipFile(zip_file, "w", compression) as zipf: strategy = strategy_class(zipf, follow_formatting=follow_filename_format) for document in documents: strategy.add_document(document) - # TODO(stumpylog): Investigate using FileResponse here - with open(temp.name, "rb") as f: - response = HttpResponse(f, content_type="application/zip") - response["Content-Disposition"] = '{}; filename="{}"'.format( - "attachment", - "documents.zip", - ) - - return response + return FileResponse(zip_file.open("rb"), as_attachment=True) @extend_schema_view(**generate_object_with_permissions_schema(StoragePathSerializer))