2020-08-31 22:15:34 +00:00
|
|
|
require("dotenv").config();
|
|
|
|
const magick = require("../utils/image.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");
|
|
|
|
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) {
|
2020-09-02 02:52:12 +00:00
|
|
|
cb(null, Date.now() + path.extname(file.originalname));
|
2020-08-31 22:15:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const upload = multer({ storage: storage });
|
|
|
|
const app = express();
|
2020-09-02 02:52:12 +00:00
|
|
|
const port = process.env.API_PORT || 3000;
|
2020-08-31 22:15:34 +00:00
|
|
|
|
|
|
|
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
|
|
|
|
|
|
|
|
app.get("/", (req, res) => {
|
2020-09-01 22:10:19 +00:00
|
|
|
res.send(`esmBot v${version}${process.env.NODE_ENV === "development" ? "-dev" : ""}`);
|
2020-08-31 22:15:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/:method", upload.single("image"), async (req, res, next) => {
|
|
|
|
const type = req.file.mimetype === "video/mp4" ? "image/gif" : req.file.mimetype;
|
|
|
|
if (!formats.includes(type)) {
|
|
|
|
return res.sendStatus(400);
|
|
|
|
}
|
|
|
|
const object = {
|
|
|
|
cmd: req.params.method,
|
|
|
|
path: req.file.path,
|
|
|
|
type: type.split("/")[1],
|
|
|
|
delay: parseInt(req.params.delay)
|
|
|
|
};
|
|
|
|
for (const param of Object.keys(req.query)) {
|
|
|
|
if (param === "delay") continue;
|
|
|
|
object[param] = req.query[param];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-09-02 02:52:12 +00:00
|
|
|
const data = await magick(object, true);
|
|
|
|
res.send(data);
|
2020-08-31 22:15:34 +00:00
|
|
|
} catch (e) {
|
|
|
|
next(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Started image API on port ${port}.`);
|
|
|
|
});
|