2019-09-13 20:02:41 +00:00
|
|
|
const fetch = require("node-fetch");
|
|
|
|
const fileType = require("file-type");
|
2020-03-15 17:54:51 +00:00
|
|
|
const { promisify } = require("util");
|
|
|
|
const writeFile = promisify(require("fs").writeFile);
|
|
|
|
const execPromise = promisify(require("child_process").exec);
|
2019-12-09 21:33:06 +00:00
|
|
|
const urlRegex = /(?:\w+:)?\/\/(\S+)/;
|
2019-09-13 20:02:41 +00:00
|
|
|
|
|
|
|
// this checks if the file is, in fact, an image
|
2020-04-23 22:38:01 +00:00
|
|
|
const typeCheck = async (image, image2, gifv = false) => {
|
2019-09-13 20:02:41 +00:00
|
|
|
// download the file to a buffer
|
|
|
|
const imageRequest = await fetch(image);
|
|
|
|
const imageBuffer = await imageRequest.buffer();
|
|
|
|
try {
|
|
|
|
// get the file type
|
2020-01-19 15:40:40 +00:00
|
|
|
const imageType = await fileType.fromBuffer(imageBuffer);
|
2019-09-13 20:02:41 +00:00
|
|
|
// check if the file is a jpeg, png, or webp
|
2020-02-18 20:44:39 +00:00
|
|
|
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
|
|
|
|
if (gifv) formats.push("video/mp4");
|
|
|
|
if (imageType && formats.includes(imageType.mime)) {
|
2019-09-13 20:02:41 +00:00
|
|
|
// if it is, then return the url with the file type
|
2020-02-18 20:44:39 +00:00
|
|
|
const path = `/tmp/${Math.random().toString(36).substring(2, 15)}.${imageType.ext}`;
|
|
|
|
await writeFile(path, imageBuffer);
|
2020-03-15 17:54:51 +00:00
|
|
|
const payload = {
|
2020-02-18 20:44:39 +00:00
|
|
|
data: imageBuffer,
|
2020-02-26 17:29:13 +00:00
|
|
|
type: imageType.ext !== "mp4" ? (imageType.ext === "jpg" ? "jpeg" : imageType.ext) : "gif",
|
2020-04-23 22:38:01 +00:00
|
|
|
path: path,
|
|
|
|
url: image2
|
2019-09-13 20:02:41 +00:00
|
|
|
};
|
2020-03-15 17:54:51 +00:00
|
|
|
if (gifv) payload.delay = (await execPromise(`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate ${path}`)).stdout.replace("\n", "");
|
|
|
|
return payload;
|
2019-09-13 20:02:41 +00:00
|
|
|
} else {
|
2019-11-11 20:54:19 +00:00
|
|
|
// if not, then return false
|
|
|
|
return false;
|
2019-09-13 20:02:41 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2019-11-11 20:54:19 +00:00
|
|
|
throw error;
|
2019-09-13 20:02:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// this checks for the latest message containing an image and returns the url of the image
|
|
|
|
module.exports = async (cmdMessage) => {
|
|
|
|
// we start by getting the messages
|
|
|
|
const messages = await cmdMessage.channel.getMessages();
|
|
|
|
// iterate over each message
|
|
|
|
for (const message of messages) {
|
|
|
|
// check the attachments first
|
2020-02-18 20:44:39 +00:00
|
|
|
if (message.embeds.length !== 0) {
|
|
|
|
// embeds can have 2 possible entries with images, we check the thumbnail first
|
|
|
|
if (message.embeds[0].type === "gifv") {
|
2020-04-23 22:38:01 +00:00
|
|
|
const type = await typeCheck(message.embeds[0].video.url, message.embeds[0].video.url, true);
|
2020-02-18 20:44:39 +00:00
|
|
|
if (type === false) continue;
|
|
|
|
return type;
|
|
|
|
} else if (message.embeds[0].thumbnail) {
|
2020-04-23 22:38:01 +00:00
|
|
|
const type = await typeCheck(message.embeds[0].thumbnail.proxy_url, message.embeds[0].thumbnail.url);
|
2020-02-18 20:44:39 +00:00
|
|
|
if (type === false) continue;
|
|
|
|
return type;
|
|
|
|
// if there isn't a thumbnail check the image area
|
|
|
|
} else if (message.embeds[0].image) {
|
2020-04-23 22:38:01 +00:00
|
|
|
const type = await typeCheck(message.embeds[0].image.proxy_url, message.embeds[0].image.url);
|
2020-02-18 20:44:39 +00:00
|
|
|
if (type === false) continue;
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
} else if (message.attachments.length !== 0) {
|
2019-09-13 20:02:41 +00:00
|
|
|
// get type of file
|
2020-04-23 22:38:01 +00:00
|
|
|
const type = await typeCheck(message.attachments[0].proxy_url, message.attachments[0].url);
|
2019-09-13 20:02:41 +00:00
|
|
|
// move to the next message if the file isn't an image
|
2019-11-11 20:54:19 +00:00
|
|
|
if (type === false) continue;
|
2019-09-13 20:02:41 +00:00
|
|
|
// if the file is an image then return it
|
|
|
|
return type;
|
2019-12-09 21:33:06 +00:00
|
|
|
// if there's nothing in the attachments check the urls in the message if there are any
|
|
|
|
} else if (urlRegex.test(message.content)) {
|
|
|
|
// get url
|
|
|
|
const url = message.content.match(urlRegex);
|
|
|
|
// get type of file
|
2020-04-23 22:38:01 +00:00
|
|
|
const type = await typeCheck(url[0], url[0]);
|
2019-12-09 21:33:06 +00:00
|
|
|
// move to the next message if the file isn't an image
|
|
|
|
if (type === false) continue;
|
|
|
|
// if the file is an image then return it
|
|
|
|
return type;
|
|
|
|
// if there's no urls then check the embeds
|
2019-09-13 20:02:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 17:29:13 +00:00
|
|
|
};
|