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

Function handler

plugins/u-aupload.js:11–66  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

uploadFileFunction · 0.90

Tested by

no test coverage detected