Merge pull request #1897 from nolramaf/feat/validate-video-type-before-uploading-to-s3
Some checks are pending
Build Docker image / Build and Deploy (push) Waiting to run

feat/validate video type before uploading to S3
This commit is contained in:
Davidson Gomes 2025-09-17 14:27:03 -03:00 committed by GitHub
commit 7ba878742e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 22 deletions

View File

@ -459,6 +459,14 @@ export class BusinessStartupService extends ChannelStartupService {
mediaType = 'video';
}
if (mediaType == 'video' && !this.configService.get<S3>('S3').SAVE_VIDEO) {
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'];
const contentDisposition = result.headers['content-disposition'];
@ -1205,9 +1213,8 @@ export class BusinessStartupService extends ChannelStartupService {
const token = this.token;
const headers = { Authorization: `Bearer ${token}` };
const url = `${this.configService.get<WaBusiness>('WA_BUSINESS').URL}/${
this.configService.get<WaBusiness>('WA_BUSINESS').VERSION
}/${this.number}/media`;
const url = `${this.configService.get<WaBusiness>('WA_BUSINESS').URL}/${this.configService.get<WaBusiness>('WA_BUSINESS').VERSION
}/${this.number}/media`;
const res = await axios.post(url, formData, { headers });
return res.data.id;

View File

@ -381,7 +381,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,
),
);
@ -978,16 +978,16 @@ export class BaileysStartupService extends ChannelStartupService {
const messagesRepository: Set<string> = 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) {
@ -1205,6 +1205,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]);
}
@ -1275,6 +1277,12 @@ export class BaileysStartupService extends ChannelStartupService {
if (isMedia) {
if (this.configService.get<S3>('S3').ENABLE) {
try {
if (isVideo && !this.configService.get<S3>('S3').SAVE_VIDEO) {
this.logger.warn('Video upload is disabled. Skipping video upload.');
// Skip video upload by returning early from this block
return;
}
const message: any = received;
// Verificação adicional para garantir que há conteúdo de mídia real
@ -2168,6 +2176,8 @@ export class BaileysStartupService extends ChannelStartupService {
messageSent?.message?.ptvMessage ||
messageSent?.message?.audioMessage;
const isVideo = messageSent?.message?.videoMessage;
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled && !isIntegration) {
this.chatwootService.eventWhatsapp(
Events.SEND_MESSAGE,
@ -2192,6 +2202,10 @@ export class BaileysStartupService extends ChannelStartupService {
if (isMedia && this.configService.get<S3>('S3').ENABLE) {
try {
if (isVideo && !this.configService.get<S3>('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

View File

@ -316,6 +316,7 @@ export type S3 = {
USE_SSL?: boolean;
REGION?: string;
SKIP_POLICY?: boolean;
SAVE_VIDEO?: boolean;
};
export type CacheConf = { REDIS: CacheConfRedis; LOCAL: CacheConfLocal };
@ -721,6 +722,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: {

View File

@ -3,7 +3,13 @@ import { configService, S3 } from '@config/env.config';
const getTypeMessage = (msg: any) => {
let mediaId: string;
if (configService.get<S3>('S3').ENABLE) mediaId = msg.message?.mediaUrl;
if (
configService.get<S3>('S3').ENABLE &&
(configService.get<S3>('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}`