Skip to content

Commit 09b589d

Browse files
committed
add cli tests, promises, daemonOn
1 parent 4978b6a commit 09b589d

File tree

5 files changed

+145
-67
lines changed

5 files changed

+145
-67
lines changed

src/cli/commands/files/add.js

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ log.error = debug('cli:version:error')
88
const bs58 = require('bs58')
99
const fs = require('fs')
1010
const parallelLimit = require('run-parallel-limit')
11+
const async = require('async')
1112
const path = require('path')
1213
const glob = require('glob')
1314

@@ -35,6 +36,50 @@ function checkPath (inPath, recursive) {
3536
return inPath
3637
}
3738

39+
function daemonOn (res, inPath, ipfs) {
40+
const files = []
41+
if (res.length !== 0) {
42+
const index = inPath.lastIndexOf('/')
43+
async.eachLimit(res, 10, (element, callback) => {
44+
if (fs.statSync(element).isDirectory()) {
45+
callback()
46+
} else {
47+
const filePair = {
48+
path: element.substring(index + 1, element.length),
49+
content: fs.createReadStream(element)
50+
}
51+
files.push(filePair)
52+
callback()
53+
}
54+
}, (err) => {
55+
if (err) {
56+
throw err
57+
}
58+
ipfs.add(files, (err, res) => {
59+
if (err) {
60+
throw err
61+
}
62+
res.forEach((goRes) => {
63+
console.log('added', goRes.Hash, goRes.Name)
64+
})
65+
})
66+
})
67+
} else {
68+
const filePair = {
69+
path: inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length),
70+
content: fs.createReadStream(inPath)
71+
}
72+
files.push(filePair)
73+
ipfs.add(files, (err, res) => {
74+
if (err) {
75+
throw err
76+
}
77+
console.log('added', res[0].Hash, res[0].Name)
78+
})
79+
}
80+
return
81+
}
82+
3883
module.exports = Command.extend({
3984
desc: 'Add a file to IPFS using the UnixFS data format',
4085

@@ -59,41 +104,45 @@ module.exports = Command.extend({
59104
if (err) {
60105
throw err
61106
}
62-
ipfs.files.add((err, i) => {
63-
if (err) {
64-
throw err
65-
}
66-
var filePair
67-
i.on('data', (file) => {
68-
console.log('added', bs58.encode(file.multihash).toString(), file.path)
69-
})
70-
i.once('end', () => {
71-
return
72-
})
73-
if (res.length !== 0) {
74-
const index = inPath.lastIndexOf('/')
75-
parallelLimit(res.map((element) => (callback) => {
76-
if (!fs.statSync(element).isDirectory()) {
77-
i.write({
78-
path: element.substring(index + 1, element.length),
79-
stream: fs.createReadStream(element)
80-
})
81-
}
82-
callback()
83-
}), 10, (err) => {
84-
if (err) {
85-
throw err
86-
}
87-
i.end()
107+
if (utils.isDaemonOn()) {
108+
daemonOn(res, inPath, ipfs)
109+
} else {
110+
ipfs.files.add((err, i) => {
111+
if (err) {
112+
throw err
113+
}
114+
var filePair
115+
i.on('data', (file) => {
116+
console.log('added', bs58.encode(file.multihash).toString(), file.path)
88117
})
89-
} else {
90-
rs = fs.createReadStream(inPath)
91-
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
92-
filePair = {path: inPath, stream: rs}
93-
i.write(filePair)
94-
i.end()
95-
}
96-
})
118+
i.once('end', () => {
119+
return
120+
})
121+
if (res.length !== 0) {
122+
const index = inPath.lastIndexOf('/')
123+
parallelLimit(res.map((element) => (callback) => {
124+
if (!fs.statSync(element).isDirectory()) {
125+
i.write({
126+
path: element.substring(index + 1, element.length),
127+
stream: fs.createReadStream(element)
128+
})
129+
}
130+
callback()
131+
}), 10, (err) => {
132+
if (err) {
133+
throw err
134+
}
135+
i.end()
136+
})
137+
} else {
138+
rs = fs.createReadStream(inPath)
139+
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
140+
filePair = {path: inPath, stream: rs}
141+
i.write(filePair)
142+
i.end()
143+
}
144+
})
145+
}
97146
})
98147
})
99148
}

src/core/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@ function IPFS (repoInstance) {
5858
this.libp2p = libp2p(this)
5959
this.files = files(this)
6060
this.cat = files(this).cat // Alias for js-ipfs-api cat
61+
this.add = files(this).add // Alias for js-ipfs-api add
6162
this.bitswap = bitswap(this)
6263
}

src/http-api/resources/files.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,44 +65,51 @@ exports.add = {
6565
// hapi doesn't permit object streams: http://hapijs.com/api#replyerr-result
6666
serialize._readableState.objectMode = false
6767

68-
var fileAdder = request.server.app.ipfs.files.add()
69-
70-
fileAdder.on('data', (file) => {
71-
serialize.write({
72-
Name: file.path,
73-
Hash: multihash.toB58String(file.multihash)
74-
})
75-
filesAdded++
76-
})
77-
78-
fileAdder.on('end', () => {
79-
if (filesAdded === 0 && filesParsed) {
68+
request.server.app.ipfs.files.add((err, fileAdder) => {
69+
if (err) {
8070
return reply({
81-
Message: 'Failed to add files.',
71+
Message: err,
8272
Code: 0
8373
}).code(500)
84-
} else {
85-
serialize.end()
86-
return reply(serialize)
87-
.header('x-chunked-output', '1')
88-
.header('content-type', 'application/json')
8974
}
90-
})
9175

92-
parser.on('file', (fileName, fileStream) => {
93-
var filePair = {
94-
path: fileName,
95-
stream: fileStream
96-
}
97-
filesParsed = true
98-
fileAdder.write(filePair)
99-
})
76+
fileAdder.on('data', (file) => {
77+
serialize.write({
78+
Name: file.path,
79+
Hash: multihash.toB58String(file.multihash)
80+
})
81+
filesAdded++
82+
})
10083

101-
parser.on('end', () => {
102-
if (!filesParsed) {
103-
return reply("File argument 'data' is required.").code(400).takeover()
104-
}
105-
fileAdder.end()
84+
fileAdder.on('end', () => {
85+
if (filesAdded === 0 && filesParsed) {
86+
return reply({
87+
Message: 'Failed to add files.',
88+
Code: 0
89+
}).code(500)
90+
} else {
91+
serialize.end()
92+
return reply(serialize)
93+
.header('x-chunked-output', '1')
94+
.header('content-type', 'application/json')
95+
}
96+
})
97+
98+
parser.on('file', (fileName, fileStream) => {
99+
var filePair = {
100+
path: fileName,
101+
stream: fileStream
102+
}
103+
filesParsed = true
104+
fileAdder.write(filePair)
105+
})
106+
107+
parser.on('end', () => {
108+
if (!filesParsed) {
109+
return reply("File argument 'data' is required.").code(400).takeover()
110+
}
111+
fileAdder.end()
112+
})
106113
})
107114
}
108115
}

test/cli-tests/test-files.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ describe('files', () => {
1212
env.IPFS_PATH = repoPath
1313

1414
describe('api offline', () => {
15+
it('add', (done) => {
16+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', process.cwd() + '/test/test-data/node.json'], {env})
17+
.run((err, stdout, exitcode) => {
18+
expect(err).to.not.exist
19+
expect(exitcode).to.equal(0)
20+
expect(stdout[0])
21+
.to.equal('added QmRRdjTN2PjyEPrW73GBxJNAZrstH5tCZzwHYFJpSTKkhe node.json')
22+
done()
23+
})
24+
})
25+
1526
it('cat', (done) => {
1627
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env})
1728
.run((err, stdout, exitcode) => {
@@ -40,6 +51,17 @@ describe('files', () => {
4051
})
4152
})
4253

54+
it('add', (done) => {
55+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', process.cwd() + '/test/test-data/node.json'], {env})
56+
.run((err, stdout, exitcode) => {
57+
expect(err).to.not.exist
58+
expect(exitcode).to.equal(0)
59+
expect(stdout[0])
60+
.to.equal('added QmRRdjTN2PjyEPrW73GBxJNAZrstH5tCZzwHYFJpSTKkhe node.json')
61+
done()
62+
})
63+
})
64+
4365
it('cat', (done) => {
4466
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env})
4567
.run((err, stdout, exitcode) => {

test/http-api-tests/test-files.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function singleFileServer (filename) {
1616
})
1717
}
1818

19-
2019
module.exports = (httpAPI) => {
2120
describe('files', () => {
2221
describe('api', () => {

0 commit comments

Comments
 (0)