Permalink
Browse files

add dat.createBlobReadStream

  • Loading branch information...
maxogden committed May 29, 2014
1 parent 66e2080 commit 228d0980242e06aa19cf9bbff894a0e63a0edcd1
Showing with 70 additions and 9 deletions.
  1. +14 −0 docs/api.md
  2. +4 −2 index.js
  3. +20 −0 lib/commands.js
  4. +4 −7 lib/rest-handler.js
  5. +28 −0 test/tests/write-streams.js
View
@@ -158,6 +158,20 @@ Returns a writable stream that you can stream a binary blob attachment into. Cal
If specified `row` should be a JS object you want to attach the blob to, obeying the same update/conflict rules as `db.put`. If not specified a new row will be created.
## createBlobReadStream
```js
var blobWriter = db.createBlobReadStream(key, filename, [options])
```
Returns a readable stream of blob data.
`key` is the key of the row where the blob is stored. `filename` is the name of the attached blob. both are required.
### Options
* `version` (default latest) - the version of the row to get
## listen
```js
View
@@ -1,6 +1,7 @@
var fs = require('fs')
var tty = require('tty')
var path = require('path')
var os = require('os')
var debug = require('debug')('dat.init')
var request = require('request').defaults({json: true})
@@ -77,8 +78,9 @@ function Dat(dir, opts, onReady) {
loadMeta()
})
}
// the 2000 here is thanks to windows server, which errors w/ lower values
if (!tty.isatty(0)) setTimeout(read, 2000)
// windows server fails when timeout is lower than 2000
// timeout if called from CLI && is not a mac
if (!tty.isatty(0) && !(os.platform().match('darwin'))) setTimeout(read, 2000)
else read()
}
View
@@ -454,6 +454,26 @@ dat.createBlobWriteStream = function(options, doc, cb) {
return blobWrite
}
dat.createBlobReadStream = function(key, name, opts) {
var self = this
if (!opts) opts = {}
var proxy = through()
self.get(key, opts, function(err, doc) {
if (err) return proxy.emit('error', err)
if (!doc.attachments[name]) return proxy.emit('error', new Error('file is not attached to row'))
var readStream = self.blobs.createReadStream(doc.attachments[name].hash)
readStream.pipe(proxy)
})
return proxy
}
dat.createWriteStream = function(options) {
return writeStream(this, options)
}
View
@@ -44,14 +44,11 @@ RestHandler.prototype.blob = function(req, res, opts) {
var self = this
if (req.method === 'GET') {
var id = opts.id
self.dat.get(id, function(err, doc) {
var attachment
if (doc && doc.attachments) {
attachment = doc.attachments[opts.filename]
}
if (err || !attachment) return self.error(res, 404, {"error": "Not Found"})
self.dat.blobs.createReadStream(attachment.hash).pipe(res)
var blob = self.dat.createBlobReadStream(opts.id, opts.filename, opts)
blob.on('error', function(err) {
return self.error(res, 404, {"error": "Not Found"})
})
blob.pipe(res)
return
}
@@ -27,6 +27,33 @@ module.exports.blobWriteStream = function(test, common) {
})
}
module.exports.blobReadStream = function(test, common) {
test('getting a blob read stream by row key + name', function(t) {
common.getDat(t, function(dat, done) {
var ws = dat.createBlobWriteStream('write-streams.js', function(err, doc) {
t.notOk(err, 'no blob write err')
var attachment = doc.attachments['write-streams.js']
t.ok(attachment, 'doc has attachment')
var rs = dat.createBlobReadStream(doc.id, 'write-streams.js')
rs.on('error', function(e) {
t.false(e, 'no read stream err')
done()
})
rs.pipe(concat(function(file) {
t.equal(file.length, attachment.size, 'attachment size is correct')
done()
}))
})
fs.createReadStream(path.join(__dirname, 'write-streams.js')).pipe(ws)
})
})
}
module.exports.singleNdjsonObject = function(test, common) {
test('piping a single ndjson object into a write stream', function(t) {
common.getDat(t, function(dat, done) {
@@ -648,6 +675,7 @@ module.exports.keepTotalRowCount = function(test, common) {
module.exports.all = function (test, common) {
module.exports.blobWriteStream(test, common)
module.exports.blobReadStream(test, common)
module.exports.singleNdjsonObject(test, common)
module.exports.singleNdjsonString(test, common)
module.exports.multipleNdjsonObjects(test, common)

0 comments on commit 228d098

Please sign in to comment.