Skip to content

Instantly share code, notes, and snippets.

@nginnever
Created February 18, 2016 21:52
Show Gist options
  • Save nginnever/3dc943b8e011e595dc49 to your computer and use it in GitHub Desktop.
Save nginnever/3dc943b8e011e595dc49 to your computer and use it in GitHub Desktop.
stream buffer piped to fixedSizeChunker then work is done on it
function bufferImporter (buffer, callback) {
if(!Buffer.isBuffer(buffer)) {
callback('buffer importer must take a buffer')
}
const links = [] // { Hash: , Size: , Name: }
var bufferStream = new stream
bufferStream.push(buffer)
bufferStream.push(null)
bufferStream.pipe(new FixedSizeChunker(262145))
.pipe(through2((chunk, enc, cb) => {
console.log(chunk)
// TODO: check if this is right (I believe it should be type 'raw'
// https://github.com/ipfs/go-ipfs/issues/2331
const raw = new UnixFS('file', chunk)
console.log('Size of the chunk '+raw.fileSize())
const node = new mDAG.DAGNode(raw.marshal())
dagService.add(node, function (err) {
if (err) {
return log.err(err)
}
links.push({
Hash: node.multihash(),
Size: node.size(),
leafSize: raw.fileSize(),
Name: ''
})
cb()
})
}, (cb) => {
const file = new UnixFS('file')
const parentNode = new mDAG.DAGNode()
links.forEach((l) => {
file.addBlockSize(l.leafSize)
const link = new mDAG.DAGLink(l.Name, l.Size, l.Hash)
parentNode.addRawLink(link)
})
parentNode.data = file.marshal()
dagService.add(parentNode, (err) => {
if (err) {
return log.err(err)
}
//const pathSplit = path.split('/')
//const fileName = pathSplit[pathSplit.length - 1]
callback(null, {
Hash: parentNode.multihash(),
Size: parentNode.size()
//Name: fileName
}) && cb()
})
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment