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

implement recursive file export #72

Merged
merged 1 commit into from
Sep 11, 2016
Merged
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
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