Heavy work on the image detection code, fixed mention prefixes not being properly registered

This commit is contained in:
TheEssem 2020-10-18 16:53:35 -05:00
parent 6cd9878632
commit cf5c649384
57 changed files with 240 additions and 443 deletions

View file

@ -2,51 +2,32 @@ require("dotenv").config();
const magick = require("../utils/image.js");
const { version } = require("../package.json");
const express = require("express");
const multer = require("multer");
const path = require("path");
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, "/tmp/");
},
filename: function(req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
const execPromise = require("util").promisify(require("child_process").exec);
const app = express();
const port = 3000;
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
function isValidJSON(json) {
try {
JSON.parse(json);
} catch (e) {
return false;
}
return true;
}
app.get("/", (req, res) => {
res.send(`esmBot v${version}`);
});
app.post("/run", upload.single("image"), async (req, res, next) => {
const type = req.file ? (req.file.mimetype === "video/mp4" ? "image/gif" : req.file.mimetype) : "image/png";
if (!formats.includes(type)) {
return res.sendStatus(400);
}
if (!isValidJSON(req.body.data)) return res.sendStatus(400);
const object = JSON.parse(req.body.data);
app.post("/run", express.json(), async (req, res, next) => {
const object = req.body;
if (!magick.check(object.cmd)) return res.sendStatus(400);
object.path = req.file ? req.file.path : null;
object.type = type.split("/")[1];
try {
let type;
if (object.path) {
type = await magick.getType(object.path);
if (!type) {
return res.sendStatus(400);
}
object.type = type.split("/")[1];
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);
res.contentType(type ? type : "png");
res.send(data);
} catch (e) {
next(e);