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

add http resource, files core upgrades, cat cli upgrades, add/cat interface-core integration #256

Closed
wants to merge 5 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@
"kumavis <kumavis@users.noreply.github.com>",
"nginnever <ginneversource@gmail.com>"
]
}
}
132 changes: 106 additions & 26 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ log.error = debug('cli:version:error')
const bs58 = require('bs58')
const fs = require('fs')
const parallelLimit = require('run-parallel-limit')
const async = require('async')
const path = require('path')
const glob = require('glob')

Expand Down Expand Up @@ -35,6 +36,50 @@ 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 +104,71 @@ 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('/')
parallelLimit(res.map((element) => (callback) => {
if (!fs.statSync(element).isDirectory()) {
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
}
callback()
}), 10, (err) => {
if (utils.isDaemonOn()) {
daemonOn(res, inPath, ipfs)
} else {
ipfs.files.add((err, i) => {
if (err) {
throw err
}
i.end()
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('/')
parallelLimit(res.map((element) => (callback) => {
if (!fs.statSync(element).isDirectory()) {
element.substring(index + 1, element.length)
i.write({
path: element.substring(index + 1, element.length),
stream: fs.createReadStream(element)
})
} else {
fs.readdir(element, (err, files) => {
if (err) {
throw err
}
if (files.length === 0) {
i.write({
path: element.substring(index + 1, element.length),
stream: null
})
}
})
}
callback()
}), 10, (err) => {
if (err) {
throw err
}
i.end()
})
} else {
if (!fs.statSync(inPath).isDirectory()) {
rs = fs.createReadStream(inPath)
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
filePair = {path: inPath, stream: rs}
i.write(filePair)
i.end()
} else {
fs.readdir(inPath, (err, files) => {
if (err) {
throw err
}
if (files.length === 0) {
i.write({
path: inPath,
stream: null
})
}
})
}
}
})
} 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
6 changes: 2 additions & 4 deletions src/cli/commands/files/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ module.exports = Command.extend({
})
return
}
ipfs.files.cat(path, (err, res) => {
ipfs.files.cat(path, (err, file) => {
if (err) {
throw (err)
}
res.on('data', (data) => {
data.stream.pipe(process.stdout)
})
file.pipe(process.stdout)
})
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ function IPFS (repoInstance) {
this.object = object(this)
this.libp2p = libp2p(this)
this.files = files(this)
this.cat = files(this).cat // Alias for js-ipfs-api cat
this.add = files(this).add // Alias for js-ipfs-api add
this.bitswap = bitswap(this)
}
25 changes: 17 additions & 8 deletions src/core/ipfs/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
const Importer = require('ipfs-unixfs-engine').importer
const Exporter = require('ipfs-unixfs-engine').exporter
const UnixFS = require('ipfs-unixfs')
const promisify = require('promisify-es6')

module.exports = function files (self) {
return {
add: (arr, callback) => {
add: promisify((arr, callback) => {
if (typeof arr === 'function') {
callback = arr
arr = undefined
Expand All @@ -15,7 +16,7 @@ module.exports = function files (self) {
callback = function noop () {}
}
if (arr === undefined) {
return new Importer(self._dagS)
callback(null, new Importer(self._dagS))
}

const i = new Importer(self._dagS)
Expand All @@ -34,8 +35,13 @@ module.exports = function files (self) {
})

i.end()
},
cat: (hash, callback) => {
}),

cat: promisify((hash, callback) => {
if (typeof hash === 'function') {
callback = hash
return callback('You must supply a multihash', null)
}
self._dagS.get(hash, (err, fetchedNode) => {
if (err) {
return callback(err, null)
Expand All @@ -45,13 +51,16 @@ module.exports = function files (self) {
callback('This dag node is a directory', null)
} else {
const exportStream = Exporter(hash, self._dagS)
callback(null, exportStream)
exportStream.once('data', (object) => {
callback(null, object.stream)
})
}
})
},
get: (hash, callback) => {
}),

get: promisify((hash, callback) => {
var exportFile = Exporter(hash, self._dagS)
callback(null, exportFile)
}
})
}
}
69 changes: 67 additions & 2 deletions src/http-api/resources/files.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'

const bs58 = require('bs58')
const multihash = require('multihashes')
const ndjson = require('ndjson')
const multipart = require('ipfs-multipart')
const debug = require('debug')
const log = debug('http-api:files')
log.error = debug('http-api:files:error')
Expand Down Expand Up @@ -42,8 +45,70 @@ exports.cat = {
Code: 0
}).code(500)
}
stream.on('data', (data) => {
return reply(data.stream)
return reply(stream)
})
}
}

exports.add = {
handler: (request, reply) => {
if (!request.payload) {
return reply('Array, Buffer, or String is required.').code(400).takeover()
}

const parser = multipart.reqParser(request.payload)

var filesParsed = false
var filesAdded = 0

var serialize = ndjson.serialize()
// hapi doesn't permit object streams: http://hapijs.com/api#replyerr-result
serialize._readableState.objectMode = false

request.server.app.ipfs.files.add((err, fileAdder) => {
if (err) {
return reply({
Message: err,
Code: 0
}).code(500)
}

fileAdder.on('data', (file) => {
serialize.write({
Name: file.path,
Hash: multihash.toB58String(file.multihash)
})
filesAdded++
})

fileAdder.on('end', () => {
if (filesAdded === 0 && filesParsed) {
return reply({
Message: 'Failed to add files.',
Code: 0
}).code(500)
} else {
serialize.end()
return reply(serialize)
.header('x-chunked-output', '1')
.header('content-type', 'application/json')
}
})

parser.on('file', (fileName, fileStream) => {
var filePair = {
path: fileName,
stream: fileStream
}
filesParsed = true
fileAdder.write(filePair)
})

parser.on('end', () => {
if (!filesParsed) {
return reply("File argument 'data' is required.").code(400).takeover()
}
fileAdder.end()
})
})
}
Expand Down
12 changes: 12 additions & 0 deletions src/http-api/routes/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ module.exports = (server) => {
handler: resources.files.cat.handler
}
})

api.route({
method: '*',
path: '/api/v0/add',
config: {
payload: {
parse: false,
output: 'stream'
},
handler: resources.files.add.handler
}
})
}
2 changes: 1 addition & 1 deletion src/http-api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = (server) => {
require('./object')(server)
// require('./repo')(server)
require('./config')(server)
require('./files')(server)
require('./swarm')(server)
require('./bitswap')(server)
require('./files')(server)
}
2 changes: 1 addition & 1 deletion test/cli-tests/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const createTempNode = require('../utils/temp-node')
const repoPath = require('./index').repoPath

describe('bitswap', function () {
this.timeout(20000)
this.timeout(80000)
const env = _.clone(process.env)
env.IPFS_PATH = repoPath

Expand Down
Loading