diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-07-21 15:24:01 +0000 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2015-08-11 17:00:27 +0200 |
commit | cb0c0b7905be251191e9ef78908d84ed617283c8 (patch) | |
tree | 27b18154775599ff7098562b7dd34b9a71ed9eec /service | |
parent | 8d62e433994e65484f97dd4f89b2e4279ce4b3dd (diff) |
Fixed attachments_resource.py.
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/resources/attachments_resource.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 83c7156d..0a903cc4 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -21,6 +21,7 @@ from twisted.protocols.basic import FileSender from twisted.python.log import err from twisted.web import server from twisted.web.resource import Resource +from twisted.internet import defer class AttachmentResource(Resource): @@ -35,21 +36,26 @@ class AttachmentResource(Resource): def render_GET(self, request): encoding = request.args.get('encoding', [None])[0] filename = request.args.get('filename', [self.attachment_id])[0] - attachment = self.mail_service.attachment(self.attachment_id, encoding) - request.setHeader(b'Content-Type', b'application/force-download') request.setHeader(b'Content-Disposition', bytes('attachment; filename=' + filename)) + + d = self._send_attachment(encoding, filename, request) + d.addErrback(err) + + return server.NOT_DONE_YET + + @defer.inlineCallbacks + def _send_attachment(self, encoding, filename, request): + attachment = yield self.mail_service.attachment(self.attachment_id, encoding) + bytes_io = io.BytesIO(attachment['content']) - d = FileSender().beginFileTransfer(bytes_io, request) - def cb_finished(_): + try: + yield FileSender().beginFileTransfer(bytes_io, request) + finally: bytes_io.close() request.finish() - d.addErrback(err).addCallback(cb_finished) - - return server.NOT_DONE_YET - def _extract_mimetype(self, content_type): match = re.compile('([A-Za-z-]+\/[A-Za-z-]+)').search(content_type) return match.group(1) |