blob: protocol now supports ?fallback=img on notfound

This commit is contained in:
Paul Frazee 2015-06-26 13:53:37 -05:00
parent 753b9058ec
commit f988d7fbe8
1 changed files with 20 additions and 8 deletions

View File

@ -8,6 +8,8 @@ var querystring = require('querystring')
var fs = require('fs')
module.exports = function (blobs_dir, checkout_dir) {
var fallback_img_path = path.join(__dirname, '../../node_modules/ssbplug-phoenix/img/default-prof-pic.png')
return {
// behavior for the blob: protocol
protocol: function (request) {
@ -15,7 +17,17 @@ module.exports = function (blobs_dir, checkout_dir) {
// simple fetch
var parsed = url_parse(request.url)
if (request.method == 'GET' && parsed) {
return new protocol.RequestFileJob(toPath(blobs_dir, parsed.hash))
var filepath = toPath(blobs_dir, parsed.hash)
try {
// check if the file exists
fs.statSync(filepath) // :HACK: make async when we figure out how to make a protocol-handler support that
return new protocol.RequestFileJob(filepath)
} catch (e) {
// notfound
if (parsed.qs.fallback == 'img')
return new protocol.RequestFileJob(fallback_img_path)
return new protocol.RequestErrorJob(-6)
}
}
},