Last active
February 18, 2016 21:58
-
-
Save nginnever/944bf36026d4e26a2794 to your computer and use it in GitHub Desktop.
fixedSizeChunker pushed to and piped out then work is done on it
This file contains hidden or 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
| function bufferImporter (buffer, callback) { | |
| if(!Buffer.isBuffer(buffer)) { | |
| callback('buffer importer must take a buffer') | |
| } | |
| const links = [] // { Hash: , Size: , Name: } | |
| var fsc = new FixedSizeChunker(CHUNK_SIZE) | |
| fsc.write(buffer) | |
| fsc.end() | |
| fsc.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