Update app.py
Browse files
app.py
CHANGED
|
@@ -301,6 +301,8 @@ async def do_upload_job(
|
|
| 301 |
) -> None:
|
| 302 |
temp_dir = tempfile.mkdtemp()
|
| 303 |
local_path = None
|
|
|
|
|
|
|
| 304 |
try:
|
| 305 |
await status_msg.edit_text(
|
| 306 |
"β¬οΈ Downloading from Telegram...",
|
|
@@ -478,11 +480,17 @@ async def do_download_job(
|
|
| 478 |
[[InlineKeyboardButton("β Cancel", callback_data=f"cancel_job:{job_id}")]]
|
| 479 |
),
|
| 480 |
)
|
| 481 |
-
chunk = f.read(part_size)
|
| 482 |
part_name = f"{orig_name}.part{i}of{num_parts}"
|
| 483 |
part_path = os.path.join(temp_dir, part_name)
|
|
|
|
| 484 |
with open(part_path, "wb") as p:
|
| 485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
await app.send_document(
|
| 487 |
chat_id,
|
| 488 |
document=part_path,
|
|
@@ -712,9 +720,12 @@ async def process_album_group(media_group_id: str):
|
|
| 712 |
|
| 713 |
async def upload_worker():
|
| 714 |
while True:
|
| 715 |
-
|
| 716 |
-
user_id, chat_id, file_obj, orig_filename, job_id, file_id, expected_size =
|
| 717 |
-
|
|
|
|
|
|
|
|
|
|
| 718 |
active_jobs[job_id] = {"status": "running", "cancel_event": cancel_event}
|
| 719 |
|
| 720 |
while get_free_disk() < MIN_FREE_DISK:
|
|
@@ -881,8 +892,10 @@ async def cmd_upload(_, message: Message):
|
|
| 881 |
return
|
| 882 |
|
| 883 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 884 |
-
|
| 885 |
-
|
|
|
|
|
|
|
| 886 |
await message.reply_text(f"π¦ `{filename}` queued for upload.")
|
| 887 |
|
| 888 |
|
|
@@ -946,7 +959,9 @@ async def cb_dup_keep(_, query: CallbackQuery):
|
|
| 946 |
new_filename = generate_unique_filename(data['user_id'], base, ext)
|
| 947 |
|
| 948 |
job_id = data['job_id']
|
| 949 |
-
|
|
|
|
|
|
|
| 950 |
await upload_queue.put((
|
| 951 |
data['user_id'],
|
| 952 |
data['chat_id'],
|
|
@@ -955,6 +970,7 @@ async def cb_dup_keep(_, query: CallbackQuery):
|
|
| 955 |
job_id,
|
| 956 |
data['file_id'],
|
| 957 |
data['expected_size'],
|
|
|
|
| 958 |
))
|
| 959 |
await query.message.edit_text(f"π¦ `{new_filename}` queued as a duplicate.")
|
| 960 |
await query.answer("Duplicate will be uploaded with a new name.")
|
|
@@ -1002,11 +1018,12 @@ async def cb_delete_all(_, query: CallbackQuery):
|
|
| 1002 |
async def cb_cancel_job(_, query: CallbackQuery):
|
| 1003 |
_, job_id_str = query.data.split(":")
|
| 1004 |
job_id = int(job_id_str)
|
| 1005 |
-
|
| 1006 |
-
|
|
|
|
| 1007 |
await query.answer("Cancelling...")
|
| 1008 |
else:
|
| 1009 |
-
await query.answer("Job not found.")
|
| 1010 |
|
| 1011 |
@app.on_callback_query(filters.regex("^upload:"))
|
| 1012 |
async def cb_upload(_, query: CallbackQuery):
|
|
@@ -1052,8 +1069,9 @@ async def cb_upload(_, query: CallbackQuery):
|
|
| 1052 |
file_obj = file_id
|
| 1053 |
|
| 1054 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1055 |
-
|
| 1056 |
-
|
|
|
|
| 1057 |
await query.message.edit_text(f"π¦ {len(entries)} file(s) added to queue.", reply_markup=None)
|
| 1058 |
|
| 1059 |
@app.on_callback_query(filters.regex("^upload_all:"))
|
|
@@ -1096,8 +1114,9 @@ async def cb_upload_all(_, query: CallbackQuery):
|
|
| 1096 |
file_obj = file_id
|
| 1097 |
|
| 1098 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1099 |
-
|
| 1100 |
-
|
|
|
|
| 1101 |
await query.message.edit_text(f"π¦ All {len(entries)} files added to queue.", reply_markup=None)
|
| 1102 |
|
| 1103 |
@app.on_callback_query(filters.regex("^cancel_upload:"))
|
|
|
|
| 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 |
[[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 |
+
bytes_written = 0
|
| 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 |
|
| 721 |
async def upload_worker():
|
| 722 |
while True:
|
| 723 |
+
# Unpack with cancel_event as last element
|
| 724 |
+
user_id, chat_id, file_obj, orig_filename, job_id, file_id, expected_size, cancel_event = await upload_queue.get()
|
| 725 |
+
if cancel_event.is_set():
|
| 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 |
return
|
| 893 |
|
| 894 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 895 |
+
# Create cancel_event immediately
|
| 896 |
+
cancel_event = asyncio.Event()
|
| 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 |
new_filename = generate_unique_filename(data['user_id'], base, ext)
|
| 960 |
|
| 961 |
job_id = data['job_id']
|
| 962 |
+
# Create cancel_event for the duplicate job
|
| 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 |
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 |
async def cb_cancel_job(_, query: CallbackQuery):
|
| 1019 |
_, job_id_str = query.data.split(":")
|
| 1020 |
job_id = int(job_id_str)
|
| 1021 |
+
job = active_jobs.get(job_id)
|
| 1022 |
+
if job and "cancel_event" in job:
|
| 1023 |
+
job["cancel_event"].set()
|
| 1024 |
await query.answer("Cancelling...")
|
| 1025 |
else:
|
| 1026 |
+
await query.answer("Job not found or already finished.")
|
| 1027 |
|
| 1028 |
@app.on_callback_query(filters.regex("^upload:"))
|
| 1029 |
async def cb_upload(_, query: CallbackQuery):
|
|
|
|
| 1069 |
file_obj = file_id
|
| 1070 |
|
| 1071 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1072 |
+
cancel_event = asyncio.Event()
|
| 1073 |
+
active_jobs[job_id] = {"status": "queued", "cancel_event": cancel_event}
|
| 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 |
file_obj = file_id
|
| 1115 |
|
| 1116 |
job_id = int(time.time() * 1000 + hash(filename))
|
| 1117 |
+
cancel_event = asyncio.Event()
|
| 1118 |
+
active_jobs[job_id] = {"status": "queued", "cancel_event": cancel_event}
|
| 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:"))
|