Skip to content

Commit

Permalink
🐛 Support async and sync servers in DocumentCollectionZipDownloadView
Browse files Browse the repository at this point in the history
A StreamingHttpResponse for ASGI needs an async iterator, else it will buffer everything in memory. Same with WSGI and sync iterators. This adds a StreamingHttpResponse interface that supports both
  • Loading branch information
pajowu committed Nov 20, 2024
1 parent b166ab3 commit 85f35e5
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/filingcabinet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ def get_context_data(self, **kwargs):
return context


async def as_aiter(iter):
for i in iter:
yield i


class ZipStreamResponse(StreamingHttpResponse):
@property
def streaming_content(self):
return self._sc

@streaming_content.setter
def streaming_content(self, value):
self._sc = value

def __iter__(self):
return iter(self._sc)

def __aiter__(self):
return as_aiter(iter(self._sc))


class DocumentCollectionZipDownloadView(AuthMixin, PkSlugMixin, DetailView):
model = DocumentCollection
redirect_url_name = "filingcabinet:document-collection_zip"
Expand Down Expand Up @@ -236,7 +257,7 @@ def render_to_response(self, context):
arcname=ensure_unique_filename(filename_counter, filename),
)

resp = StreamingHttpResponse(
resp = ZipStreamResponse(
archive_stream,
content_type="application/zip",
)
Expand Down

0 comments on commit 85f35e5

Please sign in to comment.