-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.js
58 lines (50 loc) · 1.43 KB
/
utils.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
const fs = require('fs')
const path = require('path')
/**
* Parse a base 64 image and return the extension and buffer
* @param {String} imageString The image data as base65 string
* @return {Object} { type: String, data: Buffer }
*/
function parseBase64Image(imageString) {
var matches = imageString.match(/^data:image\/([A-Za-z-+/]+);base64,(.+)$/);
var resource = {};
if (matches.length !== 3) {
return null;
}
resource.type = matches[1] === 'jpeg' ? 'jpg' : matches[1];
resource.data = new Buffer(matches[2], 'base64');
return resource;
}
function random (low, high) {
return Math.random() * (high - low) + low;
}
function getImageType(data) {
if(data.indexOf("jpg") !== -1) return 'jpg'
else if (data.indexOf("jpeg") !== -1) return 'jpeg'
else if(data.indexOf("png") !== -1) return 'png'
return null
}
/**
* Remove directory recursively
* @param {string} dir_path
* @see https://stackoverflow.com/a/42505874/3027390
*/
function rimraf(dir_path) {
if (fs.existsSync(dir_path)) {
fs.readdirSync(dir_path).forEach(function(entry) {
var entry_path = path.join(dir_path, entry);
if (fs.lstatSync(entry_path).isDirectory()) {
rimraf(entry_path);
} else {
fs.unlinkSync(entry_path);
}
});
fs.rmdirSync(dir_path);
}
}
module.exports = {
parseBase64Image,
random,
getImageType,
rimraf
}