Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
4022:cdb7de398c85 | 4023:78b5f356900c |
---|---|
57 def __init__(self, host): | 57 def __init__(self, host): |
58 log.info(_("plugin Attach initialization")) | 58 log.info(_("plugin Attach initialization")) |
59 self.host = host | 59 self.host = host |
60 self._u = host.plugins["UPLOAD"] | 60 self._u = host.plugins["UPLOAD"] |
61 host.trigger.add("sendMessage", self._sendMessageTrigger) | 61 host.trigger.add("sendMessage", self._sendMessageTrigger) |
62 host.trigger.add("sendMessageComponent", self._sendMessageTrigger) | |
62 self._attachments_handlers = {'clear': [], 'encrypted': []} | 63 self._attachments_handlers = {'clear': [], 'encrypted': []} |
63 self.register(self.defaultCanHandle, self.defaultAttach, False, -1000) | 64 self.register(self.defaultCanHandle, self.defaultAttach, False, -1000) |
64 | 65 |
65 def register(self, can_handle, attach, encrypted=False, priority=0): | 66 def register(self, can_handle, attach, encrypted=False, priority=0): |
66 """Register an attachments handler | 67 """Register an attachments handler |
98 | 99 |
99 It will do generic pre-treatment, and call the suitable attachments handler | 100 It will do generic pre-treatment, and call the suitable attachments handler |
100 """ | 101 """ |
101 # we check attachment for pre-treatment like large image resizing | 102 # we check attachment for pre-treatment like large image resizing |
102 # media_type will be added if missing (and if it can be guessed from path) | 103 # media_type will be added if missing (and if it can be guessed from path) |
103 attachments = data["extra"][C.MESS_KEY_ATTACHMENTS] | 104 attachments = data["extra"][C.KEY_ATTACHMENTS] |
104 tmp_dirs_to_clean = [] | 105 tmp_dirs_to_clean = [] |
105 for attachment in attachments: | 106 for attachment in attachments: |
106 if attachment.get(C.MESS_KEY_ATTACHMENTS_RESIZE, False): | 107 if attachment.get(C.KEY_ATTACHMENTS_RESIZE, False): |
107 path = Path(attachment["path"]) | 108 path = Path(attachment["path"]) |
108 try: | 109 try: |
109 media_type = attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] | 110 media_type = attachment[C.KEY_ATTACHMENTS_MEDIA_TYPE] |
110 except KeyError: | 111 except KeyError: |
111 media_type = mimetypes.guess_type(path, strict=False)[0] | 112 media_type = mimetypes.guess_type(path, strict=False)[0] |
112 if media_type is None: | 113 if media_type is None: |
113 log.warning( | 114 log.warning( |
114 _("Can't resize attachment of unknown type: {attachment}") | 115 _("Can't resize attachment of unknown type: {attachment}") |
115 .format(attachment=attachment)) | 116 .format(attachment=attachment)) |
116 continue | 117 continue |
117 attachment[C.MESS_KEY_ATTACHMENTS_MEDIA_TYPE] = media_type | 118 attachment[C.KEY_ATTACHMENTS_MEDIA_TYPE] = media_type |
118 | 119 |
119 main_type = media_type.split('/')[0] | 120 main_type = media_type.split('/')[0] |
120 if main_type == "image": | 121 if main_type == "image": |
121 report = image.check(self.host, path) | 122 report = image.check(self.host, path) |
122 if report['too_large']: | 123 if report['too_large']: |
186 uploads_d = [] | 187 uploads_d = [] |
187 to_delete = [] | 188 to_delete = [] |
188 attachments = data["extra"]["attachments"] | 189 attachments = data["extra"]["attachments"] |
189 | 190 |
190 for attachment in attachments: | 191 for attachment in attachments: |
192 if "url" in attachment and not "path" in attachment: | |
193 log.debug(f"attachment is external, we don't upload it: {attachment}") | |
194 continue | |
191 try: | 195 try: |
192 # we pop path because we don't want it to be stored, as the file can be | 196 # we pop path because we don't want it to be stored, as the file can be |
193 # only in a temporary location | 197 # only in a temporary location |
194 path = Path(attachment.pop("path")) | 198 path = Path(attachment.pop("path")) |
195 except KeyError: | 199 except KeyError: |
250 def _attachFiles(self, data, client): | 254 def _attachFiles(self, data, client): |
251 return defer.ensureDeferred(self.attachFiles(client, data)) | 255 return defer.ensureDeferred(self.attachFiles(client, data)) |
252 | 256 |
253 def _sendMessageTrigger( | 257 def _sendMessageTrigger( |
254 self, client, mess_data, pre_xml_treatments, post_xml_treatments): | 258 self, client, mess_data, pre_xml_treatments, post_xml_treatments): |
255 if mess_data['extra'].get(C.MESS_KEY_ATTACHMENTS): | 259 if mess_data['extra'].get(C.KEY_ATTACHMENTS): |
256 post_xml_treatments.addCallback(self._attachFiles, client=client) | 260 post_xml_treatments.addCallback(self._attachFiles, client=client) |
257 return True | 261 return True |
258 | 262 |
259 async def defaultCanHandle(self, client, data): | 263 async def defaultCanHandle(self, client, data): |
260 return True | 264 return True |
263 await self.upload_files(client, data) | 267 await self.upload_files(client, data) |
264 # TODO: handle xhtml-im | 268 # TODO: handle xhtml-im |
265 body_elt = data["xml"].body | 269 body_elt = data["xml"].body |
266 if body_elt is None: | 270 if body_elt is None: |
267 body_elt = data["xml"].addElement("body") | 271 body_elt = data["xml"].addElement("body") |
268 attachments = data["extra"][C.MESS_KEY_ATTACHMENTS] | 272 attachments = data["extra"][C.KEY_ATTACHMENTS] |
269 if attachments: | 273 if attachments: |
270 body_links = '\n'.join(a['url'] for a in attachments) | 274 body_links = '\n'.join(a['url'] for a in attachments) |
271 if str(body_elt).strip(): | 275 if str(body_elt).strip(): |
272 # if there is already a body, we add a line feed before the first link | 276 # if there is already a body, we add a line feed before the first link |
273 body_elt.addContent('\n') | 277 body_elt.addContent('\n') |