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

Commit

Permalink
fix: cat: test file existence after filtering (#1148)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pgte authored and daviddias committed Dec 15, 2017
1 parent 5bfde87 commit 34f28ef
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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
}

0 comments on commit 34f28ef

Please sign in to comment.