Return-Path: X-Original-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D976010569 for ; Mon, 8 Jul 2013 19:28:13 +0000 (UTC) Received: (qmail 91255 invoked by uid 500); 8 Jul 2013 19:28:13 -0000 Delivered-To: apmail-incubator-allura-commits-archive@incubator.apache.org Received: (qmail 91238 invoked by uid 500); 8 Jul 2013 19:28:13 -0000 Mailing-List: contact allura-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: allura-dev@incubator.apache.org Delivered-To: mailing list allura-commits@incubator.apache.org Received: (qmail 91228 invoked by uid 99); 8 Jul 2013 19:28:13 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Jul 2013 19:28:13 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 84DE78884FA; Mon, 8 Jul 2013 19:28:13 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: johnsca@apache.org To: allura-commits@incubator.apache.org Date: Mon, 08 Jul 2013 19:28:13 -0000 Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: [01/25] git commit: [#4657] refactor File.serve to a general helper method 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 Authored: Mon Jun 10 22:31:12 2013 +0000 Committer: Dave Brondsema 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,