From 9d73a63487ac706b6f937ed81f969371e768dcfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yachiyo=20Tsukimido=20=28=E7=8C=AB=E7=8C=AB=29?= Date: Fri, 30 Jan 2026 21:24:16 +0800 Subject: [PATCH] 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) --- src/web/inbound/media.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/web/inbound/media.ts b/src/web/inbound/media.ts index 9e7d566f4..dda83c3f6 100644 --- a/src/web/inbound/media.ts +++ b/src/web/inbound/media.ts @@ -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)}`);