Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: block.put options
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
alanshaw committed Aug 30, 2018
1 parent 231c4d7 commit 75672e5
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,62 @@
const promisify = require('promisify-es6')
const Block = require('ipfs-block')
const CID = require('cids')
const once = require('once')
const multihash = require('multihashes')
const SendOneFile = require('../utils/send-one-file')

module.exports = (send) => {
const sendOneFile = SendOneFile(send, 'block/put')

return promisify((block, cid, _callback) => {
// TODO this needs to be adjusted with the new go-ipfs http-api
if (typeof cid === 'function') {
_callback = cid
cid = {}
return promisify((block, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

const callback = once(_callback)
options = options || {}

if (Array.isArray(block)) {
return callback(new Error('block.put accepts only one block'))
}

if (typeof block === 'object' && block.data) {
block = block.data
if (Buffer.isBuffer(block)) {
block = { data: block }
}

sendOneFile(block, {}, (err, result) => {
if (!block || !block.data) {
return callback(new Error('invalid block arg'))
}

const qs = {}

if (block.cid || options.cid) {
let cid

try {
cid = new CID(block.cid || options.cid)
} catch (err) {
return callback(err)
}

const { name, length } = multihash.decode(cid.multihash)

qs.format = cid.codec
qs.mhtype = name
qs.mhlen = length
qs.version = cid.version
} else {
if (options.format) qs.format = options.format
if (options.mhtype) qs.mhtype = options.mhtype
if (options.mhlen) qs.mhlen = options.mhlen
if (options.version != null) qs.version = options.version
}

sendOneFile(block.data, { qs }, (err, result) => {
if (err) {
return callback(err) // early
}

callback(null, new Block(block, new CID(result.Key)))
callback(null, new Block(block.data, new CID(result.Key)))
})
})
}

0 comments on commit 75672e5

Please sign in to comment.