add blob-search popups on blob notfound
This commit is contained in:
parent
d3bb7cf6d7
commit
753b9058ec
42
app/index.js
42
app/index.js
@ -1,17 +1,13 @@
|
||||
var app = require('app')
|
||||
var Menu = require('menu')
|
||||
var shell = require('shell')
|
||||
var BrowserWindow = require('browser-window')
|
||||
var path = require('path')
|
||||
|
||||
var config = require('ssb-config')
|
||||
var setupRpc = require('./lib/muxrpc-ipc')
|
||||
var config = require('ssb-config')
|
||||
var windows = require('./lib/windows')
|
||||
|
||||
// Report crashes to our server.
|
||||
//require('crash-reporter').start();
|
||||
|
||||
var mainWindow
|
||||
|
||||
app.on('ready', function ready () {
|
||||
// setup blobs
|
||||
var blobs_dir = path.join(config.path, 'blobs')
|
||||
@ -24,33 +20,13 @@ app.on('ready', function ready () {
|
||||
require('protocol').registerProtocol('blob', blobs.protocol)
|
||||
|
||||
// open the web app
|
||||
mainWindow = new BrowserWindow({width: 1000, height: 720})
|
||||
// mainWindow.openDevTools()
|
||||
mainWindow.loadUrl('file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'))
|
||||
mainWindow.webContents.on('new-window', onNewWindow)
|
||||
mainWindow.on('closed', function() { mainWindow = null })
|
||||
setupRpc(sbot, mainWindow)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
var mainWindow = windows.open(
|
||||
'file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'),
|
||||
sbot,
|
||||
blobs,
|
||||
{ width: 1000, height: 720 }
|
||||
)
|
||||
mainWindow.openDevTools()
|
||||
|
||||
// setup menu
|
||||
// Menu.setApplicationMenu(Menu.buildFromTemplate([{
|
||||
|
@ -7,24 +7,6 @@ var toPull = require('stream-to-pull-stream')
|
||||
var querystring = require('querystring')
|
||||
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) {
|
||||
return {
|
||||
// behavior for the blob: protocol
|
||||
@ -50,7 +32,7 @@ module.exports = function (blobs_dir, checkout_dir) {
|
||||
fs.stat(toPath(blobs_dir, parsed.hash), done())
|
||||
findCheckoutDst(filename, parsed.hash, done())
|
||||
done(function (err, res) {
|
||||
if (!res[0][1])
|
||||
if (err && err.code == 'ENOENT')
|
||||
return cb({ notFound: true })
|
||||
|
||||
// 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
|
||||
}
|
@ -4,7 +4,7 @@ var pull = require('pull-stream')
|
||||
var pushable = require('pull-pushable')
|
||||
var Api = require('scuttlebot/lib/api')
|
||||
|
||||
module.exports = function (sbot, window) {
|
||||
module.exports = function (window, sbot, params) {
|
||||
// construct api
|
||||
var api = Api(sbot)
|
||||
for (var k in sbot.manifest) {
|
||||
@ -34,4 +34,8 @@ module.exports = function (sbot, window) {
|
||||
if (e.sender == window.webContents)
|
||||
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
53
app/lib/windows.js
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user