From ef789b84dbef4c97a80ce6f3a02b696f9da2a4d4 Mon Sep 17 00:00:00 2001 From: Sascha Kaufmann Date: Sat, 17 Jul 2021 13:59:20 +0200 Subject: [PATCH 1/6] getImage returning now the FilePath --- src/handlers/messageHandler.js | 6 +-- src/helpers/imgManager.js | 67 +++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/handlers/messageHandler.js b/src/handlers/messageHandler.js index a26f99d..82f44b1 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"){ @@ -38,8 +38,8 @@ 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 file_path = await client.imgManager.getImg(data.find("file-url").text, true, userJid); + 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..890aa84 100644 --- a/src/helpers/imgManager.js +++ b/src/helpers/imgManager.js @@ -65,46 +65,53 @@ 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 = []; + async getImg(url, isPrivate, source){ + 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 = []; - res.on("data", (data) => { - dataArr.push(data); - }); - res.on("end", () => { - let buffer = Buffer.concat(dataArr); - let date = new Date().toISOString().substring(0, 10); + res.on("data", (data) => { + dataArr.push(data); + }); + res.on("end", () => { + let buffer = Buffer.concat(dataArr); + let date = new Date().toISOString().substring(0, 10); - if(this.saveImages){ - let imageDirectory = `./images/${this.username}`; + if(this.saveImages){ + 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); + 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 file_path=`${imageDirectory}/${date}_${Date.now()}.jpeg`; + 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); + }); } } From 8a9eb9a0a6c937c8b6773f936c20e7760a33970b Mon Sep 17 00:00:00 2001 From: Sascha Kaufmann Date: Sat, 17 Jul 2021 15:12:49 +0200 Subject: [PATCH 2/6] i forget about the group part + use original file extension --- src/handlers/messageHandler.js | 8 +++++--- src/helpers/imgManager.js | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/handlers/messageHandler.js b/src/handlers/messageHandler.js index 82f44b1..f982866 100644 --- a/src/handlers/messageHandler.js +++ b/src/handlers/messageHandler.js @@ -10,8 +10,9 @@ module.exports = async (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)); + // ToDo: see Users + let file_path = await client.imgManager.getImg(data.find("file-url").text, false, groupJid, data.find("file-name").text); + 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,7 +39,8 @@ module.exports = async (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")){ - let file_path = await client.imgManager.getImg(data.find("file-url").text, true, userJid); + // ToDo: You could get gifs with kik... they dont have a file-url... resulting in an error! + let file_path = await client.imgManager.getImg(data.find("file-url").text, true, userJid, data.find("file-name").text); client.emit("receivedprivateimg", userJid, file_path); } }else if(type === "receipt"){ diff --git a/src/helpers/imgManager.js b/src/helpers/imgManager.js index 890aa84..7f8df5f 100644 --- a/src/helpers/imgManager.js +++ b/src/helpers/imgManager.js @@ -65,11 +65,12 @@ class ImageManager { await axios.put(url, buffer, {headers}); return {contentId, size, sha1, previewSha1, previewBlockhash, previewBase64}; } - async getImg(url, isPrivate, source){ + 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 = []; @@ -81,6 +82,7 @@ class ImageManager { let date = new Date().toISOString().substring(0, 10); if(this.saveImages){ + // ToDo: It should probably be files not images, because its possible to get videos^^ let imageDirectory = `./images/${this.username}`; if(isPrivate){ @@ -93,7 +95,9 @@ class ImageManager { if(!fs.existsSync(imageDirectory)){ fs.mkdirSync(imageDirectory); } - let file_path=`${imageDirectory}/${date}_${Date.now()}.jpeg`; + 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 { From f3a228c8aa1d901fcce786e6f188e81d5b11616a Mon Sep 17 00:00:00 2001 From: Sascha Kaufmann Date: Sun, 18 Jul 2021 14:01:37 +0200 Subject: [PATCH 3/6] Parse Data for diffrent Apps --- src/handlers/messageHandler.js | 8 +++--- src/helpers/imgManager.js | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/handlers/messageHandler.js b/src/handlers/messageHandler.js index f982866..8e21dd7 100644 --- a/src/handlers/messageHandler.js +++ b/src/handlers/messageHandler.js @@ -10,8 +10,8 @@ module.exports = async (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")){ - // ToDo: see Users - let file_path = await client.imgManager.getImg(data.find("file-url").text, false, groupJid, data.find("file-name").text); + 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"); @@ -39,8 +39,8 @@ module.exports = async (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")){ - // ToDo: You could get gifs with kik... they dont have a file-url... resulting in an error! - let file_path = await client.imgManager.getImg(data.find("file-url").text, true, userJid, data.find("file-name").text); + 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"){ diff --git a/src/helpers/imgManager.js b/src/helpers/imgManager.js index 7f8df5f..4fd576c 100644 --- a/src/helpers/imgManager.js +++ b/src/helpers/imgManager.js @@ -65,6 +65,52 @@ class ImageManager { await axios.put(url, buffer, {headers}); return {contentId, size, sha1, previewSha1, previewBlockhash, previewBase64}; } + 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")=="Camera").text) { + 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-tpe"].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! + } + + 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 From d20e3c0c88d1792ab74796a2aef3892b225e5a3c Mon Sep 17 00:00:00 2001 From: Sascha Kaufmann Date: Sun, 18 Jul 2021 14:15:19 +0200 Subject: [PATCH 4/6] update the img return value --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 080db00996be20892e0a91e3ef7a021f52e31cde Mon Sep 17 00:00:00 2001 From: Sascha Kaufmann Date: Sun, 18 Jul 2021 15:47:43 +0200 Subject: [PATCH 5/6] App: Camera Fix --- src/helpers/imgManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/imgManager.js b/src/helpers/imgManager.js index 4fd576c..8ab1dce 100644 --- a/src/helpers/imgManager.js +++ b/src/helpers/imgManager.js @@ -70,7 +70,7 @@ class ImageManager { 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")=="Camera").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") { From 929b0992b75b694fb58c478496de578edf5806d4 Mon Sep 17 00:00:00 2001 From: Sascha Kaufmann Date: Tue, 27 Jul 2021 19:23:52 +0200 Subject: [PATCH 6/6] Fixing Typo & Comment Console.Logs --- src/helpers/imgManager.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/helpers/imgManager.js b/src/helpers/imgManager.js index 8ab1dce..3b06ac1 100644 --- a/src/helpers/imgManager.js +++ b/src/helpers/imgManager.js @@ -69,13 +69,13 @@ class ImageManager { 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})`); + //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-tpe"].replace("/","."); + 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) { @@ -104,11 +104,11 @@ class ImageManager { if (file_url=="") { //Logger.log("error", `Unknown App!?`); - console.error(`Unknown App!?`); + //console.error(`Unknown App!?`); //process.exit(1); } - console.log("file_url: ", file_url); - console.log("file_name: ", file_name); + //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){