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