From f8b1c6e0fa5dfb52241677b56f8d4d8467bdb2e7 Mon Sep 17 00:00:00 2001 From: Marcelo Assis Date: Thu, 20 Mar 2025 18:37:58 -0300 Subject: [PATCH] fix: preserve animation in GIF and WebP stickers --- .../whatsapp/whatsapp.baileys.service.ts | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 10feb7ce..f5217060 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2703,15 +2703,36 @@ export class BaileysStartupService extends ChannelStartupService { imageBuffer = Buffer.from(response.data, 'binary'); } - const webpBuffer = await sharp(imageBuffer).webp().toBuffer(); - - return webpBuffer; + const isAnimated = image.includes('.gif') || + (image.includes('.webp') && this.isAnimatedWebp(imageBuffer)); + + if (isAnimated) { + return await sharp(imageBuffer, { animated: true }) + .webp({ quality: 80, animated: true }) + .toBuffer(); + } else { + return await sharp(imageBuffer).webp().toBuffer(); + } } catch (error) { console.error('Erro ao converter a imagem para WebP:', error); throw error; } } + private isAnimatedWebp(buffer: Buffer): boolean { + if (buffer.length < 12) return false; + + for (let i = 0; i < buffer.length - 4; i++) { + if (buffer[i] === 0x41 && // 'A' + buffer[i + 1] === 0x4E && // 'N' + buffer[i + 2] === 0x49 && // 'I' + buffer[i + 3] === 0x4D) { // 'M' + return true; + } + } + return false; + } + public async mediaSticker(data: SendStickerDto, file?: any) { const mediaData: SendStickerDto = { ...data };