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

WIP add, cat and get HTTP-API Endpoints #205

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions QmRRdjTN2PjyEPrW73GBxJNAZrstH5tCZzwHYFJpSTKkhe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "Data": "another", "Links": [ { "Name": "some link", "Hash": "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V", "Size": 8 } ] }
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"lodash.get": "^4.2.1",
"lodash.set": "^4.1.0",
"multiaddr": "^1.4.1",
"ndjson": "^1.4.3",
"path-exists": "^3.0.0",
"peer-book": "0.1.0",
"peer-id": "^0.6.6",
Expand Down
105 changes: 76 additions & 29 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,49 @@ function checkPath (inPath, recursive) {
return inPath
}

function daemonOn (res, inPath, ipfs) {
const files = []
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
async.eachLimit(res, 10, (element, callback) => {
if (fs.statSync(element).isDirectory()) {
callback()
} else {
const filePair = {
path: element.substring(index + 1, element.length),
content: fs.createReadStream(element)
}
files.push(filePair)
callback()
}
}, (err) => {
if (err) {
throw err
}
ipfs.add(files, (err, res) => {
if (err) {
throw err
}
res.forEach((goRes) => {
console.log('added', goRes.Hash, goRes.Name)
})
})
})
} else {
const filePair = {
path: inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length),
content: fs.createReadStream(inPath)
}
files.push(filePair)
ipfs.add(files, (err, res) => {
if (err) {
throw err
}
console.log('added', res[0].Hash, res[0].Name)
})
}
return
}
module.exports = Command.extend({
desc: 'Add a file to IPFS using the UnixFS data format',

Expand All @@ -59,36 +102,40 @@ module.exports = Command.extend({
if (err) {
throw err
}
const i = ipfs.files.add()
var filePair
i.on('data', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
i.once('end', () => {
return
})
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
async.eachLimit(res, 10, (element, callback) => {
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}, (err) => {
if (err) {
throw err
}
i.end()
})
if (utils.isDaemonOn()) {
daemonOn(res, inPath, ipfs)
} else {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
const i = ipfs.files.add()
var filePair
i.on('data', (file) => {
console.log('added', bs58.encode(file.multihash).toString(), file.path)
})
i.once('end', () => {
return
})
if (res.length !== 0) {
const index = inPath.lastIndexOf('/')
async.eachLimit(res, 10, (element, callback) => {
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}, (err) => {
if (err) {
throw err
}
i.end()
})
} else {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
}
}
})
})
Expand Down
9 changes: 9 additions & 0 deletions src/cli/commands/files/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ module.exports = Command.extend({
if (err) {
throw err
}
if (utils.isDaemonOn()) {
ipfs.cat(path, (err, res) => {
if (err) {
throw err
}
console.log(res.toString())
})
return
}
ipfs.files.cat(path, (err, res) => {
if (err) {
throw (err)
Expand Down
159 changes: 159 additions & 0 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
'use strict'

const bs58 = require('bs58')
// const ndjson = require('ndjson')
// const async = require('async')
const Readable = require('stream').Readable
const multipart = require('ipfs-multipart')
const debug = require('debug')
const log = debug('http-api:files')
log.error = debug('http-api:files:error')

exports = module.exports

// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args`
exports.parseKey = (request, reply) => {
if (!request.query.arg) {
return reply("Argument 'key' is required").code(400).takeover()
}

try {
return reply({
key: new Buffer(bs58.decode(request.query.arg))
})
} catch (err) {
log.error(err)
return reply({
Message: 'invalid ipfs ref path',
Code: 0
}).code(500).takeover()
}
}

exports.add = {
// pre request handler that parses the args and returns `node` which is assigned to `request.pre.args`
handler: (request, reply) => {
if (!request.payload) {
return reply('Array, Buffer, or String is required').code(400).takeover()
}
const parser = multipart.reqParser(request.payload)
var file = false
var filePair
const resArr = []
// let serialArr
// console.log(serialArr)
var i = request.server.app.ipfs.files.add()
// var serialize = ndjson.stringify()

i.on('data', (file) => {
resArr.push({
Name: file.path,
Hash: bs58.encode(file.multihash).toString()
})
})

i.on('end', () => {
if (resArr.length === 0 && file) {
return reply({
Message: 'Failed to add files',
Code: 0
}).code(500)
}

/* serialize.on('data', (line) => {
var serialArr = line
console.log(line)
return reply(serialArr)
//console.log(line)
})

async.eachSeries(resArr, (item, callback) => {
serialize.write(item)
callback()
}, (done) => {
serialize.end()
})

serialize.on('end', () => {
//console.log(serialArr.length)
//return reply(serialArr)
}) */
return reply(resArr)
})

parser.on('file', (fileName, fileStream) => {
var rs = new Readable()
var init = false
rs._read = () => {
if (init) {
return
}
init = true
}
fileStream.on('data', (data) => {
rs.push(data)
file = true
})
fileStream.on('end', () => {
rs.push(null)
filePair = {
path: fileName,
stream: rs
}
i.write(filePair)
})
})

parser.on('end', () => {
if (!file) {
return reply("File argument 'data' is required").code(400).takeover()
}
i.end()
})
}
}

exports.cat = {
// 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.cat(key, (err, ee) => {
if (err) {
log.error(err)
return reply({
Message: 'Failed to cat file: ' + err,
Code: 0
}).code(500)
}
ee.on('file', (data) => {
return reply(data.stream)
})
})
}
}

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, ee) => {
if (err) {
log.error(err)
return reply({
Message: 'Failed to get object: ' + err,
Code: 0
}).code(500)
}

return reply(ee)
})
}
}
1 change: 1 addition & 0 deletions src/http-api/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ exports.object = require('./object')
exports.config = require('./config')
exports.block = require('./block')
exports.swarm = require('./swarm')
exports.files = require('./files')
41 changes: 41 additions & 0 deletions src/http-api/routes/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const resources = require('./../resources')

module.exports = (server) => {
const api = server.select('API')

api.route({
method: '*',
path: '/api/v0/add',
config: {
payload: {
parse: false,
output: 'stream'
},
handler: resources.files.add.handler
}
})

api.route({
method: '*',
path: '/api/v0/cat',
config: {
pre: [
{ method: resources.files.cat.parseArgs, assign: 'args' }
],
handler: resources.files.cat.handler
}
})

api.route({
method: '*',
path: '/api/v0/get',
config: {
pre: [
{ method: resources.files.get.parseArgs, assign: 'args' }
],
handler: resources.files.get.handler
}
})
}
1 change: 1 addition & 0 deletions src/http-api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = (server) => {
require('./object')(server)
// require('./repo')(server)
require('./config')(server)
require('./files')(server)
require('./swarm')(server)
}
Loading