Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +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. To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`.
Reads a file or folder from `path` on the filesystem and adds it to IPFS. Options:
- **recursive**: If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories.
- **ignore**: To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`.
- **hidden**: hidden/dot files (files or folders starting with a `.`, for example, `.git/`) are not included by default. To add them, use the option `{ hidden: true }`.

```JavaScript
ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => {
Expand Down
1 change: 1 addition & 0 deletions src/get-files-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function loadPaths (opts, file) {
const globEscapedDir = escape(file) + (file.endsWith('/') ? '' : '/')
const mg = new glob.sync.GlobSync(`${globEscapedDir}**/*`, {
follow: followSymlinks,
dot: opts.hidden,
ignore: (opts.ignore || []).map(function (ignoreGlob) {
return globEscapedDir + ignoreGlob
})
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/test-folder/.hiddenTest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Aha! You found me!
69 changes: 41 additions & 28 deletions test/ipfs-api/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,53 @@ describe('.util', () => {
})
})

it('.fsAdd a directory', (done) => {
const filesPath = path.join(__dirname, '../fixtures/test-folder')
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.above(8)
done()
describe('.fsAdd should add', () => {
it('a directory', (done) => {
const filesPath = path.join(__dirname, '../fixtures/test-folder')
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.above(8)
done()
})
})
})

it('.fsAdd a directory with an odd name', (done) => {
const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]')
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.above(8)
done()
it('a directory with an odd name', (done) => {
const filesPath = path.join(__dirname, '../fixtures/weird name folder [v0]')
ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.above(8)
done()
})
})
})

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('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.equal(1)
expect(result[0].path).to.be.equal('testfile.txt')
done()
it('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.equal(1)
expect(result[0].path).to.be.equal('testfile.txt')
done()
})
})

it('a hidden file in a directory', (done) => {
const filesPath = path.join(__dirname, '../fixtures/test-folder')
ipfs.util.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.above(10)
expect(result.map(object => object.path)).to.include('test-folder/.hiddenTest.txt')
expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
done()
})
})
})

Expand Down