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 +}