Skip to content

Instantly share code, notes, and snippets.

@josselinchevalay
Created December 19, 2017 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josselinchevalay/45e1fcec195f3b0ad68e0128ec296cb4 to your computer and use it in GitHub Desktop.
Save josselinchevalay/45e1fcec195f3b0ad68e0128ec296cb4 to your computer and use it in GitHub Desktop.
sharing file
const Ipfs = require('ipfs')
const node = new Ipfs() // you can specify a config
const requestData = {peerId : "QMA65465487ET6849" , cid : "QMA687979465167TY8798"} // data send by rest api
node.start(()=>{
node.on('ready', ()=>{
ipfs.swarm.peers((err, peers)=>{
let peer = peers.filter((p) => {return p.id === requestData.peerId })[0];
if(peer){
node.files.get(requestData.cid, (err, data) =>{
console.log(data);
});
}else{
console.log('Not exist')
process.exit(1);
}
});
});
});
const Ipfs = require('ipfs')
const node = new Ipfs() // you can specify a config
const request = require('request')
const fs = require('fs')
const yourFilePath = "/your_path/test.txt"
node.start(()=>{
node.on('ready', ()=>{
let fileBuffer = fs.readfile(yourFilePath, "utf8" (err, data) => {
node.files.add({path : yourFilePath, content : fileBuffer}, (err, hash)=>{
let finalHash = hash.filter((h)=> {return '/' + h.path === yourFilePath});
node.id((identity)=>{
request.post({
url : 'your_app_host/share',
json : true,
form: {peerId : identity.ID, cid : finalHash}
}, ()=>{
console.log('send ' + finalHash + ' peerId' + identity.ID);
})
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment