From f8b1c6e0fa5dfb52241677b56f8d4d8467bdb2e7 Mon Sep 17 00:00:00 2001 From: Marcelo Assis Date: Thu, 20 Mar 2025 18:37:58 -0300 Subject: [PATCH 1/5] 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 }; From 6e7dd51679c8223a4cb14f8113e601093c6c5a8f Mon Sep 17 00:00:00 2001 From: Marcelo Assis Date: Thu, 20 Mar 2025 19:00:02 -0300 Subject: [PATCH 2/5] 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; } From 027401b8396aaf93d7ab146f2c73e94459596075 Mon Sep 17 00:00:00 2001 From: Marcelo Assis Date: Thu, 20 Mar 2025 19:13:19 -0300 Subject: [PATCH 3/5] fix: normalize file extension checks for case insensitivity in sticker conversion --- .../channel/whatsapp/whatsapp.baileys.service.ts | 6 ++++-- 1 file changed, 4 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 94aaa43f..f9a36d8c 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2725,9 +2725,11 @@ export class BaileysStartupService extends ChannelStartupService { } private isAnimated(image: string, buffer: Buffer): boolean { - if (image.includes('.gif')) return true; + const lowerCaseImage = image.toLowerCase(); - if (image.includes('.webp')) return this.isAnimatedWebp(buffer); + if (lowerCaseImage.includes('.gif')) return true; + + if (lowerCaseImage.includes('.webp')) return this.isAnimatedWebp(buffer); return false; } From 658dae0b59b98f2aa7093f46f8425b874948f960 Mon Sep 17 00:00:00 2001 From: Marcelo Assis Date: Wed, 26 Mar 2025 10:45:09 -0300 Subject: [PATCH 4/5] lint fix --- .../channel/whatsapp/whatsapp.baileys.service.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index f9a36d8c..5a7be283 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2704,11 +2704,9 @@ export class BaileysStartupService extends ChannelStartupService { } const isAnimated = this.isAnimated(image, imageBuffer); - + if (isAnimated) { - return await sharp(imageBuffer, { animated: true }) - .webp({ quality: 80, animated: true }) - .toBuffer(); + return await sharp(imageBuffer, { animated: true }).webp({ quality: 80, animated: true }).toBuffer(); } else { return await sharp(imageBuffer).webp().toBuffer(); } @@ -2720,17 +2718,17 @@ export class BaileysStartupService extends ChannelStartupService { private isAnimatedWebp(buffer: Buffer): boolean { if (buffer.length < 12) return false; - + return buffer.indexOf(Buffer.from('ANIM')) !== -1; } private isAnimated(image: string, buffer: Buffer): boolean { const lowerCaseImage = image.toLowerCase(); - + if (lowerCaseImage.includes('.gif')) return true; - + if (lowerCaseImage.includes('.webp')) return this.isAnimatedWebp(buffer); - + return false; } From 9710fbdac415e4a1f3eecf4f64fb5d2d6667baef Mon Sep 17 00:00:00 2001 From: Marcelo Assis Date: Wed, 26 Mar 2025 13:09:08 -0300 Subject: [PATCH 5/5] remove animated to webp --- .../integrations/channel/whatsapp/whatsapp.baileys.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 5a7be283..46a1012d 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -2706,7 +2706,7 @@ export class BaileysStartupService extends ChannelStartupService { const isAnimated = this.isAnimated(image, imageBuffer); if (isAnimated) { - return await sharp(imageBuffer, { animated: true }).webp({ quality: 80, animated: true }).toBuffer(); + return await sharp(imageBuffer, { animated: true }).webp({ quality: 80 }).toBuffer(); } else { return await sharp(imageBuffer).webp().toBuffer(); }