-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrender.js
424 lines (363 loc) · 10.4 KB
/
render.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
const dgram = require('dgram')
const os = require('os')
const path = require('path')
const fs = require('fs')
const EventEmitter = require('events')
const clone = require('clone')
const uuidV4 = require('uuid/v4')
const progress = require('progress-stream')
var ProgressBar = require('progressbar.js')
const dragDrop = require('drag-drop')
const body = require('stream-body')
const { remote } = require('electron')
const dialog = remote.dialog
const win = remote.getCurrentWindow()
const httpServer = require('./server')
const httpClient = require('https').request
const client = dgram.createSocket({type: 'udp4', reuseAddr: true})
const server = dgram.createSocket({type: 'udp4', reuseAddr: true})
const PORT = 4321
const MC = '224.0.0.1'
const PROGRESS_REPORT_INTERVAL = 500
// should be a lot less than the report interval above, because if we
// interrupt an animation before it's ended the animation callback is not
// run and the success messages for files, except for the last one are not
// shown
const PROGRESS_ANIMATION_DURATION = 100
const transfers = {
/*
* id: {
* id,
* from,
* started,
* filename,
* filesize,
* finished
* error: object|null
* progress: https://www.npmjs.com/package/progress-stream#progress
* }
*/
}
const transfersEmitter = new EventEmitter()
function updateTransfer (id, fields) {
if (!transfers[id]) {
transfers[id] = fields
}
const old = clone(transfers[id])
Object.assign(transfers[id], fields)
if (transfers[id].progress) {
console.log(`${Math.round(transfers[id].progress.percentage)}% ${transfers[id].filename}`)
}
transfersEmitter.emit('change', old, transfers[id])
}
function humanHostname (hostname) {
return hostname.replace(/\.local/g, '')
}
function send (o) {
const message = Buffer.from(JSON.stringify(o))
client.send(message, 0, message.length, PORT, MC)
}
document.body.classList.add(process.platform)
//
// Dont accept arbitrary drops
//
dragDrop(document.body, () => {})
//
// Advertise a message
//
setInterval(ping, 1500)
function ping (extraAttrs = {}) {
const attrs = Object.assign({}, {
event: 'join',
name: humanHostname(os.hostname()),
platform: os.platform(),
ctime: Date.now()
}, extraAttrs)
send(attrs)
}
//
// Add a close button
//
const close = document.querySelector('.close')
close.addEventListener('click', () => {
remote.app.exit()
})
const me = document.querySelector('#me')
try {
const d = fs.readFileSync(path.join(os.homedir(), 'avatar'))
me.style.backgroundImage = 'url("' + d + '")'
me.textContent = ''
} catch (ex) {
me.textContent = os.hostname()
}
//
// Drop your avatar
//
dragDrop(me, (files) => {
const reader = new window.FileReader()
reader.onerror = err => {
console.error(err)
}
reader.onload = e => {
me.style.backgroundImage = 'url("' + e.target.result + '")'
me.textContent = ''
fs.writeFileSync(path.join(os.homedir(), 'avatar'), e.target.result)
ping({refreshAvatar: true})
}
reader.readAsDataURL(files[0])
})
//
// Add our hostname to the `me` icon.
//
httpServer((req, res) => {
const filename = req.headers['x-filename']
if (req.url === '/upload') {
const message = [
'Do you want to accept the file',
filename,
'from',
humanHostname(req.headers['x-from']) + '?'
].join(' ')
const opts = {
type: 'question',
buttons: ['Ok', 'Cancel'],
title: 'Confirm',
message
}
const result = dialog.showMessageBox(win, opts)
if (result === 0) {
const dest = path.join(remote.app.getPath('downloads'), filename)
const writeStream = fs.createWriteStream(dest)
const transfer = {
id: uuidV4(),
started: Date.now(),
filename: req.headers['x-filename'],
filesize: req.headers['x-filesize'],
from: humanHostname(req.headers['x-from']),
error: null,
progress: null,
finished: false
}
updateTransfer(transfer.id, transfer)
const progressStream = progress({
length: transfer.filesize,
time: PROGRESS_REPORT_INTERVAL, /* ms */
})
progressStream.on('progress', (progress) => {
updateTransfer(transfer.id, {progress})
})
console.log(`Staring download from ${transfer.from}, filename: ${transfer.filename}, size ${transfer.filesize} bytes, id ${transfer.id}`)
req.pipe(progressStream).pipe(writeStream)
} else if (result === 1) {
res.statusCode = 403
return res.end('User rejected the upload')
}
} else if (req.url === '/avatar') {
const filepath = path.join(os.homedir(), 'avatar')
fs.readFile(filepath, (err, data) => {
if (err) {
res.statusCode = 404
return res.end('')
}
res.end(data)
})
} else if (req.url === '/linux') {
// TODO serve the app so people can download it
} else if (req.url === '/mac') {
} else if (req.url === '/win') {
}
})
const registry = {}
function getData (src, cb) {
const reader = new window.FileReader()
reader.onerror = err => cb(err)
reader.onload = e => cb(null, e.target.result)
reader.readAsArrayBuffer(src)
}
function onFilesDropped (msg, files) {
files.forEach(file => {
const opts = {
host: msg.ip,
port: 9988,
path: '/upload',
method: 'POST',
rejectUnauthorized: false,
headers: {
'Content-Type': file.type,
'x-filename': file.name,
'x-from': os.hostname(),
'x-filesize': file.size
}
}
getData(file, (err, data) => {
if (err) return console.error(err)
const req = httpClient(opts, res => {
if (res.statusCode === 403) {
showRejectedMessage(msg)
return
}
if (res.statusCode !== 200) {
res.on('data', data => {
console.error(res.statusCode, data)
})
}
})
req.end(Buffer.from(data))
})
})
}
//
// Create a tcp server
//
function joined (msg, rinfo) {
const me = humanHostname(os.hostname())
msg.name = humanHostname(msg.name)
//
// Don't show me my own machine as a peer
//
if (!process.env['DEBUG'] && msg.name === me) {
return
}
//
// If the peer is already rendered, just return
//
const selector = `[data-name="${msg.name}"]`
if (document.querySelector(selector)) return
//
// Otherwise, create a peer and add it to the list.
//
const peers = document.querySelector('#peers')
const barElement = document.createElement('div')
barElement.className = 'bar'
const peer = document.createElement('div')
peer.appendChild(barElement)
peer.className = 'peer adding adding-anim'
peer.setAttribute('data-name', msg.name)
peer.setAttribute('data-ip', rinfo.address)
peer.setAttribute('data-platform', msg.platform)
const avatar = document.createElement('div')
avatar.className = 'avatar ' + msg.platform
peer.appendChild(avatar)
const name = document.createElement('address')
name.textContent = msg.name
name.address = rinfo.address
peer.appendChild(name)
const progressBar = new ProgressBar.Circle(barElement, {
strokeWidth: 6,
easing: 'easeInOut',
duration: PROGRESS_ANIMATION_DURATION,
color: '#ED6A5A',
trailColor: 'transparent',
trailWidth: 1,
svgStyle: null
})
transfersEmitter.on('change', (old, current) => {
if (current.from !== msg.name) {
return
}
const activeForMe = Object.values(transfers).filter(t => t.from === msg.name && t.progress && (t.progress.percentage < 100 || old.progress === null || old.id === t.id) && !t.finished)
if (activeForMe.length === 0) {
return
}
const progress = activeForMe.reduce((a, v) => a + v.progress.transferred, 0) / activeForMe.reduce((a, v) => a + v.progress.length, 0)
progressBar.animate(progress, () => {
if (current.progress && current.progress.percentage == 100 && !current.finished) {
const opts = {
type: 'info',
buttons: ['Ok'],
title: 'Downloaded',
message: `✅ The file ${current.filename} was successfully downloaded.`
}
dialog.showMessageBox(win, opts)
updateTransfer(current.id, {finished: true})
}
if (progress >= 1.0) {
progressBar.set(0);
}
})
})
peers.appendChild(peer)
window.requestAnimationFrame(() => requestAnimationFrame(() => peer.classList.remove('adding')))
peer.addEventListener('transitionend', e => {
if (e.propertyName !== 'transform') return
peer.classList.remove('adding-anim')
})
//
// Add a drag drop event to the peer
//
dragDrop(peer, (files) => {
onFilesDropped(registry[peer.getAttribute('data-name')], files)
})
//
// Get the avatar from the user who joined
//
loadAvatar(rinfo.address, peer)
//
// remove inital empty message when finding peers
//
const selectorEmptyState = document.querySelector('#empty-message')
selectorEmptyState.classList.remove('show')
}
function parted (msg) {
const selector = `.peer[data-name="${msg.name}"]`
const peer = document.querySelector(selector)
if (peer) peer.parentNode.removeChild(peer)
}
function showRejectedMessage (msg) {
const opts = {
type: 'info',
buttons: ['Ok'],
title: 'Rejected',
message: `${msg.name} rejected the file.`
}
dialog.showMessageBox(win, opts)
}
function cleanUp () {
for (var key in registry) {
if (registry[key] && (Date.now() - registry[key].ctime) > 9000) {
parted(registry[key])
registry[key] = null
}
}
}
server.on('error', (err) => {
console.error(err)
server.close()
})
server.on('message', (msg, rinfo) => {
msg = JSON.parse(msg)
if (!registry[msg.name] && msg.event === 'join') {
joined(msg, rinfo)
}
if (msg.refreshAvatar) {
const selector = `.peer[data-name="${msg.name}"]`
loadAvatar(rinfo.address, document.querySelector(selector))
}
msg.ip = rinfo.address
registry[msg.name] = msg
})
function loadAvatar (address, peerEl) {
const opts = {
host: address,
port: 9988,
path: '/avatar',
rejectUnauthorized: false
}
const req = httpClient(opts, res => {
if (res.statusCode !== 200) {
return console.error('Unable to get avatar')
}
body.parse(res, (err, data) => {
if (err) return console.error('unable to get avatar')
const avatar = peerEl.querySelector('.avatar')
avatar.className = 'avatar'
peerEl.style.backgroundImage = 'url("' + data + '")'
})
})
req.end()
}
server.on('listening', () => {
console.log(`Listening on ${PORT}`)
})
server.bind(PORT)
setInterval(cleanUp, 5000)