diff options
author | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2017-06-10 21:06:21 -1000 |
---|---|---|
committer | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2017-06-10 21:24:45 -1000 |
commit | 3c505eee78211f128c02c4d38deed617b660087c (patch) | |
tree | 947d41ba17772e2d5e1abf9f55f4e0e650229335 | |
parent | cc9024cba630c57aff2e16dbbd04844e4ec61781 (diff) | |
download | patchfoo-3c505eee78211f128c02c4d38deed617b660087c.tar.gz patchfoo-3c505eee78211f128c02c4d38deed617b660087c.zip |
Serve images separately from blobs in general
-rw-r--r-- | lib/render.js | 2 | ||||
-rw-r--r-- | 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') } @@ -1031,6 +1032,36 @@ Serve.prototype.blob = function (id) { 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) + else return self.respond(500, err.message || err) + } var done = multicb({pluck: 1, spread: true}) var heresTheData = done() @@ -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) }) }) |