aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-03-05 16:26:33 -0500
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-03-05 16:26:33 -0500
commit0a5bea5a927ac8a3998f5b1659e9fa8d3c8f2a6c (patch)
tree1f43d944ba3226204d5c390cfa5fa7744a0891ce
parent1168e132e90f0653700134b7a7f0b81edfaead8e (diff)
downloadpatchfoo-0a5bea5a927ac8a3998f5b1659e9fa8d3c8f2a6c.tar.gz
patchfoo-0a5bea5a927ac8a3998f5b1659e9fa8d3c8f2a6c.zip
Set content-type when serving blobs
-rw-r--r--lib/serve.js20
-rw-r--r--package.json2
2 files changed, 15 insertions, 7 deletions
diff --git a/lib/serve.js b/lib/serve.js
index 7c4350b..7cda95d 100644
--- a/lib/serve.js
+++ b/lib/serve.js
@@ -15,6 +15,8 @@ var ssbMentions = require('ssb-mentions')
var multicb = require('multicb')
var pkg = require('../package')
var Busboy = require('busboy')
+var mime = require('mime-types')
+var ident = require('pull-identify-filetype')
module.exports = Serve
@@ -128,7 +130,7 @@ Serve.prototype.respond = function (status, message) {
Serve.prototype.respondSink = function (status, headers, cb) {
var self = this
- self.res.writeHead(status, headers)
+ if (status && headers) self.res.writeHead(status, headers)
return toPull(self.res, cb || function (err) {
if (err) self.error(err)
})
@@ -486,15 +488,19 @@ Serve.prototype.blob = function (id) {
else return self.respond(500, err.message || err)
}
if (!has) return self.respond(404, 'Not found')
- if (self.query.name) self.res.setHeader('Content-Disposition',
- 'inline; filename='+encodeURIComponent(self.query.name))
pull(
blobs.get(id),
pull.map(Buffer),
- self.respondSink(200, {
- 'Cache-Control': 'public, max-age=315360000',
- 'etag': id
- })
+ 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='+encodeURIComponent(self.query.name))
+ self.res.setHeader('Cache-Control', 'public, max-age=315360000')
+ self.res.setHeader('etag', id)
+ self.res.writeHead(200)
+ }),
+ self.respondSink()
)
})
}
diff --git a/package.json b/package.json
index 4d47f7f..9bb2c10 100644
--- a/package.json
+++ b/package.json
@@ -10,9 +10,11 @@
"human-time": "^0.0.1",
"hyperscript": "^2.0.2",
"lrucache": "^1.0.2",
+ "mime-types": "^2.1.12",
"multicb": "^1.2.1",
"pull-cat": "^1.1.11",
"pull-hash": "^1.0.0",
+ "pull-identify-filetype": "^1.1.0",
"pull-paginate": "^1.0.0",
"pull-paramap": "^1.2.1",
"pull-stream": "^3.5.0",