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

Commit 42ccb00

Browse files
committed
Handle directories with odd names
Previously, adding a directory with square brackets resulted in a strange, silent error [1]. This happened because directory names were being passed to a file- matching glob designed to recursively stream files from the directory. When filenames contained meaningful glob symbols (like [ or *), the glob matching would fail. This commit resolves this problem by escaping glob characters on filenames, before the filename is inserted into the main glob in src/get-file-streams.js L48. We add a dependency to glob-escape in the process, and a new test in test/ipfs-api/util.spec.js.
1 parent ee96a77 commit 42ccb00

File tree

11 files changed

+97
-3
lines changed

11 files changed

+97
-3
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
"detect-node": "^2.0.3",
3030
"flatmap": "0.0.3",
3131
"glob": "^7.1.1",
32+
"glob-escape": "0.0.2",
3233
"ipfs-block": "^0.5.0",
3334
"ipld-dag-pb": "^0.9.0",
3435
"is-ipfs": "^0.2.1",
3536
"isstream": "^0.1.2",
36-
"multiaddr": "^2.0.3",
3737
"lru-cache": "^4.0.1",
38+
"multiaddr": "^2.0.3",
3839
"multipart-stream": "^2.0.1",
3940
"ndjson": "^1.4.3",
4041
"once": "^1.4.0",
@@ -113,4 +114,4 @@
113114
"url": "https://github.com/ipfs/js-ipfs-api/issues"
114115
},
115116
"homepage": "https://github.com/ipfs/js-ipfs-api"
116-
}
117+
}

src/get-files-stream.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const isNode = require('detect-node')
44
const Multipart = require('multipart-stream')
55
const flatmap = require('flatmap')
6+
const escape = require('glob-escape')
67

78
function headers (file) {
89
const name = file.path || ''
@@ -44,7 +45,7 @@ function loadPaths (opts, file) {
4445
}
4546

4647
if (stats.isDirectory() && opts.recursive) {
47-
const mg = new glob.sync.GlobSync(`${file}/**/*`, {
48+
const mg = new glob.sync.GlobSync(`${escape(file)}/**/*`, {
4849
follow: followSymlinks
4950
})
5051

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const ipfs = require('../src')('localhost', 5001)
4+
5+
const f1 = 'Hello'
6+
const f2 = 'World'
7+
8+
ipfs.add([new Buffer(f1), new Buffer(f2)], function (err, res) {
9+
if (err || !res) return console.log(err)
10+
11+
for (let i = 0; i < res.length; i++) {
12+
console.log(res[i])
13+
}
14+
})
15+
16+
ipfs.add(['./files/hello.txt', './files/ipfs.txt'], function (err, res) {
17+
if (err || !res) return console.log(err)
18+
19+
for (let i = 0; i < res.length; i++) {
20+
console.log(res[i])
21+
}
22+
})
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const ipfs = require('../src')('localhost', 5001)
4+
5+
const hash = [
6+
'QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w',
7+
'QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu'
8+
]
9+
10+
ipfs.cat(hash, function (err, res) {
11+
if (err || !res) return console.log(err)
12+
13+
if (res.readable) {
14+
res.pipe(process.stdout)
15+
} else {
16+
console.log(res)
17+
}
18+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
IPFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
files/hello.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env node
2+
3+
'use strict'
4+
5+
const ipfs = require('../src')('localhost', 5001)
6+
const files = process.argv.slice(2)
7+
8+
ipfs.add(files, {recursive: true}, function (err, res) {
9+
if (err || !res) return console.log(err)
10+
11+
for (let i = 0; i < res.length; i++) {
12+
console.log('added', res[i].Hash, res[i].Name)
13+
}
14+
})
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const ipfs = require('../src')('localhost', 5001)
4+
5+
const hash = ['QmdbHK6gMiecyjjSoPnfJg6iKMF7v6E2NkoBgGpmyCoevh']
6+
7+
ipfs.ls(hash, function (err, res) {
8+
if (err || !res) return console.log(err)
9+
10+
res.Objects.forEach(function (node) {
11+
console.log(node.Hash)
12+
13+
console.log('Links [%d]', node.Links.length)
14+
node.Links.forEach(function (link, i) {
15+
console.log('[%d]', i, link)
16+
})
17+
})
18+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict'
2+
3+
const ipfs = require('../src')('localhost', 5001)
4+
5+
ipfs.commands(function (err, res) {
6+
if (err) throw err
7+
console.log(res)
8+
})

test/ipfs-api/util.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ describe('.util', () => {
5151
})
5252
})
5353

54+
it('.fsAdd a directory with an odd name', (done) => {
55+
const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]')
56+
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
57+
expect(err).to.not.exist
58+
expect(result.length).to.be.above(8)
59+
done()
60+
})
61+
})
62+
5463
it('.fsAdd a file', (done) => {
5564
const filePath = path.join(__dirname, '../fixtures/testfile.txt')
5665
ipfs.util.addFromFs(filePath, (err, result) => {

0 commit comments

Comments
 (0)