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

feat(exporter): return file sizes #71

Merged
merged 2 commits into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/exporters/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,33 @@
const UnixFS = require('ipfs-unixfs')
const pull = require('pull-stream')

function extractContent (node) {
return UnixFS.unmarshal(node.data).data
}

// Logic to export a single (possibly chunked) unixfs file.
module.exports = (node, name, ds) => {
const file = UnixFS.unmarshal(node.data)
let content

if (node.links.length === 0) {
const c = extractContent(node)
content = pull.values([c])
content = pull.values([file.data])
} else {
content = pull(
pull.values(node.links),
pull.map((link) => ds.getStream(link.hash)),
pull.flatten(),
pull.map(extractContent)
pull.map((node) => {
try {
const ex = UnixFS.unmarshal(node.data)
return ex.data
} catch (err) {
console.error(node)
throw new Error('Failed to unmarshal node')
}
})
)
}

return pull.values([{
content: content,
path: name
path: name,
size: file.fileSize()
}])
}
21 changes: 21 additions & 0 deletions test/test-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ module.exports = (repo) => {
ds = new DAGService(bs)
})

it('import and export', (done) => {
pull(
pull.values([{
path: '1.2MiB.txt',
content: pull.values([bigFile, Buffer('hello world')])
}]),
unixFSEngine.importer(ds),
pull.map((file) => {
expect(file.path).to.be.eql('1.2MiB.txt')

return exporter(file.multihash, ds)
}),
pull.flatten(),
pull.collect((err, files) => {
expect(err).to.not.exist
expect(files[0].size).to.be.eql(bigFile.length + 11)
fileEql(files[0], Buffer.concat([bigFile, Buffer('hello world')]), done)
})
)
})

it('ensure hash inputs are sanitized', (done) => {
const hash = 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8'
const mhBuf = new Buffer(bs58.decode(hash))
Expand Down