2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Fix sync callback progress not working properly for downloads

- Reduce duplicated code
- Fixes #484
This commit is contained in:
Dan 2020-09-05 12:44:48 +02:00
parent 58667d2ae8
commit ae88c851bb
2 changed files with 25 additions and 23 deletions

View File

@ -949,7 +949,8 @@ class Client(Methods, Scaffold):
offset += limit offset += limit
if progress: if progress:
await progress( func = functools.partial(
progress,
min(offset, file_size) min(offset, file_size)
if file_size != 0 if file_size != 0
else offset, else offset,
@ -957,6 +958,11 @@ class Client(Methods, Scaffold):
*progress_args *progress_args
) )
if inspect.iscoroutinefunction(progress):
await func()
else:
await self.loop.run_in_executor(self.executor, func)
r = await session.send( r = await session.send(
raw.functions.upload.GetFile( raw.functions.upload.GetFile(
location=location, location=location,
@ -1035,20 +1041,16 @@ class Client(Methods, Scaffold):
offset += limit offset += limit
if progress: if progress:
if inspect.iscoroutinefunction(progress): func = functools.partial(
await progress( progress,
min(offset, file_size) if file_size != 0 else offset, min(offset, file_size) if file_size != 0 else offset,
file_size, file_size,
*progress_args *progress_args
) )
else:
func = functools.partial(
progress,
min(offset, file_size) if file_size != 0 else offset,
file_size,
*progress_args
)
if inspect.iscoroutinefunction(progress):
await func()
else:
await self.loop.run_in_executor(self.executor, func) await self.loop.run_in_executor(self.executor, func)
if len(chunk) < limit: if len(chunk) < limit:

View File

@ -183,16 +183,16 @@ class SaveFile(Scaffold):
file_part += 1 file_part += 1
if progress: if progress:
if inspect.iscoroutinefunction(progress): func = functools.partial(
await progress(min(file_part * part_size, file_size), file_size, *progress_args) progress,
else: min(file_part * part_size, file_size),
func = functools.partial( file_size,
progress, *progress_args
min(file_part * part_size, file_size), )
file_size,
*progress_args
)
if inspect.iscoroutinefunction(progress):
await func()
else:
await self.loop.run_in_executor(self.executor, func) await self.loop.run_in_executor(self.executor, func)
except StopTransmission: except StopTransmission:
raise raise