-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (51 loc) · 1.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//init
const express = require("express");
const app = express();
//middleware
const sharp = require("sharp");
const cors = require("cors");
const multer = require("multer");
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 1 * 1024 * 1024,
},
fileFilter: fileFilter,
});
function fileFilter(req, file, cb) {
console.log(`size: ${req.headers["content-length"]}`);
if (req.headers["content-length"] > upload.limits.fileSize) {
console.log("bad file size");
cb(new Error("bad file size"), false);
} else {
console.log("ok file size");
cb(null, true);
}
}
const glitch = require("./src/scatter.js");
app.use(cors());
//static
app.use(express.static(__dirname + "/public"));
//routes
app.post("/scatter", upload.single("image"), async (req, res, next) => {
try {
//parsear si el query viene en el body o en la url
const sendQuery =
req.body.query == undefined ? req.query : JSON.parse(req.body.query);
const sendBuffer = req.file.buffer;
const process = await glitch(sendBuffer, sendQuery);
const dataUrl = `data:image/webp;base64,${process.toString("base64")}`;
res.send(dataUrl);
} catch (err) {
next(err);
}
});
app.get("/documentation", (req, res) => {
res.sendFile(__dirname + "/public/documentation.html");
});
//server
app.listen(3000, () => {
console.log("Escuchando en 3000...");
});
//export
module.exports = app;