|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const IPFS = require('./src/ipfs') |
| 5 | +const OrbitDB = require('orbit-db') |
| 6 | +const mime = require('mime') |
| 7 | + |
| 8 | +const conf = { |
| 9 | + IpfsDataDir: '/tmp/addfile-example-add', |
| 10 | + SignalServer: '127.0.0.1:9090', |
| 11 | +} |
| 12 | + |
| 13 | +console.log("Starting...") |
| 14 | + |
| 15 | +const feed = process.argv[2] || "hello-world" |
| 16 | +const filename = process.argv[3] |
| 17 | + |
| 18 | +if (!filename) { |
| 19 | + console.error('Filename required!') |
| 20 | + console.error('Usage: ipfe-add <feed-name> <filename>') |
| 21 | + process.exit(1) |
| 22 | +} |
| 23 | + |
| 24 | +IPFS.create(conf, (err, node) => { |
| 25 | + if (err) { |
| 26 | + console.log(err) |
| 27 | + } |
| 28 | + const ipfs = node |
| 29 | + const orbitdb = new OrbitDB(ipfs) |
| 30 | + const db = orbitdb.eventlog(feed) |
| 31 | + |
| 32 | + let peerList = [] |
| 33 | + let fileList = [] |
| 34 | + |
| 35 | + const readFileContents = (filePath) => { |
| 36 | + let content |
| 37 | + |
| 38 | + try { |
| 39 | + content = fs.readFileSync(filePath) |
| 40 | + } catch (e) { |
| 41 | + console.error(e) |
| 42 | + process.exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + return { |
| 46 | + content: content, |
| 47 | + mime: mime.lookup(filePath) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const addToIpfs = (name, content) => { |
| 52 | + console.log("add to ipfs", name) |
| 53 | + return ipfs.files.add([{ |
| 54 | + path: name, |
| 55 | + content: new Buffer(content) |
| 56 | + }]) |
| 57 | + } |
| 58 | + |
| 59 | + const addToOrbitDB = (file, type) => { |
| 60 | + console.log("add to orbit-db", file) |
| 61 | + return db.add({ |
| 62 | + ts: new Date().getTime(), |
| 63 | + mime: type, |
| 64 | + file: file |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + const add = (filePath) => { |
| 69 | + const file = readFileContents(filePath) |
| 70 | + return addToIpfs(filePath, file.content) |
| 71 | + .then((res) => addToOrbitDB(res[0], file.mime)) |
| 72 | + .then(() => query()) |
| 73 | + } |
| 74 | + |
| 75 | + const query = () => { |
| 76 | + const latest = db.iterator({ limit: -1 }).collect() |
| 77 | + const files = latest.reverse().map((e) => { |
| 78 | + return e.payload.value.file.path + " | " |
| 79 | + + e.payload.value.file.hash + " | " |
| 80 | + + e.payload.value.file.size + " | " |
| 81 | + + e.payload.value.mime |
| 82 | + }) |
| 83 | + |
| 84 | + let output = `` |
| 85 | + output += `------------------------------\n` |
| 86 | + output += `File | Hash | Size | Mime Type\n` |
| 87 | + output += `------------------------------\n` |
| 88 | + output += files.join('\n') + '\n' |
| 89 | + output += `------------------------------\n` |
| 90 | + console.log(output) |
| 91 | + } |
| 92 | + |
| 93 | + db.events.on('ready', () => { |
| 94 | + const timer = setInterval(() => { |
| 95 | + ipfs.pubsub.peers(feed) |
| 96 | + .then((peers) => { |
| 97 | + if (peers.length > peerList.length) { |
| 98 | + clearInterval(timer) |
| 99 | + peerList = peers |
| 100 | + console.log(`New peers for '${feed}':`) |
| 101 | + peers.forEach((e) => console.log(e)) |
| 102 | + return add(filename) |
| 103 | + } |
| 104 | + }) |
| 105 | + .catch((e) => console.error(e)) |
| 106 | + }, 1000) |
| 107 | + }) |
| 108 | +}) |
0 commit comments