Skip to content

Commit 6f58c3c

Browse files
committed
Fix bug in '.push_file()'
This commit fix very rare bug with incorrect MD5 calculation on a Telegram side for small files. Now OpenPretender() class have '.get_expected_size()' method, so we can correctly report file size on upload to the underlying Telethon library.
1 parent e8a77bd commit 6f58c3c

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

tgbox/api/remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ async def _push_file(
10611061
ifile = await upload_file(
10621062
self._tc, oe,
10631063
file_name=urlsafe_b64encode(pf.filesalt.salt).decode(),
1064-
part_size_kb=512, file_size=pf.filesize,
1064+
part_size_kb=512, file_size=oe.get_expected_size(),
10651065
progress_callback=progress_callback
10661066
)
10671067
except Exception as e:
@@ -1074,7 +1074,7 @@ async def _push_file(
10741074

10751075
ifile = await self._tc.upload_file(
10761076
oe, file_name=urlsafe_b64encode(pf.filesalt.salt).decode(),
1077-
part_size_kb=512, file_size=pf.filesize,
1077+
part_size_kb=512, file_size=oe.get_expected_size(),
10781078
progress_callback=progress_callback)
10791079
try:
10801080
if message_to_edit:

tgbox/tools.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def __init__(
361361
self, flo: BinaryIO,
362362
aes_state: 'tgbox.crypto.AESwState',
363363
hmac_state: 'hashlib.HMAC',
364-
file_size: Optional[int] = None,
364+
file_size: int
365365
):
366366
"""
367367
Arguments:
@@ -374,9 +374,8 @@ def __init__(
374374
hmac_state (``hmac.HMAC``):
375375
``HMAC`` initialized with ``HMACKey``
376376
377-
file_size (``int``, optional):
378-
File size of ``flo``. If not specified,
379-
we will try to seek.
377+
file_size (``int``):
378+
File size of ``flo``.
380379
"""
381380
self._aes_state = aes_state
382381
self._hmac_state = hmac_state
@@ -412,6 +411,22 @@ def concat_metadata(self, metadata: bytes) -> None:
412411
self._buffered_bytes += metadata
413412
self._concated_metadata_size = len(metadata)
414413

414+
def get_expected_size(self):
415+
"""
416+
Returns expected actual Telegram document size after
417+
upload. We use it in ``push_file()``
418+
"""
419+
if not self._concated_metadata_size:
420+
raise Exception('You need to concat metadata firstly')
421+
# self._file_size already include size of Metadata, but
422+
# we need to calculate size of encrypted File *with*
423+
# padding, so firstly we are required to subtract
424+
# self._concated_metadata_size from self._file_size
425+
# for correct calculation. 32 here is HMAC blob
426+
expected = self._file_size - self._concated_metadata_size
427+
expected = (expected + (16 - expected % 16)) + 32
428+
return expected + self._concated_metadata_size
429+
415430
async def read(self, size: int=-1) -> bytes:
416431
"""
417432
Returns ``size`` bytes from async Generator.

0 commit comments

Comments
 (0)