Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 4768052

Browse files
committed
Add CLI examples
1 parent 124e0f4 commit 4768052

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

examples/ipfm/ipfe-add.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
})

examples/ipfm/ipfe-daemon.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict'
2+
3+
// const IpfsDaemon = require('ipfs-daemon/src/ipfs-browser-daemon')
4+
const IPFS = require('./src/ipfs')
5+
const OrbitDB = require('orbit-db')
6+
7+
const userId = Math.floor(Math.random() * 1000)
8+
9+
const conf = {
10+
IpfsDataDir: '/tmp/addfile-example',
11+
SignalServer: '127.0.0.1:9090',
12+
}
13+
14+
console.log("Starting...")
15+
16+
const feed = process.argv[2] || "hello-world"
17+
18+
IPFS.create(conf, (err, node) => {
19+
if (err) {
20+
console.log(err)
21+
}
22+
const ipfs = node
23+
const orbitdb = new OrbitDB(ipfs)
24+
const db = orbitdb.eventlog(feed)
25+
26+
let peerList = []
27+
let fileList = []
28+
29+
db.events.on('ready', () => console.log("DB ready!"))
30+
db.events.on('history', () => console.log("DB updated!"))
31+
db.events.on('data', () => console.log("DB updated locally!"))
32+
33+
setInterval(() => {
34+
ipfs.pubsub.peers(feed)
35+
.then((peers) => {
36+
if (peers.length > peerList.length) {
37+
peerList = peers
38+
console.log(`New peers for '${feed}':`)
39+
peers.forEach((e) => console.log(e))
40+
}
41+
})
42+
.catch((e) => console.error(e))
43+
}, 1000)
44+
45+
const query = () => {
46+
const latest = db.iterator({ limit: -1 }).collect()
47+
if (latest.length > fileList.length) {
48+
fileList = latest
49+
50+
const files = latest.reverse().map((e) => {
51+
return e.payload.value.file.path + " | "
52+
+ e.payload.value.file.hash + " | "
53+
+ e.payload.value.file.size + " | "
54+
+ e.payload.value.mime
55+
})
56+
57+
if (latest.length > 0) {
58+
let output = ``
59+
output += `------------------------------\n`
60+
output += `File | Hash | Size | Mime Type\n`
61+
output += `------------------------------\n`
62+
output += files.join('\n') + '\n'
63+
output += `------------------------------\n`
64+
console.log(output)
65+
}
66+
}
67+
}
68+
69+
setInterval(query, 1000)
70+
})

0 commit comments

Comments
 (0)