diff --git a/backend/package.json b/backend/package.json index 6f404f93..a2b583ce 100644 --- a/backend/package.json +++ b/backend/package.json @@ -54,6 +54,7 @@ "@types/express": "^4.17.13", "@types/factory-girl": "^5.0.2", "@types/faker": "^5.1.3", + "@types/fluent-ffmpeg": "^2.1.21", "@types/jest": "^26.0.15", "@types/jsonwebtoken": "^8.5.0", "@types/lodash": "^4.14.185", diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index dfac2c77..c74056b7 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -12,6 +12,7 @@ import { Client } from "whatsapp-web.js"; +import * as ffmpeg from "fluent-ffmpeg"; import Contact from "../../models/Contact"; import Ticket from "../../models/Ticket"; import Message from "../../models/Message"; @@ -30,6 +31,8 @@ import UpdateTicketService from "../TicketServices/UpdateTicketService"; import CreateContactService from "../ContactServices/CreateContactService"; import formatBody from "../../helpers/Mustache"; +ffmpeg.setFfmpegPath("/usr/bin/ffmpeg"); + const request = require("request"); interface Session extends Client { @@ -141,12 +144,45 @@ const verifyMediaMessage = async ( media.filename = `${new Date().getTime()}${originalFilename}`; } + // try { + // await writeFileAsync( + // join(__dirname, "..", "..", "..", "public", media.filename), + // media.data, + // "base64" + // ); + // } catch (err: any) { + // Sentry.captureException(err); + // logger.error(err); + // } + try { await writeFileAsync( join(__dirname, "..", "..", "..", "public", media.filename), media.data, "base64" - ); + ) + .then(() => { + console.log("Arquivo OGG salvo com sucesso!"); + return new Promise((resolve, reject) => { + ffmpeg(`./public/${media.filename}`) + .toFormat("mp3") + .save(`./public/${media.filename}`.replace(".ogg", ".mp3")) + .on("end", () => { + resolve(); + }) + .on("error", err => { + reject(err); + }); + }); + }) + .then(() => { + console.log("Conversão concluída!"); + // Aqui você pode fazer o que desejar com o arquivo MP3 convertido. + }) + .catch(err => { + console.error("Ocorreu um erro:", err); + // Trate o erro de acordo com sua lógica de aplicativo. + }); } catch (err: any) { Sentry.captureException(err); logger.error(err); @@ -156,7 +192,7 @@ const verifyMediaMessage = async ( id: msg.id.id, ticketId: ticket.id, contactId: msg.fromMe ? undefined : contact.id, - body: msg.body || media.filename, + body: msg.body, fromMe: msg.fromMe, read: msg.fromMe, mediaUrl: media.filename, @@ -164,7 +200,7 @@ const verifyMediaMessage = async ( quotedMsgId: quotedMsg?.id }; - await ticket.update({ lastMessage: msg.body || media.filename }); + await ticket.update({ lastMessage: msg.body }); const newMessage = await CreateMessageService({ messageData }); return newMessage; diff --git a/docs/INSTALL_VPS.md b/docs/INSTALL_VPS.md index 95ef9770..0d6019d1 100644 --- a/docs/INSTALL_VPS.md +++ b/docs/INSTALL_VPS.md @@ -121,7 +121,7 @@ sudo apt-get install -y nodejs 20. Instalação de libs ```bash -sudo apt install apt-transport-https ca-certificates curl software-properties-common git +sudo apt install apt-transport-https ca-certificates curl software-properties-common git ffmpeg ``` 21. Atualizando diff --git a/frontend/src/components/Audio/index.jsx b/frontend/src/components/Audio/index.jsx index ad41f31d..2dcdefbb 100644 --- a/frontend/src/components/Audio/index.jsx +++ b/frontend/src/components/Audio/index.jsx @@ -5,56 +5,77 @@ const LS_NAME = 'audioMessageRate'; const Audio = ({url}) => { const audioRef = useRef(null); - const [audioRate, setAudioRate] = useState( parseFloat(localStorage.getItem(LS_NAME) || "1") ); + const [audioRate, setAudioRate] = useState(parseFloat(localStorage.getItem(LS_NAME) || "1")); const [showButtonRate, setShowButtonRate] = useState(false); - + const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; + useEffect(() => { - audioRef.current.playbackRate = audioRate; - localStorage.setItem(LS_NAME, audioRate); + audioRef.current.playbackRate = audioRate; + localStorage.setItem(LS_NAME, audioRate); }, [audioRate]); + useEffect(() => { - audioRef.current.onplaying = () => { - setShowButtonRate(true); - }; - audioRef.current.onpause = () => { - setShowButtonRate(false); - }; - audioRef.current.onended = () => { - setShowButtonRate(false); - }; + audioRef.current.onplaying = () => { + setShowButtonRate(true); + }; + audioRef.current.onpause = () => { + setShowButtonRate(false); + }; + audioRef.current.onended = () => { + setShowButtonRate(false); + }; }, []); - - const toogleRate = () => { - let newRate = null; - - switch(audioRate) { - case 0.5: - newRate = 1; - break; - case 1: - newRate = 1.5; - break; - case 1.5: - newRate = 2; - break; - case 2: - newRate = 0.5; - break; - default: - newRate = 1; - break; - } - - setAudioRate(newRate); + + const toggleRate = () => { + let newRate = null; + + switch (audioRate) { + case 0.5: + newRate = 1; + break; + case 1: + newRate = 1.5; + break; + case 1.5: + newRate = 2; + break; + case 2: + newRate = 0.5; + break; + default: + newRate = 1; + break; + } + + setAudioRate(newRate); }; - + + const getAudioSource = () => { + let sourceUrl = url; + + if (isIOS) { + sourceUrl = sourceUrl.replace(".ogg", ".mp3"); + } + + return ( + + ); + }; + return ( - <> - - {showButtonRate && } - + <> + + {showButtonRate && ( + + )} + ); }