Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getImage returning now the FilePath #32

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions src/handlers/messageHandler.js
Original file line number Diff line number Diff line change
@@ -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"){
Expand All @@ -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
Expand All @@ -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;
Expand Down
117 changes: 87 additions & 30 deletions src/helpers/imgManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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-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!
}

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);

});
}
}
Expand Down