-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64.js
More file actions
44 lines (36 loc) · 1.02 KB
/
Copy pathBase64.js
File metadata and controls
44 lines (36 loc) · 1.02 KB
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
const path = require('path');
const fs = require('fs');
class Base64 {
constructor() { }
/**
* @param {string} filename
*/
async encode_base64(filename) {
fs.readFile(path.join(__dirname, '/public/', filename), function(error, data) {
if (error) {
throw error;
} else {
let buf = Buffer.from(data);
let base64 = buf.toString('base64');
// console.log('Base64 ' + filename + ': ' + base64);
return base64;
}
});
}
/**
* @param {string} base64str
* @param {string} filename
*/
async decode_base64(base64str, filename) {
let buf = Buffer.from(base64str, 'base64');
fs.writeFile(path.join(__dirname, '/resource/images', filename), buf, function(error) {
if (error) {
throw error;
} else {
console.log('File created from base64 string!');
return true;
}
});
}
}
module.exports.Base64 = Base64;