Commit 50e9d469 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

store generated .zip files in /tmp

close #272
parent b7572f2e
Pipeline #40102 failed with stage
in 1 minute and 19 seconds
......@@ -15,6 +15,7 @@ import json
import logging
import os
import shutil
import tempfile
import zipfile
import natsort
......@@ -29,7 +30,7 @@ from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.db import transaction
from django.db.models import Count
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect, FileResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse, reverse_lazy
from django.utils.crypto import get_random_string
......@@ -906,14 +907,18 @@ class JobFileDownloadAll(LoginRequiredMixin, SingleObjectMixin, View):
The method gets the job ID, recover each file related to this job,
archive into a ZIP file and return it.
The ZIP file is stored as an anonymous file in /tmp/ then streamed with
FileResponse. This is better that keepingthe whole file in memory
because it may be large (and linux has a quite efficient page cache).
"""
job = self.get_object()
s = io.BytesIO()
tmp_file = tempfile.TemporaryFile()
zip_subdir = str(job.id)
zip_filename = 'job_%s.zip' % zip_subdir
zip_file = zipfile.ZipFile(s, 'w')
zip_file = zipfile.ZipFile(tmp_file, 'w')
for filename in os.listdir(job.data_dir):
# NOTE: calling lookup_job_file is a security feature
......@@ -924,7 +929,8 @@ class JobFileDownloadAll(LoginRequiredMixin, SingleObjectMixin, View):
zip_file.close()
response = HttpResponse(s.getvalue(), content_type='application/x-zip-compressed')
tmp_file.seek(0)
response = FileResponse(tmp_file, content_type='application/x-zip-compressed')
response["Content-Disposition"] = "attachment; filename={0}".format(zip_filename)
return response
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment