add site-link lookup to the blob http host

This commit is contained in:
Paul Frazee 2015-08-22 18:24:44 -05:00
parent 025e7c5d39
commit c3bf0f4296
1 changed files with 50 additions and 35 deletions

View File

@ -6,6 +6,7 @@ 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')
var URL = require('url')
module.exports = function (sbot, config) { module.exports = function (sbot, config) {
var fallback_img_path = path.join(__dirname, '../../node_modules/ssb-patchwork-ui/img/default-prof-pic.png') var fallback_img_path = path.join(__dirname, '../../node_modules/ssb-patchwork-ui/img/default-prof-pic.png')
@ -71,8 +72,14 @@ module.exports = function (sbot, config) {
"sandbox allow-scripts" "sandbox allow-scripts"
) )
if (req.url.slice(-7) != '.sha256' && opts.serveFiles) { // local files
// try to serve from local FS if the path is not a supported hash if (req.url.charAt(1) != '&' && req.url.charAt(1) != '@') {
if (!opts.serveFiles) {
res.writeHead(404)
res.end('File not found')
return
}
return fs.createReadStream(req.url) return fs.createReadStream(req.url)
.on('error', function () { .on('error', function () {
res.writeHead(404) res.writeHead(404)
@ -81,34 +88,51 @@ module.exports = function (sbot, config) {
.pipe(res) .pipe(res)
} }
// serve blob // blobs
var parsed = url_parse(req.url) // var parsed = url_parse(req.url)
sbot.blobs.has(parsed.hash, function(err, has) { var parsed = URL.parse(req.url, true)
if (!has) { if (req.url.charAt(1) == '&')
sbot.blobs.want(parsed.hash, nowaitOpts, id) serveblob(parsed.pathname.slice(1), parsed.query.fallback, res)
if (parsed.qs.fallback) { else {
var p = (parsed.qs.fallback == 'video') ? fallback_video_path : fallback_img_path sbot.patchwork.getSiteLink(parsed.pathname.slice(1), function (err, link) {
console.log('falling back', p) if (err) {
return fs.createReadStream(p) res.writeHead(404)
.on('error', function () { res.end('File not found')
res.writeHead(404) } else {
res.end('File not found') if (link.type)
}) res.setHeader('Content-Type', link.type)
.pipe(res) serveblob(link.link, null, res)
} }
res.writeHead(404) })
res.end('File not found') }
return
}
pull(
sbot.blobs.get(parsed.hash),
toPull(res)
)
})
} }
} }
} }
function serveblob (hash, fallback, res) {
sbot.blobs.has(hash, function(err, has) {
if (!has) {
sbot.blobs.want(hash, nowaitOpts, id)
if (fallback) {
var p = (fallback == 'video') ? fallback_video_path : fallback_img_path
return fs.createReadStream(p)
.on('error', function () {
res.writeHead(404)
res.end('File not found')
})
.pipe(res)
}
res.writeHead(404)
res.end('File not found')
return
}
pull(
sbot.blobs.get(hash),
toPull(res)
)
})
}
// helper to create a filename in checkout_dir that isnt already in use // helper to create a filename in checkout_dir that isnt already in use
// - cb(err, filepath, nocopy) - if nocopy==true, no need to do the copy operation // - cb(err, filepath, nocopy) - if nocopy==true, no need to do the copy operation
function findCheckoutDst (filename, hash, cb) { function findCheckoutDst (filename, hash, cb) {
@ -162,14 +186,5 @@ var url_parse =
module.exports.url_parse = function (str) { module.exports.url_parse = function (str) {
var parts = re.exec(str) var parts = re.exec(str)
if (parts) if (parts)
return { hash: parts[1], qs: querystring.parse(parts[2]) } return { path: parts[1], qs: querystring.parse(parts[2]) }
}
// blob url builder
var url_stringify =
module.exports.url_stringify = function (hash, qs) {
var url = 'blob:'+hash
if (qs && typeof qs == 'object')
url += '?' + querystring.stringify(qs)
return url
} }