From b93d49a1805bf9e9a9d93ea2aa6a1fccb5756a13 Mon Sep 17 00:00:00 2001 From: Gunnar Schaefer <gsfr@stanford.edu> Date: Thu, 19 Mar 2015 17:14:36 -0700 Subject: [PATCH] add streaming downloads --- containers.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/containers.py b/containers.py index 6fc5be2f..8e2d68ee 100644 --- a/containers.py +++ b/containers.py @@ -195,11 +195,14 @@ class Container(base.RequestHandler): self.abort(404, 'no such file') filename = file_info['name'] + file_info['ext'] filepath = os.path.join(self.app.config['data_path'], str(_id)[-3:] + '/' + str(_id), filename) - ticket = util.download_ticket('single', filepath, filename, file_info['size']) - tkt_id = self.app.db.downloads.insert(ticket) if self.request.method == 'GET': - self.redirect_to('download', _abort=True, ticket=tkt_id) - return {'url': self.uri_for('download', _full=True, ticket=tkt_id)} + self.response.app_iter = open(filepath, 'rb') + self.response.headers['Content-Length'] = str(file_info['size']) # must be set after setting app_iter + self.response.headers['Content-Type'] = 'application/octet-stream' + else: + ticket = util.download_ticket('single', filepath, filename, file_info['size']) + tkt_id = self.app.db.downloads.insert(ticket) + return {'url': self.uri_for('download', _full=True, ticket=tkt_id)} def put_file(self, cid=None): """ @@ -328,12 +331,14 @@ class Container(base.RequestHandler): break else: self.abort(404, 'no such file') - - ticket = util.download_ticket('single', fpath, fname, a_info['size']) - tkt_id = self.app.db.downloads.insert(ticket) if self.request.method == 'GET': - self.redirect_to('download', _abort=True, ticket=tkt_id) - return {'url': self.uri_for('download', _full=True, ticket=tkt_id)} + self.response.app_iter = open(fpath, 'rb') + self.response.headers['Content-Length'] = str(a_info['size']) # must be set after setting app_iter + self.response.headers['Content-Type'] = 'application/octet-stream' + else: + ticket = util.download_ticket('single', fpath, fname, a_info['size']) + tkt_id = self.app.db.downloads.insert(ticket) + return {'url': self.uri_for('download', _full=True, ticket=tkt_id)} def delete_attachment(self, cid): """Delete one attachment.""" -- GitLab