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

Commit

Permalink
chore: cleanup, apply CR
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Sep 12, 2016
1 parent b0a6db9 commit 4050195
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 67 deletions.
38 changes: 20 additions & 18 deletions src/core/ipfs/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,26 @@ module.exports = function files (self) {
return callback(new Error('You must supply a multihash'))
}

pull(
pull.values([hash]),
pull.asyncMap(self._dagS.get.bind(self._dagS)),
pull.take(1),
pull.map((node) => {
const data = UnixFS.unmarshal(node.data)
if (data.type === 'directory') {
return pull.error(new Error('This dag node is a directory'))
}

return exporter(hash, self._dagS)
}),
pull.flatten(),
pull.collect((err, files) => {
if (err) return callback(err)
callback(null, toStream.source(files[0].content))
})
)
self._dagS.get(hash, (err, node) => {
if (err) {
return callback(err)
}

const data = UnixFS.unmarshal(node.data)
if (data.type === 'directory') {
return callback(
new Error('This dag node is a directory')
)
}

pull(
exporter(hash, self._dagS),
pull.collect((err, files) => {
if (err) return callback(err)
callback(null, toStream.source(files[0].content))
})
)
})
}),

get: promisify((hash, callback) => {
Expand Down
29 changes: 18 additions & 11 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ exports.get = {
const pack = tar.pack()

ipfs.files.getPull(key, (err, stream) => {
if (err) return handleError(err)
if (err) {
log.error(err)

reply({
Message: 'Failed to get file: ' + err,
Code: 0
}).code(500)
return
}

pull(
stream,
Expand All @@ -91,7 +99,15 @@ exports.get = {
}
}),
pull.onEnd((err) => {
if (err) return handleError(err)
if (err) {
log.error(err)

reply({
Message: 'Failed to get file: ' + err,
Code: 0
}).code(500)
return
}

pack.finalize()
})
Expand All @@ -101,15 +117,6 @@ exports.get = {
// to pull values through
reply(pack).header('X-Stream-Output', '1')
})

function handleError (err) {
log.error(err)

reply({
Message: 'Failed to get file: ' + err,
Code: 0
}).code(500)
}
}
}

Expand Down
38 changes: 0 additions & 38 deletions test/http-api/ipfs-api/test-files.js

This file was deleted.

0 comments on commit 4050195

Please sign in to comment.