From 6e7dd51679c8223a4cb14f8113e601093c6c5a8f Mon Sep 17 00:00:00 2001 From: Marcelo Assis Date: Thu, 20 Mar 2025 19:00:02 -0300 Subject: [PATCH] fix: preserve animation in GIF and WebP stickers --- .../whatsapp/whatsapp.baileys.service.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index f5217060..94aaa43f 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2703,8 +2703,7 @@ export class BaileysStartupService extends ChannelStartupService { imageBuffer = Buffer.from(response.data, 'binary'); } - const isAnimated = image.includes('.gif') || - (image.includes('.webp') && this.isAnimatedWebp(imageBuffer)); + const isAnimated = this.isAnimated(image, imageBuffer); if (isAnimated) { return await sharp(imageBuffer, { animated: true }) @@ -2722,14 +2721,14 @@ export class BaileysStartupService extends ChannelStartupService { 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 buffer.indexOf(Buffer.from('ANIM')) !== -1; + } + + private isAnimated(image: string, buffer: Buffer): boolean { + if (image.includes('.gif')) return true; + + if (image.includes('.webp')) return this.isAnimatedWebp(buffer); + return false; }