From b6d136ab10136f9d881b239a338a4c0eea2e65f0 Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Sat, 10 Jun 2017 15:04:24 -0700 Subject: fixes --- lib/serve.js | 40 ++++++++++++++++++++++++++-------------- package.json | 3 ++- 2 files changed, 28 insertions(+), 15 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') diff --git a/package.json b/package.json index f398f61..e70fa96 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,14 @@ "busboy": "^0.2.14", "emoji-named-characters": "^1.0.2", "emoji-server": "^1.0.0", + "hashlru": "^2.1.0", "human-time": "^0.0.1", "hyperscript": "^2.0.2", - "hashlru": "^2.1.0", "jpeg-autorotate": "^3.0.0", "mime-types": "^2.1.12", "multicb": "^1.2.1", "pull-cat": "^1.1.11", + "pull-concat": "^1.1.1", "pull-git-packidx-parser": "^1.0.0", "pull-hash": "^1.0.0", "pull-hyperscript": "^0.2.2", -- cgit v1.2.3