forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
66 lines (57 loc) · 1.6 KB
/
files.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
56
57
58
59
60
61
62
63
64
65
66
'use strict'
const Importer = require('ipfs-unixfs-engine').importer
const Exporter = require('ipfs-unixfs-engine').exporter
const UnixFS = require('ipfs-unixfs')
const promisify = require('promisify-es6')
module.exports = function files (self) {
return {
add: promisify((arr, callback) => {
if (typeof arr === 'function') {
callback = arr
arr = undefined
}
if (callback === undefined) {
callback = function noop () {}
}
if (arr === undefined) {
callback(null, new Importer(self._dagS))
}
const i = new Importer(self._dagS)
const res = []
i.on('data', (info) => {
res.push(info)
})
i.once('end', () => {
callback(null, res)
})
arr.forEach((tuple) => {
i.write(tuple)
})
i.end()
}),
cat: promisify((hash, callback) => {
if (typeof hash === 'function') {
callback = hash
return callback('You must supply a multihash', null)
}
self._dagS.get(hash, (err, fetchedNode) => {
if (err) {
return callback(err, null)
}
const data = UnixFS.unmarshal(fetchedNode.data)
if (data.type === 'directory') {
callback('This dag node is a directory', null)
} else {
const exportStream = Exporter(hash, self._dagS)
exportStream.once('data', (object) => {
callback(null, object.stream)
})
}
})
}),
get: promisify((hash, callback) => {
var exportFile = Exporter(hash, self._dagS)
callback(null, exportFile)
})
}
}