fix(whatsapp): use stream mode for media download to fix empty audio buffer

Fixes #4636

## Problem
WhatsApp voice messages (audio/ogg) were being saved as 0-byte files while
image downloads worked correctly. The issue is that Baileys 7.x
`downloadMediaMessage` with "buffer" mode returns empty data for audio
messages.

## Solution
- Changed from "buffer" mode to "stream" mode in `downloadInboundMedia()`
- Manually collect stream chunks and concatenate into buffer
- Added validation to detect and handle empty buffers

## Testing
- Voice messages now download correctly with actual audio data
- Image downloads continue to work as before
- Tested with WhatsApp voice notes (ogg/opus format)

Before fix: 0 bytes
After fix: 11488 bytes (actual audio data)
This commit is contained in:
Yachiyo Tsukimido (猫猫) 2026-01-30 21:24:16 +08:00
parent da71eaebd2
commit 9d73a63487

View File

@ -31,15 +31,30 @@ export async function downloadInboundMedia(
return undefined;
}
try {
const buffer = (await downloadMediaMessage(
// Use stream mode instead of buffer mode to fix audio download issue
// where buffer mode returns empty data for audio messages in Baileys 7.x
const stream = await downloadMediaMessage(
msg as WAMessage,
"buffer",
"stream",
{},
{
reuploadRequest: sock.updateMediaMessage,
logger: sock.logger,
},
)) as Buffer;
);
const chunks: Buffer[] = [];
for await (const chunk of stream) {
chunks.push(chunk as Buffer);
}
const buffer = Buffer.concat(chunks);
// Validate buffer is not empty
if (!buffer || buffer.length === 0) {
logVerbose(`downloadMediaMessage returned empty buffer for ${mimetype}`);
return undefined;
}
return { buffer, mimetype };
} catch (err) {
logVerbose(`downloadMediaMessage failed: ${String(err)}`);