This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
forked from jcheong0428/maskon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
63 lines (55 loc) · 1.79 KB
/
server.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
61
62
63
const express = require("express");
const path = require("path");
const { get } = require("request");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const viewsDir = path.join(__dirname, "views");
app.use(express.static(viewsDir));
app.use(express.static(path.join(__dirname, "./public")));
app.use(express.static(path.join(__dirname, "./images")));
app.use(express.static(path.join(__dirname, "./weights")));
app.use(express.static(path.join(__dirname, "./dist")));
app.get("/", (req, res) => res.redirect("/mask_on_profile"));
app.get("/mask_on_profile", (req, res) =>
res.sendFile(path.join(viewsDir, "maskOnProfile.html"))
);
app.get("/badge", (req, res) =>
res.sendFile(path.join(viewsDir, "badge.html"))
);
app.post("/fetch_external_image", async (req, res) => {
const { imageUrl } = req.body;
if (!imageUrl) {
return res.status(400).send("imageUrl param required");
}
try {
const externalResponse = await request(imageUrl);
res.set("content-type", externalResponse.headers["content-type"]);
return res.status(202).send(Buffer.from(externalResponse.body));
} catch (err) {
return res.status(404).send(err.toString());
}
});
var port = process.env.PORT || 3000;
app.listen(port, () => console.log("Listening on port " + port));
function request(url, returnBuffer = true, timeout = 10000) {
return new Promise(function (resolve, reject) {
const options = Object.assign(
{},
{
url,
isBuffer: true,
timeout,
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
},
},
returnBuffer ? { encoding: null } : {}
);
get(options, function (err, res) {
if (err) return reject(err);
return resolve(res);
});
});
}