]> git.parisson.com Git - telemeta.git/commitdiff
add send_file filewrapper for various exports
authoryomguy <yomguy@parisson.com>
Mon, 17 Sep 2012 11:08:33 +0000 (13:08 +0200)
committeryomguy <yomguy@parisson.com>
Mon, 17 Sep 2012 11:08:33 +0000 (13:08 +0200)
README.rst
telemeta/views/base.py

index 7b8cd4e7bb82f1d2cdec9f33837dbb945d88d284..37f735100bcdbf79a53052120790cd7f02656980 100644 (file)
@@ -172,7 +172,7 @@ To get the lastest development version, you need subversion and run::
 License
 =======
 
-CeCILL v2, compatible with GPL v2 (see `LICENSE <http://github.com/yomguy/Telemeta/blob/master/LICENSE`_)
+CeCILL v2, compatible with GPL v2 (see `LICENSE <http://github.com/yomguy/Telemeta/blob/master/LICENSE>`_)
 
 
 Sponsors
index c4f93899717566c0e855ae754e4fb44e787ace99..30ad004df9df87fc51761587d4b3a642ef560c70 100644 (file)
@@ -65,6 +65,7 @@ from django.utils.translation import ugettext
 from django.contrib.auth.forms import UserChangeForm
 from django.core.exceptions import ObjectDoesNotExist
 from django.contrib.syndication.views import Feed
+from django.core.servers.basehttp import FileWrapper
 
 from telemeta.models import *
 import telemeta.models
@@ -84,6 +85,22 @@ mods = {'item': MediaItem, 'collection': MediaCollection,
 
 # TOOLS
 
+class FixedFileWrapper(FileWrapper):
+    def __iter__(self):
+        self.filelike.seek(0)
+        return self
+
+def send_file(request, filename, content_type='image/jpeg'):
+    """
+    Send a file through Django without loading the whole file into
+    memory at once. The FileWrapper will turn the file object into an
+    iterator for chunks of 8KB.
+    """
+    wrapper = FixedFileWrapper(file(filename, 'rb'))
+    response = HttpResponse(wrapper, content_type=content_type)
+    response['Content-Length'] = os.path.getsize(filename)
+    return response
+
 def render(request, template, data = None, mimetype = None):
     return render_to_response(template, data, context_instance=RequestContext(request),
                               mimetype=mimetype)