2020-08-31 22:15:34 +00:00
|
|
|
require("dotenv").config();
|
|
|
|
const magick = require("../utils/image.js");
|
2020-11-05 21:40:18 +00:00
|
|
|
const Job = require("./job.js");
|
2020-09-01 22:10:19 +00:00
|
|
|
const { version } = require("../package.json");
|
2020-08-31 22:15:34 +00:00
|
|
|
const express = require("express");
|
2020-10-18 21:53:35 +00:00
|
|
|
const execPromise = require("util").promisify(require("child_process").exec);
|
2020-08-31 22:15:34 +00:00
|
|
|
const app = express();
|
2020-09-03 03:52:01 +00:00
|
|
|
const port = 3000;
|
2020-08-31 22:15:34 +00:00
|
|
|
|
2020-11-05 21:40:18 +00:00
|
|
|
const jobs = new Map();
|
|
|
|
|
2020-08-31 22:15:34 +00:00
|
|
|
app.get("/", (req, res) => {
|
2020-09-03 03:52:01 +00:00
|
|
|
res.send(`esmBot v${version}`);
|
2020-08-31 22:15:34 +00:00
|
|
|
});
|
|
|
|
|
2020-10-18 21:53:35 +00:00
|
|
|
app.post("/run", express.json(), async (req, res, next) => {
|
|
|
|
const object = req.body;
|
2020-10-06 19:48:22 +00:00
|
|
|
if (!magick.check(object.cmd)) return res.sendStatus(400);
|
|
|
|
|
2020-08-31 22:15:34 +00:00
|
|
|
try {
|
2020-10-18 21:53:35 +00:00
|
|
|
let type;
|
|
|
|
if (object.path) {
|
2020-10-20 01:24:53 +00:00
|
|
|
type = object.type ? object.type : await magick.getType(object.path);
|
2020-10-18 21:53:35 +00:00
|
|
|
if (!type) {
|
|
|
|
return res.sendStatus(400);
|
|
|
|
}
|
|
|
|
object.type = type.split("/")[1];
|
2020-11-05 21:40:18 +00:00
|
|
|
if (object.type !== "gif" && object.onlyGIF) return res.send({
|
|
|
|
status: "nogif"
|
|
|
|
});
|
|
|
|
object.delay = object.delay ? object.delay : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = Math.random().toString(36).substring(2, 15);
|
|
|
|
if (object.type === "gif" && !object.delay) {
|
2020-10-18 21:53:35 +00:00
|
|
|
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-11-05 21:40:18 +00:00
|
|
|
const job = new Job(object);
|
|
|
|
jobs.set(id, job);
|
|
|
|
res.send({
|
|
|
|
id: id,
|
|
|
|
status: "queued"
|
|
|
|
});
|
|
|
|
job.run();
|
2020-08-31 22:15:34 +00:00
|
|
|
} catch (e) {
|
|
|
|
next(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-05 21:40:18 +00:00
|
|
|
app.get("/status", (req, res) => {
|
|
|
|
if (!req.query.id) return res.sendStatus(400);
|
|
|
|
const job = jobs.get(req.query.id);
|
|
|
|
if (!job) return res.sendStatus(400);
|
|
|
|
const timeout = setTimeout(function() {
|
|
|
|
job.removeAllListeners();
|
|
|
|
return res.send({
|
|
|
|
id: req.query.id,
|
|
|
|
status: job.status
|
|
|
|
});
|
|
|
|
}, 10000);
|
|
|
|
job.once("data", function() {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
res.send({
|
|
|
|
id: req.query.id,
|
|
|
|
status: job.status
|
|
|
|
});
|
|
|
|
//jobs.delete(req.query.id);
|
|
|
|
});
|
|
|
|
job.on("error", function(e) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
res.status(500);
|
|
|
|
res.send({
|
|
|
|
id: req.query.id,
|
|
|
|
status: job.status,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
jobs.delete(req.query.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/image", (req, res) => {
|
|
|
|
if (!req.query.id) return res.sendStatus(400);
|
|
|
|
const job = jobs.get(req.query.id);
|
|
|
|
if (!job) return res.sendStatus(400);
|
|
|
|
if (!job.data) return res.sendStatus(400);
|
|
|
|
if (job.error) return;
|
|
|
|
jobs.delete(req.query.id);
|
|
|
|
res.contentType(job.options.type ? job.options.type : "png");
|
|
|
|
return res.send(job.data);
|
|
|
|
});
|
|
|
|
|
2020-08-31 22:15:34 +00:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Started image API on port ${port}.`);
|
|
|
|
});
|