# HG changeset patch # User Goffi # Date 1583097085 -3600 # Node ID 53bb3886b408fee82611243edca4513e0ad74dc2 # Parent 977158b56ce627485d3a32cfc7a3535fd30da208 chat: generate preview if there is not one already for non encrypted attachments diff -r 977158b56ce6 -r 53bb3886b408 cagou/plugins/plugin_wid_chat.py --- a/cagou/plugins/plugin_wid_chat.py Sun Mar 01 22:11:25 2020 +0100 +++ b/cagou/plugins/plugin_wid_chat.py Sun Mar 01 22:11:25 2020 +0100 @@ -142,7 +142,7 @@ attachment['preview'] = preview_path wid.source = preview_path - def _setDecryptedPath(self, attachment, wid, path): + def _setPath(self, attachment, wid, path): attachment['path'] = path if wid is not None: # we also need a preview for the widget @@ -160,17 +160,28 @@ self.clear_widgets() for idx, attachment in enumerate(attachments): url = attachment['url'] - to_decrypt = url.startswith("aesgcm:") + if url.startswith("aesgcm:"): + del attachment['url'] + # if the file is encrypted, we need to download it for decryption + to_download = True + else: + to_download = False if idx < 3 or len(attachments) <= 4: if ((self.mess_data.own_mess or self.chat.contact_list.isInRoster(self.mess_data.from_jid))): - wid_kwargs = { - "size_hint": (1, 1), - } - if not to_decrypt: - wid_kwargs["source"] = url - wid = AsyncImage(**wid_kwargs) + wid = AsyncImage(size_hint=(1, 1)) + if 'preview' in attachment: + wid.source = attachment["preview"] + elif 'path' in attachment: + G.host.bridge.imageGeneratePreview( + attachment['path'], + self.chat.profile, + callback=partial(self._setPreview, attachment, wid), + ) + else: + # we'll download the file, the preview will then be generated + to_download = True else: # we don't download automatically the image if the contact is not # in roster, to avoid leaking the ip @@ -179,13 +190,12 @@ else: wid = None - if to_decrypt: - # the file needs to be decrypted, it will be done by downloadURL - # and the widget source and attachment path will then be completed - del attachment['url'] + if to_download: + # the file needs to be downloaded, the widget source, + # attachment path, and preview will then be completed G.host.downloadURL( url, - callback=partial(self._setDecryptedPath, attachment, wid), + callback=partial(self._setPath, attachment, wid), dest=C.FILE_DEST_CACHE, profile=self.chat.profile, ) @@ -200,7 +210,7 @@ def on_press(self): sources = [] for attachment in self.attachments: - source = attachment.get('path', attachment.get('url')) + source = attachment.get('path') or attachment.get('url') if not source: log.warning(f"no source for {attachment}") else: