Browse Source

remove the custom blob protocol

Paul Frazee 5 years ago
parent
commit
b232250221
4 changed files with 22 additions and 28 deletions
  1. 0
    1
      app/index.js
  2. 20
    25
      app/lib/blobs.js
  3. 1
    1
      app/lib/windows.js
  4. 1
    1
      package.json

+ 0
- 1
app/index.js View File

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

+ 20
- 25
app/lib/blobs.js View File

@@ -12,27 +12,6 @@ module.exports = function (sbot, config) {
12 12
   var nowaitOpts = { nowait: true }, id = function(){}
13 13
 
14 14
   return {
15
-    // behavior for the blob: protocol
16
-    protocol: function (request) {
17
-      var protocol = require('protocol') // have to require here, doing so before app:ready causes errors
18
-      // simple fetch
19
-      var parsed = url_parse(request.url)
20
-      if (request.method == 'GET' && parsed) {
21
-        var filepath = toPath(config.blobs_dir, parsed.hash)
22
-        try {
23
-          // check if the file exists
24
-          fs.statSync(filepath) // :HACK: make async when we figure out how to make a protocol-handler support that
25
-          return new protocol.RequestFileJob(filepath)
26
-        } catch (e) {
27
-          // notfound
28
-          sbot.blobs.want(parsed.hash, nowaitOpts, id)
29
-          if (parsed.qs.fallback == 'img')
30
-            return new protocol.RequestFileJob(fallback_img_path)
31
-          return new protocol.RequestErrorJob(-6)
32
-        }
33
-      }
34
-    },
35
-
36 15
     // copy file from blobs into given dir with nice name
37 16
     checkout: function (url, cb) {
38 17
       var parsed = url_parse(url)
@@ -102,15 +81,24 @@ module.exports = function (sbot, config) {
102 81
         }
103 82
 
104 83
         // serve blob
105
-        var hash = req.url.slice(-51) // hash ids are 51 chars long
106
-        sbot.blobs.has(hash, function(err, has) {
84
+        var parsed = url_parse(req.url)
85
+        sbot.blobs.has(parsed.hash, function(err, has) {
107 86
           if (!has) {
87
+            sbot.blobs.want(parsed.hash, nowaitOpts, id)
88
+            if (parsed.qs.fallback == 'img') {
89
+              return fs.createReadStream(fallback_img_path)
90
+                .on('error', function () {
91
+                  res.writeHead(404)
92
+                  res.end('File not found')
93
+                })
94
+                .pipe(res)
95
+            }
108 96
             res.writeHead(404)
109 97
             res.end('File not found')
110 98
             return
111 99
           }
112 100
           pull(
113
-            sbot.blobs.get(hash),
101
+            sbot.blobs.get(parsed.hash),
114 102
             toPull(res)
115 103
           )
116 104
         })
@@ -159,7 +147,14 @@ module.exports = function (sbot, config) {
159 147
 }
160 148
 
161 149
 // blob url parser
162
-var re = /^pwblob:&([a-z0-9\+\/=]+\.(?:sha256|blake2s))\??(.*)$/i
150
+// var re = /^pwblob:&([a-z0-9\+\/=]+\.(?:sha256|blake2s))\??(.*)$/i
151
+// var url_parse =
152
+// module.exports.url_parse = function (str) {
153
+//   var parts = re.exec(str)
154
+//   if (parts)
155
+//     return { hash: parts[1], qs: querystring.parse(parts[2]) }
156
+// }
157
+var re = /^(?:http:\/\/localhost:7777)?\/&([a-z0-9\+\/=]+\.(?:sha256|blake2s))\??(.*)$/i
163 158
 var url_parse =
164 159
 module.exports.url_parse = function (str) {
165 160
   var parts = re.exec(str)

+ 1
- 1
app/lib/windows.js View File

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

+ 1
- 1
package.json View File

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

Loading…
Cancel
Save