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

Commit

Permalink
feat(exporter): implement recursive file export
Browse files Browse the repository at this point in the history
implement recursive file export and fixed a bug where
the data in intermediate nodes was not getting output.
  • Loading branch information
jbenet committed Sep 11, 2016
1 parent 6eef518 commit 68e09a7
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/exporters/file.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 68e09a7

Please sign in to comment.