Skip to content

Commit

Permalink
Merge pull request #1069 from AlanCezarAraujo/fix/whatsapp-images
Browse files Browse the repository at this point in the history
Correção de Imagens no Cloud API
  • Loading branch information
DavidsonGomes authored Nov 14, 2024
2 parents 8f855b4 + ef75742 commit 52cf4fa
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/api/integrations/channel/meta/whatsapp.business.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export class BusinessStartupService extends ChannelStartupService {
await this.closeClient();
}

private isMediaMessage(message: any) {
return message.document ||
message.image ||
message.audio ||
message.video
}

private async post(message: any, params: string) {
try {
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
Expand Down Expand Up @@ -301,12 +308,7 @@ export class BusinessStartupService extends ChannelStartupService {
remoteJid: this.phoneNumber,
fromMe: received.messages[0].from === received.metadata.phone_number_id,
};
if (
received?.messages[0].document ||
received?.messages[0].image ||
received?.messages[0].audio ||
received?.messages[0].video
) {
if (this.isMediaMessage(received?.messages[0])) {
messageRaw = {
key,
pushName,
Expand Down Expand Up @@ -339,7 +341,7 @@ export class BusinessStartupService extends ChannelStartupService {
? 'audio'
: 'video';

const mimetype = result.headers['content-type'];
const mimetype = result.data?.mime_type || result.headers['content-type'];

const contentDisposition = result.headers['content-disposition'];
let fileName = `${message.messages[0].id}.${mimetype.split('/')[1]}`;
Expand All @@ -352,15 +354,19 @@ export class BusinessStartupService extends ChannelStartupService {

const size = result.headers['content-length'] || buffer.data.byteLength;

const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName);
const fullName = join(`${this.instance.id}`, key.remoteJid, mediaType, fileName);

await s3Service.uploadFile(fullName, buffer.data, size, {
'Content-Type': mimetype,
});

const createdMessage = await this.prismaRepository.message.create({
data: messageRaw,
});

await this.prismaRepository.media.create({
data: {
messageId: received.messages[0].id,
messageId: createdMessage.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
Expand All @@ -371,6 +377,7 @@ export class BusinessStartupService extends ChannelStartupService {
const mediaUrl = await s3Service.getObjectUrl(fullName);

messageRaw.message.mediaUrl = mediaUrl;
messageRaw.message.base64 = buffer.data.toString('base64');
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
}
Expand Down Expand Up @@ -497,9 +504,11 @@ export class BusinessStartupService extends ChannelStartupService {
}
}

await this.prismaRepository.message.create({
data: messageRaw,
});
if (!this.isMediaMessage(received?.messages[0])) {
await this.prismaRepository.message.create({
data: messageRaw,
});
}

const contact = await this.prismaRepository.contact.findFirst({
where: { instanceId: this.instanceId, remoteJid: key.remoteJid },
Expand Down

0 comments on commit 52cf4fa

Please sign in to comment.