(sock: any, message: any, args: string[], context: BotContext)
| 36 | usage: '.mega <mega-url>', |
| 37 | |
| 38 | async handler(sock: any, message: any, args: string[], context: BotContext) { |
| 39 | const chatId = context.chatId || message.key.remoteJid; |
| 40 | const text = args.join(' ').trim(); |
| 41 | |
| 42 | if (!text) { |
| 43 | return sock.sendMessage( |
| 44 | chatId, |
| 45 | { text: `*Usage:* .mega https://mega.nz/file/xxxx#xxxx` }, |
| 46 | { quoted: message } |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | try { |
| 51 | const file = File.fromURL(text); |
| 52 | await file.loadAttributes(); |
| 53 | |
| 54 | if (((file.size as number) as any) >= 500 * 1024 * 1024) { |
| 55 | return sock.sendMessage( |
| 56 | chatId, |
| 57 | { text: '❌ *Error:* File too large (Limit: 500MB)' }, |
| 58 | { quoted: message } |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | const { key } = await sock.sendMessage(chatId, { |
| 63 | text: `🌩️ *MEGA DOWNLOAD*\n\n▢ *File:* ${file.name}\n▢ *Size:* ${formatBytes(((file.size as number) as number))}\n\n*Progress:* 0% [░░░░░░░░░░]` |
| 64 | }, { quoted: message }); |
| 65 | |
| 66 | const stream = (file as any).download(); |
| 67 | const chunks: Buffer[] = []; |
| 68 | let lastUpdate = Date.now(); |
| 69 | |
| 70 | stream.on('progress', async (info: { bytesLoaded: number; bytesTotal: number }) => { |
| 71 | const { bytesLoaded, bytesTotal } = info; |
| 72 | const percentage = Math.floor((bytesLoaded / bytesTotal) * 100); |
| 73 | |
| 74 | if (Date.now() - lastUpdate > 3000 || percentage === 100) { |
| 75 | const bar = generateBar(percentage); |
| 76 | await sock.sendMessage(chatId, { |
| 77 | text: `🌩️ *MEGA DOWNLOAD*\n\n▢ *File:* ${file.name}\n▢ *Size:* ${formatBytes(bytesTotal)}\n\n*Progress:* ${percentage}% [${bar}]`, |
| 78 | edit: key |
| 79 | }); |
| 80 | lastUpdate = Date.now(); |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | stream.on('data', (chunk: Buffer) => chunks.push(chunk)); |
| 85 | |
| 86 | stream.on('end', async () => { |
| 87 | const buffer = Buffer.concat(chunks); |
| 88 | const ext = path.extname(file.name ?? '').toLowerCase(); |
| 89 | |
| 90 | await sock.sendMessage(chatId, { |
| 91 | document: buffer, |
| 92 | fileName: file.name, |
| 93 | mimetype: MIME_TYPES[ext] || 'application/octet-stream', |
| 94 | caption: `✅ *Download Complete*\n▢ *File:* ${file.name}\n▢ *Size:* ${formatBytes(((file.size as number) as number))}` |
| 95 | }, { quoted: message }); |
nothing calls this directly
no test coverage detected