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

fix: support slashes in filenames #2

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
3 changes: 2 additions & 1 deletion src/importer/flush-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 2 additions & 5 deletions src/importer/tree-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -211,7 +212,3 @@ function createTreeBuilder (ipld, _options) {
})
}
}

function notEmpty (str) {
return Boolean(str)
}
11 changes: 11 additions & 0 deletions src/utils/to-path-components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const toPathComponents = (path = '') => {
// split on / unless escaped with \
return (path
.trim()
.match(/([^\\\][^/]|\\\/)+/g) || [])
.filter(Boolean)
}

module.exports = toPathComponents
18 changes: 18 additions & 0 deletions test/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([{
Expand Down