From 5f08f82cc2f15e15ab894ed4f0514fcc60348511 Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Tue, 1 Sep 2015 17:22:23 +0200 Subject: Added get_mail_attachment to MailStore and LeapMailStore. - Issue #435 --- .../pixelated/adapter/mailstore/leap_mailstore.py | 23 ++++++++++++++++++++++ service/pixelated/adapter/mailstore/mailstore.py | 3 +++ 2 files changed, 26 insertions(+) (limited to 'service/pixelated/adapter') diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 9b1afdd4..b72c8804 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -13,7 +13,9 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see . +import base64 from email.header import decode_header +import quopri import re from uuid import uuid4 from leap.mail.adaptors.soledad import SoledadMailAdaptor @@ -176,6 +178,27 @@ class LeapMailStore(MailStore): return defer.gatherResults(deferreds, consumeErrors=True) + @defer.inlineCallbacks + 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] + defer.returnValue({'content-type': content.content_type, 'content': self._try_decode( + content.raw, content.content_transfer_encoding)}) + else: + raise ValueError('No attachment with id %s found!' % attachment_id) + + def _try_decode(self, raw, encoding): + encoding = encoding.lower() + if encoding == 'base64': + data = base64.decodestring(raw) + elif encoding == 'quoted-printable': + data = quopri.decodestring(raw) + else: + data = str(raw) + + return bytearray(data) + @defer.inlineCallbacks def update_mail(self, mail): message = yield self._fetch_msg_from_soledad(mail.mail_id) diff --git a/service/pixelated/adapter/mailstore/mailstore.py b/service/pixelated/adapter/mailstore/mailstore.py index 425def92..60716dfe 100644 --- a/service/pixelated/adapter/mailstore/mailstore.py +++ b/service/pixelated/adapter/mailstore/mailstore.py @@ -19,6 +19,9 @@ class MailStore(object): def get_mail(self, mail_id): pass + def get_mail_attachment(self, attachment_id): + pass + def get_mails(self, mail_ids): pass -- cgit v1.2.3