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

Commit f9270b8

Browse files
authored
Merge pull request #72 from ipfs/feat/recursive-exporter
implement recursive file export
2 parents 6eef518 + 68e09a7 commit f9270b8

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/exporters/file.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
'use strict'
22

3+
const traverse = require('pull-traverse')
34
const UnixFS = require('ipfs-unixfs')
45
const pull = require('pull-stream')
56

67
// Logic to export a single (possibly chunked) unixfs file.
78
module.exports = (node, name, ds) => {
8-
const file = UnixFS.unmarshal(node.data)
9-
let content
9+
function getData (node) {
10+
try {
11+
const file = UnixFS.unmarshal(node.data)
12+
return file.data || new Buffer(0)
13+
} catch (err) {
14+
throw new Error('Failed to unmarshal node')
15+
}
16+
}
1017

11-
if (node.links.length === 0) {
12-
content = pull.values([file.data])
13-
} else {
14-
content = pull(
18+
function visitor (node) {
19+
return pull(
1520
pull.values(node.links),
1621
pull.map((link) => ds.getStream(link.hash)),
17-
pull.flatten(),
18-
pull.map((node) => {
19-
try {
20-
const ex = UnixFS.unmarshal(node.data)
21-
return ex.data
22-
} catch (err) {
23-
console.error(node)
24-
throw new Error('Failed to unmarshal node')
25-
}
26-
})
22+
pull.flatten()
2723
)
2824
}
2925

26+
let content = pull(
27+
traverse.depthFirst(node, visitor),
28+
pull.map(getData)
29+
)
30+
31+
const file = UnixFS.unmarshal(node.data)
3032
return pull.values([{
3133
content: content,
3234
path: name,

0 commit comments

Comments
 (0)