Skip to content

Commit

Permalink
fix file send
Browse files Browse the repository at this point in the history
  • Loading branch information
themanyfaceddemon committed Sep 18, 2024
1 parent 318585a commit 00ce298
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
11 changes: 7 additions & 4 deletions DMBotNetwork/main/client.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -305,16 +306,18 @@ 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 = (
cls._content_path / cls._server_name / (name + ".download")
)
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)

Expand Down
4 changes: 2 additions & 2 deletions DMBotNetwork/main/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion DMBotNetwork/main/utils/cl_unit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import base64
import json
from pathlib import Path

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 00ce298

Please sign in to comment.