core (xmpp): add name and media_type in attachements if they are missing.

This commit is contained in:
Goffi 2020-02-18 18:17:18 +01:00
commit 576c319d6e

View file

@ -21,6 +21,7 @@ import time
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 @@ class SatMessageProtocol(xmppim.MessageProtocol):
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 @@ class SatMessageProtocol(xmppim.MessageProtocol):
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"))