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

Commit 73cf78a

Browse files
feat(exporter): return file sizes
1 parent d8675cc commit 73cf78a

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/exporters/file.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,33 @@
33
const UnixFS = require('ipfs-unixfs')
44
const pull = require('pull-stream')
55

6-
function extractContent (node) {
7-
return UnixFS.unmarshal(node.data).data
8-
}
9-
106
// Logic to export a single (possibly chunked) unixfs file.
117
module.exports = (node, name, ds) => {
8+
const file = UnixFS.unmarshal(node.data)
129
let content
1310

1411
if (node.links.length === 0) {
15-
const c = extractContent(node)
16-
content = pull.values([c])
12+
content = pull.values([file.data])
1713
} else {
1814
content = pull(
1915
pull.values(node.links),
2016
pull.map((link) => ds.getStream(link.hash)),
2117
pull.flatten(),
22-
pull.map(extractContent)
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+
})
2327
)
2428
}
2529

2630
return pull.values([{
2731
content: content,
28-
path: name
32+
path: name,
33+
size: file.fileSize()
2934
}])
3035
}

0 commit comments

Comments
 (0)