from nio import * def set_client(c: AsyncClient) -> None: global client client = c def get_client() -> AsyncClient: return client def set_config(c: dict) -> None: global config config = c def get_config() -> dict: return config def create_user_dir(user: str) -> str: import hashlib import os # NOTE: I would have used the user directly, but the Spec allows MXIDs # with '/' and UNIX-based OSes will **not** like that. sha = hashlib.sha256(bytes(user, 'ascii')).hexdigest() path = os.path.join(get_config()["data"], sha) if not os.path.exists(path): os.makedirs(path) return os.path.abspath(path) async def send_png(room: MatrixRoom, filename: str) -> None: import imagesize import os basename = os.path.basename(filename) w, h = imagesize.get(filename) file = open(filename, "r+b") size = os.path.getsize(filename) filename = os.path.basename(filename) resp, keys = await client.upload(file, content_type="image/png", filename=basename) if not isinstance(resp, UploadResponse): raise OSError("Couldn't upload file.") content = { "body": basename, "info": { "size": size, "mimetype": "image/png", "w": w, "h": h }, "msgtype": "m.image", "url": resp.content_uri, } file.close() await client.room_send(room.room_id, message_type="m.room.message", content=content)