Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: cat: filtering out result files
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Dec 14, 2017
1 parent 21f49a3 commit f6c15c6
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,19 @@ 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) { return d.abort(err) }
if (files && files.length > 1) {
files = files.filter((file) => file.path === ipfsPath)
files = files.filter(filterFile)
}
if (!files || !files.length) {
return d.abort(new Error('No such file'))
Expand Down Expand Up @@ -286,3 +291,11 @@ module.exports = function files (self) {
lsPullStreamImmutable: _lsPullStreamImmutable
}
}

function normalizePath (path) {
if (path.charAt(path.length - 1) === '/') {
path = path.substring(0, path.length - 1)
}

return path
}

0 comments on commit f6c15c6

Please sign in to comment.