From 631cdc5ef6a401351964f23a570ce4f147c614ed Mon Sep 17 00:00:00 2001 From: TheEssem Date: Mon, 31 Aug 2020 17:15:34 -0500 Subject: [PATCH] Initial API work --- api/index.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 4 ++-- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 api/index.js diff --git a/api/index.js b/api/index.js new file mode 100644 index 0000000..76ef1a1 --- /dev/null +++ b/api/index.js @@ -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}.`); +}); \ No newline at end of file diff --git a/package.json b/package.json index 386c12f..868b78b 100644 --- a/package.json +++ b/package.json @@ -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" } }