sharing file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| }); | |
| }); | |
| }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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