From 293f655274e63a066dc8a6b136fd0e9760ef26b8 Mon Sep 17 00:00:00 2001 From: Marlon Alves Date: Mon, 1 Sep 2025 19:41:56 -0300 Subject: [PATCH 1/4] feat/validate video type before uploading to S3 --- .../channel/meta/whatsapp.business.service.ts | 9 ++++-- .../whatsapp/whatsapp.baileys.service.ts | 32 +++++++++++++------ src/config/env.config.ts | 2 ++ src/utils/getConversationMessage.ts | 22 +++++++------ 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index d3c35bee..39edeb09 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -459,6 +459,10 @@ export class BusinessStartupService extends ChannelStartupService { mediaType = 'video'; } + if (mediaType == 'video' && !this.configService.get('S3').SAVE_VIDEO) { + throw new Error('Video upload is disabled.'); + } + const mimetype = result.data?.mime_type || result.headers['content-type']; const contentDisposition = result.headers['content-disposition']; @@ -1205,9 +1209,8 @@ export class BusinessStartupService extends ChannelStartupService { const token = this.token; const headers = { Authorization: `Bearer ${token}` }; - const url = `${this.configService.get('WA_BUSINESS').URL}/${ - this.configService.get('WA_BUSINESS').VERSION - }/${this.number}/media`; + const url = `${this.configService.get('WA_BUSINESS').URL}/${this.configService.get('WA_BUSINESS').VERSION + }/${this.number}/media`; const res = await axios.post(url, formData, { headers }); return res.data.id; diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 758a5bf9..edd3afcc 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -368,7 +368,7 @@ export class BaileysStartupService extends ChannelStartupService { qrcodeTerminal.generate(qr, { small: true }, (qrcode) => this.logger.log( `\n{ instance: ${this.instance.name} pairingCode: ${this.instance.qrcode.pairingCode}, qrcodeCount: ${this.instance.qrcode.count} }\n` + - qrcode, + qrcode, ), ); @@ -961,16 +961,16 @@ export class BaileysStartupService extends ChannelStartupService { const messagesRepository: Set = new Set( chatwootImport.getRepositoryMessagesCache(instance) ?? - ( - await this.prismaRepository.message.findMany({ - select: { key: true }, - where: { instanceId: this.instanceId }, - }) - ).map((message) => { - const key = message.key as { id: string }; + ( + await this.prismaRepository.message.findMany({ + select: { key: true }, + where: { instanceId: this.instanceId }, + }) + ).map((message) => { + const key = message.key as { id: string }; - return key.id; - }), + return key.id; + }), ); if (chatwootImport.getRepositoryMessagesCache(instance) === null) { @@ -1188,6 +1188,8 @@ export class BaileysStartupService extends ChannelStartupService { received?.message?.ptvMessage || received?.message?.audioMessage; + const isVideo = received?.message?.videoMessage; + if (this.localSettings.readMessages && received.key.id !== 'status@broadcast') { await this.client.readMessages([received.key]); } @@ -1258,6 +1260,10 @@ export class BaileysStartupService extends ChannelStartupService { if (isMedia) { if (this.configService.get('S3').ENABLE) { try { + if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { + throw new Error('Video upload is disabled.'); + } + const message: any = received; // Verificação adicional para garantir que há conteúdo de mídia real @@ -2143,6 +2149,8 @@ export class BaileysStartupService extends ChannelStartupService { messageSent?.message?.ptvMessage || messageSent?.message?.audioMessage; + const isVideo = messageSent?.message?.videoMessage; + if (this.configService.get('CHATWOOT').ENABLED && this.localChatwoot?.enabled && !isIntegration) { this.chatwootService.eventWhatsapp( Events.SEND_MESSAGE, @@ -2167,6 +2175,10 @@ export class BaileysStartupService extends ChannelStartupService { if (isMedia && this.configService.get('S3').ENABLE) { try { + if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { + throw new Error('Video upload is disabled.'); + } + const message: any = messageRaw; // Verificação adicional para garantir que há conteúdo de mídia real diff --git a/src/config/env.config.ts b/src/config/env.config.ts index c59acd38..9ef80dc1 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -282,6 +282,7 @@ export type S3 = { USE_SSL?: boolean; REGION?: string; SKIP_POLICY?: boolean; + SAVE_VIDEO: boolean; }; export type CacheConf = { REDIS: CacheConfRedis; LOCAL: CacheConfLocal }; @@ -653,6 +654,7 @@ export class ConfigService { USE_SSL: process.env?.S3_USE_SSL === 'true', REGION: process.env?.S3_REGION, SKIP_POLICY: process.env?.S3_SKIP_POLICY === 'true', + SAVE_VIDEO: process.env?.S3_SAVE_VIDEO === 'true', }, AUTHENTICATION: { API_KEY: { diff --git a/src/utils/getConversationMessage.ts b/src/utils/getConversationMessage.ts index a7650968..a34695e7 100644 --- a/src/utils/getConversationMessage.ts +++ b/src/utils/getConversationMessage.ts @@ -3,7 +3,13 @@ import { configService, S3 } from '@config/env.config'; const getTypeMessage = (msg: any) => { let mediaId: string; - if (configService.get('S3').ENABLE) mediaId = msg.message?.mediaUrl; + if ( + configService.get('S3').ENABLE && + (configService.get('S3').SAVE_VIDEO || + (msg?.message?.videoMessage === undefined && + msg?.message?.viewOnceMessageV2?.message?.videoMessage === undefined)) + ) + mediaId = msg.message?.mediaUrl; else mediaId = msg.key?.id; const types = { @@ -32,16 +38,14 @@ const getTypeMessage = (msg: any) => { ? `videoMessage|${mediaId}${msg?.message?.videoMessage?.caption ? `|${msg?.message?.videoMessage?.caption}` : ''}` : undefined, documentMessage: msg?.message?.documentMessage - ? `documentMessage|${mediaId}${ - msg?.message?.documentMessage?.caption ? `|${msg?.message?.documentMessage?.caption}` : '' - }` + ? `documentMessage|${mediaId}${msg?.message?.documentMessage?.caption ? `|${msg?.message?.documentMessage?.caption}` : '' + }` : undefined, documentWithCaptionMessage: msg?.message?.documentWithCaptionMessage?.message?.documentMessage - ? `documentWithCaptionMessage|${mediaId}${ - msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption - ? `|${msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption}` - : '' - }` + ? `documentWithCaptionMessage|${mediaId}${msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption + ? `|${msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption}` + : '' + }` : undefined, externalAdReplyBody: msg?.contextInfo?.externalAdReply?.body ? `externalAdReplyBody|${msg.contextInfo.externalAdReply.body}` From e48e878b4f56b9dd562edaded30739d032e3edf4 Mon Sep 17 00:00:00 2001 From: nolramaf <30306355+nolramaf@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:49:55 -0300 Subject: [PATCH 2/4] Update src/api/integrations/channel/meta/whatsapp.business.service.ts Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- .../integrations/channel/meta/whatsapp.business.service.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 39edeb09..0b76b87a 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -460,7 +460,11 @@ export class BusinessStartupService extends ChannelStartupService { } if (mediaType == 'video' && !this.configService.get('S3').SAVE_VIDEO) { - throw new Error('Video upload is disabled.'); + this.logger?.info?.('Video upload attempted but is disabled by configuration.'); + return { + success: false, + message: 'Video upload is currently disabled. Please contact support if you need this feature enabled.', + }; } const mimetype = result.data?.mime_type || result.headers['content-type']; From 9beb38d807a6487ea5ce7ef96ab4ad2f4f7d478a Mon Sep 17 00:00:00 2001 From: nolramaf <30306355+nolramaf@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:50:01 -0300 Subject: [PATCH 3/4] Update src/config/env.config.ts Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- src/config/env.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/env.config.ts b/src/config/env.config.ts index 9ef80dc1..2ceaa6b3 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -282,7 +282,7 @@ export type S3 = { USE_SSL?: boolean; REGION?: string; SKIP_POLICY?: boolean; - SAVE_VIDEO: boolean; + SAVE_VIDEO?: boolean; }; export type CacheConf = { REDIS: CacheConfRedis; LOCAL: CacheConfLocal }; From 9ab6f9ad765ad5ab5506949fc056fa863f5fec13 Mon Sep 17 00:00:00 2001 From: nolramaf <30306355+nolramaf@users.noreply.github.com> Date: Mon, 1 Sep 2025 19:50:08 -0300 Subject: [PATCH 4/4] Update src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- .../integrations/channel/whatsapp/whatsapp.baileys.service.ts | 4 +++- 1 file changed, 3 insertions(+), 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 edd3afcc..0dc2471f 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1261,7 +1261,9 @@ export class BaileysStartupService extends ChannelStartupService { if (this.configService.get('S3').ENABLE) { try { if (isVideo && !this.configService.get('S3').SAVE_VIDEO) { - throw new Error('Video upload is disabled.'); + this.logger.warn('Video upload is disabled. Skipping video upload.'); + // Skip video upload by returning early from this block + return; } const message: any = received;