From 11cb4ca47d1fd818f91a348f0d6713fe1cd401fe Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Thu, 1 Sep 2016 18:06:47 +0200 Subject: [PATCH] feat(files): get interface-ipfs-core files tests pass through http-api using ipfs-api --- src/http-api/resources/files.js | 62 ++++++++++++++++++- src/http-api/routes/files.js | 14 +++++ .../test-files.js | 6 +- 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/src/http-api/resources/files.js b/src/http-api/resources/files.js index 5d4dbda052..46cff0093c 100644 --- a/src/http-api/resources/files.js +++ b/src/http-api/resources/files.js @@ -4,8 +4,10 @@ const bs58 = require('bs58') const ndjson = require('ndjson') const multipart = require('ipfs-multipart') const debug = require('debug') +const tar = require('tar-stream') const log = debug('http-api:files') log.error = debug('http-api:files:error') +const async = require('async') exports = module.exports @@ -44,8 +46,55 @@ exports.cat = { Code: 0 }).code(500) } + return reply(stream).header('X-Stream-Output', '1') + }) + } +} + +exports.get = { + // uses common parseKey method that returns a `key` + parseArgs: exports.parseKey, + + // main route handler which is called after the above `parseArgs`, but only if the args were valid + handler: (request, reply) => { + const key = request.pre.args.key + + request.server.app.ipfs.files.get(key, (err, stream) => { + if (err) { + log.error(err) + return reply({ + Message: 'Failed to get file: ' + err, + Code: 0 + }).code(500) + } + var pack = tar.pack() + const files = [] stream.on('data', (data) => { - return reply(data) + files.push(data) + }) + const processFile = (file) => { + return (callback) => { + if (!file.content) { // is directory + pack.entry({name: file.path, type: 'directory'}) + callback() + } else { // is file + const fileContents = [] + file.content.on('data', (data) => { + fileContents.push(data) + }) + file.content.on('end', () => { + pack.entry({name: file.path}, Buffer.concat(fileContents)) + callback() + }) + } + } + } + stream.on('end', () => { + const callbacks = files.map(processFile) + async.series(callbacks, () => { + pack.finalize() + reply(pack).header('X-Stream-Output', '1') + }) }) }) } @@ -75,9 +124,10 @@ exports.add = { } fileAdder.on('data', (file) => { + const filePath = file.path ? file.path : file.hash serialize.write({ - Name: file.path, - Hash: bs58.encode(file.node.multihash()).toString() + Name: filePath, + Hash: file.hash }) filesAdded++ }) @@ -104,6 +154,12 @@ exports.add = { filesParsed = true fileAdder.write(filePair) }) + parser.on('directory', (directory) => { + fileAdder.write({ + path: directory, + content: '' + }) + }) parser.on('end', () => { if (!filesParsed) { diff --git a/src/http-api/routes/files.js b/src/http-api/routes/files.js index ddf722f850..da57b3f2f1 100644 --- a/src/http-api/routes/files.js +++ b/src/http-api/routes/files.js @@ -6,6 +6,7 @@ module.exports = (server) => { const api = server.select('API') api.route({ + // TODO fix method method: '*', path: '/api/v0/cat', config: { @@ -17,6 +18,19 @@ module.exports = (server) => { }) api.route({ + // TODO fix method + method: '*', + path: '/api/v0/get', + config: { + pre: [ + { method: resources.files.get.parseArgs, assign: 'args' } + ], + handler: resources.files.get.handler + } + }) + + api.route({ + // TODO fix method method: '*', path: '/api/v0/add', config: { diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js index 4a322b948e..4762a26f76 100644 --- a/test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js +++ b/test/http-api/interface-ipfs-core-over-ipfs-api/test-files.js @@ -2,7 +2,6 @@ 'use strict' -/* const test = require('interface-ipfs-core') const FactoryClient = require('./../../utils/factory-http') @@ -17,8 +16,5 @@ const common = { fc.dismantle(callback) } } -*/ -// TODO -// needs: https://github.com/ipfs/js-ipfs/pull/323 -// test.files(common) +test.files(common)