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

Commit 270be71

Browse files
authored
Merge pull request #71 from ipfs/fixes-for-cat
feat(exporter): return file sizes
2 parents d8675cc + 15d5687 commit 270be71

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/exporters/file.js

Lines changed: 13 additions & 8 deletions
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
}

test/test-exporter.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ module.exports = (repo) => {
2424
ds = new DAGService(bs)
2525
})
2626

27+
it('import and export', (done) => {
28+
pull(
29+
pull.values([{
30+
path: '1.2MiB.txt',
31+
content: pull.values([bigFile, Buffer('hello world')])
32+
}]),
33+
unixFSEngine.importer(ds),
34+
pull.map((file) => {
35+
expect(file.path).to.be.eql('1.2MiB.txt')
36+
37+
return exporter(file.multihash, ds)
38+
}),
39+
pull.flatten(),
40+
pull.collect((err, files) => {
41+
expect(err).to.not.exist
42+
expect(files[0].size).to.be.eql(bigFile.length + 11)
43+
fileEql(files[0], Buffer.concat([bigFile, Buffer('hello world')]), done)
44+
})
45+
)
46+
})
47+
2748
it('ensure hash inputs are sanitized', (done) => {
2849
const hash = 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8'
2950
const mhBuf = new Buffer(bs58.decode(hash))

0 commit comments

Comments
 (0)