/
helpers.js
executable file
·121 lines (120 loc) · 3.29 KB
/
helpers.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
module.exports = {
/**
This function reads a folder and writes the contents to a json
@param req Request
@param res Response
@param dir string, Directory with files
@param Writepath string, Directory to save the JSON
@param getDir boolean, true; to write only the dir, not the files inside the dir
@param status Returns with Callback. Gives status of function
@param filetypes Regex with acceptable filetypes
@param callback Callback function
*/
getLocalFiles: function (req, res, dir, suffix, callback) {
var colors = require('colors');
var fs = require('fs');
var async = require('async');
scan(dir, suffix, callback);
function scan(dir, suffix, callback) {
fs.readdir(dir, function(err, files) {
var returnFiles = [];
async.each(files,function(file, next) {
var filePath = dir + '/' + file;
fs.stat(filePath, function(err, stat) {
if (err) {
return next(err);
}
if (stat.isDirectory()) {
scan(filePath, suffix, function(err, results) {
if (err) {
return next(err);
}
returnFiles = returnFiles.concat(results);
next();
})
}
else if (stat.isFile()) {
if (file.match(suffix)) {
returnFiles.push( {href: filePath, dir: dir, file: file} );
}
next();
}
});
}, function(err) {
callback(err, returnFiles);
});
});
};
},
/**
This funtion does an ajax call
@param url URL to call
@param callback Callback function
*/
xhrCall: function (url,callback) {
var request = require("request")
, colors = require('colors');
request({
url: url,
headers: {"Accept": "application/json"},
method: "GET"
}, function (error, response, body) {
if(!error){
callback(body);
}else{
console.log('Helper: XHR Error',error .red);
setTimeout(function(){
console.log('Helper: Waiting for retry...' .yellow)
},10000);
}
});
},
/**
This funtion removes corrupted directories.
@param req Request
@param res Response
@param checkDir dir to check
@param redirectUrl Path to redirect to after corrupted dirs have been removed
*/
removeBadDir: function (req, res, checkDir){
console.log(checkDir);
var rimraf = require('rimraf')
, colors = require('colors');
rimraf(checkDir, function (e) {
if(!e){
console.log('Removed bad dir', checkDir .yellow);
setTimeout(function(){
/* Send string to client to trigger reload of bad item */
console.log('Bad dir was found,sending message to client' .yellow);
res.send('bad dir');
},1000);
} else {
console.log('Removing dir error:', e .red);
}
});
},
/**
This funtion writes data to a JSON file and then returns the contents to the client. (Ajax)
@param req Request
@param res Response
@param writePath Path to store file
@param dataToWrite Data to write to file
*/
writeToFile: function (req,res,writePath,dataToWrite){
var fs = require('fs')
, colors = require('colors');
fs.writeFile(writePath, dataToWrite, function(e) {
if (!e) {
fs.readFile(writePath, 'utf8', function (err, data) {
if(!err){
res.send(data);
}else{
console.log('Cannot read data', err .red);
}
});
}else{
console.log('Error getting data', e .red);
};
});
}
}