File size: 27,672 Bytes
1ef6a70
 
 
 
 
 
 
 
 
 
 
 
ad92359
 
 
 
 
 
 
 
 
1ef6a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad92359
1ef6a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad92359
 
1ef6a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad92359
1ef6a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad92359
1ef6a70
 
 
ad92359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ef6a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import os
import asyncio 
import time
import math
import re
import subprocess
from pyrogram import Client, filters, enums
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
from pyrogram.errors import FloodWait, RPCError
from config import API_ID, API_HASH, ADMINS, CHANNEL_ID, WAITING_TIME
from db import db

# ⚡ SUPER FAST DOWNLOADER IMPORT
try:
    from TechVJ.TechVJ.fast_download import fast_download_file
except ImportError:
    try:
        from TechVJ.fast_download import fast_download_file
    except ImportError:
        from fast_download import fast_download_file

HELP_TXT = """📚 **User Guide & Manual**
━━━━━━━━━━━━━━━━━━━━

**Step 1 — Access & Plans**
यह एक प्राइवेट वीआईपी बॉट है।
• प्लान्स देखने के लिए: `/plans`
• अपनी वैलिडिटी देखने के लिए: `/myplan`
• एक्टिवेशन या रिन्यू के लिए एडमिन से संपर्क करें: @Avigat1210

**Step 2 — Account Login**
• `/login` कमांड भेजें ➔ अपना फ़ोन नंबर भेजें (`+91XXXXXXXXXX`)
• Telegram से आया OTP कोड भेजें (उदा: `1-2-3-4-5`)
• यदि 2-Step Verification लगा है तो पासवर्ड डालें।
• अकाउंट हटाने के लिए: `/logout`

**Step 3 — Content Clone**
`/clone` कमांड भेजें, आपको 3 चीज़ों की आवश्यकता होगी:
 1. पहली पोस्ट का लिंक (Start Point)
 2. अंतिम पोस्ट का लिंक (End Point — सिंगल के लिए `/single`)
 3. टारगेट चैनल/ग्रुप की ID या लिंक (बॉट में पाने के लिए `/skip`)

🖼️ **Thumbnail Set Karne Ka Tarika:**
1. `/clone` शुरू करें और स्टेप 3 पूरा करें।
2. सेटिंग्स मेन्यू में **🖼️ Set Thumbnail** दबाएँ।
3. कोई भी फ़ोटो भेजें — वह थंबनेल सिर्फ आपके इस टास्क के सभी वीडियो पर लगेगा!

🔄 **Auto-Resume Feature:**
यदि कोई टास्क बीच में रुक जाए, तो उसी जगह से दोबारा शुरू करने के लिए `/resume` कमांड भेजें।

🔗 **Message link kaise milega?**
मैसेज पर लॉन्ग प्रेस करें ➔ **Copy Link**
• Private: `https://t.me/c/1234567/100`
• Public: `https://t.me/channelname/100`
• Topic/Forum: `https://t.me/c/1234567/5/100`

━━━━━━━━━━━━━━━━━━━━
**उपलब्ध कमांड्स:**
`/start` `/clone` `/resume` `/cancel` `/login` `/logout` `/id` `/plans` `/myplan` `/help`

Help chahiye? Contact Admin: @Avigat1210"""

class batch_temp(object):
    IS_BATCH = {}
    CLONE_DATA = {}
    LAST_SAVED_ID = {} # ऑटो-रिज्यूम ट्रैकर

