2020-08-28 02:34:12 +00:00
|
|
|
const magick = require("../build/Release/image.node");
|
2020-09-01 22:10:19 +00:00
|
|
|
const fetch = require("node-fetch");
|
2020-08-28 02:34:12 +00:00
|
|
|
const { promisify } = require("util");
|
2020-10-18 21:53:35 +00:00
|
|
|
const AbortController = require("abort-controller");
|
|
|
|
const fileType = require("file-type");
|
|
|
|
const execPromise = promisify(require("child_process").exec);
|
2020-11-05 21:40:18 +00:00
|
|
|
const servers = require("../servers.json").image;
|
2020-10-18 21:53:35 +00:00
|
|
|
|
|
|
|
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
|
2020-08-28 02:34:12 +00:00
|
|
|
|
2020-10-06 19:48:22 +00:00
|
|
|
exports.run = async (object, fromAPI = false) => {
|
2020-09-02 02:52:12 +00:00
|
|
|
if (process.env.API === "true" && !fromAPI) {
|
2020-11-05 21:40:18 +00:00
|
|
|
const currentServer = servers[Math.floor(Math.random() * servers.length)];
|
|
|
|
const req = await fetch(`${currentServer}/run`, {
|
2020-09-01 22:10:19 +00:00
|
|
|
method: "POST",
|
2020-10-18 21:53:35 +00:00
|
|
|
body: JSON.stringify(object),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
2020-09-01 22:10:19 +00:00
|
|
|
});
|
2020-11-05 21:40:18 +00:00
|
|
|
const json = await req.json();
|
|
|
|
if (json.status === "nogif") return {
|
2020-10-20 01:24:53 +00:00
|
|
|
buffer: "nogif",
|
|
|
|
type: null
|
|
|
|
};
|
2020-11-05 21:40:18 +00:00
|
|
|
let data;
|
|
|
|
while (!data) {
|
|
|
|
const statusReq = await fetch(`${currentServer}/status?id=${json.id}`);
|
|
|
|
const statusJSON = await statusReq.json();
|
|
|
|
if (statusJSON.status === "success") {
|
|
|
|
const imageReq = await fetch(`${currentServer}/image?id=${json.id}`);
|
|
|
|
data = {
|
|
|
|
buffer: await imageReq.buffer(),
|
|
|
|
type: imageReq.headers.get("content-type").split("/")[1]
|
|
|
|
};
|
|
|
|
} else if (statusJSON.status === "error") {
|
|
|
|
throw new Error(statusJSON.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
2020-09-01 22:10:19 +00:00
|
|
|
} else {
|
2020-10-18 21:53:35 +00:00
|
|
|
let type;
|
|
|
|
if (!fromAPI && object.path) {
|
2020-11-05 21:40:18 +00:00
|
|
|
const newType = (object.type ? object.type : await this.getType(object.path));
|
|
|
|
type = newType ? newType.split("/")[1] : "png";
|
2020-10-20 01:24:53 +00:00
|
|
|
if (type !== "gif" && object.onlyGIF) return {
|
|
|
|
buffer: "nogif",
|
|
|
|
type: null
|
|
|
|
};
|
2020-10-18 21:53:35 +00:00
|
|
|
object.type = type;
|
|
|
|
const delay = (await execPromise(`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate ${object.path}`)).stdout.replace("\n", "");
|
|
|
|
object.delay = (100 / delay.split("/")[0]) * delay.split("/")[1];
|
|
|
|
}
|
2020-09-01 22:10:19 +00:00
|
|
|
const data = await promisify(magick[object.cmd])(object);
|
2020-10-18 21:53:35 +00:00
|
|
|
return fromAPI ? data : {
|
|
|
|
buffer: data,
|
|
|
|
type: type
|
|
|
|
};
|
2020-09-01 22:10:19 +00:00
|
|
|
}
|
2020-10-06 19:48:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.check = (cmd) => {
|
|
|
|
return magick[cmd] ? true : false;
|
2020-10-18 21:53:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getType = async (image) => {
|
|
|
|
let type;
|
|
|
|
const controller = new AbortController();
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
}, 25000);
|
|
|
|
try {
|
|
|
|
const imageRequest = await fetch(image, { signal: controller.signal, highWaterMark: 512 });
|
|
|
|
const imageBuffer = await imageRequest.buffer();
|
|
|
|
const imageType = await fileType.fromBuffer(imageBuffer);
|
|
|
|
if (imageType && formats.includes(imageType.mime)) {
|
|
|
|
type = imageType.mime;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name === "AbortError") {
|
|
|
|
throw Error("Timed out");
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
return type;
|
2020-08-28 02:34:12 +00:00
|
|
|
};
|