|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const IPFS = require('../../src/core') // replace this by line below |
| 4 | +// const IPFS = require('ipfs') |
| 5 | + |
| 6 | +/* |
| 7 | + * Create a new IPFS instance, using default repo (fs) on default path (~/.ipfs) |
| 8 | + */ |
| 9 | +const node = new IPFS() |
| 10 | + |
| 11 | +const fs = require('fs') |
| 12 | + |
| 13 | +/* |
| 14 | + * Display version of js-ipfs |
| 15 | + */ |
| 16 | +node.version(gotVersion) |
| 17 | + |
| 18 | +function gotVersion (err, version) { |
| 19 | + if (err) { |
| 20 | + return console.error(err) |
| 21 | + } |
| 22 | + |
| 23 | + console.log(version) |
| 24 | + |
| 25 | + /* |
| 26 | + * Load the config into memory (generate the Public Key from the Private Key) |
| 27 | + */ |
| 28 | + node.load((err) => { |
| 29 | + if (err) { |
| 30 | + return console.log(err) |
| 31 | + } |
| 32 | + console.log('Repo was loaded\n') |
| 33 | + |
| 34 | + /* |
| 35 | + * Our instance is set, now let's goOnline (turn on bitswap) and do cool |
| 36 | + * stuff |
| 37 | + */ |
| 38 | + |
| 39 | + node.goOnline((err) => { |
| 40 | + if (err) { |
| 41 | + return console.log(err) |
| 42 | + } |
| 43 | + |
| 44 | + // We can test to see if we actually are online if we want to |
| 45 | + if (node.isOnline()) { |
| 46 | + console.log('\nYep, we are online') |
| 47 | + } |
| 48 | + |
| 49 | + /* |
| 50 | + * Add a file to IPFS - Complete Files API on: |
| 51 | + * https://github.com/ipfs/interface-ipfs-core/tree/master/API/files |
| 52 | + */ |
| 53 | + |
| 54 | + const file = { |
| 55 | + path: 'hello.txt', |
| 56 | + content: fs.createReadStream('./hello.txt') |
| 57 | + } |
| 58 | + |
| 59 | + node.files.add(file, (err, result) => { |
| 60 | + if (err) { |
| 61 | + return console.error(err) |
| 62 | + } |
| 63 | + |
| 64 | + /* |
| 65 | + * Awesome we've added a file so let's retrieve and |
| 66 | + * display its contents from IPFS |
| 67 | + */ |
| 68 | + |
| 69 | + console.log('\n', result, '\n') |
| 70 | + |
| 71 | + node.files.cat(result[0].hash, (err, stream) => { |
| 72 | + if (err) { |
| 73 | + return console.error(err) |
| 74 | + } |
| 75 | + |
| 76 | + console.log('file content: \n') |
| 77 | + |
| 78 | + stream.pipe(process.stdout) |
| 79 | + stream.on('end', process.exit) |
| 80 | + }) |
| 81 | + }) |
| 82 | + }) |
| 83 | + }) |
| 84 | +} |
0 commit comments