comparison sat/core/xmpp.py @ 3920:993cc8e56aef

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.
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 16:02:05 +0200
parents 944f51f9c2b4
children f7fa3993df28
comparison
equal deleted inserted replaced
3919:7b2c51bcc8f5 3920:993cc8e56aef
1300 d.addCallback(self._onMessageStartWorkflow, client, message_elt, post_treat) 1300 d.addCallback(self._onMessageStartWorkflow, client, message_elt, post_treat)
1301 1301
1302 def completeAttachments(self, data): 1302 def completeAttachments(self, data):
1303 """Complete missing metadata of attachments""" 1303 """Complete missing metadata of attachments"""
1304 for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []): 1304 for attachment in data['extra'].get(C.MESS_KEY_ATTACHMENTS, []):
1305 if "name" not in attachment: 1305 if "name" not in attachment and "url" in attachment:
1306 name = (Path(unquote(urlparse(attachment['url']).path)).name 1306 name = (Path(unquote(urlparse(attachment['url']).path)).name
1307 or C.FILE_DEFAULT_NAME) 1307 or C.FILE_DEFAULT_NAME)
1308 attachment["name"] = name 1308 attachment["name"] = name
1309 if C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE not in attachment: 1309 if ((C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE not in attachment
1310 and "name" in attachment)):
1310 media_type = mimetypes.guess_type(attachment['name'], strict=False)[0] 1311 media_type = mimetypes.guess_type(attachment['name'], strict=False)[0]
1311 if media_type: 1312 if media_type:
1312 attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] = media_type 1313 attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] = media_type
1313 1314
1314 return data 1315 return data