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

Commit f6c15c6

Browse files
committed
fix: cat: filtering out result files
1 parent 21f49a3 commit f6c15c6

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/core/components/files.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,19 @@ 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) => {
138143
if (err) { return d.abort(err) }
139144
if (files && files.length > 1) {
140-
files = files.filter((file) => file.path === ipfsPath)
145+
files = files.filter(filterFile)
141146
}
142147
if (!files || !files.length) {
143148
return d.abort(new Error('No such file'))
@@ -286,3 +291,11 @@ module.exports = function files (self) {
286291
lsPullStreamImmutable: _lsPullStreamImmutable
287292
}
288293
}
294+
295+
function normalizePath (path) {
296+
if (path.charAt(path.length - 1) === '/') {
297+
path = path.substring(0, path.length - 1)
298+
}
299+
300+
return path
301+
}

0 commit comments

Comments
 (0)