Skip to content

Commit

Permalink
Use StreamingHttpResponse when downloading zip streams
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Jul 18, 2024
1 parent a0e1acd commit 4669adf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
11 changes: 9 additions & 2 deletions app/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from django.db import transaction
from django.http import FileResponse
from django.http import HttpResponse
from django.http import StreamingHttpResponse
from app.vendor import zipfly
from rest_framework import status, serializers, viewsets, filters, exceptions, permissions, parsers
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny
Expand Down Expand Up @@ -340,8 +342,13 @@ def download_file_response(request, filePath, content_disposition, download_file


def download_file_stream(request, stream, content_disposition, download_filename=None):
response = HttpResponse(FileWrapper(stream),
content_type=(mimetypes.guess_type(download_filename)[0] or "application/zip"))
if isinstance(stream, zipfly.ZipStream):
f = stream.generator()
else:
# This should never happen, but just in case..
raise exceptions.ValidationError("stream not a zipstream instance")

response = StreamingHttpResponse(f, content_type=(mimetypes.guess_type(download_filename)[0] or "application/zip"))

response['Content-Type'] = mimetypes.guess_type(download_filename)[0] or "application/zip"
response['Content-Disposition'] = "{}; filename={}".format(content_disposition, download_filename)
Expand Down
13 changes: 8 additions & 5 deletions app/vendor/zipfly.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def get(self):
def size(self):
return self._size


class ZipFly:

def __init__(self,
Expand Down Expand Up @@ -280,13 +279,17 @@ def get_size(self):
class ZipStream:
def __init__(self, paths):
self.paths = paths
self.generator = None
self._generator = None

def lazy_load(self, chunksize):
if self.generator is None:
if self._generator is None:
zfly = ZipFly(paths=self.paths, mode='w', chunksize=chunksize)
self.generator = zfly.generator()
self._generator = zfly.generator()

def read(self, count):
self.lazy_load(count)
return next(self.generator)
return next(self._generator)

def generator(self):
self.lazy_load(0x8000)
return self._generator
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "WebODM",
"version": "2.5.3",
"version": "2.5.4",
"description": "User-friendly, extendable application and API for processing aerial imagery.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 4669adf

Please sign in to comment.