forked from SkyCryptWebsite/SkyCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpotion.js
More file actions
48 lines (37 loc) · 1.11 KB
/
Copy pathpotion.js
File metadata and controls
48 lines (37 loc) · 1.11 KB
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
import cors from "cors";
import express from "express";
import * as app from "../app.js";
import * as helper from "../helper.js";
import * as renderer from "../renderer.js";
const router = express.Router();
router.use(cors());
router.all("/:type/:color", cors(), async (req, res, next) => {
try {
const { type, color } = req.params;
if (!["normal", "splash"].includes(type)) {
handleError(res, new Error("invalid armor type: " + type), 400);
return;
}
if (!/^[0-9a-fA-F]{6}$/.test(color)) {
handleError(res, new Error("invalid color: #" + color), 400);
return;
}
const file = await renderer.getPotion(type, color);
res.set("X-Cluster-ID", `${helper.getClusterId()}`);
res.setHeader("Cache-Control", `public, max-age=${app.CACHE_MAX_AGE}`);
res.contentType("image/png");
res.send(file);
} catch (e) {
next(e);
}
});
router.use((err, req, res, next) => {
handleError(res, err);
});
export function handleError(res, err, status = 500, logged = true) {
if (logged) {
console.error(err);
}
res.status(status).send(err.message);
}
export { router };