To transfer an attachment from cradle to a client without buffering, you can pipe its readableStream to the reponse's writableStream.
The long version
A variant of cradle's db.getAttachment
returns a readableStream
(see streaming from cradle's docs). express' res
object on the other hand serves as a writableStream
. This means you should be able* to pipe an attachment to it like this:
// respond to a request like '/preview/?doc=xxxxxx&image=img1.jpg'
app.get('/preview/', function(req, res){
// fetch query parameters from url (?doc=...&image=...)
var id = req.query.doc
var filename = req.query.image
// create a readableStream from the doc's attachment
var readStream = db.getAttachment(id, filename, function (err) {
// note: no second argument
// this inner function will be executed
// once the stream is finished
// or has failed
if (err)
return console.dir(err)
else
console.dir('the stream has been successfully piped')
})
// set the appropriate headers here
res.setHeader("Content-Type", "image/jpeg")
// pipe the attachment to the client's response
readStream.pipe(res)
})
Or, slightly shorter:
app.get('/preview/', function(req, res){
res.setHeader("Content-Type", "image/png")
db.getAttachment(req.query.doc, req.query.image, someErrorHandlerFunction).pipe(res)
})
*I'm not at work, so sadly I cannot verify this code will run. Drop me a line if you have problems.