forked from C9E4F7/DTube-snap-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap-processor-cmds.js
executable file
·102 lines (80 loc) · 2.59 KB
/
snap-processor-cmds.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const ipfsAPI = require('ipfs-http-client');
const fs = require('fs');
var ipfsIp = process.env.IPFSIP || '127.0.0.1';
var ipfsPort = process.env.IPFSPORT || '5001';
var ipfsProtocol = process.env.IPFSPROTOCOL || 'http';
var cmds = {
shell_cmds: {
// make sure all files and directories exist where they should
createResizeCmd: (filePath) => {
return `convert ` + filePath + ` -resize 640x360 -gravity Center -crop 640x360 ./snap/resizedImg`
},
// make sure all files and directories exist where they should
createOverlayCmd: (filePath) => {
return `composite -gravity NorthEast overlay.png ` + filePath +` ./snap/overlayedImg`
}
},
ipfs_cmds: {
// uploads file to ipfs, second parameter is the property to update within process response
ipfsUpload: (filePath, prop) => {
//Connceting to our http api
const ipfs = ipfsAPI(ipfsIp, ipfsPort, {protocol: ipfsProtocol})
let fileToUpload = fs.readFileSync(filePath);
let testBuffer = new Buffer.from(fileToUpload);
ipfs.add(testBuffer, function (err, file) {
if (err) {
console.log(err);
process.exit();
}
// updating relevant process response fields
cmds.setObjPropToValue(cmds.processResponse, prop+".progress", "100.00%");
cmds.setObjPropToValue(cmds.processResponse, prop+".lastTimeProgress", Date());
cmds.setObjPropToValue(cmds.processResponse, prop+".step", "success");
cmds.setObjPropToValue(cmds.processResponse, prop+".hash", file[0].hash);
cmds.setObjPropToValue(cmds.processResponse, prop+".fileSize", file[0].size);
});
}
},
// function for setting deep nested object property values
setObjPropToValue: (obj, path, value) => {
var i;
path = path.split(/(?:\.|\[|\])+/);
for (i = 0; i < path.length - 1; i++)
obj = obj[path[i]];
obj[path[i]] = value;
},
checkIfFinished: () => {
var func = setInterval(()=>{
if (cmds.processResponse.ipfsAddSource.progress == "100.00%" && cmds.processResponse.ipfsAddOverlay.progress == "100.00%"){
clearInterval(func);
// wait before ending process
setTimeout(()=>{
process.exit();
},20000);
}
},2000);
},
processResponse: {
"ipfsAddSource": {
"progress": "0.00%",
"encodeSize": "source",
"lastTimeProgress": null,
"errorMessage": null,
"step": "Started",
"positionInQueue": null,
"hash": null,
"fileSize": null
},
"ipfsAddOverlay": {
"progress": "Waiting in queue...",
"encodeSize": "source",
"lastTimeProgress": null,
"errorMessage": null,
"step": "Waiting",
"positionInQueue": 1,
"hash": null,
"fileSize": null
}
}
}
module.exports = cmds