-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipfsAPI.js
72 lines (67 loc) · 1.74 KB
/
ipfsAPI.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
const lwip=require('lwip')
var _thumbnailHash;
function getImageThumbnailHash(path){
return new Promise((resolve,reject)=>{
lwip.open(path,(error,image)=>{
image.batch()
.scale(0.3)
.writeFile('./thumbNail.png',(err)=>{
ipfs.util.addFromFs("./thumbNail.png",(err,res)=>{
if(err){
console.log("Error while adding thumbNail");
}
else{
_thumbnailHash=res[0]["hash"];
resolve();
}
})
})
})
});
}
function getIPFSImageData(multihash){
ipfs.files.cat(multihash,(error,stream)=>{
var writeStream=fs.createWriteStream(imageDataDir + multihash + '.png');
stream.pipe(writeStream,{end:false});
console.log('done');
});
}
function ipfsAddToDisk(hash,thumbnailHash){
console.log("ipfsAddToDisk_BEGIN");
getIPFSImageData(hash);
getIPFSImageData(thumbnailHash);
console.log("ipfsAddToDisk_END");
}
function deleteFileFromIPFSSendTransaction(path,tag){
return new Promise((resolve,reject)=>{
ipfs.util.addFromFs(path,(error,response)=>{
if(error){
console.log("File already deleted.");
}
else{
sendTransactionToDelete(address,response[0]["hash"],tag);
resolve();
}
})
})
}
function addFileToIPFSAndSendTransaction(path,tag,geolocation){
return new Promise((resolve,reject)=>{
ipfs.util.addFromFs(path,(error,response)=>{
if(error){
console.log(path, error);
console.log("Error while adding file.");
}
else{
console.log("Added file to IPFS");
getImageThumbnailHash(path)
.then(()=>{
console.log("thumbnailHash is: " + _thumbnailHash);
thumbnailHashToImageHash[_thumbnailHash]=response[0]["hash"];
sendTransactionToAdd(address,response[0]["hash"],_thumbnailHash,tag,geolocation);
resolve();
})
}
})
})
}