forked from benkaiser/stretto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
79 lines (66 loc) · 1.79 KB
/
util.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
// This file is a bunch of utility functions
var fs = require('fs');
var path = require('path');
var walk = function(dir, done) {
var results = [];
var strip_results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
if (file[0] === '.') {
if (!--pending) done(null, results, strip_results);
return;
}
file = path.join(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results, strip_results);
});
} else {
results.push(file);
if (!--pending) done(null, results, strip_results);
}
});
});
});
};
exports.walk = walk;
var contains = function(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
};
exports.contains = contains;
// function to get the IP of the current machine on the network
var cached_ip = null;
var getip = function(callback) {
if (cached_ip === null) {
require('dns').lookup(require('os').hostname(), function(err, add, fam) {
callback(add);
// save it for the next call
cached_ip = add;
});
} else {
callback(cached_ip);
}
};
exports.getip = getip;
exports.decodeBase64Image = function(dataString) {
var matches = dataString.match(/^data:image\/([A-Za-z-+\/]+);base64,(.+)$/);
var response = {};
if (matches === null) {
console.log(dataString);
console.log('Invalid file uploaded');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
};