forked from blockchain-certificates/obi-baking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (70 loc) · 2.02 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
73
74
75
76
77
78
79
80
81
82
var commander = require('commander');
var fs = require('fs');
var pngitxt = require('png-itxt');
var https = require('https');
function bake(imageToBake, blockcertFileName, bakedImage) {
var bufs = [];
fs.createReadStream(blockcertFileName)
.on('end', function () {
var buf = Buffer.concat(bufs);
fs.createReadStream(imageToBake)
.on('end', function () {
console.log('Output file is ' + bakedImage);
})
.pipe(pngitxt.set('openbadges', buf.toString()))
.pipe(fs.createWriteStream(bakedImage));
})
.on('data', function (d) {
bufs.push(d);
})
.pipe(fs.createWriteStream(bakedImage));
}
function extract(bakedImage) {
fs.createReadStream(bakedImage)
.pipe(pngitxt.get('openbadges', function (data, err) {
if (!err && data) {
console.log(data);
}
}));
}
function extract_url(bakedImageUrl) {
https.get(bakedImageUrl, function (res) {
res.pipe(pngitxt.get('openbadges', function (data, err) {
if (!err && data) {
console.log(data);
}
}));
});
}
var program = require('commander');
program
.version('0.0.1')
.option('-x, --extract', 'extract assertion from baked file')
.option('-b, --bake', 'bake assertion into file')
.option('-i, --image <path>', 'Image to bake')
.option('-o, --bakedImage <path>', 'Baked image output file')
.option('-B, --blockcert <path>', 'Blockcert assertion to bake')
.parse(process.argv);
if (program.bake) {
if (!program.image || !program.bakedImage || !program.blockcert) {
program.outputHelp();
return;
}
var imageToBake = program.image;
var bakedImage = program.bakedImage;
var blockcertFileName = program.blockcert;
bake(imageToBake, blockcertFileName, bakedImage);
} else if (program.extract) {
if (!program.bakedImage) {
program.outputHelp();
return;
}
var bakedImage = program.bakedImage;
if (bakedImage.indexOf('http') === 0) {
extract_url(bakedImage);
} else {
extract(bakedImage);
}
} else {
program.outputHelp();
}