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