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

Commit 3171fab

Browse files
committed
fix: support slashes in filenames
Someone, somewhere, will have a slash in a file name. This upsets the importer and exporter at the moment. This PR fixes the importer.
1 parent d736bc2 commit 3171fab

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

src/importer/flush-tree.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const dagPB = require('ipld-dag-pb')
66
const mapValues = require('async/mapValues')
77
const waterfall = require('async/waterfall')
88
const persist = require('../utils/persist')
9+
const toPathComponents = require('../utils/to-path-components')
910
const DAGLink = dagPB.DAGLink
1011
const DAGNode = dagPB.DAGNode
1112

@@ -54,7 +55,7 @@ function createTree (files) {
5455
const fileTree = {}
5556

5657
files.forEach((file) => {
57-
let splitted = file.path.split('/')
58+
let splitted = toPathComponents(file.path)
5859
if (splitted.length === 1) {
5960
return // adding just one file
6061
}

src/importer/tree-builder.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const pushable = require('pull-pushable')
99
const DirFlat = require('./dir-flat')
1010
const flatToShard = require('./flat-to-shard')
1111
const Dir = require('./dir')
12+
const toPathComponents = require('../utils/to-path-components')
1213

1314
module.exports = createTreeBuilder
1415

@@ -91,7 +92,7 @@ function createTreeBuilder (ipld, _options) {
9192
// ---- Add to tree
9293

9394
function addToTree (elem, callback) {
94-
const pathElems = (elem.path || '').split('/').filter(notEmpty)
95+
const pathElems = toPathComponents(elem.path || '')
9596
let parent = tree
9697
const lastIndex = pathElems.length - 1
9798

@@ -211,7 +212,3 @@ function createTreeBuilder (ipld, _options) {
211212
})
212213
}
213214
}
214-
215-
function notEmpty (str) {
216-
return Boolean(str)
217-
}

src/utils/to-path-components.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const toPathComponents = (path = '') => {
4+
// split on / unless escaped with \
5+
return (path
6+
.trim()
7+
.match(/([^\\\][^/]|\\\/)+/g) || [])
8+
.filter(Boolean)
9+
}
10+
11+
module.exports = toPathComponents

test/importer.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,24 @@ module.exports = (repo) => {
309309
)
310310
})
311311

312+
it('small file with an escaped slash in the title', (done) => {
313+
const filePath = `small-\\/file-${Math.random()}.txt`
314+
315+
pull(
316+
pull.values([{
317+
path: filePath,
318+
content: pull.values([smallFile])
319+
}]),
320+
importer(ipld, options),
321+
pull.collect((err, files) => {
322+
expect(err).to.not.exist()
323+
expect(files.length).to.equal(1)
324+
expect(files[0].path).to.equal(filePath)
325+
done()
326+
})
327+
)
328+
})
329+
312330
it('small file (smaller than a chunk)', (done) => {
313331
pull(
314332
pull.values([{

0 commit comments

Comments
 (0)