-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
157 lines (137 loc) · 3.77 KB
/
utils.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
const fs = require("fs");
const lineReader = require("line-reader");
const crypto = require("crypto");
const path = require("path");
const axios = require("axios");
const progressBar = require("progress");
function getDateTimeISOFormat(initDate) {
// get current Japan time
const newDate = new Date(
new Date().toLocaleString("en-US", { timeZone: "Asia/Tokyo" })
);
const date = initDate || newDate;
return date.toISOString().replace(/T|-|:/g, "").replace(/\..+/, "");
}
function roundFloat(num, decimalPlaces = 2) {
return +(Math.round(num + "e+" + decimalPlaces) + "e-" + decimalPlaces);
}
function downloadFromUlr(url, dest) {
return new Promise(async (resolve, reject) => {
try {
const { data, headers } = await axios({
url,
method: "GET",
responseType: "stream",
});
const totalLength = parseInt(headers["content-length"], 10);
const lengthInKB = totalLength / 1024;
const pBar = new progressBar(
`-> downloading [:bar] ${roundFloat(lengthInKB, 0)}(KB) :elapsed(s) :percent :customRateInKB(KB/s) `, // prettier-ignore
{
width: 40,
complete: "=",
incomplete: " ",
total: totalLength,
}
);
const writer = fs.createWriteStream(path.resolve(dest));
data.on("data", (chunk) =>
pBar.tick(chunk.length, { customRateInKB: chunk.length / 8 })
);
data.pipe(writer);
writer.on("finish", () => {
resolve(true);
});
} catch (err) {
reject(err);
}
});
}
function readPackagesFile(packagesFile) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(packagesFile)) {
reject(new Error("File not exist"));
}
const debs = [];
let deb = {};
lineReader.eachLine(packagesFile, (line, isLast) => {
const lineAttr = line.split(":");
if (line.length <= 1) {
debs.push(deb);
deb = {};
} else {
const attr1 = lineAttr.slice(1).join(":").trim();
if (!lineAttr[0] || !attr1) return;
deb[lineAttr[0]] = attr1;
}
if (isLast) {
debs.filter((deb) => deb.Filename);
resolve(debs);
}
});
});
}
function getFileSize(file) {
if (fs.existsSync(file)) {
const stats = fs.statSync(file);
const fileSizeInBytes = stats["size"];
return fileSizeInBytes;
}
return 0;
}
function checkFileSum(file, md5, sha1, sha256, strict = false) {
return new Promise((resolve, reject) => {
try {
if (!fs.existsSync(file)) {
resolve(false);
return;
}
const s = fs.ReadStream(file);
const md5Algo = crypto.createHash("md5");
const sha1Algo = crypto.createHash("sha1");
const sha256Algo = crypto.createHash("sha256");
s.on("data", function (d) {
md5Algo.update(d);
sha1Algo.update(d);
sha256Algo.update(d);
});
s.on("end", function () {
const md5Actual = md5Algo.digest("hex");
const sha1Actual = sha1Algo.digest("hex");
const sha256Actual = sha256Algo.digest("hex");
// strict mode: compare all type of hash
if (strict) {
if (
md5 === md5Actual &&
sha1 === sha1Actual &&
sha256 === sha256Actual
) {
resolve(true);
} else {
resolve(false);
}
} else {
if (
md5 === md5Actual ||
sha1 === sha1Actual ||
sha256 === sha256Actual
) {
resolve(true);
} else {
resolve(false);
}
}
});
} catch (err) {
reject(err);
}
});
}
module.exports = {
getDateTimeISOFormat,
roundFloat,
downloadFromUlr,
readPackagesFile,
getFileSize,
checkFileSum,
};