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

Commit

Permalink
feat(files): get interface-ipfs-core files tests pass through http-api
Browse files Browse the repository at this point in the history
using ipfs-api
  • Loading branch information
victorb authored and daviddias committed Sep 12, 2016
1 parent e29f429 commit 11cb4ca
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
62 changes: 59 additions & 3 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')
})
})
})
}
Expand Down Expand Up @@ -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++
})
Expand All @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions src/http-api/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = (server) => {
const api = server.select('API')

api.route({
// TODO fix method
method: '*',
path: '/api/v0/cat',
config: {
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

'use strict'

/*
const test = require('interface-ipfs-core')
const FactoryClient = require('./../../utils/factory-http')

Expand All @@ -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)

0 comments on commit 11cb4ca

Please sign in to comment.