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

@ -1,13 +1,15 @@
var path = require('path') var path = require('path')
var multicb = require('multicb') var multicb = require('multicb')
var toPath = require('multiblob/util').toPath var toPath = require('multiblob/util').toPath
var createHash = require('multiblob/util').createHash var createHash = require('multiblob/util').createHash
var pull = require('pull-stream') var pull = require('pull-stream')
var toPull = require('stream-to-pull-stream') var toPull = require('stream-to-pull-stream')
var querystring = require('querystring') var querystring = require('querystring')
var fs = require('fs') var fs = require('fs')
module.exports = function (blobs_dir, checkout_dir) { module.exports = function (blobs_dir, checkout_dir) {
var fallback_img_path = path.join(__dirname, '../../node_modules/ssbplug-phoenix/img/default-prof-pic.png')
return { return {
// behavior for the blob: protocol // behavior for the blob: protocol
protocol: function (request) { protocol: function (request) {
@ -15,7 +17,17 @@ module.exports = function (blobs_dir, checkout_dir) {
// simple fetch // simple fetch
var parsed = url_parse(request.url) var parsed = url_parse(request.url)
if (request.method == 'GET' && parsed) { 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)
}
} }
}, },