From 16075b15d4e76364dbb960ecb239e46e3501dc18 Mon Sep 17 00:00:00 2001 From: niklas Date: Sun, 20 Sep 2020 15:01:20 +0200 Subject: maybe add correct dimensions and preview --- signald/main.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/signald/main.py b/signald/main.py index 75eb8a9..48901f4 100644 --- a/signald/main.py +++ b/signald/main.py @@ -1,9 +1,14 @@ import json +import cStringIO import random import re import socket +import base64 + +from io import BytesIO from typing import Iterator, List # noqa +from PIL import Image from .types import Attachment, Message # We'll need to know the compiled RE object later. @@ -154,13 +159,30 @@ class Signal: block: Whether to block while sending. If you choose not to block, you won't get an exception if there are any errors. """ + + # TODO: find out if sending them on non-images causes problems + width = 0 + height = 0 + preview = "" + + # TODO: do for all images types + if filename.endswith("jpg") or filename.endswith("png"): + with Image.open(filepath) as img: + width, height = img.size + buffer = cStringIO.StringIO() + image.save(buffer, format="JPEG") + preview = base64.b64encode(buffer.getvalue()) + payload = { "type": "send", "username": self.username, "recipientAddress": recipient, "attachments": [ { - "filename": filename + "filename": filename, + "width": width, + "height": height, + "preview": preview } ] } @@ -192,13 +214,30 @@ class Signal: block: Whether to block while sending. If you choose not to block, you won't get an exception if there are any errors. """ + + # TODO: find out if sending them on non-images causes problems + width = 0 + height = 0 + preview = "" + + # TODO: do for all images types + if filename.endswith("jpg") or filename.endswith("png"): + with Image.open(filepath) as img: + width, height = img.size + buffer = cStringIO.StringIO() + image.save(buffer, format="JPEG") + preview = base64.b64encode(buffer.getvalue()) + payload = { "type": "send", "username": self.username, "recipientGroupId": recipient_group_id, "attachments": [ { - "filename": filename + "filename": filename, + "width": width, + "height": height, + "preview": preview } ] } -- cgit v1.2.3-54-g00ecf