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

Commit

Permalink
feat: migrate files to use IPLD Resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 29, 2016
1 parent 5cb10cc commit 0fb1a1a
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ const pull = require('pull-stream')
const sort = require('pull-sort')
const toStream = require('pull-stream-to-stream')
const toPull = require('stream-to-pull-stream')
const CID = require('cids')

module.exports = function files (self) {
const createAddPullStream = () => {
return pull(
pull.map(normalizeContent),
pull.flatten(),
importer(self._dagService),
importer(self._ipldResolver),
pull.asyncMap(prepareFile.bind(null, self))
)
}
Expand All @@ -36,7 +37,7 @@ module.exports = function files (self) {

pull(
pull.values(normalizeContent(data)),
importer(self._dagService),
importer(self._ipldResolver),
pull.asyncMap(prepareFile.bind(null, self)),
sort((a, b) => {
if (a.path < b.path) return 1
Expand All @@ -52,7 +53,7 @@ module.exports = function files (self) {
return callback(new Error('You must supply a multihash'))
}

self._dagService.get(hash, (err, node) => {
self._ipldResolver.get(new CID(hash), (err, node) => {
if (err) {
return callback(err)
}
Expand All @@ -65,9 +66,11 @@ module.exports = function files (self) {
}

pull(
exporter(hash, self._dagService),
exporter(hash, self._ipldResolver),
pull.collect((err, files) => {
if (err) return callback(err)
if (err) {
return callback(err)
}
callback(null, toStream.source(files[0].content))
})
)
Expand All @@ -76,7 +79,7 @@ module.exports = function files (self) {

get: promisify((hash, callback) => {
callback(null, toStream.source(pull(
exporter(hash, self._dagService),
exporter(hash, self._ipldResolver),
pull.map((file) => {
if (file.content) {
file.content = toStream.source(file.content)
Expand All @@ -89,15 +92,17 @@ module.exports = function files (self) {
}),

getPull: promisify((hash, callback) => {
callback(null, exporter(hash, self._dagService))
callback(null, exporter(hash, self._ipldResolver))
})
}
}

function prepareFile (self, file, cb) {
const bs58mh = multihashes.toB58String(file.multihash)
self.object.get(file.multihash, (err, node) => {
if (err) return cb(err)
if (err) {
return cb(err)
}

cb(null, {
path: file.path || bs58mh,
Expand Down

0 comments on commit 0fb1a1a

Please sign in to comment.