def humanbytes(size):
    if not size:
        return "0 B"
    power = 1024
    t_n = 0
    power_labels = {0: ' ', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
    while size > power:
        size /= power
        t_n += 1
    return f"{round(size, 2)} {power_labels[t_n]}B"

def time_formatter(seconds: int) -> str:
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    tmp = ((str(days) + "d, ") if days else "") + \
        ((str(hours) + "h, ") if hours else "") + \
        ((str(minutes) + "m, ") if minutes else "") + \
        ((str(seconds) + "s") if seconds else "")
    return tmp if tmp else "0s"

async def progress_bar(current, total, ud_type, message, start, fname=""):
    now = time.time()
    diff = now - start
    if round(diff % 4.0) == 0 or current == total:
        percentage = current * 100 / total if total > 0 else 0
        speed = current / diff if diff > 0 else 0
        elapsed_time = round(diff)
        eta = round((total - current) / speed) if speed > 0 else 0
        
        elapsed_str = time_formatter(elapsed_time)
        eta_str = time_formatter(eta)

        bar_length = 10
        filled_length = int(bar_length * current // total) if total > 0 else 0
        bar = '▰' * filled_length + '▱' * (bar_length - filled_length)

        fname_text = f"📁 **File:** `{fname[:30]}...`\n" if fname else ""

        status_text = (
            f"**{ud_type}**\n"
            f"{fname_text}"
            f"[{bar}] {percentage:.1f}%\n"
            f"⚡ **Speed:** {humanbytes(speed)}/s\n"
            f"📦 **Done:** {humanbytes(current)} / {humanbytes(total)}\n"
            f"⏱️ **ETA:** {eta_str} | **Time:** {elapsed_str}"
        )
        try:
            await message.edit_text(status_text)
        except Exception:
            pass

def generate_auto_thumb(video_path: str, thumb_path: str):
    try:
        cmd = [
            "ffmpeg", "-ss", "00:00:02", "-i", video_path,
            "-vframes", "1", "-q:v", "2", thumb_path, "-y"
        ]
        subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        if os.path.exists(thumb_path):
            return thumb_path
    except Exception:
        pass
    return None

@Client.on_message(filters.command("id"))
async def get_id(client: Client, message: Message):
    chat_id = message.chat.id
    user_id = message.from_user.id if message.from_user else (message.sender_chat.id if message.sender_chat else "Unknown")
    response = f"📍 **चैट/ग्रुप ID:** `{chat_id}`\n👤 **आपकी ID:** `{user_id}`"
    if message.reply_to_message:
        rep = message.reply_to_message
        rep_user = rep.from_user.id if rep.from_user else (rep.sender_chat.id if rep.sender_chat else "Anonymous")
        response += f"\n\n↩️ **रिप्लाई यूजर ID:** `{rep_user}`"
        if rep.forward_from_chat:
            response += f"\n📢 **फॉरवर्ड चैट ID:** `{rep.forward_from_chat.id}`"
    await message.reply_text(response)

@Client.on_message(filters.command(["help"]))
async def send_help(client: Client, message: Message):
    await message.reply_text(HELP_TXT)

@Client.on_message(filters.command(["start"]))
async def send_start(client: Client, message: Message):
    welcome_text = (
        "👋 **Welcome to Save Restricted Content Bot!**\n"
        "━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n"
        "Kisi bhi **private ya public** Telegram channel/group se files clone karo — "
        "chahe **forwarding band (Restricted)** ho tab bhi.\n\n"
        "✅ Forward-restricted channel/group supported\n"
        "✅ Topics/Threads groups supported\n"
        "✅ 2GB+ files · Custom caption & link entities editing\n"
        "✅ Custom transfer thumbnail support\n"
        "✅ Resume broken transfers (`/resume`)\n\n"
        "Access lene ke liye admin se sampark karein: @Avigat1210\n"
        "Puri guide dekhne ke liye /help send karein."
    )
    await message.reply_text(welcome_text)

@Client.on_message(filters.command(["cancel"]))
async def send_cancel(client: Client, message: Message):
    user_id = message.from_user.id
    batch_temp.IS_BATCH[user_id] = True
    await message.reply_text("🛑 **टास्क रोक दिया गया है।** यदि इसे वापस वहीं से चलाना हो तो `/resume` भेजें।")

def parse_telegram_link(link: str):
    link = link.strip().replace("?single", "")
    c_match = re.search(r"t\.me/c/(\d+)(?:/\d+)?/(\d+)", link)
    if c_match:
        return int("-100" + c_match.group(1)), int(c_match.group(2))
    b_match = re.search(r"t\.me/b/([^/]+)/(\d+)", link)
    if b_match:
        return b_match.group(1), int(b_match.group(2))
    p_match = re.search(r"t\.me/([^/]+)(?:/\d+)?/(\d+)", link)
    if p_match:
        return p_match.group(1), int(p_match.group(2))
    return None, None

@Client.on_message(filters.command(["resume"]) & filters.private)
async def resume_command(client: Client, message: Message):
    user_id = message.from_user.id
    task = batch_temp.CLONE_DATA.get(user_id)
    last_id = batch_temp.LAST_SAVED_ID.get(user_id)
    if not task or not last_id:
        return await message.reply_text("⚠️ कोई रुका हुआ पिछला टास्क नहीं मिला। कृपया नया टास्क शुरू करने के लिए `/clone` करें।")
    
    if last_id >= task["end_id"]:
        return await message.reply_text("✅ पिछला टास्क पहले ही पूरा हो चुका था।")

    task["start_id"] = last_id + 1
    await message.reply_text(f"🔄 **टास्क रिज्यूम किया जा रहा है...** (ID `{task['start_id']}` से `{task['end_id']}` तक)")
    await start_clone_task(client, user_id)

@Client.on_message(filters.command(["clone", "clon"]) & filters.private)
async def clone_command(client: Client, message: Message):
    user_id = message.from_user.id
    if not db.is_user_vip(user_id, ADMINS):
        return await message.reply_text("🔒 **यह फीचर केवल एक्टिव वीआईपी मेंबर्स के लिए है!**\n\nAccess lene ke liye admin se sampark karein: @Avigat1210")
    if batch_temp.IS_BATCH.get(user_id) == False:
        return await message.reply_text("⚠️ आपका एक टास्क पहले से चल रहा है। इंतज़ार करें या /cancel करें।")

    user_session = db.get_session(user_id)
    if not user_session:
        return await message.reply_text("🔑 पहले **/login** करें।")

    guide_text = (
        "📖 **कंटेंट क्लोनिंग दिशा-निर्देश:**\n"
        "━━━━━━━━━━━━━━━━━━━━\n"
        "👉 **स्टेप 1:** जहाँ से कॉपी करना है, उसकी **पहली लिंक (Start Link)** भेजें:\n"
        "(रद्द करने के लिए /cancel लिखें)"
    )
    first_msg = await client.ask(user_id, guide_text, filters=filters.text, timeout=300)
    if first_msg.text == "/cancel":
        return await first_msg.reply("❌ प्रोसेस रद्द कर दिया गया।")
    
    start_link = first_msg.text.strip()
    s_chat, s_id = parse_telegram_link(start_link)
    if not s_id:
        return await first_msg.reply("❌ अमान्य टेलीग्राम लिंक! दोबारा /clone करें।")

    last_msg = await client.ask(
        user_id, 
        "👉 **स्टेप 2:** अब **अंतिम लिंक (End Link)** भेजें:\n(सिंगल पोस्ट के लिए `/single` लिखें)",
        filters=filters.text, 
        timeout=300
    )
    if last_msg.text == "/cancel":
        return await last_msg.reply("❌ प्रोसेस रद्द कर दिया गया।")
    
    if last_msg.text.strip() == "/single":
        e_chat, e_id = s_chat, s_id
    else:
        end_link = last_msg.text.strip()
        e_chat, e_id = parse_telegram_link(end_link)
        if not e_id:
            return await last_msg.reply("❌ अमान्य अंतिम लिंक! दोबारा /clone करें।")

    dest_msg = await client.ask(
        user_id,
        "👉 **स्टेप 3:** जहाँ अपलोड करवाना है, उस **चैनल/ग्रुप का लिंक या ID** भेजें:\n(बॉट में पाने के लिए `/skip` लिखें)",
        filters=filters.text,
        timeout=300
    )
    if dest_msg.text == "/cancel":
        return await dest_msg.reply("❌ प्रोसेस रद्द कर दिया गया।")

    dest_val = dest_msg.text.strip()
    if dest_val == "/skip":
        target_chat = user_id
    else:
        try:
            target_chat = int(dest_val)
        except ValueError:
            target_chat = dest_val.replace("https://t.me/", "@")

    batch_temp.CLONE_DATA[user_id] = {
        "source_chat": s_chat,
        "start_id": min(s_id, e_id),
        "end_id": max(s_id, e_id),
        "target_chat": target_chat,
        "custom_caption": None,
        "extra_caption": None,
        "replace_find": None,
        "replace_with": None,
        "thumb_path": None
    }
    await show_customization_menu(client, user_id)

async def show_customization_menu(client: Client, user_id: int):
    data = batch_temp.CLONE_DATA.get(user_id, {})
    cap_status = "सेट है ✅" if data.get("custom_caption") else "डिफ़ॉल्ट"
    ext_status = "सेट है ✅" if data.get("extra_caption") else "कोई नहीं"
    thm_status = "सेट है ✅" if data.get("thumb_path") else "डिफ़ॉल्ट"
    rep_status = f"`{data.get('replace_find')}` ➔ `{data.get('replace_with')}`" if data.get("replace_find") else "कोई नहीं"

    panel_text = (
        "⚙️ **कंटेंट कस्टमाइज़ेशन सेटिंग्स:**\n"
        "━━━━━━━━━━━━━━━━━━━━\n"
        f"📝 नया कैप्शन: `{cap_status}`\n"
        f"🔄 रिप्लेस: {rep_status}\n"
        f"➕ अतिरिक्त कैप्शन: `{ext_status}`\n"
        f"🖼️ थंबनेल: `{thm_status}`\n"
        f"🎯 टारगेट चैट: `{data.get('target_chat')}`\n"
        f"📊 रेंज: `{data.get('start_id')}` से `{data.get('end_id')}`\n"
        "━━━━━━━━━━━━━━━━━━━━\n"
        "एडिट करें या तुरंत ट्रांसफर के लिए **🚀 Confirm & Start** दबाएँ:"
    )
    buttons = InlineKeyboardMarkup([
        [InlineKeyboardButton("📝 Edit Full Caption", callback_data="set_caption"),
         InlineKeyboardButton("🔄 Replace in Caption", callback_data="set_replace")],
        [InlineKeyboardButton("➕ Add Extra Caption", callback_data="set_extra"),
         InlineKeyboardButton("🖼️ Set Thumbnail", callback_data="set_thumb")],
        [InlineKeyboardButton("🗑️ Clear Settings", callback_data="clear_settings"),
         InlineKeyboardButton("🚀 Confirm & Start", callback_data="confirm_start")]
    ])
    await client.send_message(user_id, panel_text, reply_markup=buttons)

@Client.on_callback_query()
async def callback_handler(client: Client, query: CallbackQuery):
    user_id = query.from_user.id
    if user_id not in batch_temp.CLONE_DATA:
        return await query.answer("⚠️ सेशन एक्सपायर हो गया, दोबारा /clone करें।", show_alert=True)

    data = query.data
    if data == "set_caption":
        await query.message.delete()
        msg = await client.ask(user_id, "✍️ **नया कस्टम कैप्शन भेजें** (हाइपरलिंक व फॉर्मेटिंग मान्य है):", filters=filters.text, timeout=180)
        batch_temp.CLONE_DATA[user_id]["custom_caption"] = msg.text.markdown
        await show_customization_menu(client, user_id)
    elif data == "set_replace":
        await query.message.delete()
        find_msg = await client.ask(user_id, "🔍 क्या खोजना/हटाना चाहते हैं? वह शब्द भेजें:", filters=filters.text, timeout=180)
        if find_msg.text == "/cancel":
            return await show_customization_menu(client, user_id)
        with_msg = await client.ask(user_id, f"✏️ `{find_msg.text}` की जगह क्या लगाना चाहते हैं? (खाली रखने के लिए `/empty`):", filters=filters.text, timeout=180)
        if with_msg.text == "/cancel":
            return await show_customization_menu(client, user_id)
        batch_temp.CLONE_DATA[user_id]["replace_find"] = find_msg.text
        batch_temp.CLONE_DATA[user_id]["replace_with"] = "" if with_msg.text.strip() == "/empty" else with_msg.text.markdown
        await show_customization_menu(client, user_id)
    elif data == "set_extra":
        await query.message.delete()
        msg = await client.ask(user_id, "✍️ **अतिरिक्त कैप्शन भेजें** (हाइपरलिंक्स मान्य हैं):", filters=filters.text, timeout=180)
        batch_temp.CLONE_DATA[user_id]["extra_caption"] = msg.text.markdown
        await show_customization_menu(client, user_id)
    elif data == "set_thumb":
        await query.message.delete()
        msg = await client.ask(user_id, "🖼️ **थंबनेल के लिए फ़ोटो भेजें:**", filters=filters.photo, timeout=180)
        user_thumb_dir = os.path.join("thumbs", str(user_id))
        os.makedirs(user_thumb_dir, exist_ok=True)
        thumb_file = os.path.join(user_thumb_dir, "thumb.jpg")
        await client.download_media(msg.photo, file_name=thumb_file)
        batch_temp.CLONE_DATA[user_id]["thumb_path"] = thumb_file
        await msg.reply_text("✅ आपका थंबनेल सुरक्षित सेव कर लिया गया!")
        await show_customization_menu(client, user_id)
    elif data == "clear_settings":
        batch_temp.CLONE_DATA[user_id]["custom_caption"] = None
        batch_temp.CLONE_DATA[user_id]["extra_caption"] = None
        batch_temp.CLONE_DATA[user_id]["replace_find"] = None
        batch_temp.CLONE_DATA[user_id]["replace_with"] = None
        tpath = batch_temp.CLONE_DATA[user_id].get("thumb_path")
        if tpath and os.path.exists(tpath):
            os.remove(tpath)
        batch_temp.CLONE_DATA[user_id]["thumb_path"] = None
        await query.answer("सेटिंग्स रीसेट हो गईं।")
        await query.message.delete()
        await show_customization_menu(client, user_id)
    elif data == "confirm_start":
        await query.message.delete()
        await start_clone_task(client, user_id)

async def start_clone_task(client: Client, user_id: int):
    task = batch_temp.CLONE_DATA.get(user_id)
    if not task:
        return await client.send_message(user_id, "❌ कोई सक्रिय टास्क नहीं मिला।")

    source_chat = task["source_chat"]
    start_id = task["start_id"]
    end_id = task["end_id"]
    target_chat = task["target_chat"]

    user_session = db.get_session(user_id)
    user_api_id = int(db.get_api_id(user_id) or API_ID)
    user_api_hash = db.get_api_hash(user_id) or API_HASH

    acc = Client(
        f"user_session_{user_id}", 
        session_string=user_session, 
        api_hash=user_api_hash, 
        api_id=user_api_id, 
        in_memory=True,
        workers=150,
        max_concurrent_transmissions=16
    )
    try:
        await acc.start()
    except Exception as e:
        return await client.send_message(user_id, f"❌ सेशन कनेक्ट एरर: `{e}`")

    try:
        await acc.join_chat(source_chat)
    except Exception:
        pass

    batch_temp.IS_BATCH[user_id] = False
    success = 0
    not_found = 0
    retrying = 0
    total_bytes = 0
    start_time = time.time()

    status_msg = await client.send_message(user_id, "🚀 **कंटेंट ट्रांसफर शुरू हो रहा है...**")

    for msgid in range(start_id, end_id + 1):
        if batch_temp.IS_BATCH.get(user_id):
            break

        attempts = 0
        while attempts < 5:
            try:
                ok, sz = await process_media(client, acc, user_id, source_chat, msgid, target_chat, task, status_msg)
                if ok:
                    success += 1
                    total_bytes += sz
                    batch_temp.LAST_SAVED_ID[user_id] = msgid
                else:
                    not_found += 1
                break
            except FloodWait as fw:
                await status_msg.edit_text(f"⏳ **Rate-limit:** {fw.value} सेकंड इंतज़ार कर रहे हैं... (ऑटो-रिज्यूम होगा)")
                await asyncio.sleep(fw.value + 2)
                attempts += 1
            except RPCError as rpc:
                print(f"RPC Error on {msgid}: {rpc}")
                await asyncio.sleep(3)
                attempts += 1
            except Exception as e:
                print(f"General error on {msgid}: {e}")
                retrying += 1
                break

        await asyncio.sleep(WAITING_TIME)

    try:
        await acc.stop()
    except Exception:
        pass

    batch_temp.IS_BATCH[user_id] = True
    tpath = task.get("thumb_path")
    if tpath and os.path.exists(tpath):
        os.remove(tpath)

    elapsed_time = max(1, int(time.time() - start_time))
    avg_speed = f"{humanbytes(total_bytes / elapsed_time)}/s"

    summary_card = (
        "🏁 **Transfer Finished**\n"
        "━━━━━━━━━━━━━━━━━━━━\n"
        f"✅ Success:         {success}\n"
        f"🗑️ Not Found:       {not_found}\n"
        f"🔁 Retrying:         {retrying}\n"
        f"📦 Total Size:      {humanbytes(total_bytes)}\n"
        f"⚡ Avg Speed:       {avg_speed}\n"
        f"⏱️ Time:            {time_formatter(elapsed_time)}\n"
        "━━━━━━━━━━━━━━━━━━━━"
    )
    await status_msg.edit_text(summary_card)

def get_file_name_from_msg(msg: Message) -> str:
    if msg.document and msg.document.file_name:
        return msg.document.file_name
    elif msg.video and msg.video.file_name:
        return msg.video.file_name
    elif msg.video:
        return f"video_{msg.id}.mp4"
    elif msg.audio and msg.audio.file_name:
        return msg.audio.file_name
    elif msg.audio:
        return f"audio_{msg.id}.mp3"
    elif msg.photo:
        return f"photo_{msg.id}.jpg"
    return f"file_{msg.id}.bin"

async def process_media(client: Client, acc: Client, user_id: int, source_chat, msgid: int, target_chat, task: dict, status_msg: Message):
    msg = None
    try:
        msg = await acc.get_messages(source_chat, msgid)
    except Exception:
        try:
            msg = await client.get_messages(source_chat, msgid)
        except Exception:
            return False, 0

    if not msg or msg.empty:
        return False, 0

    final_caption = msg.caption.markdown if msg.caption else ""

    if task.get("custom_caption"):
        final_caption = task["custom_caption"]
    elif task.get("replace_find"):
        find_w = task["replace_find"]
        with_w = task.get("replace_with") or ""
        if final_caption:
            final_caption = final_caption.replace(find_w, with_w)

    if task.get("extra_caption"):
        if final_caption:
            final_caption = f"{final_caption}\n\n{task['extra_caption']}"
        else:
            final_caption = task["extra_caption"]

    if msg.text:
        text_to_send = msg.text.markdown
        if task.get("custom_caption"):
            text_to_send = task["custom_caption"]
        elif task.get("replace_find"):
            find_w = task["replace_find"]
            with_w = task.get("replace_with") or ""
            text_to_send = text_to_send.replace(find_w, with_w)
        if task.get("extra_caption"):
            text_to_send += f"\n\n{task['extra_caption']}"
        await client.send_message(target_chat, text_to_send, parse_mode=enums.ParseMode.MARKDOWN)
        return True, len(text_to_send.encode('utf-8'))

    file_name = get_file_name_from_msg(msg)
    user_down_dir = os.path.join("downloads", str(user_id))
    os.makedirs(user_down_dir, exist_ok=True)
    down_path = os.path.join(user_down_dir, f"{msgid}_{file_name}")

    down_start = time.time()
    await status_msg.edit_text(f"📥 **Downloading ID:** `{msgid}`\n📁 **File:** `{file_name}`...")

    # ⚡ FAST MULTI-PART DOWNLOADER
    if msg.photo:
        file_path = await acc.download_media(msg, file_name=down_path)
    else:
        try:
            file_path = await fast_download_file(
                acc,
                msg,
                out_path=down_path,
                progress_func=progress_bar,
                progress_args=("📥 Downloading ID: " + str(msgid), status_msg, down_start, file_name)
            )
        except Exception as e:
            print(f"Fast download failed, fallback to normal: {e}")
            file_path = await acc.download_media(
                msg,
                file_name=down_path,
                progress=progress_bar,
                progress_args=("📥 Downloading ID: " + str(msgid), status_msg, down_start, file_name)
            )

    if not file_path or not os.path.exists(file_path):
        return False, 0

    size = os.path.getsize(file_path)
    up_start = time.time()
    await status_msg.edit_text(f"📤 **Uploading ID:** `{msgid}`\n📁 **File:** `{file_name}`...")

    thumb_to_use = task.get("thumb_path")
    created_temp_thumb = None

    if not thumb_to_use and msg.video:
        auto_thumb_path = os.path.join(user_down_dir, f"thumb_{msgid}.jpg")
        created_temp_thumb = generate_auto_thumb(file_path, auto_thumb_path)
        thumb_to_use = created_temp_thumb

    try:
        if msg.photo:
            await client.send_photo(
                target_chat, file_path, caption=final_caption, parse_mode=enums.ParseMode.MARKDOWN
            )
        elif msg.document:
            await client.send_document(
                target_chat, file_path, thumb=thumb_to_use, caption=final_caption, parse_mode=enums.ParseMode.MARKDOWN,
                progress=progress_bar, progress_args=(f"📤 Uploading ID: {msgid}", status_msg, up_start, file_name)
            )
        elif msg.video:
            await client.send_video(
                target_chat, file_path, thumb=thumb_to_use, caption=final_caption, parse_mode=enums.ParseMode.MARKDOWN,
                duration=msg.video.duration or 0, width=msg.video.width or 0, height=msg.video.height or 0,
                progress=progress_bar, progress_args=(f"📤 Uploading ID: {msgid}", status_msg, up_start, file_name)
            )
        elif msg.audio:
            await client.send_audio(
                target_chat, file_path, thumb=thumb_to_use, caption=final_caption, parse_mode=enums.ParseMode.MARKDOWN,
                progress=progress_bar, progress_args=(f"📤 Uploading ID: {msgid}", status_msg, up_start, file_name)
            )
        else:
            await client.send_document(target_chat, file_path, thumb=thumb_to_use, caption=final_caption, parse_mode=enums.ParseMode.MARKDOWN)
    except Exception as e:
        print(f"Upload error: {e}")
        return False, 0
    finally:
        if os.path.exists(file_path):
            os.remove(file_path)
        if created_temp_thumb and os.path.exists(created_temp_thumb):
            os.remove(created_temp_thumb)

    return True, size