Files: c4c384106bcf50acf4c0fcb3f7b842ad68f25bcb / app / page / blob.js
2016 bytesRaw
1 | const nest = require('depnest') |
2 | const pull = require('pull-stream') |
3 | const { h, Value, when } = require('mutant') |
4 | |
5 | exports.gives = nest('app.page.blob') |
6 | |
7 | exports.needs = nest({ |
8 | 'blob.sync.url': 'first', |
9 | 'sbot.pull.stream': 'first' |
10 | }) |
11 | |
12 | exports.create = (api) => { |
13 | return nest('app.page.blob', blobPage) |
14 | |
15 | function blobPage (location) { |
16 | const { blob } = location |
17 | const title = blob.slice(0, 9) + '...' |
18 | const src = api.blob.sync.url(blob) |
19 | |
20 | const content = Value() |
21 | isImage(blob, (err, isImage) => { |
22 | if (err) return console.log(err) |
23 | |
24 | if (isImage) content.set(h('img', { src })) |
25 | else content.set(h('iframe', { src, sandbox: '' })) |
26 | }) |
27 | |
28 | const page = h('Blob', { title }, [ |
29 | content, |
30 | h('a', { href: api.blob.sync.url(blob), target: '_blank' }, 'Open in browser') |
31 | ]) |
32 | |
33 | return page |
34 | } |
35 | |
36 | // helpers |
37 | |
38 | function isImage (blob, cb) { |
39 | pull( |
40 | api.sbot.pull.stream(server => server.blobs.get(blob)), |
41 | pull.take(1), |
42 | pull.collect((err, data) => { |
43 | if (err) throw err |
44 | |
45 | // dig into the headers and get the 'magic numbers' |
46 | // mix: I copied this from the internet, it ight be terrible! |
47 | const arr = (new Uint8Array(data[0])).subarray(0, 4) |
48 | var header = '' |
49 | for (var i = 0; i < arr.length; i++) header += arr[i].toString(16) |
50 | // console.log(header) |
51 | |
52 | switch (header) { |
53 | case '89504e47': |
54 | // 'image/png' |
55 | return cb(null, true) |
56 | case '47494638': |
57 | // 'image/gif' |
58 | return cb(null, true) |
59 | case 'ffd8ffe0': |
60 | case 'ffd8ffe1': |
61 | case 'ffd8ffe2': |
62 | case 'ffd8ffe3': |
63 | case 'ffd8ffe8': |
64 | // 'image/jpeg' |
65 | return cb(null, true) |
66 | case 'ffd8ffdb': |
67 | // 'image/???' |
68 | return cb(null, true) |
69 | default: |
70 | return cb(null, false) |
71 | // type = 'unknown' // Or you can use the blob.type as fallback |
72 | } |
73 | }) |
74 | ) |
75 | } |
76 | } |
77 |
Built with git-ssb-web