Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

files.get #271

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
11 changes: 11 additions & 0 deletions src/api/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

module.exports = (send) => {
return function get (path, opts, cb) {
if (typeof opts === 'function' && !cb) {
cb = opts
opts = {}
}
return send('get', path, opts, null, cb)
}
}
1 change: 1 addition & 0 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function requireCommands () {
diag: require('./api/diag'),
id: require('./api/id'),
files: require('./api/files'),
get: require('./api/get'),
log: require('./api/log'),
ls: require('./api/ls'),
mount: require('./api/mount'),
Expand Down
101 changes: 101 additions & 0 deletions test/api/get.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-env mocha */
/* globals apiClients */
'use strict'

const expect = require('chai').expect
const isNode = require('detect-node')
const fs = require('fs')
const bl = require('bl')

const path = require('path')
const streamEqual = require('stream-equal')

let testfile
let testfileBig

if (isNode) {
testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt'))
testfileBig = fs.createReadStream(path.join(__dirname, '/../15mb.random'), { bufferSize: 128 })
} else {
testfile = require('raw!../testfile.txt')
}

describe('.get', () => {
it('get with no compression args', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
expect(err).to.not.exist
res.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain(testfile.toString())
done()
}))
})
})

it('get with archive true', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {archive: true}, (err, res) => {
expect(err).to.not.exist
res.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain(testfile.toString())
done()
}))
})
})

it('get err with out of range compression level', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 10}, (err, res) => {
expect(err).to.exist
expect(err.toString()).to.equal('Error: Compression level must be between 1 and 9')
done()
})
})

it('get with compression level', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 1}, (err, res) => {
expect(err).to.not.exist
done()
})
})

it.skip('get BIG file', (done) => {
if (!isNode) {
return done()
}

apiClients.a.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => {
expect(err).to.not.exist

// Do not blow out the memory of nodejs :)
streamEqual(res, testfileBig, (err, equal) => {
expect(err).to.not.exist
expect(equal).to.be.true
done()
})
})
})

describe('promise', () => {
it.skip('get', (done) => {
return apiClients.a.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
.then((res) => {
let buf = ''
res
.on('error', (err) => {
throw err
})
.on('data', (data) => {
buf += data
})
.on('end', () => {
expect(buf).to.contain(testfile.toString())
done()
})
})
})
})
})