Browse Source

add blob-search popups on blob notfound

Paul Frazee 5 years ago
parent
commit
753b9058ec
4 changed files with 86 additions and 53 deletions
  1. 9
    33
      app/index.js
  2. 19
    19
      app/lib/blobs.js
  3. 5
    1
      app/lib/muxrpc-ipc.js
  4. 53
    0
      app/lib/windows.js

+ 9
- 33
app/index.js View File

@@ -1,17 +1,13 @@
1 1
 var app = require('app')
2 2
 var Menu = require('menu')
3
-var shell = require('shell')
4
-var BrowserWindow = require('browser-window')
5 3
 var path = require('path')
6 4
 
7
-var config = require('ssb-config') 
8
-var setupRpc = require('./lib/muxrpc-ipc')
5
+var config = require('ssb-config')
6
+var windows = require('./lib/windows')
9 7
 
10 8
 // Report crashes to our server.
11 9
 //require('crash-reporter').start();
12 10
 
13
-var mainWindow
14
-
15 11
 app.on('ready', function ready () {
16 12
   // setup blobs
17 13
   var blobs_dir = path.join(config.path, 'blobs')
@@ -24,33 +20,13 @@ app.on('ready', function ready () {
24 20
     require('protocol').registerProtocol('blob', blobs.protocol)
25 21
 
26 22
     // open the web app
27
-    mainWindow = new BrowserWindow({width: 1000, height: 720})
28
-    // mainWindow.openDevTools()
29
-    mainWindow.loadUrl('file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'))
30
-    mainWindow.webContents.on('new-window', onNewWindow)
31
-    mainWindow.on('closed', function() { mainWindow = null })
32
-    setupRpc(sbot, mainWindow)
33
-
34
-    function onNewWindow (e, url) {
35
-      e.preventDefault() // hell naw
36
-      if (url.indexOf('blob:') === 0) {
37
-        // open the file
38
-        blobs.checkout(url, function (err, path) {
39
-          if (err) {
40
-            if (err.badUrl)
41
-              alert('Error: Not a valid file reference')
42
-            else if (err.notFound)
43
-              alert('Error: This file has not yet been synced. Please try again soon.') // :TODO: show 'search' window
44
-            else
45
-              console.log(err) // :TODO: something nicer
46
-          } else
47
-            shell.openItem(path)
48
-        })
49
-      } else {
50
-        // open in the browser
51
-        shell.openExternal(url)
52
-      }
53
-    }
23
+    var mainWindow = windows.open(
24
+      'file://' + path.join(__dirname, '../node_modules/ssbplug-phoenix/home.html'),
25
+      sbot,
26
+      blobs,
27
+      { width: 1000, height: 720 }
28
+    )
29
+    mainWindow.openDevTools()
54 30
 
55 31
     // setup menu
56 32
     // Menu.setApplicationMenu(Menu.buildFromTemplate([{

+ 19
- 19
app/lib/blobs.js View File

@@ -7,24 +7,6 @@ var toPull = require('stream-to-pull-stream')
7 7
 var querystring = require('querystring')
8 8
 var fs = require('fs')
9 9
 
10
-// blob url parser
11
-var re = /^blob:([a-z0-9\+\/=]+\.blake2s)\??(.*)$/i
12
-var url_parse =
13
-module.exports.url_parse = function (str) {
14
-  var parts = re.exec(str)
15
-  if (parts)
16
-    return { hash: parts[1], qs: querystring.parse(parts[2]) }
17
-}
18
-
19
-// blob url builder
20
-var url_stringify =
21
-module.exports.url_stringify = function (hash, qs) {
22
-  var url = 'blob:'+hash
23
-  if (qs && typeof qs == 'object')
24
-    url += '?' + querystring.stringify(qs)
25
-  return url
26
-}
27
-
28 10
 module.exports = function (blobs_dir, checkout_dir) {
29 11
   return {
30 12
     // behavior for the blob: protocol
@@ -50,7 +32,7 @@ module.exports = function (blobs_dir, checkout_dir) {
50 32
       fs.stat(toPath(blobs_dir, parsed.hash), done())
51 33
       findCheckoutDst(filename, parsed.hash, done())
52 34
       done(function (err, res) {
53
-        if (!res[0][1])
35
+        if (err && err.code == 'ENOENT')
54 36
           return cb({ notFound: true })
55 37
 
56 38
         // do we need to copy?
@@ -113,4 +95,22 @@ module.exports = function (blobs_dir, checkout_dir) {
113 95
       })
114 96
     }
115 97
   }
98
+}
99
+
100
+// blob url parser
101
+var re = /^blob:([a-z0-9\+\/=]+\.blake2s)\??(.*)$/i
102
+var url_parse =
103
+module.exports.url_parse = function (str) {
104
+  var parts = re.exec(str)
105
+  if (parts)
106
+    return { hash: parts[1], qs: querystring.parse(parts[2]) }
107
+}
108
+
109
+// blob url builder
110
+var url_stringify =
111
+module.exports.url_stringify = function (hash, qs) {
112
+  var url = 'blob:'+hash
113
+  if (qs && typeof qs == 'object')
114
+    url += '?' + querystring.stringify(qs)
115
+  return url
116 116
 }

+ 5
- 1
app/lib/muxrpc-ipc.js View File

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

+ 53
- 0
app/lib/windows.js View File

@@ -0,0 +1,53 @@
1
+var BrowserWindow = require('browser-window')
2
+var path = require('path')
3
+var shell = require('shell')
4
+var setupRpc = require('./muxrpc-ipc')
5
+var blobslib = require('./blobs')
6
+
7
+var windows = []
8
+
9
+var open =
10
+module.exports.open = function (url, sbot, blobs, opts, params) {
11
+  var win = new BrowserWindow(opts)
12
+  win.loadUrl(url)
13
+  setupRpc(win, sbot, params)
14
+  
15
+  win.on('closed', function() {
16
+    var i = windows.indexOf(win)
17
+    windows.splice(i, 1)
18
+    win = null
19
+  })
20
+  
21
+  win.webContents.on('new-window', function (e, url) {
22
+    e.preventDefault() // hell naw
23
+    if (url.indexOf('blob:') === 0) {
24
+      // open the file
25
+      blobs.checkout(url, function (err, filepath) {
26
+        if (err) {
27
+          if (err.badUrl)
28
+            alert('Error: Not a valid file reference')
29
+          else if (err.notFound) {
30
+            // register want
31
+            var hash = blobslib.url_parse(url).hash
32
+            sbot.blobs.want(hash, {nowait: true}, function(){})
33
+            // open search interface
34
+            open(
35
+              'file://' + path.join(__dirname, '../../node_modules/ssbplug-phoenix/blob-search.html'),
36
+              sbot,
37
+              blobs,
38
+              { width: 600, height: 650 },
39
+              { url: url, hash: hash }
40
+            )
41
+          } else
42
+            console.log(err) // :TODO: something nicer
43
+        } else
44
+          shell.openItem(filepath)
45
+      })
46
+    } else {
47
+      // open in the browser
48
+      shell.openExternal(url)
49
+    }
50
+  })
51
+
52
+  return win
53
+}

Loading…
Cancel
Save