You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2013/07/02 21:44:35 UTC

[3/3] git commit: [#4657] refactor File.serve to a general helper method

[#4657] refactor File.serve to a general helper method


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/d9b5b87e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/d9b5b87e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/d9b5b87e

Branch: refs/heads/db/4657
Commit: d9b5b87e4cb717a394fc41237edc96fcd9da3d92
Parents: ea38582
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Mon Jun 10 22:31:12 2013 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Tue Jul 2 19:44:20 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/utils.py        | 25 ++++++++++++++++++++++++-
 Allura/allura/model/filesystem.py | 20 +++++---------------
 2 files changed, 29 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d9b5b87e/Allura/allura/lib/utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 7c490e2..a88511c 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -37,7 +37,7 @@ from formencode import Invalid
 from tg.decorators import before_validate
 from pylons import response
 from pylons import tmpl_context as c
-from paste.deploy.converters import asbool
+from paste.deploy.converters import asbool, asint
 from paste.httpheaders import CACHE_CONTROL, EXPIRES
 from webhelpers.html import literal
 from webob import exc
@@ -471,3 +471,26 @@ def take_while_true(source):
         yield x
         x = source()
 
+
+def serve_file(fp, filename, content_type, last_modified=None, cache_expires=None, size=None, embed=True):
+    '''Sets the response headers and serves as a wsgi iter'''
+    pylons.response.headers['Content-Type'] = ''
+    pylons.response.content_type = content_type.encode('utf-8')
+    pylons.response.cache_expires = cache_expires or asint(tg.config.get('files_expires_header_secs', 60 * 60))
+    pylons.response.last_modified = last_modified
+    if size:
+        pylons.response.content_length = size
+    if 'Pragma' in pylons.response.headers:
+        del pylons.response.headers['Pragma']
+    if 'Cache-Control' in pylons.response.headers:
+        del pylons.response.headers['Cache-Control']
+    if not embed:
+        pylons.response.headers.add(
+            'Content-Disposition',
+            'attachment;filename="%s"' % filename.encode('utf-8'))
+    # http://code.google.com/p/modwsgi/wiki/FileWrapperExtension
+    block_size = 4096
+    if 'wsgi.file_wrapper' in tg.request.environ:
+        return tg.request.environ['wsgi.file_wrapper'](fp, block_size)
+    else:
+        return iter(lambda: fp.read(block_size), '')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d9b5b87e/Allura/allura/model/filesystem.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/filesystem.py b/Allura/allura/model/filesystem.py
index c56e595..1bb0087 100644
--- a/Allura/allura/model/filesystem.py
+++ b/Allura/allura/model/filesystem.py
@@ -23,7 +23,6 @@ import pylons
 import PIL
 from gridfs import GridFS
 from tg import config
-from paste.deploy.converters import asint
 
 from ming import schema
 from ming.orm import session, FieldProperty
@@ -109,20 +108,11 @@ class File(MappedClass):
 
     def serve(self, embed=True):
         '''Sets the response headers and serves as a wsgi iter'''
-        fp = self.rfile()
-        pylons.response.headers['Content-Type'] = ''
-        pylons.response.content_type = self.content_type.encode('utf-8')
-        pylons.response.cache_expires = asint(config.get('files_expires_header_secs', 60 * 60))
-        pylons.response.last_modified = self._id.generation_time
-        if 'Pragma' in pylons.response.headers:
-            del pylons.response.headers['Pragma']
-        if 'Cache-Control' in pylons.response.headers:
-            del pylons.response.headers['Cache-Control']
-        if not embed:
-            pylons.response.headers.add(
-                'Content-Disposition',
-                'attachment;filename="%s"' % self.filename.encode('utf-8'))
-        return iter(fp)
+        gridfs_file = self.rfile()
+        return utils.serve_file(gridfs_file, self.filename, self.content_type,
+                                last_modified=self._id.generation_time,
+                                size=gridfs_file.length,
+                                embed=embed)
 
     @classmethod
     def save_thumbnail(cls, filename, image,