-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
196 lines (171 loc) · 6.41 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const express = require("express");
const compression = require("compression");
const path = require("path");
const puppeteer = require("puppeteer");
const app = express();
const {
getTheme,
getFaviconTheme,
getBadge,
getUserData,
getMonkeyTypeThemesData,
getMonkeyTypeBadgesData,
} = require("./public/script/monkeytypeData");
const { getOGSvg, getSvg } = require("./public/script/generateSvg");
require("dotenv").config();
app.use(compression());
app.use(express.static("public"));
app.use("/styles", express.static("dist"));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "public", "views"));
app.use("/node_modules", express.static(path.join(__dirname, "node_modules")));
app.get("/", (req, res) => {
const data = {
domain: process.env.DOMAIN,
};
res.render("index", { data });
});
app.get("/mr-command/theme", async (req, res) => {
const themesData = await getMonkeyTypeThemesData();
res.set("Content-Type", "application/json");
res.send(themesData);
});
app.get("/mr-command/badge", async (req, res) => {
const badgesData = await getMonkeyTypeBadgesData();
res.set("Content-Type", "application/json");
res.send(badgesData);
});
app.get("/mr-command/favicon", async (req, res) => {
const faviconData = getFaviconTheme();
res.render("favicon", { faviconData });
});
app.get("/mr-command/logo", async (req, res) => {
const faviconData = getFaviconTheme();
res.render("logo", { faviconData });
});
app.get("/sitemap.xml", (req, res) => {
res.sendFile(path.join(__dirname, "public/assets", "sitemap.xml"));
});
app.get("/robots.txt", (req, res) => {
res.sendFile(path.join(__dirname, "public/assets", "robots.txt"));
});
app.get(
["/og-image/:userId/:themeName", "/og-image/:userId"],
async (req, res) => {
const userId = req.params.userId;
const themeName = req.params.themeName;
req.query.lbpb == "true" ? (leaderBoards = personalBests = true) : null;
const userData = await getUserData(userId);
const theme = getTheme(themeName);
if (userData === undefined || userData.name === undefined) {
const svg = await getOGSvg(null, theme, null);
res.set("Content-Type", "image/svg+xml");
res.send(svg);
return;
}
let badge = null;
if (userData.inventory !== null && userData.inventory !== undefined) {
if (userData.inventory.badges.length !== 0) {
badge = getBadge(userData.inventory.badges[0].id);
for (let i = 0; i < userData.inventory.badges.length; i++) {
if (userData.inventory.badges[i].selected === true) {
badge = getBadge(userData.inventory.badges[i].id);
break;
}
}
}
}
const ogSvg = await getOGSvg(userData, theme, badge);
try {
const browser = await puppeteer.launch({
args: ["--no-sandbox", "--disable-setuid-sandbox"],
});
const page = await browser.newPage();
await page.setContent(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG to PNG</title>
</head>
<body>
<div id="svg-container">${ogSvg}</div>
</body>
</html>
`);
const svgElement = await page.$("#svg-container svg");
// Get bounding box of the SVG element
const boundingBox = await svgElement.boundingBox();
if (!boundingBox) {
throw new Error(
"Could not get bounding box of the SVG element",
);
}
// Set viewport to the size of the SVG
await page.setViewport({
width: Math.ceil(boundingBox.width),
height: Math.ceil(boundingBox.height),
deviceScaleFactor: 4,
});
const pngBuffer = await svgElement.screenshot({ type: "png" });
await browser.close();
res.setHeader("Content-Type", "image/png");
res.send(pngBuffer);
} catch (error) {
console.error("Error converting SVG to PNG:", error);
res.status(500).send("Error converting SVG to PNG");
}
},
);
app.get(["/:userId/:themeName", "/:userId"], async (req, res) => {
const data = {
domain: process.env.DOMAIN,
userId: req.params.userId,
theme: getTheme(
req.params.themeName ? req.params.themeName : "serika_dark",
),
userData: await getUserData(req.params.userId),
svgUrl: `${process.env.DOMAIN}/generate-svg/${req.params.userId}/${req.params.themeName}?lbpb=true`,
};
res.render("user", { data });
});
app.get("/generate-svg/:userId/:themeName", async (req, res) => {
const userId = req.params.userId;
const themeName = req.params.themeName;
let leaderBoards = req.query.lb == "true" ? true : false;
let personalBests = req.query.pb == "true" ? true : false;
req.query.lbpb == "true" ? (leaderBoards = personalBests = true) : null;
const userData = await getUserData(userId);
const theme = getTheme(themeName);
if (userData === undefined || userData.name === undefined) {
const svg = await getSvg(null, theme, null, false, false);
res.set("Content-Type", "image/svg+xml");
res.send(svg);
return;
}
let badge = null;
if (userData.inventory !== null && userData.inventory !== undefined) {
if (userData.inventory.badges.length !== 0) {
badge = getBadge(userData.inventory.badges[0].id);
for (let i = 0; i < userData.inventory.badges.length; i++) {
if (userData.inventory.badges[i].selected === true) {
badge = getBadge(userData.inventory.badges[i].id);
break;
}
}
}
}
const svg = await getSvg(
userData,
theme,
badge,
leaderBoards,
personalBests,
);
res.set("Content-Type", "image/svg+xml");
res.send(svg);
});
app.listen(process.env.PORT || 3000, async () => {
console.log("Server started on port " + (process.env.PORT || 3000));
});