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

fix: support slashes in filenames #4

Merged
merged 1 commit into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"chai": "^4.2.0",
"detect-node": "^2.0.4",
"dirty-chai": "^2.0.1",
"ipfs-unixfs-importer": "~0.34.0",
"ipfs-unixfs-importer": "~0.36.0",
"ipld": "~0.20.0",
"ipld-dag-pb": "~0.15.0",
"pull-pushable": "^2.2.0",
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function pathBaseAndRest (path) {

return {
base: pathBase,
rest: pathRest.split('/').filter(Boolean)
rest: toPathComponents(pathRest)
}
}

Expand Down Expand Up @@ -104,3 +104,11 @@ function join (paths) {
return acc + path
}, '')
}

const toPathComponents = (path = '') => {
// split on / unless escaped with \
return (path
.trim()
.match(/([^\\\][^/]|\\\/)+/g) || [])
.filter(Boolean)
}
28 changes: 28 additions & 0 deletions test/exporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,34 @@ describe('exporter', () => {
})
})

it('small file in a directory with an escaped slash in the title', (done) => {
const fileName = `small-\\/file-${Math.random()}.txt`
const filePath = `/foo/${fileName}`

pull(
pull.values([{
path: filePath,
content: pull.values([smallFile])
}]),
importer(ipld),
pull.collect((err, files) => {
expect(err).to.not.exist()

const path = `/ipfs/${new CID(files[1].multihash).toBaseEncodedString()}/${fileName}`

pull(
exporter(path, ipld),
pull.collect((err, files) => {
expect(err).to.not.exist()
expect(files.length).to.equal(1)
expect(files[0].path).to.equal(fileName)
done()
})
)
})
)
})

it('exports a chunk of a file with no links', (done) => {
const offset = 0
const length = 5
Expand Down