This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #671 from ipfs/feat/block-put-flags
CLI - add `block put` flags and `block get` CID support
- Loading branch information
Showing
9 changed files
with
93 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,93 @@ | ||
'use strict' | ||
|
||
const utils = require('../../utils') | ||
const mh = require('multihashes') | ||
const bl = require('bl') | ||
const fs = require('fs') | ||
const Block = require('ipfs-block') | ||
const CID = require('cids') | ||
const multihashing = require('multihashing-async') | ||
const waterfall = require('async/waterfall') | ||
const debug = require('debug') | ||
const log = debug('cli:block') | ||
log.error = debug('cli:block:error') | ||
|
||
function addBlock (buf) { | ||
function addBlock (buf, opts) { | ||
let block = new Block(buf) | ||
let cid | ||
|
||
utils.getIPFS((err, ipfs) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
waterfall([ | ||
(cb) => ipfs.block.put(new Block(buf), cb), | ||
(block, cb) => block.key(cb) | ||
], (err, key) => { | ||
(cb) => generateHash(block, opts, cb), | ||
(mhash, cb) => generateCid(mhash, block, opts, cb), | ||
(cb) => ipfs.block.put(block, cid, cb) | ||
], (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
console.log(mh.toB58String(key)) | ||
console.log(cid.toBaseEncodedString()) | ||
}) | ||
}) | ||
|
||
function generateHash (block, opts, cb) { | ||
if (opts.mhlen === undefined) { | ||
multihashing(buf, opts.mhtype, cb) | ||
} else { | ||
multihashing(buf, opts.mhtype, opts.mhlen, cb) | ||
} | ||
} | ||
|
||
function generateCid (mhash, block, opts, cb) { | ||
cid = new CID(opts.verison, opts.format, mhash) | ||
cb() | ||
} | ||
} | ||
|
||
module.exports = { | ||
command: 'put [data]', | ||
|
||
describe: 'Stores input as an IPFS block', | ||
|
||
builder: {}, | ||
builder: { | ||
format: { | ||
alias: 'f', | ||
describe: 'cid format for blocks to be created with.', | ||
default: 'v0' | ||
}, | ||
mhtype: { | ||
describe: 'multihash hash function', | ||
default: 'sha2-256' | ||
}, | ||
mhlen: { | ||
describe: 'multihash hash length', | ||
default: undefined | ||
} | ||
}, | ||
|
||
handler (argv) { | ||
// parse options | ||
if (argv.format === 'v0') { | ||
argv.verison = 0 | ||
argv.format = 'dag-pb' | ||
argv.mhtype = 'sha2-256' | ||
} else { | ||
argv.verison = 1 | ||
} | ||
|
||
if (argv.data) { | ||
return addBlock(fs.readFileSync(argv.data)) | ||
return addBlock(fs.readFileSync(argv.data), argv) | ||
} | ||
|
||
process.stdin.pipe(bl((err, input) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
addBlock(input) | ||
addBlock(input, argv) | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,39 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
// const dagPB = require('ipld-dag-pb') | ||
// const dagCBOR = require('ipld-dag-cbor') | ||
|
||
// const CID = require('cids') | ||
// const mh = require('multihashes') | ||
const dagPB = require('ipld-dag-pb') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
|
||
module.exports = function dag (self) { | ||
return { | ||
put: promisify((dagNode, multicodec, hashAlg, callback) => { | ||
// TODO | ||
// serialize | ||
// get right hash | ||
// generate cid | ||
// put in IPLD Resolver | ||
|
||
/* | ||
self._ipldResolver.put({ | ||
node: node, | ||
cid: new CID(node.multihash) | ||
} | ||
*/ | ||
switch (multicodec) { | ||
case 'dag-pb': {} break | ||
case 'dag-cbor': {} break | ||
default: | ||
callback(new Error('IPLD Format not supported')) | ||
case 'dag-pb': dagPB.util.cid(dagNode, gotCid); break | ||
case 'dag-cbor': dagCBOR.util.cid(dagNode, gotCid); break | ||
default: callback(new Error('IPLD Format not supported')) | ||
} | ||
|
||
function gotCid (err, cid) { | ||
if (err) { | ||
return callback(err) | ||
} | ||
self._ipldResolver.put({ | ||
node: dagNode, | ||
cid: cid | ||
}, callback) | ||
} | ||
}), | ||
get: promisify((cid, callback) => { | ||
self.ipldResolver.get(cid, callback) | ||
self._ipldResolver.get(cid, callback) | ||
}), | ||
rm: promisify((cid, callback) => { | ||
// TODO once pinning is complete, this remove operation has to first | ||
// verify that some pinning chain is not broken with the operation | ||
self.ipldResolver.remove(cid, callback) | ||
self._ipldResolver.remove(cid, callback) | ||
}), | ||
resolve: promisify((cid, path, callback) => { | ||
self.ipldResolver.resolve(cid, path, callback) | ||
self._ipldResolver.resolve(cid, path, callback) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.