2020-08-28 02:34:12 +00:00
|
|
|
const magick = require("../build/Release/image.node");
|
2021-01-10 01:50:29 +00:00
|
|
|
const { Worker } = require("worker_threads");
|
2020-09-01 22:10:19 +00:00
|
|
|
const fetch = require("node-fetch");
|
2020-10-18 21:53:35 +00:00
|
|
|
const AbortController = require("abort-controller");
|
2020-11-17 14:52:12 +00:00
|
|
|
const net = require("net");
|
|
|
|
const dgram = require("dgram");
|
2020-10-18 21:53:35 +00:00
|
|
|
const fileType = require("file-type");
|
2020-11-05 21:40:18 +00:00
|
|
|
const servers = require("../servers.json").image;
|
2021-01-10 01:50:29 +00:00
|
|
|
const path = require("path");
|
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-12-26 02:27:45 +00:00
|
|
|
const chooseServer = async (ideal) => {
|
|
|
|
if (ideal.length === 0) throw "No available servers";
|
|
|
|
const sorted = ideal.sort((a, b) => {
|
2021-01-08 18:08:10 +00:00
|
|
|
return b.load - a.load;
|
2020-12-26 02:27:45 +00:00
|
|
|
});
|
|
|
|
return sorted[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
const getIdeal = () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const socket = dgram.createSocket("udp4");
|
|
|
|
let serversLeft = servers.length;
|
|
|
|
const idealServers = [];
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
socket.close(async () => {
|
|
|
|
try {
|
|
|
|
const server = await chooseServer(idealServers);
|
|
|
|
resolve(server);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 5000);
|
|
|
|
socket.on("message", async (msg, rinfo) => {
|
|
|
|
const opcode = msg.readUint8(0);
|
|
|
|
const res = parseInt(msg.slice(1, msg.length).toString());
|
|
|
|
if (opcode === 0x3) {
|
|
|
|
serversLeft--;
|
|
|
|
idealServers.push({
|
|
|
|
addr: rinfo.address,
|
|
|
|
load: res
|
|
|
|
});
|
|
|
|
if (!serversLeft) {
|
|
|
|
clearTimeout(timeout);
|
2020-12-26 18:17:10 +00:00
|
|
|
socket.close(async () => {
|
|
|
|
try {
|
|
|
|
const server = await chooseServer(idealServers);
|
|
|
|
resolve(server);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
2020-12-26 02:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (const server of servers) {
|
|
|
|
socket.send(Buffer.from([0x2]), 8080, server, (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-26 17:48:19 +00:00
|
|
|
exports.check = (cmd) => {
|
|
|
|
return magick[cmd] ? true : false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getType = async (image) => {
|
|
|
|
if (!image.startsWith("http")) {
|
|
|
|
const imageType = await fileType.fromFile(image);
|
|
|
|
if (imageType && formats.includes(imageType.mime)) {
|
|
|
|
return imageType.mime;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let type;
|
|
|
|
const controller = new AbortController();
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
}, 25000);
|
|
|
|
try {
|
2021-01-04 17:10:57 +00:00
|
|
|
const imageRequest = await fetch(image, { signal: controller.signal, headers: {
|
|
|
|
"Range": "bytes=0-1023"
|
|
|
|
}});
|
2021-01-04 16:29:18 +00:00
|
|
|
clearTimeout(timeout);
|
2020-11-26 17:48:19 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2021-01-10 01:50:29 +00:00
|
|
|
exports.run = object => {
|
2020-11-17 14:52:12 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2021-01-10 01:50:29 +00:00
|
|
|
if (process.env.API === "true") {
|
|
|
|
// Connect to best image server
|
2020-12-26 02:27:45 +00:00
|
|
|
const currentServer = await getIdeal();
|
2020-11-17 14:52:12 +00:00
|
|
|
const socket = dgram.createSocket("udp4");
|
2021-01-10 01:50:29 +00:00
|
|
|
const data = Buffer.concat([Buffer.from([0x1 /* queue job */]), Buffer.from(JSON.stringify(object))]);
|
2020-12-14 18:39:18 +00:00
|
|
|
|
2020-12-18 02:32:19 +00:00
|
|
|
const timeout = setTimeout(() => {
|
2021-01-10 01:50:29 +00:00
|
|
|
socket.close();
|
2021-01-04 16:29:18 +00:00
|
|
|
reject("UDP timed out");
|
2020-12-14 18:39:18 +00:00
|
|
|
}, 25000);
|
2021-01-10 01:50:29 +00:00
|
|
|
|
2020-12-14 18:39:18 +00:00
|
|
|
let jobID;
|
2020-11-17 14:52:12 +00:00
|
|
|
socket.on("message", (msg) => {
|
|
|
|
const opcode = msg.readUint8(0);
|
2020-12-14 18:39:18 +00:00
|
|
|
const req = msg.slice(37, msg.length);
|
|
|
|
const uuid = msg.slice(1, 36).toString();
|
2021-01-10 01:50:29 +00:00
|
|
|
if (opcode === 0x0) { // Job queued
|
2020-12-18 02:32:19 +00:00
|
|
|
clearTimeout(timeout);
|
2020-12-14 18:39:18 +00:00
|
|
|
jobID = uuid;
|
2021-01-10 01:50:29 +00:00
|
|
|
} else if (opcode === 0x1) { // Job completed successfully
|
|
|
|
// the image API sends all job responses over the same socket; make sure this is ours
|
2020-12-14 18:39:18 +00:00
|
|
|
if (jobID === uuid) {
|
2020-12-26 02:27:45 +00:00
|
|
|
const client = net.createConnection(req.toString(), currentServer.addr);
|
2020-12-14 18:39:18 +00:00
|
|
|
const array = [];
|
|
|
|
client.on("data", (rawData) => {
|
|
|
|
array.push(rawData);
|
|
|
|
});
|
|
|
|
client.once("end", () => {
|
|
|
|
const data = Buffer.concat(array);
|
2021-01-10 01:50:29 +00:00
|
|
|
// The response data is given as the file extension/ImageMagick type of the image (e.g. "png"), followed
|
|
|
|
// by a newline, followed by the image data.
|
|
|
|
const delimIndex = data.indexOf("\n");
|
|
|
|
socket.close();
|
|
|
|
if (delimIndex === -1) reject("Could not parse response");
|
2020-12-14 18:39:18 +00:00
|
|
|
const payload = {
|
2021-01-10 01:50:29 +00:00
|
|
|
// Take just the image data
|
|
|
|
buffer: data.slice(delimIndex + 1),
|
|
|
|
type: data.slice(0, delimIndex).toString()
|
2020-12-14 18:39:18 +00:00
|
|
|
};
|
|
|
|
resolve(payload);
|
|
|
|
});
|
|
|
|
client.on("error", (err) => {
|
2021-01-10 01:50:29 +00:00
|
|
|
socket.close();
|
2020-12-26 02:27:45 +00:00
|
|
|
reject(err);
|
2020-12-14 18:39:18 +00:00
|
|
|
});
|
|
|
|
}
|
2021-01-10 01:50:29 +00:00
|
|
|
} else if (opcode === 0x2) { // Job errored
|
|
|
|
if (jobID === uuid) {
|
|
|
|
socket.close();
|
|
|
|
reject(req);
|
|
|
|
}
|
2020-11-17 14:52:12 +00:00
|
|
|
}
|
|
|
|
});
|
2021-01-10 01:50:29 +00:00
|
|
|
|
2020-12-26 02:27:45 +00:00
|
|
|
socket.send(data, 8080, currentServer.addr, (err) => {
|
2021-01-10 01:50:29 +00:00
|
|
|
if (err) {
|
|
|
|
socket.close();
|
|
|
|
reject(err);
|
|
|
|
}
|
2020-11-17 14:52:12 +00:00
|
|
|
});
|
2021-01-10 01:50:29 +00:00
|
|
|
} else {
|
|
|
|
// Called from command (not using image API)
|
|
|
|
const worker = new Worker(path.join(__dirname, "image-runner.js"), {
|
2020-11-26 17:48:19 +00:00
|
|
|
workerData: object
|
|
|
|
});
|
|
|
|
worker.on("message", (data) => {
|
|
|
|
resolve({
|
|
|
|
buffer: Buffer.from([...data.buffer]),
|
2021-01-10 01:50:29 +00:00
|
|
|
type: data.fileExtension
|
2020-11-26 17:48:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
worker.on("error", reject);
|
2020-11-17 14:52:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|