From 68e09a74ca67cf229a78018d037aae8e2e5de969 Mon Sep 17 00:00:00 2001 From: jbenet Date: Sun, 11 Sep 2016 01:11:55 -0400 Subject: [PATCH] feat(exporter): implement recursive file export implement recursive file export and fixed a bug where the data in intermediate nodes was not getting output. --- src/exporters/file.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/exporters/file.js b/src/exporters/file.js index 7dfb4bfb..a468047c 100644 --- a/src/exporters/file.js +++ b/src/exporters/file.js @@ -1,32 +1,34 @@ 'use strict' +const traverse = require('pull-traverse') const UnixFS = require('ipfs-unixfs') const pull = require('pull-stream') // Logic to export a single (possibly chunked) unixfs file. module.exports = (node, name, ds) => { - const file = UnixFS.unmarshal(node.data) - let content + function getData (node) { + try { + const file = UnixFS.unmarshal(node.data) + return file.data || new Buffer(0) + } catch (err) { + throw new Error('Failed to unmarshal node') + } + } - if (node.links.length === 0) { - content = pull.values([file.data]) - } else { - content = pull( + function visitor (node) { + return pull( pull.values(node.links), pull.map((link) => ds.getStream(link.hash)), - pull.flatten(), - 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') - } - }) + pull.flatten() ) } + let content = pull( + traverse.depthFirst(node, visitor), + pull.map(getData) + ) + + const file = UnixFS.unmarshal(node.data) return pull.values([{ content: content, path: name,