diff options
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) |