|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const extend = require('deep-extend') |
| 4 | +const assert = require('assert') |
| 5 | +const UnixFS = require('ipfs-unixfs') |
| 6 | +const pull = require('pull-stream') |
| 7 | +const through = require('pull-through') |
| 8 | +const parallel = require('async/parallel') |
| 9 | +const waterfall = require('async/waterfall') |
| 10 | +const dagPB = require('ipld-dag-pb') |
| 11 | +const CID = require('cids') |
| 12 | + |
| 13 | +const reduce = require('./reduce') |
| 14 | + |
| 15 | +const DAGNode = dagPB.DAGNode |
| 16 | + |
| 17 | +const defaultOptions = { |
| 18 | + chunkerOptions: { |
| 19 | + maxChunkSize: 262144 |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +module.exports = function (createChunker, ipldResolver, createReducer, _options) { |
| 24 | + const options = extend({}, defaultOptions, _options) |
| 25 | + |
| 26 | + return function (source, files) { |
| 27 | + return function (items, cb) { |
| 28 | + parallel(items.map((item) => (cb) => { |
| 29 | + if (!item.content) { |
| 30 | + // item is a directory |
| 31 | + return createAndStoreDir(item, (err, node) => { |
| 32 | + if (err) { |
| 33 | + return cb(err) |
| 34 | + } |
| 35 | + source.push(node) |
| 36 | + files.push(node) |
| 37 | + cb() |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + // item is a file |
| 42 | + createAndStoreFile(item, (err, node) => { |
| 43 | + if (err) { |
| 44 | + return cb(err) |
| 45 | + } |
| 46 | + source.push(node) |
| 47 | + files.push(node) |
| 48 | + cb() |
| 49 | + }) |
| 50 | + }), cb) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function createAndStoreDir (item, callback) { |
| 55 | + // 1. create the empty dir dag node |
| 56 | + // 2. write it to the dag store |
| 57 | + |
| 58 | + const d = new UnixFS('directory') |
| 59 | + waterfall([ |
| 60 | + (cb) => DAGNode.create(d.marshal(), cb), |
| 61 | + (node, cb) => { |
| 62 | + ipldResolver.put({ |
| 63 | + node: node, |
| 64 | + cid: new CID(node.multihash) |
| 65 | + }, (err) => cb(err, node)) |
| 66 | + } |
| 67 | + ], (err, node) => { |
| 68 | + if (err) { |
| 69 | + return callback(err) |
| 70 | + } |
| 71 | + callback(null, { |
| 72 | + path: item.path, |
| 73 | + multihash: node.multihash, |
| 74 | + size: node.size |
| 75 | + }) |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + function createAndStoreFile (file, callback) { |
| 80 | + if (Buffer.isBuffer(file.content)) { |
| 81 | + file.content = pull.values([file.content]) |
| 82 | + } |
| 83 | + |
| 84 | + if (typeof file.content !== 'function') { |
| 85 | + return callback(new Error('invalid content')) |
| 86 | + } |
| 87 | + |
| 88 | + const reducer = createReducer(reduce(file, ipldResolver, options), options) |
| 89 | + |
| 90 | + let previous |
| 91 | + let count = 0 |
| 92 | + |
| 93 | + pull( |
| 94 | + file.content, |
| 95 | + createChunker(options.chunkerOptions), |
| 96 | + pull.map(chunk => new Buffer(chunk)), |
| 97 | + pull.map(buffer => new UnixFS('file', buffer)), |
| 98 | + pull.asyncMap((fileNode, callback) => { |
| 99 | + DAGNode.create(fileNode.marshal(), (err, node) => { |
| 100 | + callback(err, { DAGNode: node, fileNode: fileNode }) |
| 101 | + }) |
| 102 | + }), |
| 103 | + pull.asyncMap((leaf, callback) => { |
| 104 | + ipldResolver.put( |
| 105 | + { |
| 106 | + node: leaf.DAGNode, |
| 107 | + cid: new CID(leaf.DAGNode.multihash) |
| 108 | + }, |
| 109 | + err => callback(err, leaf) |
| 110 | + ) |
| 111 | + }), |
| 112 | + pull.map((leaf) => { |
| 113 | + return { |
| 114 | + path: file.path, |
| 115 | + multihash: leaf.DAGNode.multihash, |
| 116 | + size: leaf.DAGNode.size, |
| 117 | + leafSize: leaf.fileNode.fileSize(), |
| 118 | + name: '' |
| 119 | + } |
| 120 | + }), |
| 121 | + through( // mark as single node if only one single node |
| 122 | + function onData (data) { |
| 123 | + count++ |
| 124 | + if (previous) { |
| 125 | + this.queue(previous) |
| 126 | + } |
| 127 | + previous = data |
| 128 | + }, |
| 129 | + function ended () { |
| 130 | + if (previous) { |
| 131 | + if (count === 1) { |
| 132 | + previous.single = true |
| 133 | + } |
| 134 | + this.queue(previous) |
| 135 | + } |
| 136 | + this.queue(null) |
| 137 | + } |
| 138 | + ), |
| 139 | + reducer, |
| 140 | + pull.collect((err, roots) => { |
| 141 | + if (err) { |
| 142 | + callback(err) |
| 143 | + } else { |
| 144 | + assert.equal(roots.length, 1, 'should result in exactly one root') |
| 145 | + callback(null, roots[0]) |
| 146 | + } |
| 147 | + }) |
| 148 | + ) |
| 149 | + } |
| 150 | +} |
0 commit comments