|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const log = (line) => { |
| 4 | + const output = document.getElementById('output') |
| 5 | + let message |
| 6 | + |
| 7 | + if (line.message) { |
| 8 | + message = `Error: ${line.message.toString()}` |
| 9 | + } else { |
| 10 | + message = line |
| 11 | + } |
| 12 | + |
| 13 | + if (message) { |
| 14 | + const node = document.createTextNode(`${message}\r\n`) |
| 15 | + output.appendChild(node) |
| 16 | + |
| 17 | + output.scrollTop = output.offsetHeight |
| 18 | + |
| 19 | + return node |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const dragDrop = (ipfs) => { |
| 24 | + const container = document.querySelector('#container') |
| 25 | + |
| 26 | + container.ondragover = (event) => { |
| 27 | + event.preventDefault() |
| 28 | + } |
| 29 | + |
| 30 | + container.ondrop = (event) => { |
| 31 | + event.preventDefault() |
| 32 | + |
| 33 | + Array.prototype.slice.call(event.dataTransfer.items) |
| 34 | + .filter(item => item.kind === 'file') |
| 35 | + .map(item => item.getAsFile()) |
| 36 | + .forEach(file => { |
| 37 | + const progress = log(`IPFS: Adding ${file.name} 0%`) |
| 38 | + |
| 39 | + const reader = new window.FileReader() |
| 40 | + reader.onload = (event) => { |
| 41 | + ipfs.files.add({ |
| 42 | + path: file.name, |
| 43 | + content: ipfs.types.Buffer.from(event.target.result) |
| 44 | + }, { |
| 45 | + progress: (addedBytes) => { |
| 46 | + progress.textContent = `IPFS: Adding ${file.name} ${parseInt((addedBytes / file.size) * 100)}%\r\n` |
| 47 | + } |
| 48 | + }, (error, added) => { |
| 49 | + if (error) { |
| 50 | + return log(error) |
| 51 | + } |
| 52 | + |
| 53 | + const hash = added[0].hash |
| 54 | + |
| 55 | + log(`IPFS: Added ${hash}`) |
| 56 | + |
| 57 | + document.querySelector('#hash').value = hash |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + reader.readAsArrayBuffer(file) |
| 62 | + }) |
| 63 | + |
| 64 | + if (event.dataTransfer.items && event.dataTransfer.items.clear) { |
| 65 | + event.dataTransfer.items.clear() |
| 66 | + } |
| 67 | + |
| 68 | + if (event.dataTransfer.clearData) { |
| 69 | + event.dataTransfer.clearData() |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +module.exports.statusMessages = (stream) => { |
| 75 | + let time = 0 |
| 76 | + const timeouts = [ |
| 77 | + 'Stream: Still loading data from IPFS...', |
| 78 | + 'Stream: This can take a while depending on content availability', |
| 79 | + 'Stream: Hopefully not long now', |
| 80 | + 'Stream: *Whistles absentmindedly*', |
| 81 | + 'Stream: *Taps foot*', |
| 82 | + 'Stream: *Looks at watch*', |
| 83 | + 'Stream: *Stares at floor*', |
| 84 | + 'Stream: *Checks phone*', |
| 85 | + 'Stream: *Stares at ceiling*', |
| 86 | + 'Stream: Got anything nice planned for the weekend?' |
| 87 | + ].map(message => { |
| 88 | + time += 5000 |
| 89 | + |
| 90 | + return setTimeout(() => { |
| 91 | + log(message) |
| 92 | + }, time) |
| 93 | + }) |
| 94 | + |
| 95 | + stream.once('data', () => { |
| 96 | + log('Stream: Started receiving data') |
| 97 | + timeouts.forEach(clearTimeout) |
| 98 | + }) |
| 99 | + stream.once('error', () => { |
| 100 | + timeouts.forEach(clearTimeout) |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +const createVideoElement = () => { |
| 105 | + const videoElement = document.getElementById('video') |
| 106 | + videoElement.addEventListener('loadedmetadata', () => { |
| 107 | + videoElement.play() |
| 108 | + .catch(log) |
| 109 | + }) |
| 110 | + |
| 111 | + const events = [ |
| 112 | + 'playing', |
| 113 | + 'waiting', |
| 114 | + 'seeking', |
| 115 | + 'seeked', |
| 116 | + 'ended', |
| 117 | + 'loadedmetadata', |
| 118 | + 'loadeddata', |
| 119 | + 'canplay', |
| 120 | + 'canplaythrough', |
| 121 | + 'durationchange', |
| 122 | + 'play', |
| 123 | + 'pause', |
| 124 | + 'suspend', |
| 125 | + 'emptied', |
| 126 | + 'stalled', |
| 127 | + 'error', |
| 128 | + 'abort' |
| 129 | + ] |
| 130 | + events.forEach(event => { |
| 131 | + videoElement.addEventListener(event, () => { |
| 132 | + log(`Video: ${event}`) |
| 133 | + }) |
| 134 | + }) |
| 135 | + |
| 136 | + videoElement.addEventListener('error', () => { |
| 137 | + log(videoElement.error) |
| 138 | + }) |
| 139 | + |
| 140 | + return videoElement |
| 141 | +} |
| 142 | + |
| 143 | +module.exports.log = log |
| 144 | +module.exports.dragDrop = dragDrop |
| 145 | +module.exports.createVideoElement = createVideoElement |
0 commit comments