Initial API work

This commit is contained in:
TheEssem 2020-08-31 17:15:34 -05:00
parent f415b3bb09
commit 631cdc5ef6
2 changed files with 52 additions and 2 deletions

50
api/index.js Normal file
View file

@ -0,0 +1,50 @@
require("dotenv").config();
const magick = require("../utils/image.js");
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)); //Appending extension
}
});
const upload = multer({ storage: storage });
const app = express();
const port = 3000;
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif"];
app.get("/", (req, res) => {
res.send("Hello World!");
});
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 {
const data = await magick(object);
res.contentType(type).send(data);
} catch (e) {
next(e);
}
});
app.listen(port, () => {
console.log(`Started image API on port ${port}.`);
});

View file

@ -22,7 +22,6 @@
"@lavacord/eris": "0.0.3",
"abort-controller": "^3.0.0",
"cowsay": "^1.4.0",
"cron": "^1.8.2",
"dblapi.js": "^2.4.0",
"dotenv": "^8.2.0",
"duckduckgo-images-api": "github:benpbolton/duckduckgo-images-api",
@ -49,7 +48,8 @@
"optionalDependencies": {
"bufferutil": "^4.0.1",
"erlpack": "github:abalabahaha/erlpack",
"sodium-native": "^2.4.6",
"express": "^4.17.1",
"multer": "^1.4.2",
"zlib-sync": "^0.1.6"
}
}