add blob-search popups on blob notfound

This commit is contained in:
Paul Frazee 2015-06-26 13:30:34 -05:00
parent d3bb7cf6d7
commit 753b9058ec
4 changed files with 86 additions and 53 deletions

View File

@ -1,17 +1,13 @@
var app = require('app') var app = require('app')
var Menu = require('menu') var Menu = require('menu')
var shell = require('shell')
var BrowserWindow = require('browser-window')
var path = require('path') var path = require('path')
var config = require('ssb-config') var config = require('ssb-config')
var setupRpc = require('./lib/muxrpc-ipc') var windows = require('./lib/windows')
// Report crashes to our server. // Report crashes to our server.
//require('crash-reporter').start(); //require('crash-reporter').start();
var mainWindow
app.on('ready', function ready () { app.on('ready', function ready () {
// setup blobs // setup blobs
var blobs_dir = path.join(config.path, 'blobs') var blobs_dir = path.join(config.path, 'blobs')
@ -24,33 +20,13 @@ app.on('ready', function ready () {
require('protocol').registerProtocol('blob', blobs.protocol) require('protocol').registerProtocol('blob', blobs.protocol)
// open the web app // open the web app
mainWindow = new BrowserWindow({width: 1000, height: 720}) var mainWindow = windows.open(
// mainWindow.openDevTools() 'file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'),
mainWindow.loadUrl('file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html')) sbot,
mainWindow.webContents.on('new-window', onNewWindow) blobs,
mainWindow.on('closed', function() { mainWindow = null }) { width: 1000, height: 720 }
setupRpc(sbot, mainWindow) )
mainWindow.openDevTools()
function onNewWindow (e, url) {
e.preventDefault() // hell naw
if (url.indexOf('blob:') === 0) {
// open the file
blobs.checkout(url, function (err, path) {
if (err) {
if (err.badUrl)
alert('Error: Not a valid file reference')
else if (err.notFound)
alert('Error: This file has not yet been synced. Please try again soon.') // :TODO: show 'search' window
else
console.log(err) // :TODO: something nicer
} else
shell.openItem(path)
})
} else {
// open in the browser
shell.openExternal(url)
}
}
// setup menu // setup menu
// Menu.setApplicationMenu(Menu.buildFromTemplate([{ // Menu.setApplicationMenu(Menu.buildFromTemplate([{

View File

@ -7,24 +7,6 @@ var toPull = require('stream-to-pull-stream')
var querystring = require('querystring') var querystring = require('querystring')
var fs = require('fs') var fs = require('fs')
// blob url parser
var re = /^blob:([a-z0-9\+\/=]+\.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]) }
}
// 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
}
module.exports = function (blobs_dir, checkout_dir) { module.exports = function (blobs_dir, checkout_dir) {
return { return {
// behavior for the blob: protocol // behavior for the blob: protocol
@ -50,7 +32,7 @@ module.exports = function (blobs_dir, checkout_dir) {
fs.stat(toPath(blobs_dir, parsed.hash), done()) fs.stat(toPath(blobs_dir, parsed.hash), done())
findCheckoutDst(filename, parsed.hash, done()) findCheckoutDst(filename, parsed.hash, done())
done(function (err, res) { done(function (err, res) {
if (!res[0][1]) if (err && err.code == 'ENOENT')
return cb({ notFound: true }) return cb({ notFound: true })
// do we need to copy? // do we need to copy?
@ -113,4 +95,22 @@ module.exports = function (blobs_dir, checkout_dir) {
}) })
} }
} }
}
// blob url parser
var re = /^blob:([a-z0-9\+\/=]+\.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]) }
}
// 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
} }

View File

@ -4,7 +4,7 @@ var pull = require('pull-stream')
var pushable = require('pull-pushable') var pushable = require('pull-pushable')
var Api = require('scuttlebot/lib/api') var Api = require('scuttlebot/lib/api')
module.exports = function (sbot, window) { module.exports = function (window, sbot, params) {
// construct api // construct api
var api = Api(sbot) var api = Api(sbot)
for (var k in sbot.manifest) { for (var k in sbot.manifest) {
@ -34,4 +34,8 @@ module.exports = function (sbot, window) {
if (e.sender == window.webContents) if (e.sender == window.webContents)
e.returnValue = sbot.config e.returnValue = sbot.config
}); });
ipc.on('fetch-params', function(e) {
if (e.sender == window.webContents)
e.returnValue = params
});
} }

53
app/lib/windows.js Normal file
View File

@ -0,0 +1,53 @@
var BrowserWindow = require('browser-window')
var path = require('path')
var shell = require('shell')
var setupRpc = require('./muxrpc-ipc')
var blobslib = require('./blobs')
var windows = []
var open =
module.exports.open = function (url, sbot, blobs, opts, params) {
var win = new BrowserWindow(opts)
win.loadUrl(url)
setupRpc(win, sbot, params)
win.on('closed', function() {
var i = windows.indexOf(win)
windows.splice(i, 1)
win = null
})
win.webContents.on('new-window', function (e, url) {
e.preventDefault() // hell naw
if (url.indexOf('blob:') === 0) {
// open the file
blobs.checkout(url, function (err, filepath) {
if (err) {
if (err.badUrl)
alert('Error: Not a valid file reference')
else if (err.notFound) {
// register want
var hash = blobslib.url_parse(url).hash
sbot.blobs.want(hash, {nowait: true}, function(){})
// open search interface
open(
'file://' + path.join(__dirname, '../../node_modules/ssbplug-phoenix/blob-search.html'),
sbot,
blobs,
{ width: 600, height: 650 },
{ url: url, hash: hash }
)
} else
console.log(err) // :TODO: something nicer
} else
shell.openItem(filepath)
})
} else {
// open in the browser
shell.openExternal(url)
}
})
return win
}