diff --git a/src/importer/flush-tree.js b/src/importer/flush-tree.js index f12d121..bf26b59 100644 --- a/src/importer/flush-tree.js +++ b/src/importer/flush-tree.js @@ -6,6 +6,7 @@ const dagPB = require('ipld-dag-pb') const mapValues = require('async/mapValues') const waterfall = require('async/waterfall') const persist = require('../utils/persist') +const toPathComponents = require('../utils/to-path-components') const DAGLink = dagPB.DAGLink const DAGNode = dagPB.DAGNode @@ -54,7 +55,7 @@ function createTree (files) { const fileTree = {} files.forEach((file) => { - let splitted = file.path.split('/') + let splitted = toPathComponents(file.path) if (splitted.length === 1) { return // adding just one file } diff --git a/src/importer/tree-builder.js b/src/importer/tree-builder.js index 63d349f..386fb5c 100644 --- a/src/importer/tree-builder.js +++ b/src/importer/tree-builder.js @@ -9,6 +9,7 @@ const pushable = require('pull-pushable') const DirFlat = require('./dir-flat') const flatToShard = require('./flat-to-shard') const Dir = require('./dir') +const toPathComponents = require('../utils/to-path-components') module.exports = createTreeBuilder @@ -91,7 +92,7 @@ function createTreeBuilder (ipld, _options) { // ---- Add to tree function addToTree (elem, callback) { - const pathElems = (elem.path || '').split('/').filter(notEmpty) + const pathElems = toPathComponents(elem.path || '') let parent = tree const lastIndex = pathElems.length - 1 @@ -211,7 +212,3 @@ function createTreeBuilder (ipld, _options) { }) } } - -function notEmpty (str) { - return Boolean(str) -} diff --git a/src/utils/to-path-components.js b/src/utils/to-path-components.js new file mode 100644 index 0000000..4150d01 --- /dev/null +++ b/src/utils/to-path-components.js @@ -0,0 +1,11 @@ +'use strict' + +const toPathComponents = (path = '') => { + // split on / unless escaped with \ + return (path + .trim() + .match(/([^\\\][^/]|\\\/)+/g) || []) + .filter(Boolean) +} + +module.exports = toPathComponents diff --git a/test/importer.js b/test/importer.js index 9e4a7dc..5ad4bee 100644 --- a/test/importer.js +++ b/test/importer.js @@ -309,6 +309,24 @@ module.exports = (repo) => { ) }) + it('small file with an escaped slash in the title', (done) => { + const filePath = `small-\\/file-${Math.random()}.txt` + + pull( + pull.values([{ + path: filePath, + content: pull.values([smallFile]) + }]), + importer(ipld, options), + pull.collect((err, files) => { + expect(err).to.not.exist() + expect(files.length).to.equal(1) + expect(files[0].path).to.equal(filePath) + done() + }) + ) + }) + it('small file (smaller than a chunk)', (done) => { pull( pull.values([{