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

Commit

Permalink
feat(util): add fsAdd, streamAdd and urlAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Aug 17, 2016
1 parent 4cffb47 commit 2ae09fb
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 10 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,43 @@ Complete documentation for these methods is coming with: https://github.com/ipfs

#### Add files or entire directories from the FileSystem to IPFS

> `ipfs.util.fsAdd(path, callback)`
> `ipfs.util.fsAdd(path, option, callback)`
Reads path from disk, if it is a directory, will add it recursively, if not, will add the file.
Reads path from disk (if directory add an options `{ recursive: true }` and adds it to IPFS.

```JavaScript
ipfs.util.fsAdd(<dirpath>, { recursive: true }, (err, result) => {
if (err) {
throw err
}
console.log(result)
})
```

`files` is an array of objects describing the files that were added, such as:

```
[{
path: 'test-folder',
hash: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6',
size: 2278
},
// ...
]
```

#### Add a file from a URL to IPFS

> `ipfs.util.urlAdd(url, callback)`
```JavaScript
ipfs.util.urlAdd('http://example.com/', (err, result) => {
if (err) {
throw err
}
console.log(result)
})

```

#### Add a file from a stream to IPFS
Expand All @@ -137,6 +162,12 @@ Reads path from disk, if it is a directory, will add it recursively, if not, wil
This is very similar to `ipfs.files.add({path:'', content: stream})`. It is like the reverse of cat

```JavaScript
ipfs.util.streamAdd(<readable-stream>, (err, result) => {
if (err) {
throw err
}
console.log(result)
})
```

### Callbacks and promises
Expand Down
File renamed without changes.
12 changes: 10 additions & 2 deletions src/api/add-files.js → src/api/util/fs-add.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const isNode = require('detect-node')
const addToDagNodesTransform = require('../add-to-dagnode-transform')
const addToDagNodesTransform = require('./../../add-to-dagnode-transform')
const promisify = require('promisify-es6')

module.exports = (send) => {
Expand All @@ -12,8 +12,16 @@ module.exports = (send) => {
opts = {}
}

// opts is the real callback --
// 'callback' is being injected by promisify
if (typeof opts === 'function' &&
typeof callback === 'function') {
callback = opts
opts = {}
}

if (!isNode) {
return callback(new Error('Recursive uploads are not supported in the browser'))
return callback(new Error('fsAdd does not work in the browser'))
}

if (typeof path !== 'string') {
Expand Down
11 changes: 10 additions & 1 deletion src/api/add-url.js → src/api/util/url-add.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const Wreck = require('wreck')
const addToDagNodesTransform = require('../add-to-dagnode-transform')
const addToDagNodesTransform = require('./../../add-to-dagnode-transform')

const promisify = require('promisify-es6')

Expand All @@ -13,6 +13,14 @@ module.exports = (send) => {
opts = {}
}

// opts is the real callback --
// 'callback' is being injected by promisify
if (typeof opts === 'function' &&
typeof callback === 'function') {
callback = opts
opts = {}
}

if (typeof url !== 'string' ||
!url.startsWith('http')) {
return callback(new Error('"url" param must be an http(s) url'))
Expand All @@ -24,6 +32,7 @@ module.exports = (send) => {
if (err) {
return callback(err)
}
console.log('got page back')

sendWithTransform({
path: 'add',
Expand Down
12 changes: 7 additions & 5 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

function requireCommands () {
const cmds = {
add: require('./api/add'), // add alias
createAddStream: require('./api/add-stream'), // add stream alias
// add and createAddStream alias
add: require('./api/add'),
createAddStream: require('./api/create-add-stream'),
bitswap: require('./api/bitswap'),
block: require('./api/block'),
bootstrap: require('./api/bootstrap'),
Expand Down Expand Up @@ -33,7 +34,7 @@ function requireCommands () {
cmds.files = function (send) {
const files = require('./api/files')(send)
files.add = require('./api/add')(send)
files.createAddStream = require('./api/add-stream.js')(send)
files.createAddStream = require('./api/create-add-stream.js')(send)
files.get = require('./api/get')(send)

// aliases
Expand All @@ -46,8 +47,9 @@ function requireCommands () {

cmds.util = function (send) {
const util = {
addFiles: require('./api/add-files')(send),
addUrl: require('./api/add-url')(send)
fsAdd: require('./api/util/fs-add')(send),
streamAdd: require('./api/add')(send),
urlAdd: require('./api/util/url-add')(send)
}
return util
}
Expand Down
69 changes: 69 additions & 0 deletions test/ipfs-api/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'

const expect = require('chai').expect
const isNode = require('detect-node')
const path = require('path')
const fs = require('fs')
const FactoryClient = require('../factory/factory-client')

describe('.util', () => {
if (!isNode) {
return
}
let ipfs
let fc

before(function (done) {
this.timeout(20 * 1000) // slow CI
fc = new FactoryClient()
fc.spawnNode((err, node) => {
expect(err).to.not.exist
ipfs = node
done()
})
})

after((done) => {
fc.dismantle(done)
})

it('.streamAdd', (done) => {
const rs = fs.createReadStream(path.join(__dirname, '/../data/testfile.txt'))
rs.path = '' // clean the path for testing purposes

ipfs.util.streamAdd(rs, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.equal(1)
done()
})
})

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

it('.fsAdd a file', (done) => {
const filePath = path.join(__dirname, '../data/testfile.txt')
ipfs.util.fsAdd(filePath, (err, result) => {
expect(err).to.not.exist
expect(result.length).to.be.above(5)

done()
})
})

it('.urlAdd', (done) => {
ipfs.util.urlAdd('http://example.com/', (err, result) => {
expect(err).to.not.exist
expect(result.length).to.equal(1)
done()
})
})
})

0 comments on commit 2ae09fb

Please sign in to comment.