generated from Alex-star225/GoAnimate-2012-Online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileUtil.js
134 lines (133 loc) · 3.18 KB
/
fileUtil.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
const folder = process.env.SAVED_FOLDER;
const nodezip = require('node-zip');
const fs = require('fs');
module.exports = {
/**
*
* @param {number} n
* @param {number} l
* @returns {string}
*/
padZero(n, l = process.env.FILE_NUM_WIDTH) {
return ('' + n).padStart(l, '0');
},
/**
*
* @param {string} temp
* @param {string} info
* @returns {string}
*/
fillTemplate(temp, info) {
return temp.replace(/%s/g, info);
},
/**
* @param {string} s
* @param {string} suf
* @param {number} l
* @returns {string}
*/
getNextFile(s, suf = '.xml', l = 7) {
const regex = new RegExp(`${s}[0-9]*${suf}$`);
const dir = fs.readdirSync(folder).filter(v => v && regex.test(v));
return `${folder}/${s}${this.padZero(dir.length, l)}${suf}`;
},
/**
* @param {string} s
* @param {string} suf
* @param {number} l
* @returns {number}
*/
getNextFileId(s, suf = '.xml', l = 7) {
const indicies = this.getValidFileIndicies(s, suf, l);
return indicies.length ? indicies[indicies.length - 1] + 1 : 0;
},
/**
* @param {string} s
* @param {string} suf
* @param {number} l
* @returns {number}
*/
fillNextFileId(s, suf = '.xml', l = 7) {
const id = this.getNextFileId(s, suf);
const fn = this.getFileIndex(s, suf, id, l);
fs.writeFileSync(fn, '');
return id;
},
/**
* @param {string} s
* @param {string} suf
* @param {number} n
* @param {number} l
* @returns {string}
*/
getFileIndex(s, suf = '.xml', n, l = 7) {
return this.getFileString(s, suf, this.padZero(n, l));
},
/**
* @param {string} s
* @param {string} suf
* @param {string} name
* @returns {string}
*/
getFileString(s, suf = '.xml', name) {
return `${folder}/${s}${name}${suf}`;
},
/**
* @param {string} s
* @param {string} suf
* @param {number} l
* @returns {number[]}
*/
getValidFileIndicies(s, suf = '.xml', l = 7) {
const regex = new RegExp(`${s}[0-9]{${l}}${suf}$`);
return fs.readdirSync(folder).
filter(v => v && regex.test(v)).
map(v => Number.parseInt(v.substr(s.length, l)));
},
/**
* @param {string} s
* @param {string} suf
* @param {number} l
* @returns {string[]}
*/
getValidFileNames(s, suf = '.xml', l = 7) {
const regex = new RegExp(`${s}[0-9]{${l}}${suf}$`);
return fs.readdirSync(folder).
filter(v => v && regex.test(v)).
map(v => `${folder}/${v}`);
},
/**
* @param {string} s
* @param {string} suf
* @param {number} l
* @returns {string[]}
*/
getLastFileIndex(s, suf = '.xml', l = 7) {
const regex = new RegExp(`${s}[0-9]{${l}}${suf}$`);
const list = fs.readdirSync(folder).filter(v => v && regex.test(v));
return list.length ? Number.parseInt(list.pop().substr(s.length, l)) : -1;
},
/**
*
* @param {string} fileName
* @param {string} zipName
*/
zippy(fileName, zipName) {
if (!fs.existsSync(fileName)) return Promise.reject();
const buffer = fs.readFileSync(fileName);
const zip = nodezip.create();
this.addToZip(zip, zipName, buffer);
return zip.zip();
},
/**
*
* @param {nodezip.ZipFile} zip
* @param {string} zipName
* @param {string} buffer
*/
addToZip(zip, zipName, buffer) {
zip.add(zipName, buffer);
if (zip[zipName].crc32 < 0)
zip[zipName].crc32 += 4294967296;
},
}