Skip to content

Commit 293f655

Browse files
committed
feat/validate video type before uploading to S3
1 parent 9cdb897 commit 293f655

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

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

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

462+
if (mediaType == 'video' && !this.configService.get<S3>('S3').SAVE_VIDEO) {
463+
throw new Error('Video upload is disabled.');
464+
}
465+
462466
const mimetype = result.data?.mime_type || result.headers['content-type'];
463467

464468
const contentDisposition = result.headers['content-disposition'];
@@ -1205,9 +1209,8 @@ export class BusinessStartupService extends ChannelStartupService {
12051209
const token = this.token;
12061210

12071211
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`;
1212+
const url = `${this.configService.get<WaBusiness>('WA_BUSINESS').URL}/${this.configService.get<WaBusiness>('WA_BUSINESS').VERSION
1213+
}/${this.number}/media`;
12111214

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

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ export class BaileysStartupService extends ChannelStartupService {
368368
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
369369
this.logger.log(
370370
`\n{ instance: ${this.instance.name} pairingCode: ${this.instance.qrcode.pairingCode}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
371-
qrcode,
371+
qrcode,
372372
),
373373
);
374374

@@ -961,16 +961,16 @@ export class BaileysStartupService extends ChannelStartupService {
961961

962962
const messagesRepository: Set<string> = new Set(
963963
chatwootImport.getRepositoryMessagesCache(instance) ??
964-
(
965-
await this.prismaRepository.message.findMany({
966-
select: { key: true },
967-
where: { instanceId: this.instanceId },
968-
})
969-
).map((message) => {
970-
const key = message.key as { id: string };
971-
972-
return key.id;
973-
}),
964+
(
965+
await this.prismaRepository.message.findMany({
966+
select: { key: true },
967+
where: { instanceId: this.instanceId },
968+
})
969+
).map((message) => {
970+
const key = message.key as { id: string };
971+
972+
return key.id;
973+
}),
974974
);
975975

976976
if (chatwootImport.getRepositoryMessagesCache(instance) === null) {
@@ -1188,6 +1188,8 @@ export class BaileysStartupService extends ChannelStartupService {
11881188
received?.message?.ptvMessage ||
11891189
received?.message?.audioMessage;
11901190

1191+
const isVideo = received?.message?.videoMessage;
1192+
11911193
if (this.localSettings.readMessages && received.key.id !== 'status@broadcast') {
11921194
await this.client.readMessages([received.key]);
11931195
}
@@ -1258,6 +1260,10 @@ export class BaileysStartupService extends ChannelStartupService {
12581260
if (isMedia) {
12591261
if (this.configService.get<S3>('S3').ENABLE) {
12601262
try {
1263+
if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
1264+
throw new Error('Video upload is disabled.');
1265+
}
1266+
12611267
const message: any = received;
12621268

12631269
// Verificação adicional para garantir que há conteúdo de mídia real
@@ -2143,6 +2149,8 @@ export class BaileysStartupService extends ChannelStartupService {
21432149
messageSent?.message?.ptvMessage ||
21442150
messageSent?.message?.audioMessage;
21452151

2152+
const isVideo = messageSent?.message?.videoMessage;
2153+
21462154
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled && !isIntegration) {
21472155
this.chatwootService.eventWhatsapp(
21482156
Events.SEND_MESSAGE,
@@ -2167,6 +2175,10 @@ export class BaileysStartupService extends ChannelStartupService {
21672175

21682176
if (isMedia && this.configService.get<S3>('S3').ENABLE) {
21692177
try {
2178+
if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
2179+
throw new Error('Video upload is disabled.');
2180+
}
2181+
21702182
const message: any = messageRaw;
21712183

21722184
// 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
@@ -282,6 +282,7 @@ export type S3 = {
282282
USE_SSL?: boolean;
283283
REGION?: string;
284284
SKIP_POLICY?: boolean;
285+
SAVE_VIDEO: boolean;
285286
};
286287

287288
export type CacheConf = { REDIS: CacheConfRedis; LOCAL: CacheConfLocal };
@@ -653,6 +654,7 @@ export class ConfigService {
653654
USE_SSL: process.env?.S3_USE_SSL === 'true',
654655
REGION: process.env?.S3_REGION,
655656
SKIP_POLICY: process.env?.S3_SKIP_POLICY === 'true',
657+
SAVE_VIDEO: process.env?.S3_SAVE_VIDEO === 'true',
656658
},
657659
AUTHENTICATION: {
658660
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)