Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Fix/files get #499

Merged
merged 4 commits into from
Sep 30, 2016
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
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const gulp = require('gulp')
const parallel = require('run-parallel')
const series = require('run-series')
const parallel = require('async/parallel')
const series = require('async/series')
const createTempNode = require('./test/utils/temp-node')
const API = require('./src/http-api')

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"aegir": "^8.0.1",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"execa": "^0.4.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To spawn the cli in a consistent and nice way.

"expose-loader": "^0.7.1",
"form-data": "^2.0.0",
"fs-pull-blob-store": "^0.3.0",
Expand All @@ -59,6 +60,7 @@
"transform-loader": "^0.2.3"
},
"dependencies": {
"async": "^2.0.1",
"babel-runtime": "^6.11.6",
"bl": "^1.1.2",
"boom": "^4.0.0",
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ module.exports = {
stats.Wantlist = stats.Wantlist || []
stats.Peers = stats.Peers || []

console.log(`
bitswap status
console.log(`bitswap status
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we happy with the new "Printer" by @victorbjelkholm? Wanna use that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in another PR I would rather do that change as a single PR on its own, there are already too many things mixed in here.

blocks received: ${stats.BlocksReceived}
dup blocks received: ${stats.DupBlksReceived}
dup data received: ${stats.DupDataReceived}B
Expand Down
3 changes: 1 addition & 2 deletions src/cli/commands/block/stat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:block')
log.error = debug('cli:block:error')
Expand All @@ -24,7 +23,7 @@ module.exports = {
throw err
}

console.log('Key:', bs58.encode(stats.key).toString())
console.log('Key:', stats.key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a multihash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not currently

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log('Size:', stats.size)
})
})
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/bootstrap/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
if (err) {
throw err
}

