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

Commit

Permalink
Merge d1e272f into 94daa32
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed Apr 16, 2016
2 parents 94daa32 + d1e272f commit 7bd95f2
Show file tree
Hide file tree
Showing 60 changed files with 269 additions and 320 deletions.
112 changes: 109 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const FixedSizeChunker = require('./chunker-fixed-size')
const through2 = require('through2')
const UnixFS = require('ipfs-unixfs')
const async = require('async')
const events = require('events')
const Readable = require('stream').Readable
const pathj = require('path')

exports = module.exports

Expand Down Expand Up @@ -206,7 +209,6 @@ exports.import = (target, dagService, options, callback) => {
leafSize: raw.fileSize(),
Name: ''
})

cb()
})
}, (cb) => {
Expand Down Expand Up @@ -249,6 +251,110 @@ exports.import = (target, dagService, options, callback) => {
// function streamImporter (stream, callback) {}
}

exports.export = function () {
// export into files by hash
exports.export = function (hash, dagService, options, callback) {
if (typeof options === 'function') { callback = options; options = {} }
const ee = new events.EventEmitter()
dagService.get(hash, (err, fetchedNode) => {
if (err) {
if (callback) {
return callback(err)
}
return
}
const data = UnixFS.unmarshal(fetchedNode.data)
const type = data.type
if (type === 'directory') {
dirExporter(fetchedNode, hash, callback)
}
if (type === 'file') {
fileExporter(fetchedNode, hash, false, callback)
}
})
return ee

function fileExporter (node, name, dir, callback) {
if (typeof dir === 'function') { callback = dir; dir = {} }
var rs = new Readable()
if (node.links.length === 0) {
const unmarshaledData = UnixFS.unmarshal(node.data)
ee.emit('file', { stream: rs, path: name, dir: dir })
rs.push(unmarshaledData.data)
rs.push(null)
if (callback) {
callback()
}
return
} else {
ee.emit('file', { stream: rs, path: name, dir: dir })
var init = false
rs._read = () => {
if (init) {
return
}
init = true
async.forEachSeries(node.links, (link, callback) => {
dagService.get(link.hash, (err, res) => {
if (err) {
callback(err)
}
var unmarshaledData = UnixFS.unmarshal(res.data)
rs.push(unmarshaledData.data)
callback()
})
}, (err) => {
if (err) {
if (callback) {
return callback(err)
}
return
}
rs.push(null)
if (callback) {
callback()
}
return
})
}
}
}

function dirExporter (node, name, callback) {
var rs = new Readable()
if (node.links.length === 0) {
rs.push(node.data)
rs.push(null)
ee.emit('file', {stream: rs, path: name})
if (callback) {
callback()
}
return
} else {
async.forEachSeries(node.links, (link, callback) => {
dagService.get(link.hash, (err, res) => {
if (err) {
callback(err)
}
var unmarshaledData = UnixFS.unmarshal(res.data)
if (unmarshaledData.type === 'file') {
return (fileExporter(res, pathj.join(name, link.name), callback))
}
if (unmarshaledData.type === 'directory') {
return (dirExporter(res, pathj.join(name, link.name), callback))
}
callback()
})
}, (err) => {
if (err) {
if (callback) {
return callback(err)
}
return
}
if (callback) {
callback()
}
return
})
}
}
}
84 changes: 84 additions & 0 deletions test/buffer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const BlockService = require('ipfs-blocks').BlockService
const DAGService = require('ipfs-merkle-dag').DAGService
const DAGNode = require('ipfs-merkle-dag').DAGNode
const UnixFS = require('ipfs-unixfs')
const async = require('async')

const expect = require('chai').expect

Expand Down Expand Up @@ -82,5 +83,88 @@ module.exports = function (repo) {
})
})
})

it('export a file with no links', (done) => {
const hash = 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8'
const bs = new BlockService(repo)
const ds = new DAGService(bs)
const testExport = importer.export(hash, ds)
testExport.on('file', (data) => {
ds.get(hash, (err, fetchedNode) => {
expect(err).to.not.exist
const unmarsh = UnixFS.unmarshal(fetchedNode.data)
expect(unmarsh.data).to.deep.equal(data.stream._readableState.buffer[0])
done()
})
})
})

it.skip('export a small file with links', (done) => {
const hash = 'QmW7BDxEbGqxxSYVtn3peNPQgdDXbWkoQ6J1EFYAEuQV3Q'
const bs = new BlockService(repo)
const ds = new DAGService(bs)
const testExport = importer.export(hash, ds)
testExport.on('file', (data) => {
ds.get(hash, (err, fetchedNode) => {
expect(err).to.not.exist
var i = 0
async.forEachSeries(fetchedNode.links, (link, callback) => {
ds.get(link.hash, (err, res) => {
expect(err).to.not.exist
const unmarsh = UnixFS.unmarshal(res.data)
expect(unmarsh.data).to.deep.equal(data.stream._readableState.buffer[i])
i++
callback()
})
}, (err) => {
expect(err).to.not.exist
done()
})
})
})
})

it.skip('export a large file > 5mb', (done) => {
const hash = 'QmRQgufjp9vLE8XK2LGKZSsPCFCF6e4iynCQtNB5X2HBKE'
const bs = new BlockService(repo)
const ds = new DAGService(bs)
const testExport = importer.export(hash, ds)
testExport.on('file', (data) => {
ds.get(hash, (err, fetchedNode) => {
expect(err).to.not.exist
var i = 0
async.forEachSeries(fetchedNode.links, (link, callback) => {
ds.get(link.hash, (err, res) => {
expect(err).to.not.exist
const unmarsh = UnixFS.unmarshal(res.data)
expect(unmarsh.data).to.deep.equal(data.stream._readableState.buffer[i])
i++
callback()
})
}, (err) => {
expect(err).to.not.exist
done()
})
})
})
})

it.skip('export a directory', (done) => {
const hash = 'QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN'
const bs = new BlockService(repo)
const ds = new DAGService(bs)
const testExport = importer.export(hash, ds)
var fs = []
testExport.on('file', (data) => {
fs.push(data)
})
testExport.on('endNestDir', () => {
expect(fs[0].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/200Bytes.txt')
expect(fs[1].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/dir-another')
expect(fs[2].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/level-1/200Bytes.txt')
expect(fs[3].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/level-1/level-2')
done()
})
})
})
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

This file was deleted.

0 comments on commit 7bd95f2

Please sign in to comment.