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

Commit 5c78746

Browse files
pgterichardschneider
authored andcommitted
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
1 parent 7e4cf65 commit 5c78746

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/core/components/files.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,25 @@ module.exports = function files (self) {
130130
throw new Error('You must supply an ipfsPath')
131131
}
132132

133+
ipfsPath = normalizePath(ipfsPath)
134+
const pathComponents = ipfsPath.split('/')
135+
const restPath = normalizePath(pathComponents.slice(1).join('/'))
136+
const filterFile = (file) => (restPath && file.path === restPath) || (file.path === ipfsPath)
137+
133138
const d = deferred.source()
134139

135140
pull(
136141
exporter(ipfsPath, self._ipldResolver),
137142
pull.collect((err, files) => {
138-
if (err) { d.end(err) }
143+
if (err) { return d.abort(err) }
144+
if (files && files.length > 1) {
145+
files = files.filter(filterFile)
146+
}
139147
if (!files || !files.length) {
140148
return d.abort(new Error('No such file'))
141149
}
142150

143-
if (files.length > 1) {
144-
files = files.filter((file) => file.path === ipfsPath)
145-
}
146-
147151
const file = files[0]
148-
149152
const content = file.content
150153
if (!content && file.type === 'dir') {
151154
return d.abort(new Error('this dag node is a directory'))
@@ -288,3 +291,17 @@ module.exports = function files (self) {
288291
lsPullStreamImmutable: _lsPullStreamImmutable
289292
}
290293
}
294+
295+
function normalizePath (path) {
296+
if (Buffer.isBuffer(path)) {
297+
path = toB58String(path)
298+
}
299+
if (path.indexOf('/ipfs/') === 0) {
300+
path = path.substring('/ipfs/'.length)
301+
}
302+
if (path.charAt(path.length - 1) === '/') {
303+
path = path.substring(0, path.length - 1)
304+
}
305+
306+
return path
307+
}

0 commit comments

Comments
 (0)