ipfs.bootstrap.add(argv.peer, (err, list) => {
if (err) {
throw err
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/config/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const spawn = require('child_process').spawn
const fs = require('fs')
const temp = require('temp')
const waterfall = require('run-waterfall')
const waterfall = require('async/waterfall')
const debug = require('debug')
const log = debug('cli:config')
log.error = debug('cli:config:error')
Expand Down
16 changes: 7 additions & 9 deletions src/cli/commands/files/cat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const waterfall = require('async/waterfall')
const debug = require('debug')
const utils = require('../../utils')
const log = debug('cli:files')
Expand All @@ -14,19 +15,16 @@ module.exports = {

handler (argv) {
const path = argv['ipfs-path']
utils.getIPFS((err, ipfs) => {

waterfall([
(cb) => utils.getIPFS(cb),
(ipfs, cb) => ipfs.files.cat(path, cb)
], (err, file) => {
if (err) {
throw err
}

ipfs.files.cat(path, onFile)
file.pipe(process.stdout)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not changes to get

  • I believe we have agreed during a js-ipfs sprint call that we would start settling on async, since not is is modular, more battle tested and the documentation is just great. If we bring on new deps like run-waterfall, better just do the async/waterfall

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember making that decision, but happy to move all these run-* to modular async.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not changes to get

As mentioned in the description, fixes all over including files cat ;)

})
}
}

function onFile (err, file) {
if (err) {
throw (err)
}
file.pipe(process.stdout)
}
52 changes: 37 additions & 15 deletions src/cli/commands/files/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ log.error = debug('cli:files:error')
var fs = require('fs')
const path = require('path')
const pathExists = require('path-exists')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')

function checkArgs (hash, outPath) {
// format the output directory
Expand All @@ -33,30 +35,39 @@ function ensureDir (dir, cb) {
.catch(cb)
}

function fileHandler (result, dir) {
return function onFile (file) {
function fileHandler (dir) {
return function onFile (file, cb) {
const lastSlash = file.path.lastIndexOf('/')
// Check to see if the result is in a directory
if (file.path.lastIndexOf('/') === -1) {
if (lastSlash === -1) {
const dirPath = path.join(dir, file.path)
// Check to see if the result is a directory
if (file.dir === false) {
if (file.content) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

file.content.pipe(fs.createWriteStream(dirPath))
.once('error', cb)
.once('end', cb)
} else {
ensureDir(dirPath, (err) => {
if (err) {
throw err
}
})
ensureDir(dirPath, cb)
}
} else {
const filePath = file.path.substring(0, file.path.lastIndexOf('/') + 1)
const filePath = file.path.substring(0, lastSlash + 1)
const dirPath = path.join(dir, filePath)

ensureDir(dirPath, (err) => {
if (err) {
throw err
return cb(err)
}

file.content.pipe(fs.createWriteStream(dirPath))
if (file.content) {
const filename = file.path.substring(lastSlash)
const target = path.join(dirPath, filename)

file.content.pipe(fs.createWriteStream(target))
.once('error', cb)
.once('end', cb)
return
}
cb()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will call cb twice, once when it is written (once('end')) and once here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

})
}
}
Expand All @@ -76,17 +87,28 @@ module.exports = {
},

handler (argv) {
const dir = checkArgs(argv.ipfsPath, argv.output)
const ipfsPath = argv['ipfs-path']
const dir = checkArgs(ipfsPath, argv.output)

utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
ipfs.files.get(argv.ipfsPath, (err, result) => {

ipfs.files.get(ipfsPath, (err, stream) => {
if (err) {
throw err
}
result.on('data', fileHandler(result, dir))
console.log(`Saving file(s) to ${ipfsPath}`)
pull(
toPull.source(stream),
pull.asyncMap(fileHandler(dir)),
pull.onEnd((err) => {
if (err) {
throw err
}
})
)
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function block (self) {
return callback(err)
}
callback(null, {
key: hash,
key: multihash.toB58String(hash),
size: block.data.length
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/go-online.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const series = require('run-series')
const series = require('async/series')
const Bitswap = require('ipfs-bitswap')

module.exports = function goOnline (self) {
Expand Down
9 changes: 2 additions & 7 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const mDAG = require('ipfs-merkle-dag')
const waterfall = require('run-waterfall')
const waterfall = require('async/waterfall')
const promisify = require('promisify-es6')
const bs58 = require('bs58')
const DAGNode = mDAG.DAGNode
Expand Down Expand Up @@ -69,12 +69,7 @@ module.exports = function object (self) {
cb(err, node)
})
}
], (err, node) => {
if (err) {
return cb(err)
}
cb(null, node)
})
], cb)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/http-api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const parallel = require('run-parallel')
const parallel = require('async/parallel')
const Hapi = require('hapi')
const debug = require('debug')
const fs = require('fs')
Expand Down
12 changes: 6 additions & 6 deletions src/http-api/resources/block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const bs58 = require('bs58')
const mh = require('multihashes')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/mh/multihash/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why, we have been using mh for the multihashes module all over the place.

const multipart = require('ipfs-multipart')
const Block = require('ipfs-block')
const debug = require('debug')
Expand All @@ -17,7 +17,7 @@ exports.parseKey = (request, reply) => {

try {
return reply({
key: new Buffer(bs58.decode(request.query.arg))
key: mh.fromB58String(request.query.arg)
})
} catch (err) {
log.error(err)
Expand Down Expand Up @@ -93,7 +93,7 @@ exports.put = {
}

return reply({
Key: bs58.encode(block.key).toString(),
Key: mh.toB58String(block.key),
Size: block.data.length
})
})
Expand All @@ -112,7 +112,7 @@ exports.del = {
if (err) {
log.error(err)
return reply({
Message: 'Failed to get block stats: ' + err,
Message: 'Failed to delete block: ' + err,
Code: 0
}).code(500)
}
Expand All @@ -129,7 +129,7 @@ exports.stat = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const key = request.pre.args.key

console.log('fetching', key)
request.server.app.ipfs.block.stat(key, (err, block) => {
if (err) {
log.error(err)
Expand All @@ -140,7 +140,7 @@ exports.stat = {
}

return reply({
Key: bs58.encode(block.key).toString(),
Key: block.key,
Size: block.size
})
})
Expand Down
58 changes: 47 additions & 11 deletions src/http-api/resources/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,64 @@
'use strict'

const boom = require('boom')
const multiaddr = require('multiaddr')

exports = module.exports

// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
exports.parseKey = (request, reply) => {
if (!request.query.arg) {
return reply("Argument 'multiaddr' is required").code(400).takeover()
}

try {
return reply({
addr: multiaddr(request.query.arg)
})
} catch (err) {
return reply({
Message: 'Not a valid multiaddr',
Code: 0
}).code(500).takeover()
}
}

exports.list = (request, reply) => {
request.server.app.ipfs.bootstrap.list((err, list) => {
const ipfs = request.server.app.ipfs
ipfs.bootstrap.list((err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply(list)
})
}

exports.add = (request, reply) => {
// request.server.app.ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
exports.add = {
parseArgs: exports.parseKey,
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr

ipfs.bootstrap.add(addr.toString(), (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply()
})
}
}

exports.rm = (request, reply) => {
// request.server.app.ipfs.id((err, id) => {
// if (err) { return reply(boom.badRequest(err)) }
// return reply(id)
// })
exports.rm = {
parseArgs: exports.parseKey,
handler (request, reply) {
const ipfs = request.server.app.ipfs
const addr = request.pre.args.addr

ipfs.bootstrap.rm(addr.toString(), (err, list) => {
if (err) {
return reply(boom.badRequest(err))
}
return reply()
})
}
}
Loading