1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-03-14 22:08:39 +01:00
ipfs-boards/src/utils/ipfs.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-02-05 22:19:54 +01:00
export async function ipfsPut(content) {
2018-02-07 21:08:53 +01:00
const obj = {
content: Buffer.from(content),
path: '/'
}
const response = await window.ipfs.files.add(obj)
return response[0].hash
2018-02-08 23:52:06 +01:00
}
export async function readText(multihash) {
const buffer = await window.ipfs.object.get(multihash)
return buffer.toString('utf-8')
}
export async function getStats() {
const ipfs = window.ipfs;
const orbitDb = window.orbitDb
const dbs = {}
const stats = {}
if (ipfs) {
stats.ipfsLoaded = true
2018-02-08 23:52:06 +01:00
const peers = await ipfs.swarm.peers()
const id = await ipfs.id()
stats.peers = peers.map(p => p.peer.id._idB58String)
stats.id = id.id
} else {
stats.ipfsLoaded = false
2018-02-08 23:52:06 +01:00
}
if (orbitDb) {
stats.orbitDbLoaded = true
2018-02-08 23:52:06 +01:00
stats.pubKey = await orbitDb.key.getPublic('hex')
Object.values(window.dbs || {}).forEach(db => {
let writeable = db.access.write.indexOf('*') >= 0 || db.access.write.indexOf(stats.pubKey) >= 0
const dbInfo = {
opLogLength: db._oplog.length,
access: {
admin: db.access.admin,
read: db.access.read,
write: db.access.write,
writeable
},
peers: []
}
2018-02-09 00:42:14 +01:00
const subscription = orbitDb._pubsub._subscriptions[db.address]
2018-02-08 23:52:06 +01:00
if (subscription && subscription.room) {
2018-02-09 00:42:14 +01:00
dbInfo.peers = [...(subscription.room._peers || [])]
2018-02-08 23:52:06 +01:00
}
dbs[db.address] = dbInfo
})
} else {
stats.orbitDbLoaded = false
2018-02-08 23:52:06 +01:00
}
stats.dbs = dbs
return stats
2018-02-05 22:19:54 +01:00
}