From 00ce29816ef88068ad2dca030d8cb415a9e5bdb5 Mon Sep 17 00:00:00 2001 From: themanyfaceddemon Date: Wed, 18 Sep 2024 15:27:22 +0300 Subject: [PATCH] fix file send --- DMBotNetwork/main/client.py | 11 +++++++---- DMBotNetwork/main/server.py | 4 ++-- DMBotNetwork/main/utils/cl_unit.py | 5 ++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/DMBotNetwork/main/client.py b/DMBotNetwork/main/client.py index a302bce..03714ac 100644 --- a/DMBotNetwork/main/client.py +++ b/DMBotNetwork/main/client.py @@ -1,11 +1,12 @@ import asyncio +import base64 import inspect import json import logging from collections.abc import Callable from pathlib import Path -from typing import (Any, Dict, List, Optional, Type, Union, get_args, get_origin, - get_type_hints) +from typing import (Any, Dict, List, Optional, Type, Union, get_args, + get_origin, get_type_hints) import aiofiles @@ -305,9 +306,9 @@ async def _auth_handler(cls, code: int, receive_package: dict) -> None: async def _file_handler(cls, code: int, receive_package: dict) -> None: if code == ResponseCode.FIL_REQ: name = receive_package.get("name", None) - chunk = receive_package.get("chunk", None) + chunk_base64 = receive_package.get("chunk", None) - if not all([name, chunk]): + if not all([name, chunk_base64]): return file_path: Path = ( @@ -315,6 +316,8 @@ async def _file_handler(cls, code: int, receive_package: dict) -> None: ) file_path.parent.mkdir(parents=True, exist_ok=True) + chunk = base64.b64decode(chunk_base64.encode("utf-8")) + async with aiofiles.open(file_path, "ab") as file: await file.write(chunk) diff --git a/DMBotNetwork/main/server.py b/DMBotNetwork/main/server.py index d25541b..9a3cf60 100644 --- a/DMBotNetwork/main/server.py +++ b/DMBotNetwork/main/server.py @@ -177,7 +177,7 @@ async def _cl_handler( if not cls._is_online: await cl_unit.send_log_error("Server is shutdown") return - + try: await cls._auth(cl_unit) @@ -226,7 +226,7 @@ async def _cl_handler( get_key = receive_package.pop("net_get_key", None) if get_key is None: continue - + data = await cls._call_func( func_name, cl_unit, diff --git a/DMBotNetwork/main/utils/cl_unit.py b/DMBotNetwork/main/utils/cl_unit.py index 4fd7643..6c04d6d 100644 --- a/DMBotNetwork/main/utils/cl_unit.py +++ b/DMBotNetwork/main/utils/cl_unit.py @@ -1,4 +1,5 @@ import asyncio +import base64 import json from pathlib import Path @@ -102,8 +103,10 @@ async def send_file( await self.send_package(ResponseCode.FIL_END, name=file_name) break + chunk_base64 = base64.b64encode(chunk).decode("utf-8") + await self.send_package( - ResponseCode.FIL_REQ, name=file_name, chunk=chunk + ResponseCode.FIL_REQ, name=file_name, chunk=chunk_base64 ) async def send_log_debug(self, message: str) -> None: