Caption now supports more unicode characters, more api work, fixed multiple bugs
This commit is contained in:
parent
a03d3a5e79
commit
0600cf230f
11 changed files with 174 additions and 50 deletions
65
api/index.js
65
api/index.js
|
@ -1,11 +1,14 @@
|
|||
require("dotenv").config();
|
||||
const magick = require("../utils/image.js");
|
||||
const Job = require("./job.js");
|
||||
const { version } = require("../package.json");
|
||||
const express = require("express");
|
||||
const execPromise = require("util").promisify(require("child_process").exec);
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
const jobs = new Map();
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
res.send(`esmBot v${version}`);
|
||||
});
|
||||
|
@ -22,19 +25,71 @@ app.post("/run", express.json(), async (req, res, next) => {
|
|||
return res.sendStatus(400);
|
||||
}
|
||||
object.type = type.split("/")[1];
|
||||
if (object.type !== "gif" && object.onlyGIF) return res.send("nogif");
|
||||
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) {
|
||||
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 magick.run(object, true);
|
||||
res.contentType(type ? type : "png");
|
||||
res.send(data);
|
||||
const job = new Job(object);
|
||||
jobs.set(id, job);
|
||||
res.send({
|
||||
id: id,
|
||||
status: "queued"
|
||||
});
|
||||
job.run();
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Started image API on port ${port}.`);
|
||||
});
|
28
api/job.js
Normal file
28
api/job.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
const { EventEmitter } = require("events");
|
||||
const magick = require("../utils/image.js");
|
||||
|
||||
class Job extends EventEmitter {
|
||||
constructor(options) {
|
||||
super();
|
||||
this.options = options;
|
||||
this.status = "queued";
|
||||
this.data = null;
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
run() {
|
||||
this.status = "processing";
|
||||
magick.run(this.options, true).then(data => {
|
||||
this.status = "success";
|
||||
this.data = data;
|
||||
return this.emit("data", data, this.options.type);
|
||||
}).catch(e => {
|
||||
this.status = "error";
|
||||
this.error = e;
|
||||
return this.emit("error", e);
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Job;
|
Loading…
Add table
Add a link
Reference in a new issue