diff --git a/src/block/put.js b/src/block/put.js index 0ec1e5c5f..81d71b91c 100644 --- a/src/block/put.js +++ b/src/block/put.js @@ -5,15 +5,16 @@ const Block = require('ipfs-block') const CID = require('cids') const once = require('once') const SendOneFile = require('../utils/send-one-file') +const multihashes = require('multihashes') module.exports = (send) => { const sendOneFile = SendOneFile(send, 'block/put') - return promisify((block, cid, _callback) => { + return promisify((block, opts, _callback) => { // TODO this needs to be adjusted with the new go-ipfs http-api - if (typeof cid === 'function') { - _callback = cid - cid = {} + if (typeof opts === 'function') { + _callback = opts + opts = {} } const callback = once(_callback) @@ -22,16 +23,36 @@ module.exports = (send) => { return callback(new Error('block.put accepts only one block')) } + let cid = opts.cid + if (typeof block === 'object' && block.data) { + if (block.cid) cid = block.cid block = block.data } - sendOneFile(block, {}, (err, result) => { + let qs = Object.assign(opts, { + 'input-enc': 'raw', + hashAlg: opts.mhtype || 'sha2-256' + }) + + if (cid && CID.isCID(cid)) { + qs.format = cid.codec + qs.hashAlg = multihashes.decode(cid.multihash).name + } + delete qs.cid + + let _send = sendOneFile + + if (qs.format && qs.format.startsWith('dag-')) { + _send = SendOneFile(send, 'dag/put') + } + + _send(block, {qs}, (err, result) => { if (err) { return callback(err) // early } - - callback(null, new Block(block, new CID(result.Key))) + let cid = result.Key || result.Cid['/'] + callback(null, new Block(block, new CID(cid))) }) }) }