Skip to content

Instantly share code, notes, and snippets.

@techhjork
Last active June 27, 2022 07:27
Show Gist options
  • Save techhjork/f4c98e6fc1925392ad6d1d793402b4aa to your computer and use it in GitHub Desktop.
Save techhjork/f4c98e6fc1925392ad6d1d793402b4aa to your computer and use it in GitHub Desktop.
Anonfiles Upload Nodejs | downloading from URL
const axios = require("axios");
const fs = require("fs");
const fetch = require("node-fetch");
const FormData = require("form-data");
const request = require("request");
const Path = require("path");
// const upload = (api, event) => {
const upload = (event) => {
const { type, threadID, messageID, senderID, attachments, messageReply } =
event;
let msg = "";
if (type !== "message_reply") return;
if (messageReply !== undefined && messageReply.attachments.length > 0) {
messageReply.attachments.forEach(async (attachment) => {
console.log(attachment);
//api.sendMessage("Uploading wait some time...", threadID, messageID);
let name =
attachment.type == "sticker" ? attachment.caption : attachment.filename;
let filename = __dirname + "/imgs/" + ext(attachment.type, name);
download(attachment.url, filename, async () => {
let res = await anupload(filename)
.then((res) => {
msg = `++++++++\n${res.file.metadata.name} : ${res.file.url.full} \n\n space taken : ${res.file.metadata.size.readable}\n ++++ \n`;
//api.sendMessage(msg, threadID, messageID);
})
.catch((err) => {
msg += `Sorry for ERROR : ${err}`;
//api.sendMessage(msg, threadID, messageID);
});
});
});
} else {
//api.sendMessage("NO FILE ON REPLIED MESSAGE", threadID, messageID);
}
};
module.exports = upload;
var download = async function (uri, filename, callback) {
console.log("DOWNLOADING START");
await request(uri).pipe(fs.createWriteStream(filename)).on("close", callback);
};
const ext = (type, name) => {
switch (type) {
case "photo":
case "sticker":
return name + ".png";
break;
case "video":
case "audio":
case "file":
return name;
break;
case "animated_image":
return name + ".gif";
default:
return name;
}
};
async function anupload(filename) {
const path = Path.resolve(__dirname, filename);
let data = new FormData();
data.append("file", fs.createReadStream(path));
return new Promise((resolve, reject) => {
data.append("file", fs.createReadStream(path));
fetch("https://api.anonfiles.com/upload?token=01409157656046c4", {
method: "POST",
body: data,
})
.then((res) => res.json())
.then((res) => {
if (res.status) {
resolve(res.data);
} else {
reject(res.error);
}
})
.catch((err) => {
reject(err.message);
});
});
}
/*
if(attachments !== undefined && attachments.length > 0){
api.sendMessage("Uploading wait some time...",threadID,messageID)
attachments.forEach(async (attachment)=>{
let filename = __dirname + "/imgs/"+ attachment.filename + ".png";
download(attachment.url,filename,async ()=>{
let res = await anupload(filename).then((res)=>{
msg = `++++++++\n${res.file.metadata.name} : ${res.file.url.full} \n\n space taken : ${res.file.metadata.size.readable}\n ++++ \n`
api.sendMessage(msg,threadID,messageID)
}).catch(err=>{
msg = `Sorry for ERROR : ${err}`;
api.sendMessage(msg,threadID,messageID)
});
})
})
}else{
api.sendMessage("NO FILE ON MSG",threadID,messageID)
}
*/
upload({
type: "message_reply",
threadID: "5307926295917343",
messageID: "mid.$gABLbh-Vcsx-H0crbIGBoIWRhLMyR",
senderID: "100037131918629",
attachments: [
{
type: "photo",
ID: "1112877415933812",
filename: "image-1112877415933812",
thumbnailUrl:
"https://scontent.xx.fbcdn.net/v/t1.15752-9/289646090_1112877419267145_4221398597607969843_n.jpg?stp=cp0_dst-jpg_p50x50&_nc_cat=110&ccb=1-7&_nc_sid=ae9488&_nc_ohc=uLT3qW60qBQAX9dis5O&_nc_ad=z-m&_nc_cid=0&_nc_ht=scontent.xx&oh=03_AVJd4-2F1qnNmv9agzBVbEtVViorkU8zpG8xaSwwLwad1Q&oe=62DD9DD2",
previewUrl:
"https://scontent.xx.fbcdn.net/v/t1.15752-9/289646090_1112877419267145_4221398597607969843_n.jpg?_nc_cat=110&ccb=1-7&_nc_sid=ae9488&_nc_ohc=uLT3qW60qBQAX9dis5O&_nc_ad=z-m&_nc_cid=0&_nc_ht=scontent.xx&oh=03_AVJzYBNZUm1V8Cue5svSiu0VutV_ra2sRRq6DpNLhJfwSg&oe=62DD9DD2",
previewWidth: 240,
previewHeight: 240,
largePreviewUrl:
"https://scontent.xx.fbcdn.net/v/t1.15752-9/289646090_1112877419267145_4221398597607969843_n.jpg?_nc_cat=110&ccb=1-7&_nc_sid=ae9488&_nc_ohc=uLT3qW60qBQAX9dis5O&_nc_ad=z-m&_nc_cid=0&_nc_ht=scontent.xx&oh=03_AVJzYBNZUm1V8Cue5svSiu0VutV_ra2sRRq6DpNLhJfwSg&oe=62DD9DD2",
largePreviewWidth: 240,
largePreviewHeight: 240,
url: "https://images.unsplash.com/photo-1639912998783-b99f17c8dc3a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80",
width: 240,
height: 240,
name: "image-1112877415933812",
},
],
body: "save",
isGroup: true,
mentions: {},
timestamp: 1656255517472,
messageReply: {
threadID: "5307926295917343",
messageID: "mid.$gABLbh-Vcsx-H0bKkVWBoG1SHwjAa",
senderID: "100046721985974",
attachments: [
{
type: "photo",
ID: "1112877415933812",
filename: "image-1112877415933812",
thumbnailUrl:
"https://scontent.xx.fbcdn.net/v/t1.15752-9/289646090_1112877419267145_4221398597607969843_n.jpg?stp=cp0_dst-jpg_p50x50&_nc_cat=110&ccb=1-7&_nc_sid=ae9488&_nc_ohc=uLT3qW60qBQAX9dis5O&_nc_ad=z-m&_nc_cid=0&_nc_ht=scontent.xx&oh=03_AVJd4-2F1qnNmv9agzBVbEtVViorkU8zpG8xaSwwLwad1Q&oe=62DD9DD2",
previewUrl:
"https://scontent.xx.fbcdn.net/v/t1.15752-9/289646090_1112877419267145_4221398597607969843_n.jpg?_nc_cat=110&ccb=1-7&_nc_sid=ae9488&_nc_ohc=uLT3qW60qBQAX9dis5O&_nc_ad=z-m&_nc_cid=0&_nc_ht=scontent.xx&oh=03_AVJzYBNZUm1V8Cue5svSiu0VutV_ra2sRRq6DpNLhJfwSg&oe=62DD9DD2",
previewWidth: 240,
previewHeight: 240,
largePreviewUrl:
"https://scontent.xx.fbcdn.net/v/t1.15752-9/289646090_1112877419267145_4221398597607969843_n.jpg?_nc_cat=110&ccb=1-7&_nc_sid=ae9488&_nc_ohc=uLT3qW60qBQAX9dis5O&_nc_ad=z-m&_nc_cid=0&_nc_ht=scontent.xx&oh=03_AVJzYBNZUm1V8Cue5svSiu0VutV_ra2sRRq6DpNLhJfwSg&oe=62DD9DD2",
largePreviewWidth: 240,
largePreviewHeight: 240,
url: "https://images.unsplash.com/photo-1639912998783-b99f17c8dc3a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80",
width: 240,
height: 240,
name: "image-1112877415933812",
},
],
body: "",
mentions: {},
timestamp: 1656253930581,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment