-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (45 loc) · 1.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const IpfsApi = require('ipfs-api')
const Amorph = require('amorph')
const Q = require('q')
const amorphBase58Plugin = require('amorph-base58')
const amorphBufferPlugin = require('amorph-buffer')
const arguguard = require('arguguard')
Amorph.loadPlugin(amorphBase58Plugin)
Amorph.loadPlugin(amorphBufferPlugin)
Amorph.ready()
function IpfsAmorphApi(ipfsApiOptions) {
arguguard('IpfsAmorphApi(ipfsApiOptions)', [Object], arguments)
this.api = IpfsApi(ipfsApiOptions)
}
IpfsAmorphApi.Amorph = Amorph
IpfsAmorphApi.prototype.defer = function defer() {
arguguard('IpfsAmorphApi.defer()', [], arguments)
return Q.defer()
}
IpfsAmorphApi.prototype.addFile = function addFile(file) {
arguguard('ipfsAmorphApi.addFile(file)', [IpfsAmorphApi.Amorph], arguments)
return this.api.add(file.to('buffer')).then((results) => {
return new IpfsAmorphApi.Amorph(results[0].hash, 'base58')
})
}
IpfsAmorphApi.prototype.getFile = function getFile(multihash) {
arguguard('ipfsAmorphApi.getFile(multihash)', [IpfsAmorphApi.Amorph], arguments)
const deferred = this.defer()
this.api.get(multihash.to('base58')).then((readable) => {
readable.on('readable', () => {
const result = readable.read()
if (!result) {
return
}
const data = []
result.content.on('data', (_data) => {
data.push(..._data)
})
result.content.on('end', () => {
deferred.resolve(new IpfsAmorphApi.Amorph(data, 'array'))
})
})
})
return deferred.promise
}
module.exports = IpfsAmorphApi