diff --git a/.gitignore b/.gitignore index db6288ec0..510263842 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ test/setup/tmp-disposable-nodes-addrs.json dist coverage +**/*.swp diff --git a/README.md b/README.md index 6263151af..f1f64e5d9 100644 --- a/README.md +++ b/README.md @@ -150,10 +150,10 @@ Complete documentation for these methods is coming with: https://github.com/ipfs > `ipfs.util.addFromFs(path, option, callback)` -Reads a file from `path` on the filesystem and adds it to IPFS. If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories. +Reads a file from `path` on the filesystem and adds it to IPFS. If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories. To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`. ```JavaScript -ipfs.util.addFromFs('path/to/a/file', { recursive: true }, (err, result) => { +ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => { if (err) { throw err } diff --git a/src/get-files-stream.js b/src/get-files-stream.js index 609c6e842..73e113c8c 100644 --- a/src/get-files-stream.js +++ b/src/get-files-stream.js @@ -45,8 +45,12 @@ function loadPaths (opts, file) { } if (stats.isDirectory() && opts.recursive) { - const mg = new glob.sync.GlobSync(`${escape(file)}/**/*`, { - follow: followSymlinks + const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/') + const mg = new glob.sync.GlobSync(`${globEscapedDir}**/*`, { + follow: followSymlinks, + ignore: (opts.ignore || []).map(function (ignoreGlob) { + return globEscapedDir + ignoreGlob + }) }) return mg.found @@ -88,7 +92,7 @@ function loadPaths (opts, file) { } return { - path: file, + path: path.basename(file), content: fs.createReadStream(file) } } diff --git a/test/ipfs-api/util.spec.js b/test/ipfs-api/util.spec.js index d103844bd..3ee8619b5 100644 --- a/test/ipfs-api/util.spec.js +++ b/test/ipfs-api/util.spec.js @@ -60,11 +60,21 @@ describe('.util', () => { }) }) + it('.fsAdd add and ignore a directory', (done) => { + const filesPath = path.join(__dirname, '../fixtures/test-folder') + ipfs.util.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => { + expect(err).to.not.exist + expect(result.length).to.be.below(9) + done() + }) + }) + it('.fsAdd a file', (done) => { const filePath = path.join(__dirname, '../fixtures/testfile.txt') ipfs.util.addFromFs(filePath, (err, result) => { expect(err).to.not.exist - expect(result.length).to.be.above(5) + expect(result.length).to.be.equal(1) + expect(result[0].path).to.be.equal('testfile.txt') done() }) })