From 3c505eee78211f128c02c4d38deed617b660087c Mon Sep 17 00:00:00 2001 From: cel Date: Sat, 10 Jun 2017 21:06:21 -1000 Subject: Serve images separately from blobs in general --- lib/render.js | 2 +- lib/serve.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/render.js b/lib/render.js index c1c7edf..fdff95c 100644 --- a/lib/render.js +++ b/lib/render.js @@ -145,7 +145,7 @@ Render.prototype.markdown = function (text, mentions) { Render.prototype.imageUrl = function (ref) { var m = /^blobstore:(.*)/.exec(ref) if (m) ref = m[1] - return this.opts.img_base + ref + return this.opts.img_base + 'image/' + ref } Render.prototype.getImageAlt = function (id, fallback) { diff --git a/lib/serve.js b/lib/serve.js index 9228335..9db7007 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -298,6 +298,7 @@ Serve.prototype.path = function (url) { case '/contacts': return this.contacts(m[2]) case '/about': return this.about(m[2]) case '/git': return this.git(m[2]) + case '/image': return this.image(m[2]) } return this.respond(404, 'Not found') } @@ -1026,6 +1027,36 @@ Serve.prototype.blob = function (id) { var self = this var blobs = self.app.sbot.blobs if (self.req.headers['if-none-match'] === id) return self.respond(304) + self.app.wantSizeBlob(id, function (err, size) { + if (err) { + if (/^invalid/.test(err.message)) return self.respond(400, err.message) + else return self.respond(500, err.message || err) + } + pull( + blobs.get(id), + pull.map(Buffer), + ident(gotType), + self.respondSink() + ) + function gotType(type) { + type = type && mime.lookup(type) + if (type) self.res.setHeader('Content-Type', type) + if (typeof size === 'number') self.res.setHeader('Content-Length', 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') + self.res.setHeader('etag', id) + self.res.writeHead(200) + } + }) +} + +Serve.prototype.image = function (path) { + var id = path.substr(1) + var etag = 'image-' + id + var self = this + var blobs = self.app.sbot.blobs + if (self.req.headers['if-none-match'] === etag) return self.respond(304) self.app.wantSizeBlob(id, function (err, size) { if (err) { if (/^invalid/.test(err.message)) return self.respond(400, err.message) @@ -1068,7 +1099,7 @@ Serve.prototype.blob = function (id) { if (self.query.name) self.res.setHeader('Content-Disposition', 'inline; filename='+encodeDispositionFilename(self.query.name)) self.res.setHeader('Cache-Control', 'public, max-age=315360000') - self.res.setHeader('etag', id) + self.res.setHeader('etag', etag) self.res.writeHead(200) }) }) -- cgit v1.2.3