remove the custom blob protocol

This commit is contained in:
Paul Frazee 2015-08-10 13:09:53 -05:00
parent f0b39ad9b4
commit b232250221
4 changed files with 22 additions and 28 deletions

View File

@ -30,7 +30,6 @@ app.on('ready', function () {
// setup blob and file serving
var blobs = require('./lib/blobs')(sbot, { blobs_dir: path.join(config.path, 'blobs'), checkout_dir: app.getPath('userDesktop') })
require('protocol').registerProtocol('pwblob', blobs.protocol)
http.createServer(blobs.server({ serveFiles: false })).listen(7777)
http.createServer(blobs.server({ serveFiles: true })).listen(7778)

View File

@ -12,27 +12,6 @@ module.exports = function (sbot, config) {
var nowaitOpts = { nowait: true }, id = function(){}
return {
// behavior for the blob: protocol
protocol: function (request) {
var protocol = require('protocol') // have to require here, doing so before app:ready causes errors
// simple fetch
var parsed = url_parse(request.url)
if (request.method == 'GET' && parsed) {
var filepath = toPath(config.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
sbot.blobs.want(parsed.hash, nowaitOpts, id)
if (parsed.qs.fallback == 'img')
return new protocol.RequestFileJob(fallback_img_path)
return new protocol.RequestErrorJob(-6)
}
}
},
// copy file from blobs into given dir with nice name
checkout: function (url, cb) {
var parsed = url_parse(url)
@ -102,15 +81,24 @@ module.exports = function (sbot, config) {
}
// serve blob
var hash = req.url.slice(-51) // hash ids are 51 chars long
sbot.blobs.has(hash, function(err, has) {
var parsed = url_parse(req.url)
sbot.blobs.has(parsed.hash, function(err, has) {
if (!has) {
sbot.blobs.want(parsed.hash, nowaitOpts, id)
if (parsed.qs.fallback == 'img') {
return fs.createReadStream(fallback_img_path)
.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),
sbot.blobs.get(parsed.hash),
toPull(res)
)
})
@ -159,7 +147,14 @@ module.exports = function (sbot, config) {
}
// blob url parser
var re = /^pwblob:&([a-z0-9\+\/=]+\.(?:sha256|blake2s))\??(.*)$/i
// var re = /^pwblob:&([a-z0-9\+\/=]+\.(?:sha256|blake2s))\??(.*)$/i
// var url_parse =
// module.exports.url_parse = function (str) {
// var parts = re.exec(str)
// if (parts)
// return { hash: parts[1], qs: querystring.parse(parts[2]) }
// }
var re = /^(?:http:\/\/localhost:7777)?\/&([a-z0-9\+\/=]+\.(?:sha256|blake2s))\??(.*)$/i
var url_parse =
module.exports.url_parse = function (str) {
var parts = re.exec(str)

View File

@ -34,7 +34,7 @@ module.exports.open = function (url, sbot, blobs, opts, params) {
win.webContents.on('new-window', function (e, url) {
e.preventDefault() // hell naw
if (url.indexOf('blob:') === 0) {
if (url.indexOf('http://localhost:7777/') === 0) {
// open the file
blobs.checkout(url, function (err, filepath) {
if (err) {

View File

@ -30,7 +30,7 @@
"muxrpc": "^5.0.1",
"pull-pushable": "^1.1.4",
"pull-stream": "^2.27.0",
"scuttlebot": "^6.0.0",
"scuttlebot": "^6.1.5",
"ssb-config": "^1.0.3",
"ssb-keys": "^4.0.3",
"ssb-patchwork-api": "~0.0.1",