Switched to an image API implementation by Terradice
This commit is contained in:
parent
6761e39d70
commit
ad149156ad
6 changed files with 209 additions and 165 deletions
119
utils/image.js
119
utils/image.js
|
@ -2,63 +2,90 @@ const magick = require("../build/Release/image.node");
|
|||
const fetch = require("node-fetch");
|
||||
const { promisify } = require("util");
|
||||
const AbortController = require("abort-controller");
|
||||
const net = require("net");
|
||||
const dgram = require("dgram");
|
||||
const fileType = require("file-type");
|
||||
const execPromise = promisify(require("child_process").exec);
|
||||
const servers = require("../servers.json").image;
|
||||
|
||||
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
|
||||
|
||||
exports.run = async (object, fromAPI = false) => {
|
||||
if (process.env.API === "true" && !fromAPI) {
|
||||
const currentServer = servers[Math.floor(Math.random() * servers.length)];
|
||||
const req = await fetch(`${currentServer}/run`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(object),
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
const json = await req.json();
|
||||
if (json.status === "nogif") return {
|
||||
buffer: "nogif",
|
||||
type: null
|
||||
};
|
||||
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;
|
||||
} else {
|
||||
let type;
|
||||
if (!fromAPI && object.path) {
|
||||
const newType = (object.type ? object.type : await this.getType(object.path));
|
||||
type = newType ? newType.split("/")[1] : "png";
|
||||
if (type !== "gif" && object.onlyGIF) return {
|
||||
buffer: "nogif",
|
||||
type: null
|
||||
const getFormat = (buffer, delimiter) => {
|
||||
for (var i = 0; i < buffer.length ; i++) {
|
||||
if (String.fromCharCode(buffer[i]) === delimiter) {
|
||||
return {
|
||||
buffer: buffer.slice(0, i),
|
||||
dataStart: i
|
||||
};
|
||||
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];
|
||||
}
|
||||
const data = await promisify(magick[object.cmd])(object);
|
||||
return fromAPI ? data : {
|
||||
buffer: data,
|
||||
type: type
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
exports.run = (object, fromAPI = false) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
if (process.env.API === "true" && !fromAPI) {
|
||||
const currentServer = servers[Math.floor(Math.random() * servers.length)];
|
||||
const socket = dgram.createSocket("udp4");
|
||||
const data = Buffer.concat([Buffer.from([0x1]), Buffer.from(JSON.stringify(object))]);
|
||||
|
||||
//let jobID;
|
||||
socket.on("message", (msg) => {
|
||||
const opcode = msg.readUint8(0);
|
||||
const req = msg.slice(1, msg.length);
|
||||
if (opcode === 0x0) {
|
||||
//jobID = req;
|
||||
//console.log(`Our job UUID is: ${jobID}`);
|
||||
} else if (opcode === 0x1) {
|
||||
//console.log(`Job ${jobID} is finished!`);
|
||||
const client = net.createConnection(req.toString(), currentServer);
|
||||
const array = [];
|
||||
client.on("data", (rawData) => {
|
||||
array.push(rawData);
|
||||
if (rawData.length !== 32 * 1024) {
|
||||
client.end();
|
||||
}
|
||||
});
|
||||
client.once("end", () => {
|
||||
const data = Buffer.concat(array);
|
||||
const format = getFormat(data, "\n");
|
||||
const payload = {
|
||||
buffer: data.slice(format.dataStart + 1),
|
||||
type: format.buffer.toString().split("/")[1]
|
||||
};
|
||||
//console.log(payload);
|
||||
socket.close();
|
||||
resolve(payload);
|
||||
});
|
||||
} else if (opcode === 0x2) {
|
||||
reject(req);
|
||||
}
|
||||
});
|
||||
|
||||
socket.send(data, 8080, currentServer, (err) => {
|
||||
if (err) reject(err);
|
||||
});
|
||||
} else {
|
||||
let type;
|
||||
if (!fromAPI && object.path) {
|
||||
const newType = (object.type ? object.type : await this.getType(object.path));
|
||||
type = newType ? newType.split("/")[1] : "png";
|
||||
if (type !== "gif" && object.onlyGIF) resolve({
|
||||
buffer: "nogif",
|
||||
type: null
|
||||
});
|
||||
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];
|
||||
}
|
||||
const data = await promisify(magick[object.cmd])(object);
|
||||
resolve(fromAPI ? data : {
|
||||
buffer: data,
|
||||
type: type
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
exports.check = (cmd) => {
|
||||
return magick[cmd] ? true : false;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue