aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/serve.js40
1 files changed, 26 insertions, 14 deletions
diff --git a/lib/serve.js b/lib/serve.js
index 81ab0b7..25969c9 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -21,6 +21,7 @@ var htime = require('human-time')
var ph = require('pull-hyperscript')
var emojis = require('emoji-named-characters')
var jpeg = require('jpeg-autorotate')
+var concat = require('pull-concat')
module.exports = Serve
@@ -956,27 +957,38 @@ Serve.prototype.blob = function (id) {
}
if (!has) return self.respond(404, 'Not found')
- blobs.size(id, done())
- var rotatedSize = null
+ var heresTheData = done()
+ var heresTheType = done().bind(self, null)
pull(
blobs.get(id),
pull.map(Buffer),
- pull.asyncMap(function (buf, cb) {
- jpeg.rotate(buf, {}, function (err, buffer, orientation) {
- if (err) return cb(null, buf)
- rotatedSize = buffer.length
- return cb(null, buffer)
- })
- }),
- ident(done().bind(self, null)),
- self.respondSink()
+ ident(heresTheType),
+ concat.buffer(onFullBuffer)
)
- done(function (err, size, type) {
- if (err) console.trace(err)
+
+ function onFullBuffer (err, buffer) {
+ if (err) return heresTheData(err)
+
+ jpeg.rotate(buffer, {}, function (err, rotatedBuffer, orientation) {
+ if (!err) buffer = rotatedBuffer
+
+ heresTheData(null, buffer)
+ pull(
+ pull.once(buffer),
+ self.respondSink()
+ )
+ })
+ }
+
+ done(function (err, data, type) {
+ if (err) {
+ console.trace(err)
+ self.respond(500, err.message || err)
+ }
type = type && mime.lookup(type)
if (type) self.res.setHeader('Content-Type', type)
- if (typeof size === 'number') self.res.setHeader('Content-Length', rotatedSize || size)
+ self.res.setHeader('Content-Length', data.length)
if (self.query.name) self.res.setHeader('Content-Disposition',
'inline; filename='+encodeDispositionFilename(self.query.name))
self.res.setHeader('Cache-Control', 'public, max-age=315360000')