Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/resources/scripts/helpers.ts
7458 views
1
export const randomInt = (low: number, high: number) => Math.floor(Math.random() * (high - low) + low);
2
3
export const cleanDirectoryPath = (path: string) => path.replace(/(\/(\/*))|(^$)/g, '/');
4
5
export function fileBitsToString(mode: string, directory: boolean): string {
6
const m = parseInt(mode, 8);
7
8
let buf = '';
9
'dalTLDpSugct?'.split('').forEach((c, i) => {
10
if ((m & (1 << (32 - 1 - i))) !== 0) {
11
buf = buf + c;
12
}
13
});
14
15
if (buf.length === 0) {
16
// If the file is directory, make sure it has the directory flag.
17
if (directory) {
18
buf = 'd';
19
} else {
20
buf = '-';
21
}
22
}
23
24
'rwxrwxrwx'.split('').forEach((c, i) => {
25
if ((m & (1 << (9 - 1 - i))) !== 0) {
26
buf = buf + c;
27
} else {
28
buf = buf + '-';
29
}
30
});
31
32
return buf;
33
}
34
35
/**
36
* URL-encodes the segments of a path.
37
* This allows to use the path as part of a URL while preserving the slashes.
38
* @param path the path to encode
39
*/
40
export function encodePathSegments(path: string): string {
41
return path
42
.split('/')
43
.map((s) => encodeURIComponent(s))
44
.join('/');
45
}
46
47
export function hashToPath(hash: string): string {
48
return hash.length > 0 ? decodeURIComponent(hash.substr(1)) : '/';
49
}
50
51