From 40501955a4c9e40f5d7cb6b3d2699b6ce603d358 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Mon, 12 Sep 2016 11:36:29 +0200 Subject: [PATCH] chore: cleanup, apply CR --- src/core/ipfs/files.js | 38 +++++++++++++++------------- src/http-api/resources/files.js | 29 +++++++++++++-------- test/http-api/ipfs-api/test-files.js | 38 ---------------------------- 3 files changed, 38 insertions(+), 67 deletions(-) delete mode 100644 test/http-api/ipfs-api/test-files.js diff --git a/src/core/ipfs/files.js b/src/core/ipfs/files.js index 4f829d5269..5525a66d62 100644 --- a/src/core/ipfs/files.js +++ b/src/core/ipfs/files.js @@ -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) => { diff --git a/src/http-api/resources/files.js b/src/http-api/resources/files.js index b5309b6de6..3c3cbd1e5e 100644 --- a/src/http-api/resources/files.js +++ b/src/http-api/resources/files.js @@ -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, @@ -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() }) @@ -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) - } } } diff --git a/test/http-api/ipfs-api/test-files.js b/test/http-api/ipfs-api/test-files.js deleted file mode 100644 index 95312de30b..0000000000 --- a/test/http-api/ipfs-api/test-files.js +++ /dev/null @@ -1,38 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const expect = require('chai').expect - -module.exports = (ctl) => { - describe('.files', () => { - describe('.add', () => {}) // TODO - - describe('.cat', () => { - it('returns error for request without argument', (done) => { - ctl.cat(null, (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns error for request with invalid argument', (done) => { - ctl.cat('invalid', (err, result) => { - expect(err).to.exist - done() - }) - }) - - it('returns a buffer', (done) => { - ctl.cat('QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o', (err, result) => { - expect(err).to.not.exist - expect(result).to.deep.equal(new Buffer('hello world' + '\n')) - done() - }) - }) - }) - - describe('.get', () => {}) // TODO - - describe('.ls', () => {}) // TODO - }) -}