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

Commit 2ae09fb

Browse files
committed
feat(util): add fsAdd, streamAdd and urlAdd
1 parent 4cffb47 commit 2ae09fb

File tree

6 files changed

+129
-10
lines changed

6 files changed

+129
-10
lines changed

README.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,43 @@ Complete documentation for these methods is coming with: https://github.com/ipfs
116116

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

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

123123
```JavaScript
124+
ipfs.util.fsAdd(<dirpath>, { recursive: true }, (err, result) => {
125+
if (err) {
126+
throw err
127+
}
128+
console.log(result)
129+
})
130+
```
131+
132+
`files` is an array of objects describing the files that were added, such as:
133+
134+
```
135+
[{
136+
path: 'test-folder',
137+
hash: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6',
138+
size: 2278
139+
},
140+
// ...
141+
]
124142
```
125143

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

128146
> `ipfs.util.urlAdd(url, callback)`
129147
130148
```JavaScript
149+
ipfs.util.urlAdd('http://example.com/', (err, result) => {
150+
if (err) {
151+
throw err
152+
}
153+
console.log(result)
154+
})
155+
131156
```
132157

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

139164
```JavaScript
165+
ipfs.util.streamAdd(<readable-stream>, (err, result) => {
166+
if (err) {
167+
throw err
168+
}
169+
console.log(result)
170+
})
140171
```
141172

142173
### Callbacks and promises
File renamed without changes.

src/api/add-files.js src/api/util/fs-add.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const isNode = require('detect-node')
4-
const addToDagNodesTransform = require('../add-to-dagnode-transform')
4+
const addToDagNodesTransform = require('./../../add-to-dagnode-transform')
55
const promisify = require('promisify-es6')
66

77
module.exports = (send) => {
@@ -12,8 +12,16 @@ module.exports = (send) => {
1212
opts = {}
1313
}
1414

15+
// opts is the real callback --
16+
// 'callback' is being injected by promisify
17+
if (typeof opts === 'function' &&
18+
typeof callback === 'function') {
19+
callback = opts
20+
opts = {}
21+
}
22+
1523
if (!isNode) {
16-
return callback(new Error('Recursive uploads are not supported in the browser'))
24+
return callback(new Error('fsAdd does not work in the browser'))
1725
}
1826

1927
if (typeof path !== 'string') {

src/api/add-url.js src/api/util/url-add.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

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

66
const promisify = require('promisify-es6')
77

@@ -13,6 +13,14 @@ module.exports = (send) => {
1313
opts = {}
1414
}
1515

16+
// opts is the real callback --
17+
// 'callback' is being injected by promisify
18+
if (typeof opts === 'function' &&
19+
typeof callback === 'function') {
20+
callback = opts
21+
opts = {}
22+
}
23+
1624
if (typeof url !== 'string' ||
1725
!url.startsWith('http')) {
1826
return callback(new Error('"url" param must be an http(s) url'))
@@ -24,6 +32,7 @@ module.exports = (send) => {
2432
if (err) {
2533
return callback(err)
2634
}
35+
console.log('got page back')
2736

2837
sendWithTransform({
2938
path: 'add',

src/load-commands.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
function requireCommands () {
44
const cmds = {
5-
add: require('./api/add'), // add alias
6-
createAddStream: require('./api/add-stream'), // add stream alias
5+
// add and createAddStream alias
6+
add: require('./api/add'),
7+
createAddStream: require('./api/create-add-stream'),
78
bitswap: require('./api/bitswap'),
89
block: require('./api/block'),
910
bootstrap: require('./api/bootstrap'),
@@ -33,7 +34,7 @@ function requireCommands () {
3334
cmds.files = function (send) {
3435
const files = require('./api/files')(send)
3536
files.add = require('./api/add')(send)
36-
files.createAddStream = require('./api/add-stream.js')(send)
37+
files.createAddStream = require('./api/create-add-stream.js')(send)
3738
files.get = require('./api/get')(send)
3839

3940
// aliases
@@ -46,8 +47,9 @@ function requireCommands () {
4647

4748
cmds.util = function (send) {
4849
const util = {
49-
addFiles: require('./api/add-files')(send),
50-
addUrl: require('./api/add-url')(send)
50+
fsAdd: require('./api/util/fs-add')(send),
51+
streamAdd: require('./api/add')(send),
52+
urlAdd: require('./api/util/url-add')(send)
5153
}
5254
return util
5355
}

test/ipfs-api/util.spec.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
'use strict'
4+
5+
const expect = require('chai').expect
6+
const isNode = require('detect-node')
7+
const path = require('path')
8+
const fs = require('fs')
9+
const FactoryClient = require('../factory/factory-client')
10+
11+
describe('.util', () => {
12+
if (!isNode) {
13+
return
14+
}
15+
let ipfs
16+
let fc
17+
18+
before(function (done) {
19+
this.timeout(20 * 1000) // slow CI
20+
fc = new FactoryClient()
21+
fc.spawnNode((err, node) => {
22+
expect(err).to.not.exist
23+
ipfs = node
24+
done()
25+
})
26+
})
27+
28+
after((done) => {
29+
fc.dismantle(done)
30+
})
31+
32+
it('.streamAdd', (done) => {
33+
const rs = fs.createReadStream(path.join(__dirname, '/../data/testfile.txt'))
34+
rs.path = '' // clean the path for testing purposes
35+
36+
ipfs.util.streamAdd(rs, (err, result) => {
37+
expect(err).to.not.exist
38+
expect(result.length).to.equal(1)
39+
done()
40+
})
41+
})
42+
43+
it('.fsAdd a directory', (done) => {
44+
const filesPath = path.join(__dirname, '../data/test-folder')
45+
ipfs.util.fsAdd(filesPath, { recursive: true }, (err, result) => {
46+
expect(err).to.not.exist
47+
expect(result.length).to.be.above(8)
48+
done()
49+
})
50+
})
51+
52+
it('.fsAdd a file', (done) => {
53+
const filePath = path.join(__dirname, '../data/testfile.txt')
54+
ipfs.util.fsAdd(filePath, (err, result) => {
55+
expect(err).to.not.exist
56+
expect(result.length).to.be.above(5)
57+
58+
done()
59+
})
60+
})
61+
62+
it('.urlAdd', (done) => {
63+
ipfs.util.urlAdd('http://example.com/', (err, result) => {
64+
expect(err).to.not.exist
65+
expect(result.length).to.equal(1)
66+
done()
67+
})
68+
})
69+
})

0 commit comments

Comments
 (0)