Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os, re, sys, time, base64, hashlib, shutil, logging, secrets, sqlite3, tempfile, threading, asyncio
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Optional, List, Tuple, Union
|
|
@@ -301,8 +316,6 @@ async def do_upload_job(
|
|
| 301 |
) -> None:
|
| 302 |
temp_dir = tempfile.mkdtemp()
|
| 303 |
local_path = None
|
| 304 |
-
# Sanitise filename to prevent path traversal
|
| 305 |
-
orig_filename = os.path.basename(orig_filename)
|
| 306 |
try:
|
| 307 |
await status_msg.edit_text(
|
| 308 |
"⬇️ Downloading from Telegram...",
|
|
@@ -480,17 +493,11 @@ async def do_download_job(
|
|
| 480 |
[[InlineKeyboardButton("❌ Cancel", callback_data=f"cancel_job:{job_id}")]]
|
| 481 |
),
|
| 482 |
)
|
|
|
|
| 483 |
part_name = f"{orig_name}.part{i}of{num_parts}"
|
| 484 |
part_path = os.path.join(temp_dir, part_name)
|
| 485 |
-
# Write part in small chunks to avoid memory exhaustion
|
| 486 |
with open(part_path, "wb") as p:
|
| 487 |
-
|
| 488 |
-
while bytes_written < part_size:
|
| 489 |
-
buf = f.read(min(1024 * 1024, part_size - bytes_written))
|
| 490 |
-
if not buf:
|
| 491 |
-
break
|
| 492 |
-
p.write(buf)
|
| 493 |
-
bytes_written += len(buf)
|
| 494 |
await app.send_document(
|
| 495 |
chat_id,
|
| 496 |
document=part_path,
|
|
@@ -720,12 +727,9 @@ async def process_album_group(media_group_id: str):
|
|
| 720 |
|
| 721 |
async def upload_worker():
|
| 722 |
while True:
|
| 723 |
-
|
| 724 |
-
user_id, chat_id, file_obj, orig_filename, job_id, file_id, expected_size
|
| 725 |
-
|
| 726 |
-
upload_queue.task_done()
|
| 727 |
-
continue
|
| 728 |
-
|
| 729 |
active_jobs[job_id] = {"status": "running", "cancel_event": cancel_event}
|
| 730 |
|
| 731 |
while get_free_disk() < MIN_FREE_DISK:
|
|
@@ -892,10 +896,8 @@ async def cmd_upload(_, message: Message):
|
|
| 892 |
return
|
| 893 |
|
| 894 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
active_jobs[job_id] = {"status": "queued", "cancel_event": cancel_event}
|
| 898 |
-
await upload_queue.put((message.from_user.id, message.chat.id, file_id, filename, job_id, file_id, expected_size, cancel_event))
|
| 899 |
await message.reply_text(f"📦 `{filename}` queued for upload.")
|
| 900 |
|
| 901 |
|
|
@@ -959,9 +961,7 @@ async def cb_dup_keep(_, query: CallbackQuery):
|
|
| 959 |
new_filename = generate_unique_filename(data['user_id'], base, ext)
|
| 960 |
|
| 961 |
job_id = data['job_id']
|
| 962 |
-
|
| 963 |
-
cancel_event = asyncio.Event()
|
| 964 |
-
active_jobs[job_id] = {"status": "queued", "cancel_event": cancel_event}
|
| 965 |
await upload_queue.put((
|
| 966 |
data['user_id'],
|
| 967 |
data['chat_id'],
|
|
@@ -970,7 +970,6 @@ async def cb_dup_keep(_, query: CallbackQuery):
|
|
| 970 |
job_id,
|
| 971 |
data['file_id'],
|
| 972 |
data['expected_size'],
|
| 973 |
-
cancel_event,
|
| 974 |
))
|
| 975 |
await query.message.edit_text(f"📦 `{new_filename}` queued as a duplicate.")
|
| 976 |
await query.answer("Duplicate will be uploaded with a new name.")
|
|
@@ -1018,12 +1017,11 @@ async def cb_delete_all(_, query: CallbackQuery):
|
|
| 1018 |
async def cb_cancel_job(_, query: CallbackQuery):
|
| 1019 |
_, job_id_str = query.data.split(":")
|
| 1020 |
job_id = int(job_id_str)
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
job["cancel_event"].set()
|
| 1024 |
await query.answer("Cancelling...")
|
| 1025 |
else:
|
| 1026 |
-
await query.answer("Job not found
|
| 1027 |
|
| 1028 |
@app.on_callback_query(filters.regex("^upload:"))
|
| 1029 |
async def cb_upload(_, query: CallbackQuery):
|
|
@@ -1069,9 +1067,8 @@ async def cb_upload(_, query: CallbackQuery):
|
|
| 1069 |
file_obj = file_id
|
| 1070 |
|
| 1071 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1072 |
-
|
| 1073 |
-
|
| 1074 |
-
await upload_queue.put((user_id, query.message.chat.id, file_obj, filename, job_id, file_id, expected_size, cancel_event))
|
| 1075 |
await query.message.edit_text(f"📦 {len(entries)} file(s) added to queue.", reply_markup=None)
|
| 1076 |
|
| 1077 |
@app.on_callback_query(filters.regex("^upload_all:"))
|
|
@@ -1114,9 +1111,8 @@ async def cb_upload_all(_, query: CallbackQuery):
|
|
| 1114 |
file_obj = file_id
|
| 1115 |
|
| 1116 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1117 |
-
|
| 1118 |
-
|
| 1119 |
-
await upload_queue.put((user_id, query.message.chat.id, file_obj, filename, job_id, file_id, expected_size, cancel_event))
|
| 1120 |
await query.message.edit_text(f"📦 All {len(entries)} files added to queue.", reply_markup=None)
|
| 1121 |
|
| 1122 |
@app.on_callback_query(filters.regex("^cancel_upload:"))
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Telegram ↔ Hugging Face Dataset Storage Bot
|
| 4 |
+
-------------------------------------------
|
| 5 |
+
- AES‑SIV deterministic encryption for per‑user folders and filenames.
|
| 6 |
+
- Full support for all media types, albums, message links, instant uploads, CRUD.
|
| 7 |
+
- Real‑time progress updates for Telegram transfers (shows exact size, percentage, speed, ETA).
|
| 8 |
+
- Duplicate detection: checks both filename AND file size; prompts to skip or keep duplicate.
|
| 9 |
+
- Per‑file size limit (45 GB) enforced at upload time.
|
| 10 |
+
- Direct Hugging Face download links (removed from /files listing per request).
|
| 11 |
+
- Optimised download/upload: larger chunk size (1 MB) for Telegram downloads.
|
| 12 |
+
- Automatic disk space guard, keep‑alive pings, and system status reporting (/status).
|
| 13 |
+
- Built on Pyrogram + tgcrypto for maximum speed.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
import os, re, sys, time, base64, hashlib, shutil, logging, secrets, sqlite3, tempfile, threading, asyncio
|
| 17 |
from pathlib import Path
|
| 18 |
from typing import Optional, List, Tuple, Union
|
|
|
|
| 316 |
) -> None:
|
| 317 |
temp_dir = tempfile.mkdtemp()
|
| 318 |
local_path = None
|
|
|
|
|
|
|
| 319 |
try:
|
| 320 |
await status_msg.edit_text(
|
| 321 |
"⬇️ Downloading from Telegram...",
|
|
|
|
| 493 |
[[InlineKeyboardButton("❌ Cancel", callback_data=f"cancel_job:{job_id}")]]
|
| 494 |
),
|
| 495 |
)
|
| 496 |
+
chunk = f.read(part_size)
|
| 497 |
part_name = f"{orig_name}.part{i}of{num_parts}"
|
| 498 |
part_path = os.path.join(temp_dir, part_name)
|
|
|
|
| 499 |
with open(part_path, "wb") as p:
|
| 500 |
+
p.write(chunk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
await app.send_document(
|
| 502 |
chat_id,
|
| 503 |
document=part_path,
|
|
|
|
| 727 |
|
| 728 |
async def upload_worker():
|
| 729 |
while True:
|
| 730 |
+
job = await upload_queue.get()
|
| 731 |
+
user_id, chat_id, file_obj, orig_filename, job_id, file_id, expected_size = job
|
| 732 |
+
cancel_event = asyncio.Event()
|
|
|
|
|
|
|
|
|
|
| 733 |
active_jobs[job_id] = {"status": "running", "cancel_event": cancel_event}
|
| 734 |
|
| 735 |
while get_free_disk() < MIN_FREE_DISK:
|
|
|
|
| 896 |
return
|
| 897 |
|
| 898 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 899 |
+
active_jobs[job_id] = {"status": "queued"}
|
| 900 |
+
await upload_queue.put((message.from_user.id, message.chat.id, file_id, filename, job_id, file_id, expected_size))
|
|
|
|
|
|
|
| 901 |
await message.reply_text(f"📦 `{filename}` queued for upload.")
|
| 902 |
|
| 903 |
|
|
|
|
| 961 |
new_filename = generate_unique_filename(data['user_id'], base, ext)
|
| 962 |
|
| 963 |
job_id = data['job_id']
|
| 964 |
+
active_jobs[job_id] = {"status": "queued"}
|
|
|
|
|
|
|
| 965 |
await upload_queue.put((
|
| 966 |
data['user_id'],
|
| 967 |
data['chat_id'],
|
|
|
|
| 970 |
job_id,
|
| 971 |
data['file_id'],
|
| 972 |
data['expected_size'],
|
|
|
|
| 973 |
))
|
| 974 |
await query.message.edit_text(f"📦 `{new_filename}` queued as a duplicate.")
|
| 975 |
await query.answer("Duplicate will be uploaded with a new name.")
|
|
|
|
| 1017 |
async def cb_cancel_job(_, query: CallbackQuery):
|
| 1018 |
_, job_id_str = query.data.split(":")
|
| 1019 |
job_id = int(job_id_str)
|
| 1020 |
+
if job_id in active_jobs:
|
| 1021 |
+
active_jobs[job_id]["cancel_event"].set()
|
|
|
|
| 1022 |
await query.answer("Cancelling...")
|
| 1023 |
else:
|
| 1024 |
+
await query.answer("Job not found.")
|
| 1025 |
|
| 1026 |
@app.on_callback_query(filters.regex("^upload:"))
|
| 1027 |
async def cb_upload(_, query: CallbackQuery):
|
|
|
|
| 1067 |
file_obj = file_id
|
| 1068 |
|
| 1069 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1070 |
+
active_jobs[job_id] = {"status": "queued"}
|
| 1071 |
+
await upload_queue.put((user_id, query.message.chat.id, file_obj, filename, job_id, file_id, expected_size))
|
|
|
|
| 1072 |
await query.message.edit_text(f"📦 {len(entries)} file(s) added to queue.", reply_markup=None)
|
| 1073 |
|
| 1074 |
@app.on_callback_query(filters.regex("^upload_all:"))
|
|
|
|
| 1111 |
file_obj = file_id
|
| 1112 |
|
| 1113 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1114 |
+
active_jobs[job_id] = {"status": "queued"}
|
| 1115 |
+
await upload_queue.put((user_id, query.message.chat.id, file_obj, filename, job_id, file_id, expected_size))
|
|
|
|
| 1116 |
await query.message.edit_text(f"📦 All {len(entries)} files added to queue.", reply_markup=None)
|
| 1117 |
|
| 1118 |
@app.on_callback_query(filters.regex("^cancel_upload:"))
|