matrix-nio icon indicating copy to clipboard operation
matrix-nio copied to clipboard

Correct Way to Upload Images - (encrypted)

Open araa47 opened this issue 4 years ago • 1 comments

Hi I'm trying to find the correct way to send images, e2e encrypted using this client.

I came across this example: https://github.com/poljar/matrix-nio/blob/fc9f5e3eda25ad65936aeb95412a26af73cedf6a/nio/client/async_client.py#L2475, which does not work for me

My fix to that was this

async def send_jpg_image(self, room_id, message, filename):
        try:
            file_stat = os.stat(filename)
            async with aiofiles.open(filename, "r+b") as f:
                resp, maybe_keys = await self.upload(f,  
                content_type="image/jpeg",
                filename=filename,
                filesize=file_stat.st_size,
                encrypt=False)

                await self.room_send(
                    room_id,
                    message_type="m.room.message",
                    content = {
                        "msgtype": "m.file",
                        "url": resp.content_uri, 
                        "body": message
                    })
        except Exception as err:
            print(err)

This works, however there is no image thumbnail/preview. It just shows up as a file upload. I want the preview to show up, like when I upload an image using the element client.

Also this example does not seem to be e2e encrypted, just adding encrypt=True to the upload does not seem to solve the problem, since then the image seems corrupt

araa47 avatar Jul 26 '20 15:07 araa47

I also tried to add the following data into the content argument of self.room_send

 await self.room_send(
                    room_id,
                    message_type="m.room.message",
                    content = {
                        "msgtype": "m.file",
                        "url": resp.content_uri, 
                        "body": message,
                        "info": {
                            "h": 480,
                            "w": 800, 
                            "thumbnail_url": resp.content_uri ,
                            "mimetype": "image/jpeg", 
                            "size": file_stat.st_size
                        }
                    })

But i still cant see a preview of my image.

araa47 avatar Jul 26 '20 15:07 araa47