-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubs-processor-cmds.js
More file actions
executable file
·74 lines (60 loc) · 1.88 KB
/
Copy pathsubs-processor-cmds.js
File metadata and controls
executable file
·74 lines (60 loc) · 1.88 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
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
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 = {
ipfs_cmds: {
// uploads file to ipfs, second parameter is the property to update within 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 encoder 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;
},
processResponse:{
"ipfsAddSource": {
"progress": "0.00%",
"encodeSize": "source",
"lastTimeProgress": null,
"errorMessage": null,
"step": "Started",
"positionInQueue": null,
"hash": null,
"fileSize": null
}
},
checkIfFinished: () => {
var func = setInterval(()=>{
if (cmds.processResponse.ipfsAddSource.progress == "100.00%"){
clearInterval(func);
// wait before ending process
setTimeout(()=>{
process.exit();
},10000);
}
},2000);
}
}
module.exports = cmds