diff options
Diffstat (limited to 'lib/serve.js')
-rw-r--r-- | lib/serve.js | 33 |
1 files changed, 32 insertions, 1 deletions
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) }) }) |