Browse Source

blob: protocol now supports ?fallback=img on notfound

Paul Frazee 5 years ago
parent
commit
f988d7fbe8
1 changed files with 20 additions and 8 deletions
  1. 20
    8
      app/lib/blobs.js

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

@@ -1,13 +1,15 @@
1
-var path = require('path')
2
-var multicb = require('multicb')
3
-var toPath = require('multiblob/util').toPath
4
-var createHash = require('multiblob/util').createHash
5
-var pull = require('pull-stream')
6
-var toPull = require('stream-to-pull-stream')
1
+var path        = require('path')
2
+var multicb     = require('multicb')
3
+var toPath      = require('multiblob/util').toPath
4
+var createHash  = require('multiblob/util').createHash
5
+var pull        = require('pull-stream')
6
+var toPull      = require('stream-to-pull-stream')
7 7
 var querystring = require('querystring')
8
-var fs = require('fs')
8
+var fs          = require('fs')
9 9
 
10 10
 module.exports = function (blobs_dir, checkout_dir) {
11
+  var fallback_img_path = path.join(__dirname, '../../node_modules/ssbplug-phoenix/img/default-prof-pic.png')
12
+
11 13
   return {
12 14
     // behavior for the blob: protocol
13 15
     protocol: function (request) {
@@ -15,7 +17,17 @@ module.exports = function (blobs_dir, checkout_dir) {
15 17
       // simple fetch
16 18
       var parsed = url_parse(request.url)
17 19
       if (request.method == 'GET' && parsed) {
18
-        return new protocol.RequestFileJob(toPath(blobs_dir, parsed.hash))
20
+        var filepath = toPath(blobs_dir, parsed.hash)
21
+        try {
22
+          // check if the file exists
23
+          fs.statSync(filepath) // :HACK: make async when we figure out how to make a protocol-handler support that
24
+          return new protocol.RequestFileJob(filepath)
25
+        } catch (e) {
26
+          // notfound
27
+          if (parsed.qs.fallback == 'img')
28
+            return new protocol.RequestFileJob(fallback_img_path)
29
+          return new protocol.RequestErrorJob(-6)
30
+        }
19 31
       }
20 32
     },
21 33
 

Loading…
Cancel
Save