diff --git a/README.md b/README.md index b67a20a..4b77275 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ Kik.on("receivedgroupimg", (groupJid, senderJid, img) => { console.log(`Received image from ${sender.jid} in group ${group.jid}`) }) ``` -`img`: a [`buffer`](https://nodejs.org/api/buffer.html) object representing the image +`img`: path to the image ##### Group is Typing @@ -254,7 +254,7 @@ Kik.on("receivedprivateimg", (senderJid, img) => { }) ``` -`img`: a [`buffer`](https://nodejs.org/api/buffer.html) object representing the image +`img`: path to the image ##### Private Is Typing diff --git a/src/handlers/messageHandler.js b/src/handlers/messageHandler.js index a26f99d..8e21dd7 100644 --- a/src/handlers/messageHandler.js +++ b/src/handlers/messageHandler.js @@ -1,4 +1,4 @@ -module.exports = (client, callbacks, id, data) => { +module.exports = async (client, callbacks, id, data) => { let type = data.find("message").attrs.type; if(type === "groupchat"){ @@ -10,8 +10,9 @@ module.exports = (client, callbacks, id, data) => { }else if(data.find("is-typing")){ client.emit("grouptyping", groupJid, userJid, data.find("is-typing").attrs.val === "true"); }else if(data.find("images")){ - client.emit("receivedgroupimg", groupJid, userJid, client.imgManager.getImg(data.find("file-url").text, - false, groupJid)); + let parseData = client.imgManager.parseAppData(data); + let file_path = await client.imgManager.getImg(parseData.file_url, false, groupJid,parseData.file_name); + client.emit("receivedgroupimg", groupJid, userJid, file_path); }else if(data.find("status")){ let status = data.find("status"); //userJid and groupJid are different for status @@ -38,8 +39,9 @@ module.exports = (client, callbacks, id, data) => { }else if(type === "is-typing"){ client.emit("privatetyping", userJid, data.find("is-typing").attrs.val === "true"); }else if(data.find("images")){ - client.emit("receivedprivateimg", userJid, client.imgManager.getImg(data.find("file-url").text, - true, userJid)); + let parseData = client.imgManager.parseAppData(data); + let file_path = await client.imgManager.getImg(parseData.file_url, true, userJid,parseData.file_name); + client.emit("receivedprivateimg", userJid, file_path); } }else if(type === "receipt"){ let receipt = data.find("receipt").attrs.type; diff --git a/src/helpers/imgManager.js b/src/helpers/imgManager.js index 3ef154c..3b06ac1 100644 --- a/src/helpers/imgManager.js +++ b/src/helpers/imgManager.js @@ -65,46 +65,103 @@ class ImageManager { await axios.put(url, buffer, {headers}); return {contentId, size, sha1, previewSha1, previewBlockhash, previewBase64}; } - getImg(url, isPrivate, source){ - //first request returns a 302 with a url - https.get(url, (res) => { - //second req returns the actual image - https.get(res.headers.location, (res) => { - let dataArr = []; + parseAppData(data) { + let file_url=""; + let file_name="fu.png"; + //Logger.log("info", `Received data from app: (${data.find("app-name").text})`); + //console.log(`Received data from app: (${data.find("app-name").text})`); + if ((data.find("app-name").text=="Gallery") || (data.find("app-name").text=="Camera")) { + file_url=data.find("file-url").text; + file_name=data.find("file-name").text + } else if (data.find("app-name").text=="GIF") { + file_url=data.find("uris").find("uri").text; + file_name=data.find("uris").find("uri").attrs["file-content-type"].replace("/","."); + } else if (data.find("app-name").text=="Stickers") { + let tmp_elements=data.find("extras").findAll("item"); + for (let i in tmp_elements) { + let element=tmp_elements[i]; + if (element.find("key").text=="sticker_url") { // Probably a Array.Find would work too + file_url=element.find("val").text; + let tmp_urlparts=file_url.split("/"); + file_name=tmp_urlparts[tmp_urlparts.length-1]; + } + } + } + else if (data.find("app-name").text=="Sketch") { + let tmp_elements=data.find("extras").findAll("item"); + for (let i in tmp_elements) { + let element=tmp_elements[i]; + if (element.find("key").text=="jsonData") { // Probably a Array.Find would work too + let jsonData = JSON.parse(element.find("val").text); + file_url=jsonData.image; + file_name="i_dont_see_it.png"; + } + } + } + else if (data.find("app-name").text=="Memes") { + // Sorry i dont get it! + } - res.on("data", (data) => { - dataArr.push(data); - }); - res.on("end", () => { - let buffer = Buffer.concat(dataArr); - let date = new Date().toISOString().substring(0, 10); + if (file_url=="") { + //Logger.log("error", `Unknown App!?`); + //console.error(`Unknown App!?`); + //process.exit(1); + } + //console.log("file_url: ", file_url); + //console.log("file_name: ", file_name); + return {"file_url": file_url, "file_name": file_name}; + } + async getImg(url, isPrivate, source, file_name){ + return new Promise((resolve, reject) => { + //first request returns a 302 with a url + https.get(url, async (res) => { + //second req returns the actual image + + https.get(res.headers.location, (res) => { + let dataArr = []; - if(this.saveImages){ - let imageDirectory = `./images/${this.username}`; + res.on("data", (data) => { + dataArr.push(data); + }); + res.on("end", () => { + let buffer = Buffer.concat(dataArr); + let date = new Date().toISOString().substring(0, 10); - if(isPrivate){ - imageDirectory += "/private"; - }else{ - imageDirectory += "/groups"; - } - imageDirectory += `/${source}`; - //make sure the directory exists, if not create it - if(!fs.existsSync(imageDirectory)){ - fs.mkdirSync(imageDirectory); + if(this.saveImages){ + // ToDo: It should probably be files not images, because its possible to get videos^^ + let imageDirectory = `./images/${this.username}`; + + if(isPrivate){ + imageDirectory += "/private"; + }else{ + imageDirectory += "/groups"; + } + imageDirectory += `/${source}`; + //make sure the directory exists, if not create it + if(!fs.existsSync(imageDirectory)){ + fs.mkdirSync(imageDirectory); + } + let tmp_arr = file_name.split("."); + let file_extension=tmp_arr[tmp_arr.length-1]; + let file_path=`${imageDirectory}/${date}_${Date.now()}.${file_extension}`; + fs.writeFileSync(file_path, buffer); + resolve(file_path); + } else { + resolve(); } - fs.writeFileSync(`${imageDirectory}/${date}_${Date.now()}.jpeg`, buffer); - } - return buffer; + }); + }).on("error", (err) => { + console.log("Error downloading image:"); + console.log(err); + reject(); }); }).on("error", (err) => { console.log("Error downloading image:"); console.log(err); + reject(); }); - - }).on("error", (err) => { - console.log("Error downloading image:"); - console.log(err); + }); } }