Skip to content

Commit

Permalink
Merge pull request #5 from ipfs/nested-dirs
Browse files Browse the repository at this point in the history
WIP: fix nested dirs bug
  • Loading branch information
daviddias committed Feb 17, 2016
2 parents 2fc41ec + 05180a7 commit a8cfdfc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const CHUNK_SIZE = 262144

// Use a layout + chunkers to convert a directory (or file) to the layout format
exports.import = (options, callback) => {
// options.path -> what to import
// options.recursive -> follow dirs
// options.chunkers -> obj with chunkers to each type of data, { default: dumb-chunker }
// options.dag-service-> instance of block service
// options.path : what to import
// options.recursive : follow dirs
// options.chunkers : obj with chunkers to each type of data, { default: dumb-chunker }
// options.dag-service : instance of block service
const dagService = options.dagService

const stats = fs.statSync(options.path)
Expand Down Expand Up @@ -137,7 +137,7 @@ exports.import = (options, callback) => {
} if (stats.isDirectory()) {
return dirImporter(filePath, cb)
} else {
return callback(new Error('Found a weird file' + path + file))
return cb(new Error('Found a weird file' + path + file))
}
},
(err, results) => {
Expand All @@ -146,24 +146,27 @@ exports.import = (options, callback) => {
}
results.forEach((result) => {
dirNode.addRawLink(new mDAG.DAGLink(result.Name, result.Size, result.Hash))
dirNode.data = dirUnixFS.marshal()
dagService.add(dirNode, (err) => {
if (err) {
return callback(err)
}
})

const split = path.split('/')
const dirName = split[split.length - 1]
dirNode.data = dirUnixFS.marshal()

callback(null, {
Hash: dirNode.multihash(),
Size: dirNode.size(),
Name: dirName
})
dagService.add(dirNode, (err) => {
if (err) {
return callback(err)
}

const split = path.split('/')
const dirName = split[split.length - 1]

callback(null, {
Hash: dirNode.multihash(),
Size: dirNode.size(),
Name: dirName
})
})
})
}

// function bufferImporter (path) {}
// function streamImporter (path) {}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/test-data/dir-nested.block
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
5
" $��G�,�A�4{���x�Z/.����D`� 200Bytes.txt�3
" Y��9_)a���˹2�R�m�Ŗke�9�� dir-another0
" Ty�5;_9Yf�q��F�Lhyl���/��level-1�

Expand Down
4 changes: 4 additions & 0 deletions tests/test-data/dir-nested/200Bytes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
�wx���xM��{
D���zH/&^�� ��RS���/��v,��R
�=��N���g~���pf1�\[�>�%��U�1�@Q���׀2&m6�q���Q؁��]��|���!�K E�~J֕읝�o�j��b�n3�eT�)D+;s
컓��:Ty!c�3����\*���T7��E?[��Pv}��A+�c�x�~�e�
Expand Down
4 changes: 4 additions & 0 deletions tests/test-data/dir-nested/level-1/200Bytes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
�wx���xM��{
D���zH/&^�� ��RS���/��v,��R
�=��N���g~���pf1�\[�>�%��U�1�@Q���׀2&m6�q���Q؁��]��|���!�K E�~J֕읝�o�j��b�n3�eT�)D+;s
컓��:Ty!c�3����\*���T7��E?[��Pv}��A+�c�x�~�e�
Expand Down
23 changes: 23 additions & 0 deletions tests/test-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('layout: importer', function () {
const small = __dirname + '/test-data/200Bytes.txt'
const dirSmall = __dirname + '/test-data/dir-small'
const dirBig = __dirname + '/test-data/dir-big'
const dirNested = __dirname + '/test-data/dir-nested'

var ds

Expand Down Expand Up @@ -159,6 +160,28 @@ describe('layout: importer', function () {
})
})

it('import a nested directory', (done) => {
importer.import({
path: dirNested,
dagService: ds,
recursive: true
}, function (err, stats) {
expect(err).to.not.exist
expect(bs58.encode(stats.Hash).toString()).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN')

ds.get(stats.Hash, (err, node) => {
expect(err).to.not.exist
expect(node.links.length).to.equal(3)

const dirNode = new DAGNode()
dirNode.unMarshal(fs.readFileSync(dirNested + '.block'))
expect(node.links).to.deep.equal(dirNode.links)
expect(node.data).to.deep.equal(dirNode.data)
done()
})
})
})

it.skip('import a buffer', (done) => {})
it.skip('import from a stream', (done) => {})
})
Expand Down

0 comments on commit a8cfdfc

Please sign in to comment.