Skip to content

Commit

Permalink
add cli tests, promises, daemonOn
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed May 23, 2016
1 parent 4978b6a commit 09b589d
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 67 deletions.
117 changes: 83 additions & 34 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ log.error = debug('cli:version:error')
const bs58 = require('bs58')
const fs = require('fs')
const parallelLimit = require('run-parallel-limit')
const async = require('async')
const path = require('path')
const glob = require('glob')

Expand Down Expand Up @@ -35,6 +36,50 @@ function checkPath (inPath, recursive) {
return inPath
}

function daemonOn (res, inPath, ipfs) {
const files = []
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
async.eachLimit(res, 10, (element, callback) => {
if (fs.statSync(element).isDirectory()) {
callback()
} else {
const filePair = {
path: element.substring(index + 1, element.length),
content: fs.createReadStream(element)
}
files.push(filePair)
callback()
}
}, (err) => {
if (err) {
throw err
}
ipfs.add(files, (err, res) => {
if (err) {
throw err
}
res.forEach((goRes) => {
console.log('added', goRes.Hash, goRes.Name)
})
})
})
} else {
const filePair = {
path: inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length),
content: fs.createReadStream(inPath)
}
files.push(filePair)
ipfs.add(files, (err, res) => {
if (err) {
throw err
}
console.log('added', res[0].Hash, res[0].Name)
})
}
return
}

module.exports = Command.extend({
desc: 'Add a file to IPFS using the UnixFS data format',

Expand All @@ -59,41 +104,45 @@ module.exports = Command.extend({
if (err) {
throw err
}
ipfs.files.add((err, i) => {
if (err) {
throw err
}
var filePair
i.on('data', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
i.once('end', () => {
return
})
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
parallelLimit(res.map((element) => (callback) => {
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}), 10, (err) => {
if (err) {
throw err
}
i.end()
if (utils.isDaemonOn()) {
daemonOn(res, inPath, ipfs)
} else {
ipfs.files.add((err, i) => {
if (err) {
throw err
}
var filePair
i.on('data', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
} else {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
}
})
i.once('end', () => {
return
})
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
parallelLimit(res.map((element) => (callback) => {
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}), 10, (err) => {
if (err) {
throw err
}
i.end()
})
} else {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
}
})
}
})
})
}
Expand Down
1 change: 1 addition & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ function IPFS (repoInstance) {
this.libp2p = libp2p(this)
this.files = files(this)
this.cat = files(this).cat // Alias for js-ipfs-api cat
this.add = files(this).add // Alias for js-ipfs-api add
this.bitswap = bitswap(this)
}
71 changes: 39 additions & 32 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,44 +65,51 @@ exports.add = {
// hapi doesn't permit object streams: http://hapijs.com/api#replyerr-result
serialize._readableState.objectMode = false

var fileAdder = request.server.app.ipfs.files.add()

fileAdder.on('data', (file) => {
serialize.write({
Name: file.path,
Hash: multihash.toB58String(file.multihash)
})
filesAdded++
})

fileAdder.on('end', () => {
if (filesAdded === 0 && filesParsed) {
request.server.app.ipfs.files.add((err, fileAdder) => {
if (err) {
return reply({
Message: 'Failed to add files.',
Message: err,
Code: 0
}).code(500)
} else {
serialize.end()
return reply(serialize)
.header('x-chunked-output', '1')
.header('content-type', 'application/json')
}
})

parser.on('file', (fileName, fileStream) => {
var filePair = {
path: fileName,
stream: fileStream
}
filesParsed = true
fileAdder.write(filePair)
})
fileAdder.on('data', (file) => {
serialize.write({
Name: file.path,
Hash: multihash.toB58String(file.multihash)
})
filesAdded++
})

parser.on('end', () => {
if (!filesParsed) {
return reply("File argument 'data' is required.").code(400).takeover()
}
fileAdder.end()
fileAdder.on('end', () => {
if (filesAdded === 0 && filesParsed) {
return reply({
Message: 'Failed to add files.',
Code: 0
}).code(500)
} else {
serialize.end()
return reply(serialize)
.header('x-chunked-output', '1')
.header('content-type', 'application/json')
}
})

parser.on('file', (fileName, fileStream) => {
var filePair = {
path: fileName,
stream: fileStream
}
filesParsed = true
fileAdder.write(filePair)
})

parser.on('end', () => {
if (!filesParsed) {
return reply("File argument 'data' is required.").code(400).takeover()
}
fileAdder.end()
})
})
}
}
22 changes: 22 additions & 0 deletions test/cli-tests/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ describe('files', () => {
env.IPFS_PATH = repoPath

describe('api offline', () => {
it('add', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', process.cwd() + '/test/test-data/node.json'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout[0])
.to.equal('added QmRRdjTN2PjyEPrW73GBxJNAZrstH5tCZzwHYFJpSTKkhe node.json')
done()
})
})

it('cat', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env})
.run((err, stdout, exitcode) => {
Expand Down Expand Up @@ -40,6 +51,17 @@ describe('files', () => {
})
})

it('add', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', process.cwd() + '/test/test-data/node.json'], {env})
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout[0])
.to.equal('added QmRRdjTN2PjyEPrW73GBxJNAZrstH5tCZzwHYFJpSTKkhe node.json')
done()
})
})

it('cat', (done) => {
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'cat', 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'], {env})
.run((err, stdout, exitcode) => {
Expand Down
1 change: 0 additions & 1 deletion test/http-api-tests/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function singleFileServer (filename) {
})
}


module.exports = (httpAPI) => {
describe('files', () => {
describe('api', () => {
Expand Down

0 comments on commit 09b589d

Please sign in to comment.