-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix/files get #499
Fix/files get #499
Changes from all commits
7015586
5fe4674
8509199
028a98c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,7 @@ module.exports = { | |
stats.Wantlist = stats.Wantlist || [] | ||
stats.Peers = stats.Peers || [] | ||
|
||
console.log(` | ||
bitswap status | ||
console.log(`bitswap status | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we happy with the new "Printer" by @victorbjelkholm? Wanna use that? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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') | ||
|
@@ -24,7 +23,7 @@ module.exports = { | |
throw err | ||
} | ||
|
||
console.log('Key:', bs58.encode(stats.key).toString()) | ||
console.log('Key:', stats.key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a multihash? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not currently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
console.log('Size:', stats.size) | ||
}) | ||
}) | ||
|
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') | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are not changes to
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As mentioned in the description, fixes all over including |
||
}) | ||
} | ||
} | ||
|
||
function onFile (err, file) { | ||
if (err) { | ||
throw (err) | ||
} | ||
file.pipe(process.stdout) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
}) | ||
} | ||
} | ||
|
@@ -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 | ||
} | ||
}) | ||
) | ||
}) | ||
}) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict' | ||
|
||
const bs58 = require('bs58') | ||
const mh = require('multihashes') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why, we have been using |
||
const multipart = require('ipfs-multipart') | ||
const Block = require('ipfs-block') | ||
const debug = require('debug') | ||
|
@@ -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) | ||
|
@@ -93,7 +93,7 @@ exports.put = { | |
} | ||
|
||
return reply({ | ||
Key: bs58.encode(block.key).toString(), | ||
Key: mh.toB58String(block.key), | ||
Size: block.data.length | ||
}) | ||
}) | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
|
@@ -140,7 +140,7 @@ exports.stat = { | |
} | ||
|
||
return reply({ | ||
Key: bs58.encode(block.key).toString(), | ||
Key: block.key, | ||
Size: block.size | ||
}) | ||
}) | ||
|
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() | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's this for?
There was a problem hiding this comment.
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.