(sock: any, message: any, args: any, context: BotContext)
| 11 | description: 'Upload to Tmpfiles.org (temporary)', |
| 12 | usage: '.tmpfiles (reply to media or caption on media)', |
| 13 | async handler(sock: any, message: any, args: any, context: BotContext) { |
| 14 | const chatId = context.chatId || message.key.remoteJid; |
| 15 | |
| 16 | try { |
| 17 | const hasMedia = message.message?.imageMessage || |
| 18 | message.message?.videoMessage || |
| 19 | message.message?.stickerMessage || |
| 20 | message.message?.documentMessage; |
| 21 | |
| 22 | const quotedMsg = message.message?.extendedTextMessage?.contextInfo?.quotedMessage; |
| 23 | |
| 24 | if (!hasMedia && !quotedMsg) { |
| 25 | await sock.sendMessage(chatId, { text: '⚠️ Please send media with caption or reply to media!' }, { quoted: message }); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | const mediaSource = hasMedia ? message.message : quotedMsg; |
| 30 | const type = Object.keys(mediaSource).find(key => |
| 31 | ['imageMessage', 'videoMessage', 'stickerMessage', 'documentMessage'].includes(key) |
| 32 | ); |
| 33 | |
| 34 | if (!type) { |
| 35 | await sock.sendMessage(chatId, { text: '⚠️ Unsupported media type!' }, { quoted: message }); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | await sock.sendMessage(chatId, { text: 'Uploading to Tmpfiles...' }, { quoted: message }); |
| 40 | |
| 41 | const mediaType = type === 'stickerMessage' ? 'sticker' : type.replace('Message', ''); |
| 42 | const stream = await downloadContentFromMessage(mediaSource[type], mediaType as any); |
| 43 | let buffer = Buffer.from([]); |
| 44 | for await (const chunk of stream) { |
| 45 | buffer = Buffer.concat([buffer, chunk]); |
| 46 | } |
| 47 | |
| 48 | let ext = 'bin'; |
| 49 | if (type === 'imageMessage') ext = 'jpg'; |
| 50 | else if (type === 'videoMessage') ext = 'mp4'; |
| 51 | else if (type === 'stickerMessage') ext = 'webp'; |
| 52 | else if (mediaSource[type].fileName) { |
| 53 | ext = mediaSource[type].fileName.split('.').pop() || 'bin'; |
| 54 | } |
| 55 | |
| 56 | const tempDir = path.join('./temp'); |
| 57 | if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, { recursive: true }); |
| 58 | |
| 59 | const tempPath = path.join(tempDir, `tmpfiles_${Date.now()}.${ext}`); |
| 60 | fs.writeFileSync(tempPath, buffer); |
| 61 | |
| 62 | const result = await uploadToTmpfiles(tempPath); |
| 63 | |
| 64 | await sock.sendMessage(chatId, { |
| 65 | text: `✅ *Tmpfiles Upload Success!*\n\n🔗 Direct: ${result.url}\n📄 Page: ${result.page_url}` |
| 66 | }, { quoted: message }); |
| 67 | |
| 68 | fs.unlinkSync(tempPath); |
| 69 | |
| 70 | } catch(error: any) { |
nothing calls this directly
no test coverage detected