From e5d718f982e0cd3fc85da00d3abdccce1907e488 Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Wed, 2 Sep 2015 12:35:33 +0200 Subject: Download attachments from mail store instead of querier - Issue #435 - Improved error handling of attachment resource --- service/pixelated/adapter/mailstore/leap_mailstore.py | 4 ++-- service/pixelated/adapter/services/mail_service.py | 4 ++-- service/pixelated/resources/attachments_resource.py | 11 ++++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) (limited to 'service/pixelated') diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index a3a5e984..993f413c 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -18,7 +18,7 @@ from email.header import decode_header import quopri import re from uuid import uuid4 -from leap.mail.adaptors.soledad import SoledadMailAdaptor +from leap.mail.adaptors.soledad import SoledadMailAdaptor, ContentDocWrapper from twisted.internet import defer from pixelated.adapter.mailstore.body_parser import BodyParser from pixelated.adapter.mailstore.mailstore import MailStore, underscore_uuid @@ -182,7 +182,7 @@ class LeapMailStore(MailStore): def get_mail_attachment(self, attachment_id): results = yield self.soledad.get_from_index('by-type-and-payloadhash', 'cnt', attachment_id) if attachment_id else [] if len(results): - content = results[0] + content = ContentDocWrapper(**results[0].content) defer.returnValue({'content-type': content.content_type, 'content': self._try_decode( content.raw, content.content_transfer_encoding)}) else: diff --git a/service/pixelated/adapter/services/mail_service.py b/service/pixelated/adapter/services/mail_service.py index 4ff42046..2c576e98 100644 --- a/service/pixelated/adapter/services/mail_service.py +++ b/service/pixelated/adapter/services/mail_service.py @@ -65,8 +65,8 @@ class MailService(object): def mail(self, mail_id): return self.mail_store.get_mail(mail_id, include_body=True) - def attachment(self, attachment_id, encoding): - return self.querier.attachment(attachment_id, encoding) + def attachment(self, attachment_id): + return self.mail_store.get_mail_attachment(attachment_id) @defer.inlineCallbacks def mail_exists(self, mail_id): diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 0a903cc4..a78022ec 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -18,7 +18,7 @@ import io import re from twisted.protocols.basic import FileSender -from twisted.python.log import err +from twisted.python.log import msg from twisted.web import server from twisted.web.resource import Resource from twisted.internet import defer @@ -34,23 +34,28 @@ class AttachmentResource(Resource): self.mail_service = mail_service def render_GET(self, request): + def error_handler(failure): + msg(failure, 'attachment not found') + request.code = 404 + request.finish() encoding = request.args.get('encoding', [None])[0] filename = request.args.get('filename', [self.attachment_id])[0] 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) + d.addErrback(error_handler) return server.NOT_DONE_YET @defer.inlineCallbacks def _send_attachment(self, encoding, filename, request): - attachment = yield self.mail_service.attachment(self.attachment_id, encoding) + attachment = yield self.mail_service.attachment(self.attachment_id) bytes_io = io.BytesIO(attachment['content']) try: + request.code = 200 yield FileSender().beginFileTransfer(bytes_io, request) finally: bytes_io.close() -- cgit v1.2.3