This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
functions.js
88 lines (73 loc) · 2.41 KB
/
functions.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
/* eslint-disable camelcase */
module.exports = {
async doRedirect(req, res, redirect, extra) {
if (typeof redirect === "string") return res.redirect(redirect);
if (redirect.error || redirect.success) {
if (!req.session.variables) req.session.variables = {};
req.session.variables.error = redirect.error;
req.session.variables.success = redirect.success;
}
return res.redirect(redirect.path + (extra || ""));
},
async parseNumber(number) {
switch (typeof number) {
case "string":
if (number.length === 0) return null;
number = Number(number);
if (isNaN(number)) return undefined;
return Math.round(number);
case "number":
return Math.round(number);
default:
return undefined;
}
},
async getUserResource(req) {
let pkg = req.session.data.dbinfo.package || process.env.packages.default;
if (!process.env.packages.list[pkg]) pkg = process.env.packages.default;
const packageinfo = process.env.packages.list[pkg];
packageinfo.id = pkg;
req.session.data.dbinfo = await process.db.fetchAccountPterodactylID(
req.session.data.panelinfo.id
);
const extra = {
memory: req.session.data.dbinfo?.memory || 0,
disk: req.session.data.dbinfo?.disk || 0,
cpu: req.session.data.dbinfo?.cpu || 0,
servers: req.session.data.dbinfo?.servers || 0,
};
const total = {
memory: packageinfo.memory + extra.memory,
disk: packageinfo.disk + extra.disk,
cpu: packageinfo.cpu + extra.cpu,
servers: packageinfo.servers + extra.servers,
};
const user_servers = req.session.data.panelinfo.relationships.servers.data;
const current = {
memory: 0,
disk: 0,
cpu: 0,
servers: user_servers.length,
};
for (const server of user_servers) {
current.memory += server.attributes.limits.memory;
current.disk += server.attributes.limits.disk;
current.cpu += server.attributes.limits.cpu;
}
return {
packageinfo: packageinfo,
extra: extra,
total: total,
current: current,
};
},
makeid(length) {
var result = "";
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
},
};