helper.js
1003 Bytes
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
var fs = require('fs');
var request = require('request-promise').defaults({jar: true})
module.exports = {
isValidFileSize: function(dirname, maxsize) {
return fs.statSync(dirname)['size'] < maxsize;
},
getLengthOfContent: function(string) {
return Buffer.byteLength(string, 'utf8');
},
mkdirIfNotExist: function(dirname) {
var _dirname = dirname.split('/');
var parentdir = _dirname.slice(0, _dirname.length-2).join('/');
if (!fs.existsSync(parentdir)){
fs.mkdirSync(parentdir);
}
if (!fs.existsSync(dirname)){
fs.mkdirSync(dirname);
}
},
deleteFolderRecursive: function(path) {
var self = this;
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file, index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) {
self.deleteFolderRecursive(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
};