-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate.js
52 lines (46 loc) · 1.71 KB
/
generate.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
/**
* Created by Hisune on 2016/7/28.
* User: [email protected]
*
* 生成一个临时url
* node generate pug_name [pug or ctrl] [link number] [expire sec]
*/
'use strict';
if(process.argv.length < 3){
console.log('plz run as "node generate ppts_name [url number]"');
process.exit(1)
}
const fs = require('fs'),
config = require('./config.js');
process.argv[2] = process.argv[2].replace(/\W/gi, ''); // 过滤
if(!fs.existsSync(__dirname + '/ppts/' + process.argv[2] + '.pug')){
console.log('pug file "' + process.argv[2] + '" not exists');
process.exit(1)
}
let count = 0, generateName = 'pug', generateExpire = config.generate_default_timeout, now = Date.now() / 1000 | 0;
if(process.argv[3] && (process.argv[3] == 'pug' || process.argv[3] == 'ctrl'))
generateName = process.argv[3];
if(process.argv[4])
count = parseInt(process.argv[4]);
if(process.argv[5])
generateExpire = parseInt(process.argv[5]);
if(count < 1)
count = config.generate_default_num;
if(generateExpire != 0)
generateExpire = generateExpire + now;
let generateDir = __dirname + '/generate/' + generateName + '/';
// 过期检测
let generates = fs.readdirSync(generateDir);
generates.forEach((item) => {
let file = generateDir + item, content = fs.readFileSync(file).toString().split(',');
if(now > content[1] && content[1] != 0)
fs.unlinkSync(file);
});
console.log('generate ' + count + ' ' + generateName + ' key:');
for(let i = 0; i < count; i++){
require('crypto').randomBytes(12, (err, buffer) => {
let token = buffer.toString('hex');
fs.writeFileSync(generateDir + token, process.argv[2] + ',' + generateExpire);
console.log(config.url + 'ppt?' + generateName + '=' + token);
});
}