changeset 3175:b9a2dd4d750a

core (xmpp): add `name` and `media_type` in attachements if they are missing.
author Goffi <goffi@goffi.org>
date Tue, 18 Feb 2020 18:17:18 +0100
parents c90f27ce52b0
children 4b5c77673015
files sat/core/xmpp.py
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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"))