Skip to content

Commit bc8cfab

Browse files
committed
fix: blocking in download_media
1 parent 4cb57eb commit bc8cfab

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

swibots/api/chat/methods/download_media.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ async def download_media(
4242
directory=directory,
4343
file_name=file_name,
4444
in_memory=in_memory,
45-
block=block,
4645
progress=progress,
4746
progress_args=progress_args,
4847
)

swibots/api/chat/models/message.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ async def download(
549549
progress: Callable = None,
550550
progress_args: tuple = (),
551551
) -> Optional[Union[BinaryIO, bytes]]:
552+
if not file_name and self.media_info:
553+
file_name = self.media_info.description
552554
return await self.app.download_media(
553555
self, file_name, in_memory, block, progress, progress_args
554556
)

swibots/bot_app.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import swibots
99
from contextlib import suppress
1010
from pathlib import Path
11+
from httpx import AsyncClient
1112
from signal import signal as signal_fn, SIGINT, SIGTERM, SIGABRT
1213
from io import BytesIO
1314
from swibots.bots import Bot
@@ -273,19 +274,17 @@ async def handle_download(
273274
file_name: str,
274275
directory="downloads/",
275276
in_memory: bool = False,
276-
block: bool = True,
277277
progress: DownloadProgressCallback = None,
278278
progress_args: tuple = (),
279279
):
280280
if directory is None or directory == "":
281281
directory = "downloads/"
282282
if not in_memory:
283283
os.makedirs(directory, exist_ok=True)
284-
temp_file_path = (
284+
file_path = (
285285
os.path.abspath(re.sub("\\\\", "/", os.path.join(directory, file_name)))
286-
+ ".temp"
287286
)
288-
file = BytesIO() if in_memory else open(temp_file_path, "wb")
287+
file = BytesIO() if in_memory else open(file_path, "wb")
289288

290289
d_progress = DownloadProgress(
291290
total=0,
@@ -295,15 +294,13 @@ async def handle_download(
295294
url=url,
296295
)
297296

298-
if progress:
299-
await progress(d_progress, *progress_args)
300297
try:
301-
with httpx.stream("GET", url) as response:
298+
async with AsyncClient().stream("GET", url) as response:
302299
d_progress.total = int(response.headers["Content-Length"])
303300
d_progress.downloaded = response.num_bytes_downloaded
304301
d_progress.client = response
305302
d_progress.started = True
306-
for chunk in response.iter_bytes():
303+
async for chunk in response.aiter_bytes():
307304
file.write(chunk)
308305
d_progress.downloaded += len(chunk)
309306
if progress:
@@ -312,7 +309,7 @@ async def handle_download(
312309
except BaseException as e:
313310
if not in_memory:
314311
file.close()
315-
os.remove(temp_file_path)
312+
os.remove(file_path)
316313
if isinstance(e, CancelError):
317314
return None
318315
if isinstance(e, asyncio.CancelledError):
@@ -323,8 +320,6 @@ async def handle_download(
323320
return file
324321
else:
325322
file.close()
326-
file_path = os.path.splitext(temp_file_path)[0]
327-
shutil.move(temp_file_path, file_path)
328323
return file_path
329324

330325
async def _on_app_stop(self):

0 commit comments

Comments
 (0)