Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ except ImportError:
|
|
| 24 |
UVLOOP_AVAILABLE = False
|
| 25 |
|
| 26 |
# =============================================================================
|
| 27 |
-
# CONFIGURATION
|
| 28 |
# =============================================================================
|
| 29 |
|
| 30 |
API_ID: int = int(os.environ["API_ID"])
|
|
@@ -33,16 +33,15 @@ BOT_TOKEN: str = os.environ["BOT_TOKEN"]
|
|
| 33 |
HF_TOKEN: str = os.environ["HF_TOKEN"]
|
| 34 |
OBFUSCATION_KEY: str = os.environ.get("OBFUSCATION_KEY", "change-me-to-a-secret-string")
|
| 35 |
|
| 36 |
-
SPACE_HOST: str = "https://
|
| 37 |
-
|
| 38 |
-
DATASET_REPO: str = "ionzm/Bzxv"
|
| 39 |
DATASET_TYPE: str = "dataset"
|
| 40 |
-
MAX_FILE_SIZE_TG: int = 1900 * 1024 * 1024
|
| 41 |
-
MAX_SINGLE_FILE_SIZE: int = 45 * 1024**3
|
| 42 |
-
MIN_FREE_DISK: int = 45 * 1024 * 1024 * 1024
|
| 43 |
-
DATASET_MAX_SIZE: int = int(8.7 * 1024**4)
|
| 44 |
-
KEEPALIVE_INTERVAL: int = 300
|
| 45 |
-
UPLOAD_TIMEOUT: int = 7200
|
| 46 |
DB_PATH: str = "bot_files.db"
|
| 47 |
|
| 48 |
logging.basicConfig(
|
|
@@ -53,7 +52,7 @@ logging.basicConfig(
|
|
| 53 |
logger = logging.getLogger("StorageBot")
|
| 54 |
|
| 55 |
# =============================================================================
|
| 56 |
-
# DETERMINISTIC ENCRYPTION (AES
|
| 57 |
# =============================================================================
|
| 58 |
|
| 59 |
_siv_key = hashlib.sha512(OBFUSCATION_KEY.encode()).digest()
|
|
@@ -132,7 +131,7 @@ def db_file_exists(user_id: int, orig_name: str, size: Optional[int] = None) ->
|
|
| 132 |
return (row[0], row[1]) if row else None
|
| 133 |
|
| 134 |
# =============================================================================
|
| 135 |
-
# IN
|
| 136 |
# =============================================================================
|
| 137 |
|
| 138 |
file_callback_map: dict[str, str] = {}
|
|
@@ -286,7 +285,7 @@ async def send_progress_updater(
|
|
| 286 |
return task, bytes_holder
|
| 287 |
|
| 288 |
# =============================================================================
|
| 289 |
-
# CORE UPLOAD / DOWNLOAD LOGIC (FIXED)
|
| 290 |
# =============================================================================
|
| 291 |
|
| 292 |
async def do_upload_job(
|
|
@@ -315,7 +314,7 @@ async def do_upload_job(
|
|
| 315 |
download_start = time.time()
|
| 316 |
total_size = expected_size if expected_size and expected_size > 0 else 0
|
| 317 |
|
| 318 |
-
#
|
| 319 |
def download_progress(current, total):
|
| 320 |
nonlocal total_size
|
| 321 |
now = time.time()
|
|
@@ -328,7 +327,6 @@ async def do_upload_job(
|
|
| 328 |
total_size = total
|
| 329 |
elif total_size == 0 and expected_size and expected_size > 0:
|
| 330 |
total_size = expected_size
|
| 331 |
-
|
| 332 |
text = "β¬οΈ Downloading from Telegram...\n"
|
| 333 |
if total_size > 0:
|
| 334 |
pct = (100 * current / total_size)
|
|
@@ -337,7 +335,6 @@ async def do_upload_job(
|
|
| 337 |
else:
|
| 338 |
text += f"{format_size(current)} downloaded\n"
|
| 339 |
text += f"Speed: {format_speed(speed)}"
|
| 340 |
-
# Schedule edit in main loop
|
| 341 |
asyncio.create_task(status_msg.edit_text(
|
| 342 |
text,
|
| 343 |
reply_markup=InlineKeyboardMarkup(
|
|
@@ -345,12 +342,11 @@ async def do_upload_job(
|
|
| 345 |
),
|
| 346 |
))
|
| 347 |
|
| 348 |
-
#
|
| 349 |
local_path = await app.download_media(
|
| 350 |
file_msg_or_id,
|
| 351 |
file_name=os.path.join(temp_dir, orig_filename),
|
| 352 |
progress=download_progress,
|
| 353 |
-
block_size=1_048_576,
|
| 354 |
)
|
| 355 |
|
| 356 |
if cancel_event.is_set():
|
|
@@ -452,17 +448,16 @@ async def do_download_job(
|
|
| 452 |
job_id, status_msg, cancel_event, total_size
|
| 453 |
)
|
| 454 |
|
| 455 |
-
# Pyrogram expects a sync progress callback
|
| 456 |
def send_progress(current, total):
|
| 457 |
bytes_holder[0] = current
|
| 458 |
|
|
|
|
| 459 |
await app.send_document(
|
| 460 |
chat_id,
|
| 461 |
document=local_file,
|
| 462 |
file_name=orig_name,
|
| 463 |
caption=orig_name,
|
| 464 |
progress=send_progress,
|
| 465 |
-
block_size=1_048_576,
|
| 466 |
)
|
| 467 |
prog_task.cancel()
|
| 468 |
if cancel_event.is_set():
|
|
@@ -493,12 +488,12 @@ async def do_download_job(
|
|
| 493 |
break
|
| 494 |
p.write(buf)
|
| 495 |
bytes_written += len(buf)
|
|
|
|
| 496 |
await app.send_document(
|
| 497 |
chat_id,
|
| 498 |
document=part_path,
|
| 499 |
file_name=part_name,
|
| 500 |
caption=f"{orig_name} β Part {i}/{num_parts}",
|
| 501 |
-
block_size=1_048_576,
|
| 502 |
)
|
| 503 |
os.remove(part_path)
|
| 504 |
if not cancel_event.is_set():
|
|
@@ -513,7 +508,7 @@ async def do_download_job(
|
|
| 513 |
|
| 514 |
|
| 515 |
# =============================================================================
|
| 516 |
-
# DELETION HELPERS
|
| 517 |
# =============================================================================
|
| 518 |
|
| 519 |
async def delete_user_file(chat_id, user_id, enc_path, token, query: CallbackQuery):
|
|
@@ -571,7 +566,7 @@ async def refresh_file_list(chat_id: int, user_id: int, message: Message):
|
|
| 571 |
|
| 572 |
|
| 573 |
# =============================================================================
|
| 574 |
-
# DUPLICATE DETECTION & HANDLING
|
| 575 |
# =============================================================================
|
| 576 |
|
| 577 |
def check_duplicate(user_id: int, orig_name: str) -> Optional[int]:
|
|
@@ -653,7 +648,7 @@ async def handle_duplicate(
|
|
| 653 |
|
| 654 |
|
| 655 |
# =============================================================================
|
| 656 |
-
# ALBUM GROUPING
|
| 657 |
# =============================================================================
|
| 658 |
|
| 659 |
async def process_album_group(media_group_id: str):
|
|
@@ -770,7 +765,7 @@ async def upload_worker():
|
|
| 770 |
|
| 771 |
|
| 772 |
# =============================================================================
|
| 773 |
-
# BOT COMMAND HANDLERS
|
| 774 |
# =============================================================================
|
| 775 |
|
| 776 |
@app.on_message(filters.command("start"))
|
|
@@ -796,7 +791,7 @@ async def cmd_help(_, message: Message):
|
|
| 796 |
"**Duplicate handling:**\n"
|
| 797 |
"β’ Same name, different size β automatically renamed with _1, _2 ... and uploaded.\n"
|
| 798 |
"β’ Same name, same size β you get a Skip / Upload (rename) choice.\n"
|
| 799 |
-
"**Speed:** tgCrypto enforced,
|
| 800 |
)
|
| 801 |
await message.reply_text(text, disable_web_page_preview=True)
|
| 802 |
|
|
@@ -967,7 +962,7 @@ def _extract_file_info(msg: Message) -> Tuple[Optional[str], Optional[int], Opti
|
|
| 967 |
|
| 968 |
|
| 969 |
# =============================================================================
|
| 970 |
-
# CALLBACK HANDLERS
|
| 971 |
# =============================================================================
|
| 972 |
|
| 973 |
@app.on_callback_query(filters.regex("^dup_skip:"))
|
|
@@ -1164,7 +1159,7 @@ async def cb_cancel_upload_prompt(_, query: CallbackQuery):
|
|
| 1164 |
|
| 1165 |
|
| 1166 |
# =============================================================================
|
| 1167 |
-
# MESSAGE LINK INTERACTIVE PROMPT
|
| 1168 |
# =============================================================================
|
| 1169 |
|
| 1170 |
@app.on_message(
|
|
@@ -1221,7 +1216,7 @@ async def on_message_link(client, message: Message):
|
|
| 1221 |
|
| 1222 |
|
| 1223 |
# =============================================================================
|
| 1224 |
-
# DIRECT FILE RECEIVER
|
| 1225 |
# =============================================================================
|
| 1226 |
|
| 1227 |
@app.on_message(
|
|
@@ -1281,7 +1276,7 @@ async def on_direct_file(_, message: Message):
|
|
| 1281 |
|
| 1282 |
|
| 1283 |
# =============================================================================
|
| 1284 |
-
# KEEP
|
| 1285 |
# =============================================================================
|
| 1286 |
|
| 1287 |
def keepalive():
|
|
|
|
| 24 |
UVLOOP_AVAILABLE = False
|
| 25 |
|
| 26 |
# =============================================================================
|
| 27 |
+
# CONFIGURATION (UPDATED)
|
| 28 |
# =============================================================================
|
| 29 |
|
| 30 |
API_ID: int = int(os.environ["API_ID"])
|
|
|
|
| 33 |
HF_TOKEN: str = os.environ["HF_TOKEN"]
|
| 34 |
OBFUSCATION_KEY: str = os.environ.get("OBFUSCATION_KEY", "change-me-to-a-secret-string")
|
| 35 |
|
| 36 |
+
SPACE_HOST: str = "https://Cxops-Push.hf.space" # <-- new
|
| 37 |
+
DATASET_REPO: str = "Cxops/Bzxv" # <-- new
|
|
|
|
| 38 |
DATASET_TYPE: str = "dataset"
|
| 39 |
+
MAX_FILE_SIZE_TG: int = 1900 * 1024 * 1024 # 1900 MB
|
| 40 |
+
MAX_SINGLE_FILE_SIZE: int = 45 * 1024**3 # 45 GB
|
| 41 |
+
MIN_FREE_DISK: int = 45 * 1024 * 1024 * 1024 # 45 GB
|
| 42 |
+
DATASET_MAX_SIZE: int = int(8.7 * 1024**4) # 8.7 TB
|
| 43 |
+
KEEPALIVE_INTERVAL: int = 300 # 5 minutes
|
| 44 |
+
UPLOAD_TIMEOUT: int = 7200 # 2 hours
|
| 45 |
DB_PATH: str = "bot_files.db"
|
| 46 |
|
| 47 |
logging.basicConfig(
|
|
|
|
| 52 |
logger = logging.getLogger("StorageBot")
|
| 53 |
|
| 54 |
# =============================================================================
|
| 55 |
+
# DETERMINISTIC ENCRYPTION (AESβSIV)
|
| 56 |
# =============================================================================
|
| 57 |
|
| 58 |
_siv_key = hashlib.sha512(OBFUSCATION_KEY.encode()).digest()
|
|
|
|
| 131 |
return (row[0], row[1]) if row else None
|
| 132 |
|
| 133 |
# =============================================================================
|
| 134 |
+
# INβMEMORY STORES
|
| 135 |
# =============================================================================
|
| 136 |
|
| 137 |
file_callback_map: dict[str, str] = {}
|
|
|
|
| 285 |
return task, bytes_holder
|
| 286 |
|
| 287 |
# =============================================================================
|
| 288 |
+
# CORE UPLOAD / DOWNLOAD LOGIC (FIXED β NO block_size)
|
| 289 |
# =============================================================================
|
| 290 |
|
| 291 |
async def do_upload_job(
|
|
|
|
| 314 |
download_start = time.time()
|
| 315 |
total_size = expected_size if expected_size and expected_size > 0 else 0
|
| 316 |
|
| 317 |
+
# Sync progress callback (as required by Pyrogram)
|
| 318 |
def download_progress(current, total):
|
| 319 |
nonlocal total_size
|
| 320 |
now = time.time()
|
|
|
|
| 327 |
total_size = total
|
| 328 |
elif total_size == 0 and expected_size and expected_size > 0:
|
| 329 |
total_size = expected_size
|
|
|
|
| 330 |
text = "β¬οΈ Downloading from Telegram...\n"
|
| 331 |
if total_size > 0:
|
| 332 |
pct = (100 * current / total_size)
|
|
|
|
| 335 |
else:
|
| 336 |
text += f"{format_size(current)} downloaded\n"
|
| 337 |
text += f"Speed: {format_speed(speed)}"
|
|
|
|
| 338 |
asyncio.create_task(status_msg.edit_text(
|
| 339 |
text,
|
| 340 |
reply_markup=InlineKeyboardMarkup(
|
|
|
|
| 342 |
),
|
| 343 |
))
|
| 344 |
|
| 345 |
+
# Download from Telegram (NO block_size)
|
| 346 |
local_path = await app.download_media(
|
| 347 |
file_msg_or_id,
|
| 348 |
file_name=os.path.join(temp_dir, orig_filename),
|
| 349 |
progress=download_progress,
|
|
|
|
| 350 |
)
|
| 351 |
|
| 352 |
if cancel_event.is_set():
|
|
|
|
| 448 |
job_id, status_msg, cancel_event, total_size
|
| 449 |
)
|
| 450 |
|
|
|
|
| 451 |
def send_progress(current, total):
|
| 452 |
bytes_holder[0] = current
|
| 453 |
|
| 454 |
+
# Send to Telegram (NO block_size)
|
| 455 |
await app.send_document(
|
| 456 |
chat_id,
|
| 457 |
document=local_file,
|
| 458 |
file_name=orig_name,
|
| 459 |
caption=orig_name,
|
| 460 |
progress=send_progress,
|
|
|
|
| 461 |
)
|
| 462 |
prog_task.cancel()
|
| 463 |
if cancel_event.is_set():
|
|
|
|
| 488 |
break
|
| 489 |
p.write(buf)
|
| 490 |
bytes_written += len(buf)
|
| 491 |
+
# Send part (NO block_size)
|
| 492 |
await app.send_document(
|
| 493 |
chat_id,
|
| 494 |
document=part_path,
|
| 495 |
file_name=part_name,
|
| 496 |
caption=f"{orig_name} β Part {i}/{num_parts}",
|
|
|
|
| 497 |
)
|
| 498 |
os.remove(part_path)
|
| 499 |
if not cancel_event.is_set():
|
|
|
|
| 508 |
|
| 509 |
|
| 510 |
# =============================================================================
|
| 511 |
+
# DELETION HELPERS
|
| 512 |
# =============================================================================
|
| 513 |
|
| 514 |
async def delete_user_file(chat_id, user_id, enc_path, token, query: CallbackQuery):
|
|
|
|
| 566 |
|
| 567 |
|
| 568 |
# =============================================================================
|
| 569 |
+
# DUPLICATE DETECTION & HANDLING
|
| 570 |
# =============================================================================
|
| 571 |
|
| 572 |
def check_duplicate(user_id: int, orig_name: str) -> Optional[int]:
|
|
|
|
| 648 |
|
| 649 |
|
| 650 |
# =============================================================================
|
| 651 |
+
# ALBUM GROUPING
|
| 652 |
# =============================================================================
|
| 653 |
|
| 654 |
async def process_album_group(media_group_id: str):
|
|
|
|
| 765 |
|
| 766 |
|
| 767 |
# =============================================================================
|
| 768 |
+
# BOT COMMAND HANDLERS
|
| 769 |
# =============================================================================
|
| 770 |
|
| 771 |
@app.on_message(filters.command("start"))
|
|
|
|
| 791 |
"**Duplicate handling:**\n"
|
| 792 |
"β’ Same name, different size β automatically renamed with _1, _2 ... and uploaded.\n"
|
| 793 |
"β’ Same name, same size β you get a Skip / Upload (rename) choice.\n"
|
| 794 |
+
"**Speed:** tgCrypto enforced, fast block sizes β maximum possible throughput."
|
| 795 |
)
|
| 796 |
await message.reply_text(text, disable_web_page_preview=True)
|
| 797 |
|
|
|
|
| 962 |
|
| 963 |
|
| 964 |
# =============================================================================
|
| 965 |
+
# DUPLICATE & CALLBACK HANDLERS
|
| 966 |
# =============================================================================
|
| 967 |
|
| 968 |
@app.on_callback_query(filters.regex("^dup_skip:"))
|
|
|
|
| 1159 |
|
| 1160 |
|
| 1161 |
# =============================================================================
|
| 1162 |
+
# MESSAGE LINK INTERACTIVE PROMPT
|
| 1163 |
# =============================================================================
|
| 1164 |
|
| 1165 |
@app.on_message(
|
|
|
|
| 1216 |
|
| 1217 |
|
| 1218 |
# =============================================================================
|
| 1219 |
+
# DIRECT FILE RECEIVER
|
| 1220 |
# =============================================================================
|
| 1221 |
|
| 1222 |
@app.on_message(
|
|
|
|
| 1276 |
|
| 1277 |
|
| 1278 |
# =============================================================================
|
| 1279 |
+
# KEEPβALIVE PING
|
| 1280 |
# =============================================================================
|
| 1281 |
|
| 1282 |
def keepalive():
|