diff options
-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) + }) }) } |