Simplified and fixed image API, updated packages

This commit is contained in:
TheEssem 2020-10-06 14:48:22 -05:00
parent f1404a8393
commit ac4e1f5dd8
55 changed files with 135 additions and 161 deletions

View file

@ -18,50 +18,36 @@ 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("/:method", upload.single("image"), async (req, res, next) => {
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);
}
const object = {
cmd: req.params.method,
path: req.file ? req.file.path : null,
type: type.split("/")[1],
delay: req.query.delay ? parseInt(req.query.delay) : 0
};
for (const param of Object.keys(req.query)) {
if (param === "delay") continue;
switch (param) {
case "sharp":
case "flop":
case "loop":
case "vertical":
case "first":
case "stretch":
case "wide":
case "soos":
case "slow":
case "resize":
case "append":
case "mc":
if (req.query[param] === "true") {
object[param] = true;
} else {
object[param] = false;
}
break;
default:
object[param] = req.query[param];
break;
}
}
if (!isValidJSON(req.body.data)) return res.sendStatus(400);
const object = JSON.parse(req.body.data);
if (!magick.check(object.cmd)) return res.sendStatus(400);
object.path = req.file ? req.file.path : null;
object.type = type.split("/")[1];
try {
const data = await magick(object, true);
const data = await magick.run(object, true);
res.contentType(type);
res.send(data);
} catch (e) {
next(e);