Skip to content

Commit 624a7d2

Browse files
matheusmartinsInspermatheusmartinsInsper
authored andcommitted
fix: preserve animation in GIF and WebP stickers
1 parent 4440fce commit 624a7d2

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,16 +2348,13 @@ export class BaileysStartupService extends ChannelStartupService {
23482348
imageBuffer = Buffer.from(response.data, 'binary');
23492349
}
23502350

2351-
const isAnimated = image.includes('.gif') ||
2352-
(image.includes('.webp') && this.isAnimatedWebp(imageBuffer));
2353-
2354-
if (isAnimated) {
2355-
return await sharp(imageBuffer, { animated: true })
2356-
.webp({ quality: 80, animated: true })
2357-
.toBuffer();
2358-
} else {
2359-
return await sharp(imageBuffer).webp().toBuffer();
2360-
}
2351+
const isAnimated = this.isAnimated(image, imageBuffer);
2352+
2353+
if (isAnimated) {
2354+
return await sharp(imageBuffer, { animated: true }).webp({ quality: 80, animated: true }).toBuffer();
2355+
} else {
2356+
return await sharp(imageBuffer).webp().toBuffer();
2357+
}
23612358
} catch (error) {
23622359
console.error('Erro ao converter a imagem para WebP:', error);
23632360
throw error;
@@ -2366,15 +2363,14 @@ export class BaileysStartupService extends ChannelStartupService {
23662363

23672364
private isAnimatedWebp(buffer: Buffer): boolean {
23682365
if (buffer.length < 12) return false;
2369-
2370-
for (let i = 0; i < buffer.length - 4; i++) {
2371-
if (buffer[i] === 0x41 && // 'A'
2372-
buffer[i + 1] === 0x4E && // 'N'
2373-
buffer[i + 2] === 0x49 && // 'I'
2374-
buffer[i + 3] === 0x4D) { // 'M'
2375-
return true;
2376-
}
2377-
}
2366+
2367+
return buffer.indexOf(Buffer.from('ANIM')) !== -1;
2368+
}
2369+
2370+
private isAnimated(image: string, buffer: Buffer): boolean {
2371+
if (image.includes('.gif')) return true;
2372+
2373+
if (image.includes('.webp')) return this.isAnimatedWebp(buffer);
23782374
return false;
23792375
}
23802376

src/api/integrations/event/websocket/websocket.controller.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,39 @@ export class WebsocketController extends EventController implements EventControl
2525

2626
this.socket = new SocketIO(httpServer, {
2727
cors: { origin: this.cors },
28-
allowRequest: async (req, callback) => {
29-
try {
30-
const url = new URL(req.url || '', 'http://localhost');
31-
const params = new URLSearchParams(url.search);
32-
33-
// Permite conexões internas do Socket.IO (EIO=4 é o Engine.IO v4)
34-
if (params.has('EIO')) {
35-
return callback(null, true);
36-
}
37-
38-
const apiKey = params.get('apikey') || (req.headers.apikey as string);
39-
40-
if (!apiKey) {
41-
this.logger.error('Connection rejected: apiKey not provided');
42-
return callback('apiKey is required', false);
43-
}
44-
45-
const instance = await this.prismaRepository.instance.findFirst({ where: { token: apiKey } });
46-
47-
if (!instance) {
48-
const globalToken = configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
49-
if (apiKey !== globalToken) {
50-
this.logger.error('Connection rejected: invalid global token');
51-
return callback('Invalid global token', false);
52-
}
53-
}
54-
55-
callback(null, true);
56-
} catch (error) {
57-
this.logger.error('Authentication error:');
58-
this.logger.error(error);
59-
callback('Authentication error', false);
60-
}
28+
allowRequest: async (req, callback) => {
29+
try {
30+
const url = new URL(req.url || '', 'http://localhost');
31+
const params = new URLSearchParams(url.search);
32+
33+
// Permite conexões internas do Socket.IO (EIO=4 é o Engine.IO v4)
34+
if (params.has('EIO')) {
35+
return callback(null, true);
36+
}
37+
38+
const apiKey = params.get('apikey') || (req.headers.apikey as string);
39+
40+
if (!apiKey) {
41+
this.logger.error('Connection rejected: apiKey not provided');
42+
return callback('apiKey is required', false);
43+
}
44+
45+
const instance = await this.prismaRepository.instance.findFirst({ where: { token: apiKey } });
46+
47+
if (!instance) {
48+
const globalToken = configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
49+
if (apiKey !== globalToken) {
50+
this.logger.error('Connection rejected: invalid global token');
51+
return callback('Invalid global token', false);
52+
}
53+
}
54+
55+
callback(null, true);
56+
} catch (error) {
57+
this.logger.error('Authentication error:');
58+
this.logger.error(error);
59+
callback('Authentication error', false);
60+
}
6161
},
6262
});
6363

0 commit comments

Comments
 (0)