diff options
author | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2017-05-22 14:40:22 -1000 |
---|---|---|
committer | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2017-05-23 23:23:06 -1000 |
commit | a589c54f4d78d84c71e340981ce987b9949c35bd (patch) | |
tree | 810c0ce6cd21cc48cb83b741888c6d413004af79 | |
parent | 2febc8b4825c6d24f5e49340f57a6e1643cabc46 (diff) | |
download | patchfoo-a589c54f4d78d84c71e340981ce987b9949c35bd.tar.gz patchfoo-a589c54f4d78d84c71e340981ce987b9949c35bd.zip |
Serve blob size
-rw-r--r-- | lib/serve.js | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/lib/serve.js b/lib/serve.js index a5c2e48..8d3eeba 100644 --- a/lib/serve.js +++ b/lib/serve.js @@ -209,7 +209,8 @@ Serve.prototype.respond = function (status, message) { Serve.prototype.respondSink = function (status, headers, cb) { var self = this - if (status && headers) self.res.writeHead(status, headers) + if (status || headers) + self.res.writeHead(status, headers || {'Content-Type': 'text/html'}) return toPull(self.res, cb || function (err) { if (err) self.app.error(err) }) @@ -908,26 +909,31 @@ 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) + var done = multicb({pluck: 1, spread: true}) blobs.want(id, function (err, has) { if (err) { if (/^invalid/.test(err.message)) return self.respond(400, err.message) else return self.respond(500, err.message || err) } if (!has) return self.respond(404, 'Not found') + blobs.size(id, done()) pull( blobs.get(id), pull.map(Buffer), - ident(function (type) { - type = type && mime.lookup(type) - if (type) self.res.setHeader('Content-Type', type) - 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) - }), + ident(done().bind(self, null)), self.respondSink() ) + done(function (err, size, type) { + 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 (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) + }) }) } |