From 57b19d85d575bf4bc44018058a320b04c0cacf33 Mon Sep 17 00:00:00 2001 From: nestordavalos Date: Thu, 28 Aug 2025 00:17:15 -0300 Subject: [PATCH] feat(whatsapp): Convert outgoing images to JPEG before sending All images sent via the Baileys integration are now pre-processed and converted to JPEG format using the `sharp` library. This ensures better compatibility and prevents potential issues with unsupported formats. - Images from URLs are now downloaded via axios before processing, which allows for the use of a proxy. - The default filename and mimetype are updated to `image.jpg` and `image/jpeg` to reflect the conversion. --- .../whatsapp/whatsapp.baileys.service.ts | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index c036a281..c70ab6f6 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2448,9 +2448,43 @@ export class BaileysStartupService extends ChannelStartupService { try { const type = mediaMessage.mediatype === 'ptv' ? 'video' : mediaMessage.mediatype; + let mediaInput: any; + if (mediaMessage.mediatype === 'image') { + let imageBuffer: Buffer; + if (isURL(mediaMessage.media)) { + let config: any = { responseType: 'arraybuffer' }; + + if (this.localProxy?.enabled) { + config = { + ...config, + httpsAgent: makeProxyAgent({ + host: this.localProxy.host, + port: this.localProxy.port, + protocol: this.localProxy.protocol, + username: this.localProxy.username, + password: this.localProxy.password, + }), + }; + } + + const response = await axios.get(mediaMessage.media, config); + imageBuffer = Buffer.from(response.data, 'binary'); + } else { + imageBuffer = Buffer.from(mediaMessage.media, 'base64'); + } + + mediaInput = await sharp(imageBuffer).jpeg().toBuffer(); + mediaMessage.fileName ??= 'image.jpg'; + mediaMessage.mimetype = 'image/jpeg'; + } else { + mediaInput = isURL(mediaMessage.media) + ? { url: mediaMessage.media } + : Buffer.from(mediaMessage.media, 'base64'); + } + const prepareMedia = await prepareWAMessageMedia( { - [type]: isURL(mediaMessage.media) ? { url: mediaMessage.media } : Buffer.from(mediaMessage.media, 'base64'), + [type]: mediaInput, } as any, { upload: this.client.waUploadToServer }, ); @@ -2464,7 +2498,7 @@ export class BaileysStartupService extends ChannelStartupService { } if (mediaMessage.mediatype === 'image' && !mediaMessage.fileName) { - mediaMessage.fileName = 'image.png'; + mediaMessage.fileName = 'image.jpg'; } if (mediaMessage.mediatype === 'video' && !mediaMessage.fileName) {