log verbose in file redis

This commit is contained in:
Davidson Gomes 2023-07-05 15:58:46 -03:00
parent bc2191eae6
commit db95de6731

View File

@ -1230,16 +1230,16 @@ export class WAStartupService {
} }
} }
private async convertToWebP(image: string) { private async convertToWebP(image: string, number: string) {
try { try {
let imagePath: string; let imagePath: string;
const timestamp = new Date().getTime(); const hash = `${number}-${new Date().getTime()}`;
const outputPath = `${join(process.cwd(), 'temp', `${timestamp}.webp`)}`; const outputPath = `${join(process.cwd(), 'temp', `${hash}.webp`)}`;
if (isBase64(image)) { if (isBase64(image)) {
const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, ''); const base64Data = image.replace(/^data:image\/(jpeg|png|gif);base64,/, '');
const imageBuffer = Buffer.from(base64Data, 'base64'); const imageBuffer = Buffer.from(base64Data, 'base64');
imagePath = `${join(process.cwd(), 'temp', `temp-${timestamp}.png`)}`; imagePath = `${join(process.cwd(), 'temp', `temp-${hash}.png`)}`;
await sharp(imageBuffer).toFile(imagePath); await sharp(imageBuffer).toFile(imagePath);
} else { } else {
const timestamp = new Date().getTime(); const timestamp = new Date().getTime();
@ -1247,7 +1247,7 @@ export class WAStartupService {
const response = await axios.get(url, { responseType: 'arraybuffer' }); const response = await axios.get(url, { responseType: 'arraybuffer' });
const imageBuffer = Buffer.from(response.data, 'binary'); const imageBuffer = Buffer.from(response.data, 'binary');
imagePath = `${join(process.cwd(), 'temp', `temp-${timestamp}.png`)}`; imagePath = `${join(process.cwd(), 'temp', `temp-${hash}.png`)}`;
await sharp(imageBuffer).toFile(imagePath); await sharp(imageBuffer).toFile(imagePath);
} }
@ -1262,7 +1262,7 @@ export class WAStartupService {
} }
public async mediaSticker(data: SendStickerDto) { public async mediaSticker(data: SendStickerDto) {
const convert = await this.convertToWebP(data.stickerMessage.image); const convert = await this.convertToWebP(data.stickerMessage.image, data.number);
const result = await this.sendMessageWithTyping( const result = await this.sendMessageWithTyping(
data.number, data.number,
{ {
@ -1286,23 +1286,23 @@ export class WAStartupService {
); );
} }
private async processAudio(audio: string) { private async processAudio(audio: string, number: string) {
let tempAudioPath: string; let tempAudioPath: string;
let outputAudio: string; let outputAudio: string;
const hash = `${number}-${new Date().getTime()}`;
if (isURL(audio)) { if (isURL(audio)) {
const timestamp = new Date().getTime(); outputAudio = `${join(process.cwd(), 'temp', `${hash}.mp4`)}`;
outputAudio = `${join(process.cwd(), 'temp', `${timestamp}.mp4`)}`; tempAudioPath = `${join(process.cwd(), 'temp', `temp-${hash}.mp3`)}`;
tempAudioPath = `${join(process.cwd(), 'temp', `temp-${timestamp}.mp3`)}`;
const timestamp = new Date().getTime();
const url = `${audio}?timestamp=${timestamp}`; const url = `${audio}?timestamp=${timestamp}`;
const response = await axios.get(url, { responseType: 'arraybuffer' }); const response = await axios.get(url, { responseType: 'arraybuffer' });
fs.writeFileSync(tempAudioPath, response.data); fs.writeFileSync(tempAudioPath, response.data);
} else { } else {
const timestamp = new Date().getTime(); outputAudio = `${join(process.cwd(), 'temp', `${hash}.mp4`)}`;
outputAudio = `${join(process.cwd(), 'temp', `${timestamp}.mp4`)}`; tempAudioPath = `${join(process.cwd(), 'temp', `temp-${hash}.mp3`)}`;
tempAudioPath = `${join(process.cwd(), 'temp', `temp-${timestamp}.mp3`)}`;
const audioBuffer = Buffer.from(audio, 'base64'); const audioBuffer = Buffer.from(audio, 'base64');
fs.writeFileSync(tempAudioPath, audioBuffer); fs.writeFileSync(tempAudioPath, audioBuffer);
@ -1321,7 +1321,7 @@ export class WAStartupService {
} }
public async audioWhatsapp(data: SendAudioDto) { public async audioWhatsapp(data: SendAudioDto) {
const convert = await this.processAudio(data.audioMessage.audio); const convert = await this.processAudio(data.audioMessage.audio, data.number);
if (typeof convert === 'string') { if (typeof convert === 'string') {
const audio = fs.readFileSync(convert).toString('base64'); const audio = fs.readFileSync(convert).toString('base64');
const result = this.sendMessageWithTyping<AnyMessageContent>( const result = this.sendMessageWithTyping<AnyMessageContent>(
@ -1600,7 +1600,8 @@ export class WAStartupService {
// if for audioMessage converte para mp3 // if for audioMessage converte para mp3
if (convertToMp4 && typeMessage === 'audioMessage') { if (convertToMp4 && typeMessage === 'audioMessage') {
const convert = await this.processAudio(buffer.toString('base64')); const number = msg.key.remoteJid.split('@')[0];
const convert = await this.processAudio(buffer.toString('base64'), number);
if (typeof convert === 'string') { if (typeof convert === 'string') {
const audio = fs.readFileSync(convert).toString('base64'); const audio = fs.readFileSync(convert).toString('base64');