From 73cf78a99457cacbbabcee83f49b9ed300b8bf17 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 9 Sep 2016 20:23:59 +0200 Subject: [PATCH 1/2] feat(exporter): return file sizes --- src/exporters/file.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/exporters/file.js b/src/exporters/file.js index c2993fcb..7dfb4bfb 100644 --- a/src/exporters/file.js +++ b/src/exporters/file.js @@ -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() }]) } From 15d5687c3656b6133ba57ef5ff72d17d5d04f279 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 9 Sep 2016 21:49:04 +0200 Subject: [PATCH 2/2] test: import - export with size check --- test/test-exporter.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/test-exporter.js b/test/test-exporter.js index baa7f174..9f3a8cad 100644 --- a/test/test-exporter.js +++ b/test/test-exporter.js @@ -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))