Browse Source

add site-link lookup to the blob http host

Paul Frazee 5 years ago
parent
commit
c3bf0f4296
1 changed files with 50 additions and 35 deletions
  1. 50
    35
      app/lib/blobs.js

+ 50
- 35
app/lib/blobs.js View File

@@ -6,6 +6,7 @@ var pull        = require('pull-stream')
6 6
 var toPull      = require('stream-to-pull-stream')
7 7
 var querystring = require('querystring')
8 8
 var fs          = require('fs')
9
+var URL         = require('url')
9 10
 
10 11
 module.exports = function (sbot, config) {
11 12
   var fallback_img_path = path.join(__dirname, '../../node_modules/ssb-patchwork-ui/img/default-prof-pic.png')
@@ -71,8 +72,14 @@ module.exports = function (sbot, config) {
71 72
           "sandbox allow-scripts"
72 73
         )
73 74
 
74
-        if (req.url.slice(-7) != '.sha256' && opts.serveFiles) {
75
-          // try to serve from local FS if the path is not a supported hash
75
+        // local files
76
+        if (req.url.charAt(1) != '&' && req.url.charAt(1) != '@') {
77
+          if (!opts.serveFiles) {
78
+            res.writeHead(404)
79
+            res.end('File not found')
80
+            return            
81
+          }
82
+
76 83
           return fs.createReadStream(req.url)
77 84
             .on('error', function () {
78 85
               res.writeHead(404)
@@ -81,34 +88,51 @@ module.exports = function (sbot, config) {
81 88
             .pipe(res)
82 89
         }
83 90
 
84
-        // serve blob
85
-        var parsed = url_parse(req.url)
86
-        sbot.blobs.has(parsed.hash, function(err, has) {
87
-          if (!has) {
88
-            sbot.blobs.want(parsed.hash, nowaitOpts, id)
89
-            if (parsed.qs.fallback) {
90
-              var p = (parsed.qs.fallback == 'video') ? fallback_video_path : fallback_img_path
91
-              console.log('falling back', p)
92
-              return fs.createReadStream(p)
93
-                .on('error', function () {
94
-                  res.writeHead(404)
95
-                  res.end('File not found')
96
-                })
97
-                .pipe(res)
91
+        // blobs
92
+        // var parsed = url_parse(req.url)
93
+        var parsed = URL.parse(req.url, true)
94
+        if (req.url.charAt(1) == '&')
95
+          serveblob(parsed.pathname.slice(1), parsed.query.fallback, res)
96
+        else {
97
+          sbot.patchwork.getSiteLink(parsed.pathname.slice(1), function (err, link) {
98
+            if (err) {
99
+              res.writeHead(404)
100
+              res.end('File not found')              
101
+            } else {
102
+              if (link.type)
103
+                res.setHeader('Content-Type', link.type)
104
+              serveblob(link.link, null, res)
98 105
             }
99
-            res.writeHead(404)
100
-            res.end('File not found')
101
-            return
102
-          }
103
-          pull(
104
-            sbot.blobs.get(parsed.hash),
105
-            toPull(res)
106
-          )
107
-        })
106
+          })
107
+        }
108 108
       }
109 109
     }
110 110
   }
111 111
 
112
+  function serveblob (hash, fallback, res) {
113
+    sbot.blobs.has(hash, function(err, has) {
114
+      if (!has) {
115
+        sbot.blobs.want(hash, nowaitOpts, id)
116
+        if (fallback) {
117
+          var p = (fallback == 'video') ? fallback_video_path : fallback_img_path
118
+          return fs.createReadStream(p)
119
+            .on('error', function () {
120
+              res.writeHead(404)
121
+              res.end('File not found')
122
+            })
123
+            .pipe(res)
124
+        }
125
+        res.writeHead(404)
126
+        res.end('File not found')
127
+        return
128
+      }
129
+      pull(
130
+        sbot.blobs.get(hash),
131
+        toPull(res)
132
+      )
133
+    })
134
+  }
135
+
112 136
   // helper to create a filename in checkout_dir that isnt already in use
113 137
   // - cb(err, filepath, nocopy) - if nocopy==true, no need to do the copy operation
114 138
   function findCheckoutDst (filename, hash, cb) {
@@ -162,14 +186,5 @@ var url_parse =
162 186
 module.exports.url_parse = function (str) {
163 187
   var parts = re.exec(str)
164 188
   if (parts)
165
-    return { hash: parts[1], qs: querystring.parse(parts[2]) }
166
-}
167
-
168
-// blob url builder
169
-var url_stringify =
170
-module.exports.url_stringify = function (hash, qs) {
171
-  var url = 'blob:'+hash
172
-  if (qs && typeof qs == 'object')
173
-    url += '?' + querystring.stringify(qs)
174
-  return url
189
+    return { path: parts[1], qs: querystring.parse(parts[2]) }
175 190
 }

Loading…
Cancel
Save