From b79960ca5e81e6cd3e54fc23941ca6fae58adaf4 Mon Sep 17 00:00:00 2001 From: Michael Garvin Date: Fri, 30 Mar 2018 15:21:32 -0700 Subject: [PATCH 1/2] feat: add wrapWithDirectory flag to files.add et al --- package.json | 6 +++--- src/cli/commands/files/add.js | 16 +++------------- src/core/components/files.js | 17 ++++++++++++++--- src/http/api/resources/files.js | 8 +++++--- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index aa93799263..d3ad8e7054 100644 --- a/package.json +++ b/package.json @@ -73,8 +73,8 @@ "expose-loader": "^0.7.5", "form-data": "^2.3.2", "hat": "0.0.3", - "ipfsd-ctl": "~0.31.0", - "interface-ipfs-core": "~0.58.0", + "interface-ipfs-core": "^0.60.1", + "ipfsd-ctl": "~0.32.0", "lodash": "^4.17.5", "mocha": "^5.0.5", "ncp": "^2.0.0", @@ -106,7 +106,7 @@ "hapi-set-header": "^1.0.2", "hoek": "^5.0.3", "human-to-milliseconds": "^1.0.0", - "ipfs-api": "^19.0.0", + "ipfs-api": "^20.0.0", "ipfs-bitswap": "~0.19.0", "ipfs-block": "~0.6.1", "ipfs-block-service": "~0.13.0", diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 23c9fdb60d..db0d2b83e0 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -14,8 +14,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 @@ -58,7 +56,6 @@ function getTotalBytes (path, recursive, cb) { function addPipeline (index, addStream, list, argv) { const { - wrapWithDirectory, quiet, quieter, silent @@ -78,17 +75,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 @@ -191,7 +180,8 @@ module.exports = { : Infinity, cidVersion: argv.cidVersion, rawLeaves: argv.rawLeaves, - onlyHash: argv.onlyHash + onlyHash: argv.onlyHash, + wrapWithDirectory: argv.wrapWithDirectory } // Temporary restriction on raw-leaves: diff --git a/src/core/components/files.js b/src/core/components/files.js index 3ec915065d..926cb50bb1 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) { 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 }) } @@ -119,7 +130,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 91861965f3..a3e04d52b0 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 }) @@ -207,7 +208,8 @@ exports.add = { cidVersion: request.query['cid-version'], rawLeaves: request.query['raw-leaves'], progress: request.query.progress ? progressHandler : null, - onlyHash: request.query['only-hash'] + onlyHash: request.query['only-hash'], + wrapWithDirectory: request.query['wrap-with-directory'] } const aborter = abortable() @@ -245,7 +247,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 } From 9449a2d742229833d7ce6c635ce7bc9c1000c445 Mon Sep 17 00:00:00 2001 From: victorbjelkholm Date: Fri, 27 Apr 2018 15:14:59 +0200 Subject: [PATCH 2/2] chore: Fix linting --- src/core/components/files.js | 1 - src/http/api/resources/files.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/components/files.js b/src/core/components/files.js index ec2e69f512..c1a8f4cb7b 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -35,7 +35,6 @@ function prepareFile (self, opts, file, callback) { (node, cb) => { const b58Hash = cid.toBaseEncodedString() - cb(null, { path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash), hash: b58Hash, diff --git a/src/http/api/resources/files.js b/src/http/api/resources/files.js index ff8fb13b1b..672ebd2aa4 100644 --- a/src/http/api/resources/files.js +++ b/src/http/api/resources/files.js @@ -248,7 +248,7 @@ exports.add = { ipfs.files.addPullStream(options), pull.map((file) => { return { - Name: file.path, //addPullStream already turned this into a hash if it wanted to + Name: file.path, // addPullStream already turned this into a hash if it wanted to Hash: file.hash, Size: file.size }