'use strict'

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) => {
  let content

  if (node.links.length === 0) {
    const c = extractContent(node)
    content = pull.values([c])
  } else {
    content = pull(
      pull.values(node.links),
      pull.map((link) => ds.getStream(link.hash)),
      pull.flatten(),
      pull.map(extractContent)
    )
  }

  return pull.values([{
    content: content,
    path: name
  }])
}

@@ -86,86 +86,8 @@
 ')%0A%0A
-function extractContent (node) %7B%0A  return UnixFS.unmarshal(node.data).data%0A%7D%0A%0A
 // L
@@ -181,16 +181,59 @@
 s) =%3E %7B%0A
+  const file = UnixFS.unmarshal(node.data)%0A
   let co
@@ -276,43 +276,8 @@
 ) %7B%0A
-    const c = extractContent(node)%0A
     
@@ -299,17 +299,25 @@
 values(%5B
-c
+file.data
 %5D)%0A  %7D e
@@ -465,22 +465,233 @@
 map(
-extractContent
+(node) =%3E %7B%0A        try %7B%0A          const ex = UnixFS.unmarshal(node.data)%0A          return ex.data%0A        %7D catch (err) %7B%0A          console.error(node)%0A          throw new Error('Failed to unmarshal node')%0A        %7D%0A      %7D
 )%0A  
@@ -745,16 +745,16 @@
 ontent,%0A
-
     path
@@ -759,16 +759,43 @@
 th: name
+,%0A    size: file.fileSize()
 %0A  %7D%5D)%0A%7D

return file sizes