Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1542,10 +1542,11 @@ export class BaileysStartupService extends ChannelStartupService {
}
}

let msg: Message | null = null;
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { pollUpdates, ...messageData } = messageRaw as any;
const msg = await this.prismaRepository.message.create({ data: messageData });
msg = await this.prismaRepository.message.create({ data: messageData });

const { remoteJid } = received.key;
const timestamp = msg.messageTimestamp;
Expand Down Expand Up @@ -1573,16 +1574,14 @@ export class BaileysStartupService extends ChannelStartupService {
} else {
this.logger.info(`Update readed messages duplicated ignored [avoid deadlock]: ${messageKey}`);
}
}

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;
}

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.');
} else {
const message: any = received;

// VerificaΓ§Γ£o adicional para garantir que hΓ‘ conteΓΊdo de mΓ­dia real
Expand All @@ -1595,38 +1594,41 @@ export class BaileysStartupService extends ChannelStartupService {

if (!media) {
this.logger.verbose('No valid media to upload (messageContextInfo only), skipping MinIO');
return;
}
} else {
const { buffer, mediaType, fileName, size } = media;
const mimetype = mimeTypes.lookup(fileName).toString();
const fullName = join(
`${this.instance.id}`,
received.key.remoteJid,
mediaType,
`${Date.now()}_${fileName}`,
);
await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype });

if (msg?.id) {
await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});
}

const mediaUrl = await s3Service.getObjectUrl(fullName);

(messageRaw.message as any).mediaUrl = mediaUrl;

const { buffer, mediaType, fileName, size } = media;
const mimetype = mimeTypes.lookup(fileName).toString();
const fullName = join(
`${this.instance.id}`,
received.key.remoteJid,
mediaType,
`${Date.now()}_${fileName}`,
);
await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype });

await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});

const mediaUrl = await s3Service.getObjectUrl(fullName);

(messageRaw.message as any).mediaUrl = mediaUrl;

await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw });
if (msg?.id) {
await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw });
}
}
}
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
}
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
}
}
}
Expand Down Expand Up @@ -1674,7 +1676,6 @@ export class BaileysStartupService extends ChannelStartupService {

messageRaw.key.addressingMode = 'pn';
}
console.log(messageRaw);

this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);

Expand Down