Fix uploading big files (closes #2)

This commit is contained in:
Lonami Exo 2017-03-25 17:14:11 +01:00
parent 3f24cb17c3
commit af4514b15e

View File

@ -30,7 +30,7 @@ from telethon.tl.functions.upload import (
# All the types we need to work with # All the types we need to work with
from telethon.tl.types import ( from telethon.tl.types import (
ChatPhotoEmpty, DocumentAttributeAudio, DocumentAttributeFilename, ChatPhotoEmpty, DocumentAttributeAudio, DocumentAttributeFilename,
InputDocumentFileLocation, InputFile, InputFileLocation, InputDocumentFileLocation, InputFile, InputFileBig, InputFileLocation,
InputMediaUploadedDocument, InputMediaUploadedPhoto, InputPeerEmpty, InputMediaUploadedDocument, InputMediaUploadedPhoto, InputPeerEmpty,
MessageMediaContact, MessageMediaDocument, MessageMediaPhoto, MessageMediaContact, MessageMediaDocument, MessageMediaPhoto,
UserProfilePhotoEmpty) UserProfilePhotoEmpty)
@ -422,7 +422,7 @@ class TelegramClient:
# Multiply the datetime timestamp by 10^6 to get the ticks # Multiply the datetime timestamp by 10^6 to get the ticks
# This is high likely going to be unique # This is high likely going to be unique
file_id = int(datetime.now().timestamp() * (10**6)) file_id = utils.generate_random_long()
hash_md5 = md5() hash_md5 = md5()
with open(file_path, 'rb') as file: with open(file_path, 'rb') as file:
@ -441,7 +441,10 @@ class TelegramClient:
# Invoke the file upload and increment both the part index and MD5 checksum # Invoke the file upload and increment both the part index and MD5 checksum
result = self.invoke(request) result = self.invoke(request)
if result: if result:
hash_md5.update(part) if not is_large:
# No need to update the hash if it's a large file
hash_md5.update(part)
if progress_callback: if progress_callback:
progress_callback(file.tell(), file_size) progress_callback(file.tell(), file_size)
else: else:
@ -453,11 +456,17 @@ class TelegramClient:
file_name = path.basename(file_path) file_name = path.basename(file_path)
# After the file has been uploaded, we can return a handle pointing to it # After the file has been uploaded, we can return a handle pointing to it
return InputFile( if is_large:
id=file_id, return InputFileBig(
parts=part_count, id=file_id,
name=file_name, parts=part_count,
md5_checksum=hash_md5.hexdigest()) name=file_name)
else:
return InputFile(
id=file_id,
parts=part_count,
name=file_name,
md5_checksum=hash_md5.hexdigest())
def send_photo_file(self, input_file, entity, caption=''): def send_photo_file(self, input_file, entity, caption=''):
"""Sends a previously uploaded input_file """Sends a previously uploaded input_file