Skip to content

Commit 7ba8787

Browse files
Merge pull request #1897 from nolramaf/feat/validate-video-type-before-uploading-to-s3
feat/validate video type before uploading to S3
2 parents d998baa + 9ab6f9a commit 7ba8787

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@ export class BusinessStartupService extends ChannelStartupService {
459459
mediaType = 'video';
460460
}
461461

462+
if (mediaType == 'video' && !this.configService.get<S3>('S3').SAVE_VIDEO) {
463+
this.logger?.info?.('Video upload attempted but is disabled by configuration.');
464+
return {
465+
success: false,
466+
message: 'Video upload is currently disabled. Please contact support if you need this feature enabled.',
467+
};
468+
}
469+
462470
const mimetype = result.data?.mime_type || result.headers['content-type'];
463471

464472
const contentDisposition = result.headers['content-disposition'];
@@ -1205,9 +1213,8 @@ export class BusinessStartupService extends ChannelStartupService {
12051213
const token = this.token;
12061214

12071215
const headers = { Authorization: `Bearer ${token}` };
1208-
const url = `${this.configService.get<WaBusiness>('WA_BUSINESS').URL}/${
1209-
this.configService.get<WaBusiness>('WA_BUSINESS').VERSION
1210-
}/${this.number}/media`;
1216+
const url = `${this.configService.get<WaBusiness>('WA_BUSINESS').URL}/${this.configService.get<WaBusiness>('WA_BUSINESS').VERSION
1217+
}/${this.number}/media`;
12111218

12121219
const res = await axios.post(url, formData, { headers });
12131220
return res.data.id;

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ export class BaileysStartupService extends ChannelStartupService {
381381
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
382382
this.logger.log(
383383
`\n{ instance: ${this.instance.name} pairingCode: ${this.instance.qrcode.pairingCode}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
384-
qrcode,
384+
qrcode,
385385
),
386386
);
387387

@@ -978,16 +978,16 @@ export class BaileysStartupService extends ChannelStartupService {
978978

979979
const messagesRepository: Set<string> = new Set(
980980
chatwootImport.getRepositoryMessagesCache(instance) ??
981-
(
982-
await this.prismaRepository.message.findMany({
983-
select: { key: true },
984-
where: { instanceId: this.instanceId },
985-
})
986-
).map((message) => {
987-
const key = message.key as { id: string };
988-
989-
return key.id;
990-
}),
981+
(
982+
await this.prismaRepository.message.findMany({
983+
select: { key: true },
984+
where: { instanceId: this.instanceId },
985+
})
986+
).map((message) => {
987+
const key = message.key as { id: string };
988+
989+
return key.id;
990+
}),
991991
);
992992

993993
if (chatwootImport.getRepositoryMessagesCache(instance) === null) {
@@ -1205,6 +1205,8 @@ export class BaileysStartupService extends ChannelStartupService {
12051205
received?.message?.ptvMessage ||
12061206
received?.message?.audioMessage;
12071207

1208+
const isVideo = received?.message?.videoMessage;
1209+
12081210
if (this.localSettings.readMessages && received.key.id !== 'status@broadcast') {
12091211
await this.client.readMessages([received.key]);
12101212
}
@@ -1275,6 +1277,12 @@ export class BaileysStartupService extends ChannelStartupService {
12751277
if (isMedia) {
12761278
if (this.configService.get<S3>('S3').ENABLE) {
12771279
try {
1280+
if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
1281+
this.logger.warn('Video upload is disabled. Skipping video upload.');
1282+
// Skip video upload by returning early from this block
1283+
return;
1284+
}
1285+
12781286
const message: any = received;
12791287

12801288
// Verificação adicional para garantir que há conteúdo de mídia real
@@ -2168,6 +2176,8 @@ export class BaileysStartupService extends ChannelStartupService {
21682176
messageSent?.message?.ptvMessage ||
21692177
messageSent?.message?.audioMessage;
21702178

2179+
const isVideo = messageSent?.message?.videoMessage;
2180+
21712181
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled && !isIntegration) {
21722182
this.chatwootService.eventWhatsapp(
21732183
Events.SEND_MESSAGE,
@@ -2192,6 +2202,10 @@ export class BaileysStartupService extends ChannelStartupService {
21922202

21932203
if (isMedia && this.configService.get<S3>('S3').ENABLE) {
21942204
try {
2205+
if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
2206+
throw new Error('Video upload is disabled.');
2207+
}
2208+
21952209
const message: any = messageRaw;
21962210

21972211
// Verificação adicional para garantir que há conteúdo de mídia real

src/config/env.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ export type S3 = {
316316
USE_SSL?: boolean;
317317
REGION?: string;
318318
SKIP_POLICY?: boolean;
319+
SAVE_VIDEO?: boolean;
319320
};
320321

321322
export type CacheConf = { REDIS: CacheConfRedis; LOCAL: CacheConfLocal };
@@ -721,6 +722,7 @@ export class ConfigService {
721722
USE_SSL: process.env?.S3_USE_SSL === 'true',
722723
REGION: process.env?.S3_REGION,
723724
SKIP_POLICY: process.env?.S3_SKIP_POLICY === 'true',
725+
SAVE_VIDEO: process.env?.S3_SAVE_VIDEO === 'true',
724726
},
725727
AUTHENTICATION: {
726728
API_KEY: {

src/utils/getConversationMessage.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { configService, S3 } from '@config/env.config';
33
const getTypeMessage = (msg: any) => {
44
let mediaId: string;
55

6-
if (configService.get<S3>('S3').ENABLE) mediaId = msg.message?.mediaUrl;
6+
if (
7+
configService.get<S3>('S3').ENABLE &&
8+
(configService.get<S3>('S3').SAVE_VIDEO ||
9+
(msg?.message?.videoMessage === undefined &&
10+
msg?.message?.viewOnceMessageV2?.message?.videoMessage === undefined))
11+
)
12+
mediaId = msg.message?.mediaUrl;
713
else mediaId = msg.key?.id;
814

915
const types = {
@@ -32,16 +38,14 @@ const getTypeMessage = (msg: any) => {
3238
? `videoMessage|${mediaId}${msg?.message?.videoMessage?.caption ? `|${msg?.message?.videoMessage?.caption}` : ''}`
3339
: undefined,
3440
documentMessage: msg?.message?.documentMessage
35-
? `documentMessage|${mediaId}${
36-
msg?.message?.documentMessage?.caption ? `|${msg?.message?.documentMessage?.caption}` : ''
37-
}`
41+
? `documentMessage|${mediaId}${msg?.message?.documentMessage?.caption ? `|${msg?.message?.documentMessage?.caption}` : ''
42+
}`
3843
: undefined,
3944
documentWithCaptionMessage: msg?.message?.documentWithCaptionMessage?.message?.documentMessage
40-
? `documentWithCaptionMessage|${mediaId}${
41-
msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption
42-
? `|${msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption}`
43-
: ''
44-
}`
45+
? `documentWithCaptionMessage|${mediaId}${msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption
46+
? `|${msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption}`
47+
: ''
48+
}`
4549
: undefined,
4650
externalAdReplyBody: msg?.contextInfo?.externalAdReply?.body
4751
? `externalAdReplyBody|${msg.contextInfo.externalAdReply.body}`

0 commit comments

Comments
 (0)