-
Notifications
You must be signed in to change notification settings - Fork 1
/
checksum.js
71 lines (65 loc) · 2.05 KB
/
checksum.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
const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
const filesize = require('filesize');
const ProgressBar = require('progress');
const public_dir = 'public';
try {
fs.statSync(public_dir);
} catch (e) {
fs.mkdirSync(public_dir);
}
let data = {};
const result_path = path.join('.', 'checksum.json');
if (fs.existsSync(result_path)) {
data = JSON.parse(fs.readFileSync(result_path, 'utf8'));
}
async function calculate(dir) {
const dir_path = path.join('./public', dir);
const results = fs.readdirSync(dir_path);
for (let i = 0; i < results.length; i++) {
const name = results[i];
const absolute_path = path.join(dir_path, name);
const file_path = path.join(dir, name);
const stats = fs.statSync(absolute_path);
if (stats.isDirectory()) {
await calculate(file_path);
} else if (stats.isFile()) {
if (data.hasOwnProperty(file_path) && data[file_path].size === stats.size) {
console.log(file_path, 'unchanged');
} else {
await new Promise((resolve, reject) => {
const bar = new ProgressBar(file_path + ' hashing [:bar] :rate/bps :percent :etas', {
complete: '=',
incomplete: ' ',
width: 20,
total: stats.size
});
const rs = fs.createReadStream(path.join('./public', file_path));
const md5 = crypto.createHash('md5');
const sha1 = crypto.createHash('sha1');
rs.on('data', data => {
md5.update(data);
sha1.update(data);
bar.tick(data.length);
});
rs.on('end', function () {
data[file_path] = {
size: stats.size,
filesize: filesize(stats.size),
md5: md5.digest('hex'),
sha1: sha1.digest('hex'),
};
resolve();
});
});
}
console.log(data[file_path]);
console.log();
}
}
}
setTimeout(async () => {
await calculate('/');
fs.writeFileSync('checksum.json', JSON.stringify(data, null, 4));
}, 0);