forked from pepesir/PEPE-SIR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (61 loc) · 1.94 KB
/
index.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
'use strict';
/**
* Module dependencies.
* @private
*/
var fs = require('fs');
/**
* Module exports.
* @public
*/
module.exports = base64ToImage;
/**
* Change base64Str to image and write image file with
the specified file name to the specified file path.
* @param {String} base64 string (mandatory)
* @param {String} file path e.g. /opt/temp/uploads/ (mandatory)
* @return {Object} optionsObj holds image type, image filename, debug e.g.{'fileName':fileName, 'type':type,'debug':true} (optional)
* @public
*/
function base64ToImage(base64Str, path, optionalObj) {
if (!base64Str || !path) {
throw new Error('Missing mandatory arguments base64 string and/or path string');
}
var optionalObj = optionalObj || {};
var imageBuffer = decodeBase64Image(base64Str);
var imageType = optionalObj.type || imageBuffer.type || 'png';
var fileName = optionalObj.fileName || 'img-' + Date.now();
var abs;
var fileName = '' + fileName;
if (fileName.indexOf('.') === -1) {
imageType = imageType.replace('image/', '');
fileName = fileName + '.' + imageType;
}
abs = path + fileName;
fs.writeFile(abs, imageBuffer.data, 'base64', function(err) {
if (err && optionalObj.debug) {
console.log("File image write error", err);
}
});
return {
'imageType': imageType,
'fileName': fileName
};
};
/**
* Decode base64 string to buffer.
*
* @param {String} base64Str string
* @return {Object} Image object with image type and data buffer.
* @public
*/
function decodeBase64Image(base64Str) {
var matches = base64Str.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
var image = {};
if (!matches || matches.length !== 3) {
throw new Error('Invalid base64 string');
}
image.type = matches[1];
image.data = new Buffer(matches[2], 'base64');
return image;
}