-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (42 loc) · 1.56 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
const express = require("express"),
fs = require("fs");
require("dotenv").config();
const app = express();
let ferrets = [];
let files = fs.readdirSync('./ferret-images/');
for (let i = 0; i < files.length; i++) {
if (files[i].endsWith(".jpg") || files[i].endsWith(".png")) {
ferrets.push(files[i]);
}
}
console.log("Loaded ferrets:", ferrets.length);
const BASEURL = process.env.BASE_URL;
app.use(express.static(__dirname + '/static'));
app.get('/json', (req, res) => {
res.header("Content-Type", "application/json");
res.end(JSON.stringify({ferret: '/ferret-image/' + getRandomFerret() }));
});
app.get('/image', (req, res) => {
res.sendFile(__dirname + '/ferret-images/' + getRandomFerret());
});
app.get('/ferret-image/:uuid', (req, res) => {
const uuid = req.params.uuid;
if (!/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.(jpg|png)/gm.test(uuid)) {
res.header("Content-Type", "application/json");
res.status(403).send(JSON.stringify({ error: "Forbidden" }));
return;
}
const file = __dirname + '/ferret-images/' + uuid;
if (!fs.existsSync(file)) {
res.header("Content-Type", "application/json");
res.status(404).send(JSON.stringify({ error: "Not found" }));
return;
}
res.sendFile(__dirname + '/ferret-images/' + uuid);
});
app.listen(process.env.PORT || 8321, () => {
console.log("Started webserver");
});
function getRandomFerret() {
return ferrets[Math.floor(Math.random()*ferrets.length)];
}