-
Notifications
You must be signed in to change notification settings - Fork 0
/
localUtil.ts
52 lines (48 loc) · 1.73 KB
/
localUtil.ts
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
import { BProperties } from "./classes/BProperties";
import BPermission from "./classes/BPermissions";
import { parse } from 'dotenv';
import { readFile, createWriteStream } from 'fs-extra';
import { promisify } from 'util';
import Player from "./classes/Player";
import request = require("request");
const readFileAsync = promisify(readFile);
export async function propertiesFileToBProperties(filePath): Promise<BProperties> {
return new BProperties(parse(await readFileAsync(filePath) as Buffer));
}
export async function permissionsFileToBPermissions(filePath): Promise<BPermission[]> {
let filedata;
try {
filedata = (await readFileAsync(filePath)).toString();
} catch (e) {
return [];
}
let json = JSON.parse(filedata);
if(!Array.isArray(json)) json = [];
return json.map(e => ({
player: Player.xuidToPlayer.get(e.xuid) ?? { xuid: e.xuid },
permission: e.permission
}));
}
export async function wgetToFile(url, filepath, progressCallback?) {
return new Promise<void>(resolve => {
const file = createWriteStream(filepath);
const req = request.get(url);
req.pipe(file).on('error', (err) => {
console.error(`Error getting url ${url}: ${err}`);
});
let length;
let currentLength = 0;
req.on('response', (data) => {
length = data.headers['content-length'];
});
req.on('data', (chunk) => {
currentLength += chunk.length;
if(progressCallback) progressCallback(currentLength * 100 / length);
// console.log("Data: " + chunk.length / length);
});
file.on('finish', async () => {
await file.close();
resolve();
});
});
}