Spaces:
Paused
Paused
File size: 8,008 Bytes
861d763 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 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
)''' |