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

fix: cat: test file existence after filtering #1148

Merged
merged 4 commits into from
Dec 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}