diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 87cdbbdf35..1abedb5122 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -15,8 +15,6 @@ const utils = require('../../utils') const print = require('../../utils').print const createProgressBar = require('../../utils').createProgressBar -const WRAPPER = 'wrapper/' - function checkPath (inPath, recursive) { // This function is to check for the following possible inputs // 1) "." add the cwd but throw error for no recursion flag @@ -59,7 +57,6 @@ function getTotalBytes (path, recursive, cb) { function addPipeline (index, addStream, list, argv) { const { - wrapWithDirectory, quiet, quieter, silent @@ -79,17 +76,9 @@ function addPipeline (index, addStream, list, argv) { pull.filter((file) => !file.isDirectory), pull.map((file) => ({ path: file.path.substring(index, file.path.length), - originalPath: file.path - })), - pull.map((file) => ({ - path: wrapWithDirectory ? WRAPPER + file.path : file.path, - content: fs.createReadStream(file.originalPath) + content: fs.createReadStream(file.path) })), addStream, - pull.map((file) => ({ - hash: file.hash, - path: wrapWithDirectory ? file.path.substring(WRAPPER.length) : file.path - })), pull.collect((err, added) => { if (err) { throw err @@ -198,7 +187,8 @@ module.exports = { cidVersion: argv.cidVersion, rawLeaves: argv.rawLeaves, onlyHash: argv.onlyHash, - hashAlg: argv.hash + hashAlg: argv.hash, + wrapWithDirectory: argv.wrapWithDirectory } // Temporary restriction on raw-leaves: diff --git a/src/core/components/files.js b/src/core/components/files.js index ee7eca713b..ec2e69f512 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -17,6 +17,8 @@ const OtherBuffer = require('buffer').Buffer const CID = require('cids') const toB58String = require('multihashes').toB58String +const WRAPPER = 'wrapper/' + function noop () {} function prepareFile (self, opts, file, callback) { @@ -33,8 +35,9 @@ function prepareFile (self, opts, file, callback) { (node, cb) => { const b58Hash = cid.toBaseEncodedString() + cb(null, { - path: file.path || b58Hash, + path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash), hash: b58Hash, size: node.size }) @@ -42,7 +45,7 @@ function prepareFile (self, opts, file, callback) { ], callback) } -function normalizeContent (content) { +function normalizeContent (opts, content) { if (!Array.isArray(content)) { content = [content] } @@ -68,6 +71,14 @@ function normalizeContent (content) { } } + if (opts.wrapWithDirectory && !data.path) { + throw new Error('Must provide a path when wrapping with a directory') + } + + if (opts.wrapWithDirectory) { + data.path = WRAPPER + data.path + } + return data }) } @@ -123,7 +134,7 @@ module.exports = function files (self) { opts.progress = progress return pull( - pull.map(normalizeContent), + pull.map(normalizeContent.bind(null, opts)), pull.flatten(), importer(self._ipld, opts), pull.asyncMap(prepareFile.bind(null, self, opts)) diff --git a/src/http/api/resources/files.js b/src/http/api/resources/files.js index f74cf0a7fe..ff8fb13b1b 100644 --- a/src/http/api/resources/files.js +++ b/src/http/api/resources/files.js @@ -148,7 +148,8 @@ exports.add = { then: Joi.boolean().valid(false).required(), otherwise: Joi.boolean().valid(false) }), - 'only-hash': Joi.boolean() + 'only-hash': Joi.boolean(), + 'wrap-with-directory': Joi.boolean() }) // TODO: Necessary until validate "recursive", "stream-channels" etc. .options({ allowUnknown: true }) @@ -208,7 +209,8 @@ exports.add = { rawLeaves: request.query['raw-leaves'], progress: request.query.progress ? progressHandler : null, onlyHash: request.query['only-hash'], - hashAlg: request.query['hash'] + hashAlg: request.query['hash'], + wrapWithDirectory: request.query['wrap-with-directory'] } const aborter = abortable() @@ -246,7 +248,7 @@ exports.add = { ipfs.files.addPullStream(options), pull.map((file) => { return { - Name: file.path ? file.path : file.hash, + Name: file.path, //addPullStream already turned this into a hash if it wanted to Hash: file.hash, Size: file.size }