MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MDX / handler

Function handler

plugins/u-aupload.ts:13–76  ·  view source on GitHub ↗
(sock: any, message: any, args: any, context: BotContext)

Source from the content-addressed store, hash-verified

11 description: 'Upload media to cloud and get URL',
12 usage: '.aupload (reply to image/video/gif/sticker)',
13 async handler(sock: any, message: any, args: any, context: BotContext) {
14 const chatId = context.chatId || message.key.remoteJid;
15
16 try {
17 const quotedMsg = message.message?.extendedTextMessage?.contextInfo?.quotedMessage;
18 if (!quotedMsg) {
19 await sock.sendMessage(chatId, { text: '⚠️ Please reply to an image, video, GIF, or sticker!' }, { quoted: message });
20 return;
21 }
22
23 const type = Object.keys(quotedMsg)[0];
24 const supportedTypes = ['imageMessage', 'videoMessage', 'stickerMessage', 'documentMessage'];
25
26 if (!supportedTypes.includes(type)) {
27 await sock.sendMessage(chatId, { text: '⚠️ Unsupported file type! Reply to image/video/gif/sticker/document' }, { quoted: message });
28 return;
29 }
30
31 await sock.sendMessage(chatId, { text: 'Uploading to cloud...' }, { quoted: message });
32
33 const mediaType = type === 'stickerMessage' ? 'sticker' : type.replace('Message', '');
34 const stream = await downloadContentFromMessage(quotedMsg[type], mediaType as any);
35 let buffer = Buffer.from([]);
36 for await (const chunk of stream) {
37 buffer = Buffer.concat([buffer, chunk]);
38 }
39
40 let ext = 'bin';
41 if (type === 'imageMessage') ext = 'jpg';
42 else if (type === 'videoMessage') ext = 'mp4';
43 else if (type === 'stickerMessage') ext = 'webp';
44 else if (type === 'documentMessage') {
45 const fileName = quotedMsg[type].fileName || 'file';
46 ext = fileName.split('.').pop() || 'bin';
47 }
48
49 const tempDir = path.join('./temp');
50 if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, { recursive: true });
51
52 const tempPath = path.join(tempDir, `upload_${Date.now()}.${ext}`);
53 fs.writeFileSync(tempPath, buffer);
54
55 const stats = fs.statSync(tempPath);
56 const fileSizeMB = (stats.size / (1024 * 1024)).toFixed(2);
57
58 const result = await uploadFile(tempPath);
59
60 await sock.sendMessage(chatId, {
61 text: `✅ *Upload Successful!*\n\n` +
62 `📊 *Service:* ${result.service}\n` +
63 `📦 *Size:* ${fileSizeMB} MB\n` +
64 `🔗 *URL:* ${result.url}\n\n` +
65 `_Click the link to view/download_`
66 }, { quoted: message });
67
68 fs.unlinkSync(tempPath);
69
70 } catch(error: any) {

Callers

nothing calls this directly

Calls 1

uploadFileFunction · 0.85

Tested by

no test coverage detected