component AP gateway: handle attachments

This commit is contained in:
Goffi 2023-03-23 15:42:21 +01:00
commit c3f97ec712

View file

@ -59,6 +59,7 @@ class AttachPlugin:
self.host = host
self._u = host.plugins["UPLOAD"]
host.trigger.add("sendMessage", self._sendMessageTrigger)
host.trigger.add("sendMessageComponent", self._sendMessageTrigger)
self._attachments_handlers = {'clear': [], 'encrypted': []}
self.register(self.defaultCanHandle, self.defaultAttach, False, -1000)
@ -100,13 +101,13 @@ class AttachPlugin:
"""
# we check attachment for pre-treatment like large image resizing
# media_type will be added if missing (and if it can be guessed from path)
attachments = data["extra"][C.MESS_KEY_ATTACHMENTS]
attachments = data["extra"][C.KEY_ATTACHMENTS]
tmp_dirs_to_clean = []
for attachment in attachments:
if attachment.get(C.MESS_KEY_ATTACHMENTS_RESIZE, False):
if attachment.get(C.KEY_ATTACHMENTS_RESIZE, False):
path = Path(attachment["path"])
try:
media_type = attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE]
media_type = attachment[C.KEY_ATTACHMENTS_MEDIA_TYPE]
except KeyError:
media_type = mimetypes.guess_type(path, strict=False)[0]
if media_type is None:
@ -114,7 +115,7 @@ class AttachPlugin:
_("Can't resize attachment of unknown type: {attachment}")
.format(attachment=attachment))
continue
attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] = media_type
attachment[C.KEY_ATTACHMENTS_MEDIA_TYPE] = media_type
main_type = media_type.split('/')[0]
if main_type == "image":
@ -188,6 +189,9 @@ class AttachPlugin:
attachments = data["extra"]["attachments"]
for attachment in attachments:
if "url" in attachment and not "path" in attachment:
log.debug(f"attachment is external, we don't upload it: {attachment}")
continue
try:
# we pop path because we don't want it to be stored, as the file can be
# only in a temporary location
@ -252,7 +256,7 @@ class AttachPlugin:
def _sendMessageTrigger(
self, client, mess_data, pre_xml_treatments, post_xml_treatments):
if mess_data['extra'].get(C.MESS_KEY_ATTACHMENTS):
if mess_data['extra'].get(C.KEY_ATTACHMENTS):
post_xml_treatments.addCallback(self._attachFiles, client=client)
return True
@ -265,7 +269,7 @@ class AttachPlugin:
body_elt = data["xml"].body
if body_elt is None:
body_elt = data["xml"].addElement("body")
attachments = data["extra"][C.MESS_KEY_ATTACHMENTS]
attachments = data["extra"][C.KEY_ATTACHMENTS]
if attachments:
body_links = '\n'.join(a['url'] for a in attachments)
if str(body_elt).strip():