incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [01/25] git commit: [#4657] refactor File.serve to a general helper method
Date Mon, 08 Jul 2013 19:28:13 GMT
Updated Branches:
  refs/heads/cj/4656 6fc50ebd2 -> 58cc0246c (forced update)


[#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/43587016
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/43587016
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/43587016

Branch: refs/heads/cj/4656
Commit: 435870162a56a4b05d35a66865f34c4ebbdccf04
Parents: ff44014
Author: Dave Brondsema <dbrondsema@slashdotmedia.com>
Authored: Mon Jun 10 22:31:12 2013 +0000
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Wed Jul 3 21:35:16 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/43587016/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/43587016/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,


Mime
View raw message