From 34f28efabf501aff6c2c7983e6c794b4896b6a2c Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 15 Dec 2017 11:09:00 +0000 Subject: [PATCH] fix: cat: test file existence after filtering (#1148) * fix: cat: test file existence *after* filtering. Should fix #1142 * fix: cat: filtering out result files * fix: cat: for when the ipfs path is a buffer * fix: cat: remove /ipfs/ prefix if there is one --- src/core/components/files.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/core/components/files.js b/src/core/components/files.js index 4a4f60c797..009e1229ad 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -130,22 +130,25 @@ module.exports = function files (self) { throw new Error('You must supply an ipfsPath') } + ipfsPath = normalizePath(ipfsPath) + const pathComponents = ipfsPath.split('/') + const restPath = normalizePath(pathComponents.slice(1).join('/')) + const filterFile = (file) => (restPath && file.path === restPath) || (file.path === ipfsPath) + const d = deferred.source() pull( exporter(ipfsPath, self._ipldResolver), pull.collect((err, files) => { - if (err) { d.end(err) } + if (err) { return d.abort(err) } + if (files && files.length > 1) { + files = files.filter(filterFile) + } if (!files || !files.length) { return d.abort(new Error('No such file')) } - if (files.length > 1) { - files = files.filter((file) => file.path === ipfsPath) - } - const file = files[0] - const content = file.content if (!content && file.type === 'dir') { return d.abort(new Error('this dag node is a directory')) @@ -288,3 +291,17 @@ module.exports = function files (self) { lsPullStreamImmutable: _lsPullStreamImmutable } } + +function normalizePath (path) { + if (Buffer.isBuffer(path)) { + path = toB58String(path) + } + if (path.indexOf('/ipfs/') === 0) { + path = path.substring('/ipfs/'.length) + } + if (path.charAt(path.length - 1) === '/') { + path = path.substring(0, path.length - 1) + } + + return path +}