From a85518152a6d2f7958759ee6ae76eca901a02dc8 Mon Sep 17 00:00:00 2001 From: Guillermo Rodriguez Date: Sun, 28 May 2023 20:44:47 -0300 Subject: [PATCH] Add frontend image reduction and fix ipfs sudo issue --- skynet/frontend/telegram.py | 18 ++++++++++++------ skynet/ipfs.py | 22 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/skynet/frontend/telegram.py b/skynet/frontend/telegram.py index c20de06..5e579d4 100644 --- a/skynet/frontend/telegram.py +++ b/skynet/frontend/telegram.py @@ -140,16 +140,22 @@ async def work_request( if file_id: image_raw = await bot.download_file(file_path) - image = Image.open(io.BytesIO(image_raw)) - w, h = image.size - logging.info(f'user sent img of size {image.size}') + with Image.open(io.BytesIO(image_raw)) as image: + w, h = image.size - if w > 512 or h > 512: - image.thumbnail((512, 512)) - logging.warning(f'resized it to {image.size}') + if w > 512 or h > 512: + logging.warning(f'user sent img of size {image.size}') + image.thumbnail((512, 512)) + logging.warning(f'resized it to {image.size}') + img_byte_arr = io.BytesIO() + image.save(img_byte_arr, format='PNG') + image_raw = img_byte_arr.getvalue() binary = image_raw.hex() + else: + binary = '' + ec, out = cleos.push_action( 'telos.gpu', 'enqueue', [account, body, binary, '20.0000 GPU'], f'{account}@{permission}' ) diff --git a/skynet/ipfs.py b/skynet/ipfs.py index 53a2c28..1e56b52 100644 --- a/skynet/ipfs.py +++ b/skynet/ipfs.py @@ -1,5 +1,6 @@ #!/usr/bin/python +import os import logging from pathlib import Path @@ -7,6 +8,7 @@ from contextlib import contextmanager as cm import docker +from docker.types import Mount from docker.models.containers import Container @@ -48,13 +50,27 @@ def open_ipfs_node(): '4001/tcp': 4001, '5001/tcp': ('127.0.0.1', 5001) }, - volumes=[ - str(Path().resolve() / 'tmp/ipfs-docker-staging') + ':/export', - str(Path().resolve() / 'tmp/ipfs-docker-data') + ':/data/ipfs' + mounts=[ + Mount( + '/export', + str(Path().resolve() / 'tmp/ipfs-docker-staging'), + 'bind' + ), + Mount( + '/data/ipfs', + str(Path().resolve() / 'tmp/ipfs-docker-data'), + 'bind' + ) ], detach=True, remove=True ) + uid = os.getuid() + gid = os.getgid() + ec, out = container.exec_run(['chown', f'{uid}:{gid}', '-R', '/export']) + assert ec == 0 + ec, out = container.exec_run(['chown', f'{uid}:{gid}', '-R', '/data/ipfs']) + assert ec == 0 try: for log in container.logs(stream=True):