diff --git a/src/api/integrations/channel/evolution/evolution.channel.service.ts b/src/api/integrations/channel/evolution/evolution.channel.service.ts index d16fbd55..28d73d7f 100644 --- a/src/api/integrations/channel/evolution/evolution.channel.service.ts +++ b/src/api/integrations/channel/evolution/evolution.channel.service.ts @@ -7,7 +7,6 @@ import { ChannelStartupService } from '@api/services/channel.service'; import { Events, wa } from '@api/types/wa.types'; import { Chatwoot, ConfigService, Openai } from '@config/env.config'; import { BadRequestException, InternalServerErrorException } from '@exceptions'; -import { deleteTempFile, getTempFile } from '@utils/getTempFile'; import { isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; import mime from 'mime'; @@ -437,7 +436,7 @@ export class EvolutionStartupService extends ChannelStartupService { public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) { const mediaData: SendMediaDto = { ...data }; - if (file) mediaData.media = await getTempFile(file, this.instanceId); + if (file) mediaData.media = file.buffer.toString('base64'); const message = await this.prepareMediaMessage(mediaData); @@ -455,8 +454,6 @@ export class EvolutionStartupService extends ChannelStartupService { isIntegration, ); - if (file) await deleteTempFile(file, this.instanceId); - return mediaSent; } @@ -486,7 +483,7 @@ export class EvolutionStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = await getTempFile(file, this.instanceId); + if (file) mediaData.audio = file.buffer.toString('base64'); const message = await this.processAudio(mediaData.audio, data.number); @@ -504,8 +501,6 @@ export class EvolutionStartupService extends ChannelStartupService { isIntegration, ); - if (file) await deleteTempFile(file, this.instanceId); - return audioSent; } diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 7eaf39ec..f3bccdfd 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -22,7 +22,6 @@ import { ChannelStartupService } from '@api/services/channel.service'; import { Events, wa } from '@api/types/wa.types'; import { Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@config/env.config'; import { BadRequestException, InternalServerErrorException } from '@exceptions'; -import { deleteTempFile, getTempFile } from '@utils/getTempFile'; import axios from 'axios'; import { arrayUnique, isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; @@ -1030,7 +1029,7 @@ export class BusinessStartupService extends ChannelStartupService { public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) { const mediaData: SendMediaDto = { ...data }; - if (file) mediaData.media = await getTempFile(file, this.instanceId); + if (file) mediaData.media = file.buffer.toString('base64'); const message = await this.prepareMediaMessage(mediaData); @@ -1048,8 +1047,6 @@ export class BusinessStartupService extends ChannelStartupService { isIntegration, ); - if (file) await deleteTempFile(file, this.instanceId); - return mediaSent; } @@ -1084,7 +1081,7 @@ export class BusinessStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = await getTempFile(file, this.instanceId); + if (file) mediaData.audio = file.buffer.toString('base64'); const message = await this.processAudio(mediaData.audio, data.number); @@ -1102,8 +1099,6 @@ export class BusinessStartupService extends ChannelStartupService { isIntegration, ); - if (file) await deleteTempFile(file, this.instanceId); - return audioSent; } diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index df675633..1d59c668 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -70,7 +70,6 @@ import { BadRequestException, InternalServerErrorException, NotFoundException } import ffmpegPath from '@ffmpeg-installer/ffmpeg'; import { Boom } from '@hapi/boom'; import { Instance } from '@prisma/client'; -import { deleteTempFile, getTempFile } from '@utils/getTempFile'; import { makeProxyAgent } from '@utils/makeProxyAgent'; import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache'; import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma'; @@ -2270,7 +2269,7 @@ export class BaileysStartupService extends ChannelStartupService { public async statusMessage(data: SendStatusDto, file?: any) { const mediaData: SendStatusDto = { ...data }; - if (file) mediaData.content = await getTempFile(file, this.instanceId); + if (file) mediaData.content = file.buffer.toString('base64'); const status = await this.formatStatusMessage(mediaData); @@ -2278,8 +2277,6 @@ export class BaileysStartupService extends ChannelStartupService { status, }); - if (file) await deleteTempFile(file, this.instanceId); - return statusSent; } @@ -2407,7 +2404,7 @@ export class BaileysStartupService extends ChannelStartupService { public async mediaSticker(data: SendStickerDto, file?: any) { const mediaData: SendStickerDto = { ...data }; - if (file) mediaData.sticker = await getTempFile(file, this.instanceId); + if (file) mediaData.sticker = file.buffer.toString('base64'); const convert = await this.convertToWebP(data.sticker); const gifPlayback = data.sticker.includes('.gif'); @@ -2426,15 +2423,13 @@ export class BaileysStartupService extends ChannelStartupService { }, ); - if (file) await deleteTempFile(file, this.instanceId); - return result; } public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) { const mediaData: SendMediaDto = { ...data }; - if (file) mediaData.media = await getTempFile(file, this.instanceId); + if (file) mediaData.media = file.buffer.toString('base64'); const generate = await this.prepareMediaMessage(mediaData); @@ -2451,8 +2446,6 @@ export class BaileysStartupService extends ChannelStartupService { isIntegration, ); - if (file) await deleteTempFile(file, this.instanceId); - return mediaSent; } @@ -2560,7 +2553,7 @@ export class BaileysStartupService extends ChannelStartupService { public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) { const mediaData: SendAudioDto = { ...data }; - if (file) mediaData.audio = await getTempFile(file, this.instanceId); + if (file) mediaData.audio = file.buffer.toString('base64'); if (!data?.encoding && data?.encoding !== false) { data.encoding = true; @@ -2581,8 +2574,6 @@ export class BaileysStartupService extends ChannelStartupService { isIntegration, ); - if (file) await deleteTempFile(file, this.instanceId); - return result; } else { throw new InternalServerErrorException('Failed to convert audio'); diff --git a/src/utils/getTempFile.ts b/src/utils/getTempFile.ts deleted file mode 100644 index 220569f2..00000000 --- a/src/utils/getTempFile.ts +++ /dev/null @@ -1,34 +0,0 @@ -import * as s3Service from '@api/integrations/storage/s3/libs/minio.server'; -import mime from 'mime-types'; - -export const getTempFile = async (file: any, instanceId: string): Promise => { - const fileName = file.originalname; - const mimetype = mime.lookup(fileName) || 'application/octet-stream'; - const folder = `${process.env.S3_BUCKET}/${instanceId}/temp`; - const fileUrl = `https://${process.env.S3_ENDPOINT}/${process.env.S3_BUCKET}/${folder}/${fileName}`; - - if (!process.env.S3_ENABLED || process.env.S3_ENABLED !== 'true') { - return file.buffer.toString('base64'); - } - - try { - if (file.buffer) { - await s3Service.uploadTempFile(folder, fileName, file.buffer, file.size, { - 'Content-Type': mimetype, - }); - } - } catch (error) { - console.error(`Erro ao fazer upload do arquivo ${fileName}:`, error); - } - - return fileUrl; -}; - -export const deleteTempFile = async (file: any, instanceId: string): Promise => { - if (!process.env.S3_ENABLED) return; - - const fileName = file.originalname; - const folder = `${process.env.S3_BUCKET}/${instanceId}/temp`; - - await s3Service.deleteFile(folder, fileName); -};