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

Commit

Permalink
feat: add missing progress bar support for http api
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Oct 10, 2017
1 parent d311dc5 commit 8ea66f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@
"bl": "^1.2.1",
"boom": "^5.2.0",
"byteman": "^1.3.5",
"cids": "~0.5.1",
"debug": "^3.0.1",
"cids": "^0.5.1",
"debug": "^3.0.1",
"file-type": "^6.1.0",
"filesize": "^3.5.10",
"fsm-event": "^2.1.0",
Expand Down Expand Up @@ -144,6 +143,7 @@
"progress": "^2.0.0",
"promisify-es6": "^1.0.3",
"pull-file": "^1.0.0",
"pull-ndjson": "^0.1.1",
"pull-paramap": "^1.2.2",
"pull-pushable": "^2.1.1",
"pull-sort": "^1.0.1",
Expand Down Expand Up @@ -220,4 +220,4 @@
"Łukasz Magiera <magik6k@users.noreply.github.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
]
}
}
36 changes: 29 additions & 7 deletions src/http/api/resources/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ log.error = debug('jsipfs:http-api:files:error')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const pushable = require('pull-pushable')
const EOL = require('os').EOL
const toStream = require('pull-stream-to-stream')
const Joi = require('joi')
const ndjson = require('pull-ndjson')

exports = module.exports

Expand Down Expand Up @@ -104,7 +104,7 @@ exports.get = {
pull(
stream,
pull.asyncMap((file, cb) => {
const header = {name: file.path}
const header = { name: file.path }
if (!file.content) {
header.type = 'directory'
pack.entry(header)
Expand Down Expand Up @@ -207,10 +207,34 @@ exports.add = {
fileAdder.end()
})

const replyStream = pushable()
const progressHandler = (bytes) => {
replyStream.push({ Bytes: bytes })
}

const options = {
'cid-version': request.query['cid-version'],
'raw-leaves': request.query['raw-leaves']
'raw-leaves': request.query['raw-leaves'],
progress: request.query['progress'] ? progressHandler : null
}

const stream = toStream.source(pull(
replyStream,
ndjson.serialize()
))

// const stream = toStream.source(replyStream.source)
// hapi is not very clever and throws if no
// - _read method
// - _readableState object
// are there :(
if (!stream._read) {
stream._read = () => {}
stream._readableState = {}
}
reply(stream)
.header('x-chunked-output', '1')
.header('content-type', 'application/json')

pull(
fileAdder,
Expand All @@ -221,7 +245,6 @@ exports.add = {
Hash: file.hash
}
}),
pull.map((file) => JSON.stringify(file) + EOL),
pull.collect((err, files) => {
if (err) {
return reply({
Expand All @@ -237,9 +260,8 @@ exports.add = {
}).code(500)
}

reply(files.join('\n'))
.header('x-chunked-output', '1')
.header('content-type', 'application/json')
files.forEach((f) => replyStream.push(f))
replyStream.end()
})
)
}
Expand Down
8 changes: 8 additions & 0 deletions test/cli/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ describe('files', () => runOnAndOff((thing) => {
ipfs = thing.ipfs
})

it('add with progress', () => {
return ipfs('files add -p src/init-files/init-docs/readme')
.then((out) => {
expect(out)
.to.eql('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme\n')
})
})

it('add', () => {
return ipfs('files add src/init-files/init-docs/readme')
.then((out) => {
Expand Down

0 comments on commit 8ea66f9

Please sign in to comment.