diff sat/plugins/plugin_misc_attach.py @ 4023:78b5f356900c

component AP gateway: handle attachments
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2023 15:42:21 +0100
parents 0ff265725489
children 524856bd7b19
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_attach.py	Thu Mar 23 15:39:48 2023 +0100
+++ b/sat/plugins/plugin_misc_attach.py	Thu Mar 23 15:42:21 2023 +0100
@@ -59,6 +59,7 @@
         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 @@
         """
         # 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 @@
                             _("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 @@
         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 @@
 
     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 @@
         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():