|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const promisify = require('promisify-es6') |
| 4 | +const waterfall = require('async/waterfall') |
| 5 | +const parallel = require('async/parallel') |
| 6 | +const series = require('async/series') |
| 7 | +const path = require('path') |
| 8 | +const { |
| 9 | + traverseTo, |
| 10 | + addLink, |
| 11 | + updateTree, |
| 12 | + updateMfsRoot |
| 13 | +} = require('./utils') |
| 14 | +const stat = require('./stat') |
| 15 | + |
| 16 | +const defaultOptions = { |
| 17 | + recursive: false, |
| 18 | + parents: false, |
| 19 | + flush: true, |
| 20 | + format: 'dag-pb', |
| 21 | + hashAlg: 'sha2-256' |
| 22 | +} |
| 23 | + |
| 24 | +module.exports = function mfsCp (ipfs) { |
| 25 | + return promisify(function () { |
| 26 | + const args = Array.prototype.slice.call(arguments) |
| 27 | + const callback = args.pop() |
| 28 | + |
| 29 | + if (args.length < 2) { |
| 30 | + return callback(new Error('Please specify a source(s) and a destination')) |
| 31 | + } |
| 32 | + |
| 33 | + let destination = args.pop() |
| 34 | + let options = {} |
| 35 | + |
| 36 | + if (typeof destination !== 'string') { |
| 37 | + options = destination |
| 38 | + destination = args.pop() |
| 39 | + } |
| 40 | + |
| 41 | + options = Object.assign({}, defaultOptions, options) |
| 42 | + |
| 43 | + const sources = args.map(source => ({ |
| 44 | + path: source, |
| 45 | + name: path.basename(source), |
| 46 | + dir: path.dirname(source) |
| 47 | + })) |
| 48 | + |
| 49 | + destination = { |
| 50 | + path: destination, |
| 51 | + name: path.basename(destination), |
| 52 | + dir: path.dirname(destination) |
| 53 | + } |
| 54 | + |
| 55 | + if (sources.length < 1) { |
| 56 | + return callback(new Error('Please specify a path to copy')) |
| 57 | + } |
| 58 | + |
| 59 | + if (sources.length === 1) { |
| 60 | + return copyToFile(ipfs, sources.pop(), destination, options, callback) |
| 61 | + } |
| 62 | + |
| 63 | + copyToDirectory(ipfs, sources, destination, options, callback) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +const copyToFile = (ipfs, source, destination, options, callback) => { |
| 68 | + waterfall([ |
| 69 | + (cb) => { |
| 70 | + parallel([ |
| 71 | + (next) => stat(ipfs)(source.path, options, next), |
| 72 | + (next) => stat(ipfs)(destination.path, options, (error) => { |
| 73 | + if (!error) { |
| 74 | + return next(new Error('Destination exists')) |
| 75 | + } |
| 76 | + |
| 77 | + next() |
| 78 | + }), |
| 79 | + (next) => traverseTo(ipfs, destination.dir, options, next) |
| 80 | + ], cb) |
| 81 | + }, |
| 82 | + ([sourceStats, _, dest], cb) => { |
| 83 | + waterfall([ |
| 84 | + (next) => addLink(ipfs, { |
| 85 | + parent: dest.node, |
| 86 | + child: sourceStats, // nb. file size here is not including protobuf wrapper so is wrong |
| 87 | + name: destination.name |
| 88 | + }, next), |
| 89 | + (newParent, next) => { |
| 90 | + dest.node = newParent |
| 91 | + updateTree(ipfs, dest, next) |
| 92 | + }, |
| 93 | + (newRoot, cb) => updateMfsRoot(ipfs, newRoot.node.multihash, cb) |
| 94 | + ], cb) |
| 95 | + } |
| 96 | + ], callback) |
| 97 | +} |
| 98 | + |
| 99 | +const copyToDirectory = (ipfs, sources, destination, options, callback) => { |
| 100 | + waterfall([ |
| 101 | + (cb) => { |
| 102 | + series([ |
| 103 | + // stat in parallel |
| 104 | + (done) => parallel( |
| 105 | + sources.map(source => (next) => stat(ipfs)(source.path, options, next)), |
| 106 | + done |
| 107 | + ), |
| 108 | + // this could end up changing the root mfs node so do it after parallel |
| 109 | + (done) => traverseTo(ipfs, destination.path, Object.assign({}, options, { |
| 110 | + createLastComponent: true |
| 111 | + }), done) |
| 112 | + ], cb) |
| 113 | + }, |
| 114 | + (results, cb) => { |
| 115 | + const dest = results.pop() |
| 116 | + const sourceStats = results[0] |
| 117 | + |
| 118 | + waterfall([ |
| 119 | + (next) => waterfall([ |
| 120 | + (done) => done(null, dest.node) |
| 121 | + ].concat( |
| 122 | + sourceStats.map((sourceStat, index) => { |
| 123 | + return (dest, done) => { |
| 124 | + return addLink(ipfs, { |
| 125 | + parent: dest, |
| 126 | + child: sourceStat, // nb. file size here is not including protobuf wrapper so is wrong |
| 127 | + name: sources[index].name |
| 128 | + }, done) |
| 129 | + } |
| 130 | + }) |
| 131 | + ), next), |
| 132 | + (newParent, next) => { |
| 133 | + dest.node = newParent |
| 134 | + |
| 135 | + updateTree(ipfs, dest, next) |
| 136 | + }, |
| 137 | + (newRoot, cb) => updateMfsRoot(ipfs, newRoot.node.multihash, cb) |
| 138 | + ], cb) |
| 139 | + } |
| 140 | + ], callback) |
| 141 | +} |
0 commit comments