Skip to content

Instantly share code, notes, and snippets.

@gerhardberger
Created February 1, 2017 21:17
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 gerhardberger/6593eca682b58f9af28da774c63671c7 to your computer and use it in GitHub Desktop.
Save gerhardberger/6593eca682b58f9af28da774c63671c7 to your computer and use it in GitHub Desktop.
const os = require('os')
const path = require('path')
const IPFS = require('ipfs')
const { createWriteStream } = require('fs')
const fileMultihash = process.argv[2]
const node = new IPFS(path.join(os.tmpDir() + '/' + new Date().toString()))
node.version((err, version) => {
if (err) return console.error(err)
console.log('IPFS Version:', version.version)
node.init({ emptyRepo: true, bits: 2048 }, err => {
if (err) return console.error(err)
node.load(err => {
if (err) return console.error(err)
node.goOnline(err => {
if (err) return console.error(err)
console.log('Node is online')
if (node.isOnline()) {
console.log('Node is now ready and online')
node.files.get(fileMultihash, (err, stream) => {
if (err) return console.error(err)
// console.log('File content:')
// stream.pipe(process.stdout)
// stream.on('end', process.exit)
stream.on('data', (file) => {
// write the file's path and contents to standard out
console.log(file.path)
file.content.pipe(createWriteStream('x' + file.path))
file.content.on('end', () => process.exit(0))
})
})
}
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment