core (xmpp): check presence of some keys in attachment before using them:

in `completeAttachments` `url` was used to complete a missing name, but it may be missing
with some attachments, thus its presence is now checked.
This commit is contained in:
Goffi 2022-10-06 16:02:05 +02:00
commit 5b40d71f40

View file

@ -1302,11 +1302,12 @@ class SatMessageProtocol(xmppim.MessageProtocol):
def completeAttachments(self, data):
"""Complete missing metadata of attachments"""
for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []):
if "name" not in attachment:
if "name" not in attachment and "url" in attachment:
name = (Path(unquote(urlparse(attachment['url']).path)).name
or C.FILE_DEFAULT_NAME)
attachment["name"] = name
if C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE not in attachment:
if ((C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE not in attachment
and "name" in attachment)):
media_type = mimetypes.guess_type(attachment['name'], strict=False)[0]
if media_type:
attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] = media_type