Spaces:
Paused
Paused
| import hashlib | |
| import io | |
| import requests | |
| from bot import TelegramBot | |
| from bot.modules.decorators import verify_user,byte_to_gb_mb | |
| from telethon import functions, types,utils,helpers,custom,errors | |
| from bot.config import Telegram, Server | |
| from math import ceil, floor | |
| from bot.modules.telegram import get_message, get_file_properties | |
| MIN_CHUNK_SIZE = 4096 | |
| async def callback(current,current_part,part_size,part_count,total,event_peer,event_id,file_name): | |
| c=int(30*current_part//part_count) | |
| progress_bar = '#' * c + '-' * (30 - c) | |
| progress_text = f'**File name:{file_name} \nFile size:{byte_to_gb_mb(total)} \nUploading: {int(100*current_part//part_count)}%\n[{progress_bar}]' | |
| print(current_part,part_count,part_size,'Uploaded', current, 'out of', total,'bytes: {:.2%}'.format(current / total)) | |
| try: | |
| await TelegramBot.edit_message(event_peer, event_id, progress_text) | |
| except errors.rpcerrorlist.MessageNotModifiedError as e: | |
| print(f"Error: {e}") | |
| except Exception as e: # Catch other potential errors | |
| print(f"An unexpected error occurred: {e}") | |
| async def upload_chunk(message_id,code,event_id,event_message,peer_id,mesage_update_id): | |
| file_ = await get_message(message_id=int(message_id)) | |
| file_name, file_size, mime_type = get_file_properties(file_) | |
| info = utils._get_file_info(file_) | |
| dc_id = info.dc_id | |
| file_size = info.size | |
| file = info.location | |
| part_size_kb = utils.get_appropriated_part_size(file_size) | |
| part_size = int(part_size_kb * 1024) | |
| if part_size % MIN_CHUNK_SIZE != 0: | |
| raise ValueError( | |
| 'The part size must be evenly divisible by 4096.') | |
| if part_size_kb > 512: | |
| raise ValueError('The part size must be less or equal to 512KB') | |
| if part_size % 1024 != 0: | |
| raise ValueError( | |
| 'The part size must be evenly divisible by 1024') | |
| # Set a default file name if None was specified | |
| file_id = helpers.generate_random_long() | |
| if event_message.lower() =='n': | |
| filename=file_name | |
| else: | |
| ext = utils._get_extension(file_name) | |
| filename = event_message+ext | |
| is_big = file_size > 10 * 1024 * 1024 | |
| hash_md5 = hashlib.md5() | |
| part_count = (file_size + part_size - 1) // part_size | |
| until_bytes = file_size - 1 | |
| from_bytes=0 | |
| until_bytes = min(until_bytes, file_size - 1) | |
| offset = from_bytes - (from_bytes % part_size) | |
| pos =0 | |
| part_index=0 | |
| #for part_index in range(part_count): | |
| async for chunk in TelegramBot.iter_download(file, offset=offset, chunk_size=part_size, stride=part_size, file_size=file_size): | |
| #limit = part_size | |
| if type(chunk)!="<class 'bytes'>": | |
| chunk=bytes(chunk) | |
| #chunk = await TelegramBot(functions.upload.GetFileRequest( | |
| #precise=True, location=file, offset=pos, limit=limit | |
| # )) | |
| if not chunk: | |
| break | |
| elif part_index >= part_count: | |
| break | |
| else: | |
| if not is_big: | |
| # Bit odd that MD5 is only needed for small files and not | |
| # big ones with more chance for corruption, but that's | |
| # what Telegram wants. | |
| hash_md5.update(chunk) | |
| # The SavePartRequest is different depending on whether | |
| # the file is too large or not (over or less than 10MB) | |
| if is_big: | |
| await TelegramBot(functions.upload.SaveBigFilePartRequest( | |
| file_id, part_index, part_count, chunk)) | |
| else: | |
| await TelegramBot(functions.upload.SaveFilePartRequest( | |
| file_id, part_index, chunk)) | |
| #yield chunk | |
| pos += len(chunk) | |
| part_index += 1 | |
| await callback(pos,part_index,part_size,part_count,file_size,peer_id,mesage_update_id,filename) | |
| if is_big: | |
| return types.InputFileBig(file_id, part_count, file_name),filename | |
| else: | |
| return custom.InputSizedFile( | |
| file_id, part_count, file_name, md5=hash_md5, size=file_size | |
| ),filename | |
| ''' if not chunk: | |
| break | |
| elif part_count == 1: | |
| if not is_big: | |
| # Bit odd that MD5 is only needed for small files and not | |
| # big ones with more chance for corruption, but that's | |
| # what Telegram wants. | |
| hash_md5.update(chunk[first_part_cut:last_part_cut]) | |
| if is_big: | |
| request = functions.upload.SaveBigFilePartRequest(file_id=file_id,file_part=current_part,file_total_parts=part_count,bytes=chunk[first_part_cut:last_part_cut]) | |
| else: | |
| request = functions.upload.SaveFilePartRequest(file_id=file_id,file_part=current_part,bytes=chunk[first_part_cut:last_part_cut]) | |
| #yield chunk[first_part_cut:last_part_cut] | |
| elif current_part == 0: | |
| if not is_big: | |
| # Bit odd that MD5 is only needed for small files and not | |
| # big ones with more chance for corruption, but that's | |
| # what Telegram wants. | |
| hash_md5.update(chunk[first_part_cut:]) | |
| if is_big: | |
| request = functions.upload.SaveBigFilePartRequest( | |
| file_id=file_id,file_part=current_part,file_total_parts=part_count,bytes=chunk[first_part_cut:]) | |
| #file_id, current_part, part_count, chunk[first_part_cut:]) | |
| else: | |
| request = functions.upload.SaveFilePartRequest( | |
| file_id=file_id,file_part=current_part,bytes=chunk[first_part_cut:]) | |
| #yield chunk[first_part_cut:] | |
| elif current_part == part_count-1: | |
| if not is_big: | |
| # Bit odd that MD5 is only needed for small files and not | |
| # big ones with more chance for corruption, but that's | |
| # what Telegram wants. | |
| hash_md5.update(chunk[:last_part_cut]) | |
| if is_big: | |
| request = functions.upload.SaveBigFilePartRequest( | |
| file_id=file_id,file_part=current_part,file_total_parts=part_count,bytes=chunk[:last_part_cut]) | |
| #file_id, current_part, part_count, chunk[:last_part_cut]) | |
| else: | |
| request = functions.upload.SaveFilePartRequest( | |
| file_id=file_id,file_part=current_part,bytes=chunk[:last_part_cut]) | |
| #yield chunk[:last_part_cut] | |
| else: | |
| if not is_big: | |
| # Bit odd that MD5 is only needed for small files and not | |
| # big ones with more chance for corruption, but that's | |
| # what Telegram wants. | |
| hash_md5.update(chunk) | |
| if is_big: | |
| request = functions.upload.SaveBigFilePartRequest( | |
| file_id=file_id,file_part=current_part,file_total_parts=part_count,bytes=chunk) | |
| #file_id, current_part, part_count, chunk) | |
| else: | |
| request = functions.upload.SaveFilePartRequest( | |
| file_id=file_id,file_part=current_part,bytes=chunk) | |
| #file_id, current_part, chunk) | |
| #yield chunk | |
| current_part += 1 | |
| pos +=len(chunk) | |
| await callback(pos,current_part,part_size,part_count,file_size,peer_id,mesage_update_id) | |
| if is_big: | |
| return types.InputFileBig(file_id, part_count, filename) | |
| else: | |
| return custom.InputSizedFile( | |
| file_id, part_count, filename, md5=hash_md5, size=file_size | |
| )''' |