|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const mh = require('multihashes') |
| 4 | +const UnixFS = require('ipfs-unixfs') |
| 5 | +const CID = require('cids') |
| 6 | +const dagPB = require('ipld-dag-pb') |
| 7 | +const asyncEach = require('async/each') |
| 8 | + |
| 9 | +const DAGLink = dagPB.DAGLink |
| 10 | +const DAGNode = dagPB.DAGNode |
| 11 | + |
| 12 | +module.exports = (files, ipldResolver, source, callback) => { |
| 13 | + // 1) convert files to a tree |
| 14 | + const fileTree = createTree(files) |
| 15 | + |
| 16 | + if (Object.keys(fileTree).length === 0) { |
| 17 | + return callback()// no dirs to be created |
| 18 | + } |
| 19 | + |
| 20 | + // 2) create sizeIndex |
| 21 | + const sizeIndex = createSizeIndex(files) |
| 22 | + |
| 23 | + // 3) bottom up flushing |
| 24 | + traverse(fileTree, sizeIndex, null, ipldResolver, source, callback) |
| 25 | +} |
| 26 | + |
| 27 | +/* |
| 28 | + * createTree |
| 29 | + * |
| 30 | + * received an array of files with the format: |
| 31 | + * { |
| 32 | + * path: // full path |
| 33 | + * multihash: // multihash of the dagNode |
| 34 | + * size: // cumulative size |
| 35 | + * } |
| 36 | + * |
| 37 | + * returns a JSON object that represents a tree where branches are the paths |
| 38 | + * and the leaves are objects with file names and respective multihashes, such |
| 39 | + * as: |
| 40 | + * { |
| 41 | + * foo: { |
| 42 | + * bar: { |
| 43 | + * baz.txt: <multihash> |
| 44 | + * } |
| 45 | + * } |
| 46 | + * } |
| 47 | + */ |
| 48 | +function createTree (files) { |
| 49 | + const fileTree = {} |
| 50 | + |
| 51 | + files.forEach((file) => { |
| 52 | + let splitted = file.path.split('/') |
| 53 | + if (splitted.length === 1) { |
| 54 | + return // adding just one file |
| 55 | + } |
| 56 | + if (splitted[0] === '') { |
| 57 | + splitted = splitted.slice(1) |
| 58 | + } |
| 59 | + var tmpTree = fileTree |
| 60 | + |
| 61 | + for (var i = 0; i < splitted.length; i++) { |
| 62 | + if (!tmpTree[splitted[i]]) { |
| 63 | + tmpTree[splitted[i]] = {} |
| 64 | + } |
| 65 | + if (i === splitted.length - 1) { |
| 66 | + tmpTree[splitted[i]] = file.multihash |
| 67 | + } else { |
| 68 | + tmpTree = tmpTree[splitted[i]] |
| 69 | + } |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + return fileTree |
| 74 | +} |
| 75 | + |
| 76 | +/* |
| 77 | + * create a size index that goes like: |
| 78 | + * { <multihash>: <size> } |
| 79 | + */ |
| 80 | +function createSizeIndex (files) { |
| 81 | + const sizeIndex = {} |
| 82 | + |
| 83 | + files.forEach((file) => { |
| 84 | + sizeIndex[mh.toB58String(file.multihash)] = file.size |
| 85 | + }) |
| 86 | + |
| 87 | + return sizeIndex |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + * expand the branches recursively (depth first), flush them first |
| 92 | + * and then traverse through the bottoum up, flushing everynode |
| 93 | + * |
| 94 | + * Algorithm tl;dr; |
| 95 | + * create a dirNode |
| 96 | + * Object.keys |
| 97 | + * If the value is an Object |
| 98 | + * create a dir Node |
| 99 | + * Object.keys |
| 100 | + * Once finished, add the result as a link to the dir node |
| 101 | + * If the value is not an object |
| 102 | + * add as a link to the dirNode |
| 103 | + */ |
| 104 | +function traverse (tree, sizeIndex, path, ipldResolver, source, done) { |
| 105 | + const keys = Object.keys(tree) |
| 106 | + |
| 107 | + let tmp = tree |
| 108 | + |
| 109 | + asyncEach(keys, (key, cb) => { |
| 110 | + if (isLeaf(tmp[key])) { |
| 111 | + cb() |
| 112 | + } else { |
| 113 | + path = path ? path + '/' + key : key |
| 114 | + console.log('->', path) |
| 115 | + |
| 116 | + traverse(tmp[key], sizeIndex, path, ipldResolver, source, (err, multihash) => { |
| 117 | + if (err) { |
| 118 | + return done(err) |
| 119 | + } |
| 120 | + tmp[key] = multihash |
| 121 | + cb() |
| 122 | + }) |
| 123 | + } |
| 124 | + }, () => { |
| 125 | + next(tmp, done) |
| 126 | + }) |
| 127 | + |
| 128 | + function next (tree, cb) { |
| 129 | + // at this stage, all keys are multihashes |
| 130 | + // create a dir node |
| 131 | + // add all the multihashes as links |
| 132 | + // return this new node multihash |
| 133 | + |
| 134 | + const keys = Object.keys(tree) |
| 135 | + |
| 136 | + const ufsDir = new UnixFS('directory') |
| 137 | + const node = new DAGNode(ufsDir.marshal()) |
| 138 | + |
| 139 | + keys.forEach((key) => { |
| 140 | + const b58mh = mh.toB58String(tree[key]) |
| 141 | + const link = new DAGLink(key, sizeIndex[b58mh], tree[key]) |
| 142 | + node.addRawLink(link) |
| 143 | + }) |
| 144 | + |
| 145 | + console.log('0---->', path) |
| 146 | + node.multihash((err, multihash) => { |
| 147 | + if (err) { |
| 148 | + return cb(err) |
| 149 | + } |
| 150 | + node.size((err, size) => { |
| 151 | + if (err) { |
| 152 | + return cb(err) |
| 153 | + } |
| 154 | + |
| 155 | + sizeIndex[mh.toB58String(multihash)] = size |
| 156 | + console.log('1---->', path) |
| 157 | + |
| 158 | + ipldResolver.put({ |
| 159 | + node: node, |
| 160 | + cid: new CID(multihash) |
| 161 | + }, (err) => { |
| 162 | + if (err) { |
| 163 | + source.push(new Error('failed to store dirNode')) |
| 164 | + } else if (path) { |
| 165 | + console.log('2---->', path) |
| 166 | + source.push({ |
| 167 | + path: path, |
| 168 | + multihash: multihash, |
| 169 | + size: size |
| 170 | + }) |
| 171 | + } |
| 172 | + |
| 173 | + cb(null, multihash) |
| 174 | + }) |
| 175 | + }) |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +function isLeaf (value) { |
| 181 | + return !(typeof value === 'object' && !Buffer.isBuffer(value)) |
| 182 | +} |
0 commit comments