Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit b69abce

Browse files
committed
fix: support slashes in filenames
Someone, somewhere, will have a slash in a filename. This upsets the importer and exporters at the moment. This PR fixes the exporter.
1 parent d6c57be commit b69abce

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"chai": "^4.2.0",
4141
"detect-node": "^2.0.4",
4242
"dirty-chai": "^2.0.1",
43-
"ipfs-unixfs-importer": "~0.34.0",
43+
"ipfs-unixfs-importer": "~0.36.0",
4444
"ipld": "~0.20.0",
4545
"ipld-dag-pb": "~0.15.0",
4646
"pull-pushable": "^2.2.0",

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function pathBaseAndRest (path) {
3131

3232
return {
3333
base: pathBase,
34-
rest: pathRest.split('/').filter(Boolean)
34+
rest: toPathComponents(pathRest)
3535
}
3636
}
3737

@@ -104,3 +104,11 @@ function join (paths) {
104104
return acc + path
105105
}, '')
106106
}
107+
108+
const toPathComponents = (path = '') => {
109+
// split on / unless escaped with \
110+
return (path
111+
.trim()
112+
.match(/([^\\\][^/]|\\\/)+/g) || [])
113+
.filter(Boolean)
114+
}

test/exporter.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,34 @@ describe('exporter', () => {
229229
})
230230
})
231231

232+
it('small file in a directory with an escaped slash in the title', (done) => {
233+
const fileName = `small-\\/file-${Math.random()}.txt`
234+
const filePath = `/foo/${fileName}`
235+
236+
pull(
237+
pull.values([{
238+
path: filePath,
239+
content: pull.values([smallFile])
240+
}]),
241+
importer(ipld),
242+
pull.collect((err, files) => {
243+
expect(err).to.not.exist()
244+
245+
const path = `/ipfs/${new CID(files[1].multihash).toBaseEncodedString()}/${fileName}`
246+
247+
pull(
248+
exporter(path, ipld),
249+
pull.collect((err, files) => {
250+
expect(err).to.not.exist()
251+
expect(files.length).to.equal(1)
252+
expect(files[0].path).to.equal(fileName)
253+
done()
254+
})
255+
)
256+
})
257+
)
258+
})
259+
232260
it('exports a chunk of a file with no links', (done) => {
233261
const offset = 0
234262
const length = 5

0 commit comments

Comments
 (0)