]> git.parisson.com Git - telemeta.git/commitdiff
add modules
authorGuillaume Pellerin <yomguy@parisson.com>
Wed, 15 May 2013 09:27:11 +0000 (11:27 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Wed, 15 May 2013 09:27:11 +0000 (11:27 +0200)
telemeta/views/collection.py
telemeta/views/core.py

index 7fb0def438c29b8dc8530e4f57f1a66b3cb17527..e4821e21d3f9080151b4634658ef707034c4b206 100644 (file)
@@ -160,3 +160,29 @@ class CollectionView(object):
             formset = MediaCollectionRelatedFormSet(instance=collection)
 
         return render(request, template, {'collection': collection, 'formset': formset,})
+
+
+class CollectionPackageView(DetailView):
+
+    model = MediaCollection
+
+    def render_to_reponse(self, context):
+        """
+        Create a ZIP file on disk and transmit it in chunks of 8KB,
+        without loading the whole file into memory. A similar approach can
+        be used for large dynamic PDF files.
+        """
+        collection = self.get_object()
+        temp = tempfile.TemporaryFile()
+        archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
+        for item in collection.items.all():
+            ext = item.file.path.splitext()[1]
+            archive.write(item.file, '%s.%s' % (code, ext))
+        archive.close()
+        wrapper = FileWrapper(temp)
+        response = HttpResponse(wrapper, content_type='application/zip')
+        response['Content-Disposition'] = "attachment; filename=%s.%s" % \
+                                             (item.code, 'zip')
+        response['Content-Length'] = temp.tell()
+        temp.seek(0)
+        return response
index f57f35e09e3ac27506ef826a7dc1ab1c2c44a9d9..9edf6d33534f2bd18ade9f8e6dc9a48ca1b3c55d 100644 (file)
@@ -41,6 +41,8 @@ import csv
 import time
 import random
 import datetime
+import tempfile
+import zipfile
 import timeside
 
 from jsonrpc import jsonrpc_method
@@ -107,8 +109,8 @@ def nginx_media_accel(request, filename):
 
     response = HttpResponse()
     url = settings.MEDIA_URL + filename
-    # let nginx determine the correct content type 
-    response['Content-Type'] = ""     
+    # let nginx determine the correct content type
+    response['Content-Type'] = ""
     response['X-Accel-Redirect'] = url
     return response