Skip to content

Commit f268571

Browse files
CyPackclaude
authored andcommitted
fix(media): explicit updateMediaMessage for Baileys RC9 reuploadRequest bug
Baileys RC9 checks error.status but Boom sets output.statusCode, causing reuploadRequest to never trigger automatically. Fix: in getBase64FromMediaMessage catch block, explicitly call this.client.updateMediaMessage() with 30s timeout to get fresh CDN URL, then retry download. Falls back to downloadContentFromMessage if updateMediaMessage also fails. Technique validated in OwnPilot retryMediaFromMetadata() — 174/205 SOR files recovered. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cd800f2 commit f268571

1 file changed

Lines changed: 46 additions & 19 deletions

File tree

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3903,30 +3903,57 @@ export class BaileysStartupService extends ChannelStartupService {
39033903
{ logger: P({ level: 'error' }) as any, reuploadRequest: this.client.updateMediaMessage },
39043904
);
39053905
} catch {
3906-
this.logger.error('Download Media failed, trying to retry in 5 seconds...');
3907-
await new Promise((resolve) => setTimeout(resolve, 5000));
3908-
const mediaType = Object.keys(msg.message).find((key) => key.endsWith('Message'));
3909-
if (!mediaType) throw new Error('Could not determine mediaType for fallback');
3906+
this.logger.error('Download Media failed, attempting explicit updateMediaMessage (Baileys RC9 reuploadRequest bug workaround)...');
39103907

3908+
// Baileys RC9 bug: reuploadRequest callback never triggers because downloadMediaMessage
3909+
// checks error.status but Boom sets output.statusCode — so the automatic re-upload path
3910+
// is dead code. Fix: explicitly call updateMediaMessage to get a fresh CDN URL,
3911+
// then retry the download. This mirrors the technique used in OwnPilot retryMediaFromMetadata().
39113912
try {
3912-
const media = await downloadContentFromMessage(
3913-
{
3914-
mediaKey: msg.message?.[mediaType]?.mediaKey,
3915-
directPath: msg.message?.[mediaType]?.directPath,
3916-
url: `https://mmg.whatsapp.net${msg?.message?.[mediaType]?.directPath}`,
3917-
},
3918-
await this.mapMediaType(mediaType),
3913+
const REUPLOAD_TIMEOUT_MS = 30_000;
3914+
this.logger.info('Requesting media re-upload from sender device via updateMediaMessage...');
3915+
const updatedMsg = await Promise.race([
3916+
this.client.updateMediaMessage({ key: msg.key, message: msg.message }),
3917+
new Promise<never>((_, reject) =>
3918+
setTimeout(
3919+
() => reject(new Error('updateMediaMessage timed out after 30s — sender device may be offline')),
3920+
REUPLOAD_TIMEOUT_MS,
3921+
),
3922+
),
3923+
]);
3924+
buffer = await downloadMediaMessage(
3925+
updatedMsg,
3926+
'buffer',
39193927
{},
3928+
{ logger: P({ level: 'error' }) as any, reuploadRequest: this.client.updateMediaMessage },
39203929
);
3921-
const chunks = [];
3922-
for await (const chunk of media) {
3923-
chunks.push(chunk);
3930+
this.logger.info('Download Media successful after explicit updateMediaMessage!');
3931+
} catch (reuploadErr) {
3932+
this.logger.error(`updateMediaMessage failed: ${reuploadErr?.message} — falling back to downloadContentFromMessage...`);
3933+
await new Promise((resolve) => setTimeout(resolve, 5000));
3934+
const mediaType = Object.keys(msg.message).find((key) => key.endsWith('Message'));
3935+
if (!mediaType) throw new Error('Could not determine mediaType for fallback');
3936+
3937+
try {
3938+
const media = await downloadContentFromMessage(
3939+
{
3940+
mediaKey: msg.message?.[mediaType]?.mediaKey,
3941+
directPath: msg.message?.[mediaType]?.directPath,
3942+
url: `https://mmg.whatsapp.net${msg?.message?.[mediaType]?.directPath}`,
3943+
},
3944+
await this.mapMediaType(mediaType),
3945+
{},
3946+
);
3947+
const chunks = [];
3948+
for await (const chunk of media) {
3949+
chunks.push(chunk);
3950+
}
3951+
buffer = Buffer.concat(chunks);
3952+
this.logger.info('Download Media with downloadContentFromMessage was successful!');
3953+
} catch (fallbackErr) {
3954+
this.logger.error('Download Media with downloadContentFromMessage also failed!');
3955+
throw fallbackErr;
39243956
}
3925-
buffer = Buffer.concat(chunks);
3926-
this.logger.info('Download Media with downloadContentFromMessage was successful!');
3927-
} catch (fallbackErr) {
3928-
this.logger.error('Download Media with downloadContentFromMessage also failed!');
3929-
throw fallbackErr;
39303957
}
39313958
}
39323959
const typeMessage = getContentType(msg.message);

0 commit comments

Comments
 (0)