From b4070f9b60e49b2c378bd85b4d0834101eae2632 Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Tue, 6 Jun 2017 11:32:38 -0700 Subject: Use JPEG EXIF data to rotate image blobs. --- lib/serve.js | 13 ++++++++++++- package.json | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/serve.js b/lib/serve.js index 9d5eb82..81ab0b7 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -20,6 +20,7 @@ var ident = require('pull-identify-filetype') var htime = require('human-time') var ph = require('pull-hyperscript') var emojis = require('emoji-named-characters') +var jpeg = require('jpeg-autorotate') module.exports = Serve @@ -954,10 +955,20 @@ Serve.prototype.blob = function (id) { else return self.respond(500, err.message || err) } if (!has) return self.respond(404, 'Not found') + blobs.size(id, done()) + var rotatedSize = 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() ) @@ -965,7 +976,7 @@ Serve.prototype.blob = function (id) { if (err) console.trace(err) type = type && mime.lookup(type) if (type) self.res.setHeader('Content-Type', type) - if (typeof size === 'number') self.res.setHeader('Content-Length', size) + if (typeof size === 'number') self.res.setHeader('Content-Length', rotatedSize || size) 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 465e93f..f398f61 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "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", -- cgit v1.2.3