# HG changeset patch # User Goffi # Date 1582046238 -3600 # Node ID b9a2dd4d750afc04e2617962ba9ba2f32622f818 # Parent c90f27ce52b0025324ba59d6bf9adec032216f13 core (xmpp): add `name` and `media_type` in attachements if they are missing. diff -r c90f27ce52b0 -r b9a2dd4d750a sat/core/xmpp.py --- a/sat/core/xmpp.py Tue Feb 18 18:17:18 2020 +0100 +++ b/sat/core/xmpp.py Tue Feb 18 18:17:18 2020 +0100 @@ -21,6 +21,7 @@ import calendar import uuid import mimetypes +from urllib.parse import urlparse, unquote from functools import partial import shortuuid from sat.core.i18n import _ @@ -1102,6 +1103,7 @@ if not cont: return data = self.parseMessage(message_elt) + post_treat.addCallback(self.completeAttachments) post_treat.addCallback(self.skipEmptyMessage) post_treat.addCallback(self.addToHistory) post_treat.addCallback(self.bridgeSignal, data) @@ -1125,6 +1127,18 @@ d.addCallback(self._onMessageStartWorkflow, client, message_elt, post_treat) + def completeAttachments(self, data): + """Complete missing metadata of attachments""" + for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []): + if "name" not in attachment: + name = unquote(urlparse(attachment['url']).path.rsplit('/', 1)[-1]) + attachment["name"] = name + if C.MESS_KEY_MEDIA_TYPE not in attachment: + media_type = mimetypes.guess_type(attachment['url'], strict=False)[0] + if media_type: + attachment[C.MESS_KEY_MEDIA_TYPE] = media_type + return data + def skipEmptyMessage(self, data): if not data["message"] and not data["extra"] and not data["subject"]: raise failure.Failure(exceptions.CancelError("Cancelled empty message"))