fix: sanitize base64 strings in REST API by stripping data url prefixes and whitespaces
This commit is contained in:
@@ -726,8 +726,14 @@ app.post('/api/send-media', async (req, res) => {
|
||||
if (!phone || !base64 || !mimetype) return res.status(400).json({ error: 'phone, base64, and mimetype are required' });
|
||||
|
||||
try {
|
||||
// Sanitize the base64 string (strip data url prefixes if present)
|
||||
let cleanBase64 = base64.trim();
|
||||
if (cleanBase64.includes(';base64,')) {
|
||||
cleanBase64 = cleanBase64.split(';base64,')[1];
|
||||
}
|
||||
|
||||
const { MessageMedia } = require('whatsapp-web.js');
|
||||
const media = new MessageMedia(mimetype, base64, filename || 'file');
|
||||
const media = new MessageMedia(mimetype, cleanBase64, filename || 'file');
|
||||
const chatId = phone.includes('@') ? phone : `${phone}@c.us`;
|
||||
const sentMsg = await waClient.sendMessage(chatId, media, { caption: caption || '' });
|
||||
res.status(200).json({ success: true, messageId: sentMsg.id.id });
|
||||
@@ -737,6 +743,7 @@ app.post('/api/send-media', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// ─── HTTP Health Endpoint ──────────────────────────────────────────────────
|
||||
app.get('/health', (req, res) => {
|
||||
res.status(200).json({
|
||||
|
||||
Reference in New Issue
Block a user