-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
88 lines (84 loc) · 3.17 KB
/
main.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
'use strict';
const nmmes = require('nmmes-backend');
const Path = require('path');
const fs = require('fs-extra');
const crc = require('crc');
module.exports = class Checksum extends nmmes.Module {
constructor(args, logger) {
super(require('./package.json'), logger);
this.options = Object.assign(nmmes.Module.defaults(Checksum), args);
}
async executable(map) {
let _self = this;
const options = this.options;
let parsed = Path.parse(this.video.output.path);
parsed.base = options.format;
let output = Path.format(parsed);
let stream = fs.createReadStream(this.video.output.path);
this.logger.trace(`Calculating ${options.algo}...`);
parsed.hashfmt = options.algo;
parsed.hash = await new Promise((resolve, reject) => {
switch (options.algo) {
case 'crc32c':
{
const crc32c = require('sse4_crc32');
let digest = new crc32c.CRC32();
stream
.on('error', err => {
return reject(err);
}).on('data', chunk => {
digest.update(chunk);
}).on('end', async () => {
return resolve(digest.crc().toString(16).toUpperCase());
});
break;
}
case 'crc1':
case 'crc8':
case 'crc16':
case 'crc24':
case 'crc32':
case 'crcjam':
{
let value = '';
stream
.on('error', err => {
return reject(err);
}).on('data', chunk => {
value = crc[options.algo](chunk, value);
}).on('end', async () => {
return resolve(value.toString(16).toUpperCase());
});
break;
}
default:
return reject(new Error(`Invalid checksum algorithm`));
}
});
this.logger.debug(`${options.algo} calculated to: ${parsed.hash}`);
for (let [key, value] of Object.entries(parsed)) {
output = output.replace(`{${key}}`, value);
}
await fsmove(this.video.output.path, output);
this.video.output = Path.parse(output);
this.video.output.path = output;
return {};
};
static options() {
return {
'algo': {
default: 'crc32c',
describe: 'Algorithm to hash with.',
choices: ['crc32c', 'crc1', 'crc8', 'crc16', 'crc24', 'crc32', 'crcjam'],
type: 'string',
group: 'Advanced:'
},
'format': {
default: '{name}-[{hashfmt}-{hash}]{ext}',
describe: 'The file name format for the output file.',
type: 'string',
group: 'Advanced:'
}
};
}
}