This commit is contained in:
Egor Aristov 2020-07-03 17:08:00 +03:00
parent 9fab87b045
commit a685dd427a

View File

@ -216,17 +216,14 @@ async def upload_file(client: TelegramClient, source: typing.BinaryIO, expectedS
async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: DownloadInfo): async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: DownloadInfo):
dcId, inputFileLocation = get_input_location(dInfo.message) dcId, inputFileLocation = get_input_location(dInfo.message)
chunkSize = pow(2, 20) chunkSize = pow(2, 19)
realSize = int(dInfo.part_info['real_size']) realSize = int(dInfo.part_info['real_size'])
totalBytesDownloaded = 0 totalBytesDownloaded = 0
lastRealTimeMeasurement = time.time() lastRealTimeMeasurement = time.time()
while totalBytesDownloaded < realSize: while totalBytesDownloaded < realSize:
offset = totalBytesDownloaded offset = totalBytesDownloaded
limit = chunkSize if offset + chunkSize < realSize else realSize - offset limit = chunkSize
if limit % pow(2, 12) != 0:
extraBytes = pow(2, 12) - limit % pow(2, 12)
limit += extraBytes
try: try:
downloadResult: File = await client(GetFileRequest( downloadResult: File = await client(GetFileRequest(
@ -242,6 +239,8 @@ async def download_part(client: TelegramClient, dest: typing.BinaryIO, dInfo: Do
newMessage = await client.get_messages(dInfo.dialog, ids=dInfo.message.id) newMessage = await client.get_messages(dInfo.dialog, ids=dInfo.message.id)
dcId, inputFileLocation = get_input_location(newMessage) dcId, inputFileLocation = get_input_location(newMessage)
continue continue
else:
raise e
buffer = downloadResult.bytes buffer = downloadResult.bytes
bufLen = len(buffer) bufLen = len(buffer)
totalBytesDownloaded += bufLen totalBytesDownloaded += bufLen
@ -377,11 +376,13 @@ async def download(config: Config, filename):
destination = sys.stdout.buffer destination = sys.stdout.buffer
await check_logged_in(config) await check_logged_in(config)
try:
filename = filename.strip() filename = filename.strip()
nameHash = hashlib.sha256(filename.encode('utf-8')).hexdigest() nameHash = hashlib.sha256(filename.encode('utf-8')).hexdigest()
fileMessages = await config.client.get_messages(config.dialog, search=f'#telecup_file_{nameHash}') fileMessages = await config.client.get_messages(config.dialog, search=f'#telecup_file_{nameHash}')
if len(fileMessages) == 0: if len(fileMessages) == 0:
click.echo(f'`{filename}` not found', err=True) click.echo(f'`{filename}` not found', err=True)
return
msg = fileMessages[0] msg = fileMessages[0]
fileInfo = parse_message(msg.message) fileInfo = parse_message(msg.message)
if fileInfo and fileInfo['name'] == filename: if fileInfo and fileInfo['name'] == filename:
@ -389,4 +390,9 @@ async def download(config: Config, filename):
click.echo('OK', err=True) click.echo('OK', err=True)
else: else:
click.echo('Message FileInfo corrupt', err=True) click.echo('Message FileInfo corrupt', err=True)
finally:
await config.client.disconnect() await config.client.disconnect()
if __name__ == '__main__':
cli()