From b23225022186831640bd0f6aea39e4aa8a06c524 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Mon, 10 Aug 2015 13:09:53 -0500 Subject: [PATCH] remove the custom blob protocol --- app/index.js | 1 - app/lib/blobs.js | 45 ++++++++++++++++++++------------------------- app/lib/windows.js | 2 +- package.json | 2 +- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/index.js b/app/index.js index dd322a6..d49fcdd 100644 --- a/app/index.js +++ b/app/index.js @@ -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) diff --git a/app/lib/blobs.js b/app/lib/blobs.js index 8d41fba..f4381e9 100644 --- a/app/lib/blobs.js +++ b/app/lib/blobs.js @@ -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) diff --git a/app/lib/windows.js b/app/lib/windows.js index deee9db..749267d 100644 --- a/app/lib/windows.js +++ b/app/lib/windows.js @@ -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) { diff --git a/package.json b/package.json index e3e9413..33cbb99 100644 --- a/package.json +++ b/package.json @@ -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",