-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
370 lines (277 loc) · 7.83 KB
/
app.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
"use strict";
const MAX_MSG_SIZE = 400
const file_input = $("DIV.form INPUT[type='file']");
const file_dropzone = $("#drop-zone");
const send_submit_btn = $(".form.send BUTTON");
const bg_folder = $("#bg-folder");
const get_submit_btn = $("#bg-folder BUTTON");
const ws_input = $("DIV.form.get INPUT.ws")
const http_input = $("DIV.form.send INPUT.http")
const usr_tok_input = $(".form.send INPUT.token")
const errbar = $(".errbar")
let received_file = ""
const get_headers = (token) => new Headers({
"Content-Type": "application/json",
"Accept-Content": "application/json",
"Authorization": "Basic " + btoa(token),
});
const request_data = {
};
const pctage = (num, ofnum) => `${(num / ofnum * 100).toFixed(0)}%`
function set_errbar(text) {
errbar.textContent = text
errbar.classList.add("active")
setTimeout(() => {
errbar.classList.remove("active")
errbar.textContent = ""
}, 3000)
}
(function()
{
const params = new URLSearchParams(window.location.search)
//let this_page = new URL(window.location)
//this_page.searchParams.append("apiEndpoint", "foo")
//this_page.searchParams.append("apiToken", "bar")
if (!params) {
return
}
const api_token = params.get("apiEndpoint")
const api_endpoint = params.get("apiToken")
http_input.value = api_endpoint
usr_tok_input.value = api_token
ws_input.value = api_endpoint
$("DIV.form.get INPUT.token").value = api_token
})();
(async function()
{
let file;
file_dropzone.addEventListener("dragover", ev => {
ev.preventDefault()
})
file_dropzone.addEventListener("drop", ev => {
file = get_file_drop(ev)
});
file_input.addEventListener("change", () => {
if (file = file_input.files[0])
console.log(file)
update_file_input(file)
});
send_submit_btn.addEventListener("click", async () => {
const target_http = $(".form.send INPUT.http ").value
const target_token = $(".form.send INPUT.token").value
const target_address = $(".form.send INPUT.address").value
if (!file || !target_http || !target_token || !target_address) {
set_errbar("Please fill out all fields.")
//send_submit_btn.textContent = "Something's wrong!"
}
if (!await send_file(file, target_http, target_token, target_address)) {
send_submit_btn.textContent = "somethings wrong"
//send_submit_button.classList.add("err")
return
}
send_submit_btn.textContent = "File Sent!"
Collapse_prompt()
});
get_submit_btn.addEventListener("click", async () => {
const from_ws = $(".form.get INPUT.ws").value
const from_token = $(".form.get INPUT.token").value
console.log("trying to fetch")
if (!from_ws && !from_token)
set_errbar("Please fill out all fields.")
if (await fetch_file(from_ws, from_token))
console.log("done?")
get_submit_btn.textContent = "Please Wait..."
});
})();
function
get_file_drop(ev)
{
ev.preventDefault()
let file;
if (!ev.dataTransfer.items) {
return;
}
for (let i = 0; i < ev.dataTransfer.items.length; i++) {
if (ev.dataTransfer.items[i].kind !== "file") {
return;
}
file = ev.dataTransfer.items[i].getAsFile();
update_file_input(file)
}
return file
}
function
gen_download_link(name, dataURL)
{
const link = document.createElement("A")
link.download = name
link.href = dataURL
link.textContent = name
link.className = "download-icon"
bg_folder.appendChild(link)
}
async function
dataURL_from_file(file)
{
return new Promise((resolve,reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = _ => resolve(reader.result)
reader.onerror = err => reject(err)
})
}
async function
update_file_input(file)
{
let reader = new FileReader()
let text = await file.text()
let file_icon = document.createElement("DIV")
file_icon.classList += "file-icon"
file_icon.classList += " prompt-item"
file_icon.innerHTML = `${file.name}<BR/>
${(file.size/1024).toFixed(2)}kB`
file_icon.onclick = () => {
file_input.disabled = true
file_input.value = null
file_dropzone.removeChild(file_icon)
setTimeout(_ => {file_input.disabled = false}, 100)
}
file_dropzone.appendChild(file_icon)
}
async function
handle_received_chunk(data)
{
received_file += get_decoded_msg(data)
}
function
get_decoded_msg(data)
{
const td = new TextDecoder()
console.log("decode_ws_message: raw data: ", data)
const rlp_u8arr = new Uint8Array(JSON.parse(`[${data}]`))
const deco_u8arr = (decode_RLP(rlp_u8arr))[0]
console.log("decode_ws_message: deco_u8arr: ", deco_u8arr)
let decostr = td.decode(deco_u8arr)
console.log("decode_ws_message: decostr: ", decostr.toString())
return decostr
}
async function
send_file(file, http_url, token, addr)
{
const dataURL = await dataURL_from_file(file)
const address = await fetch_hopr_address(http_url, token)
if (!dataURL) {
return false
}
const nchunks = Math.ceil(dataURL.length / MAX_MSG_SIZE)
let acc = new Array(nchunks)
const meta = JSON.stringify({
filename: file.name,
nchunks: nchunks,
/*filesize:*/
})
/* Send the metadata first. */
await send_msg(http_url,
meta,
address,
token)
let i = 0, from = 0
while (i < nchunks) {
send_submit_btn.innerHTML = `Please Wait...<BR/>
${pctage(i, nchunks)} Sent`
acc[i] = dataURL.substr(from, MAX_MSG_SIZE)
console.log(`sending chunk ${i}/${nchunks}...`)
await send_msg(http_url,
acc[i],
address,
token)
from += MAX_MSG_SIZE
i++
}
return true
}
async function
fetch_hopr_address(http_url, token)
{
let url = new URL(http_url)
url.protocol = "https:"
url.pathname = "/api/v2/account/addresses"
const headers = get_headers(token)
const account = await fetch(url, {
headers: headers,
}).then(res => res.json())
return account.hopr
}
async function
send_msg(to_http, message, address, token)
{
const url = new URL(to_http)
url.pathname = "/api/v2/messages"
const headers = get_headers(token)
const body = JSON.stringify({
recipient: address,
body: message,
});
const req = {
method: "POST",
headers: headers,
body: body,
}
await fetch(url, req)
}
async function
fetch_file(from_url, token)
{
const ws_url = new URL(from_url)
ws_url.protocol = "wss:"
ws_url.pathname = "/api/v2/messages/websocket"
ws_url.search = "?apiToken=" + token
let meta = null
let first = true
let remaining = null
const socket = new WebSocket(ws_url)
let err;
socket.onopen = (ev) => {
console.log("socket opened!")
bg_folder.style.border = "3px solid limegreen"
bg_folder.style.borderTop = "none"
}
socket.onerror = (ev) => {
err = true;
set_errbar("Can't reach that HOPR node, please try again later.")
//get_submit_btn.innerHTML = "Can't reach that websocket. Sorry! <BR/> Click to try again"
//get_submit_btn.classList.add("err")
}
const handle_message = (ev) => {
if (first) {
const deco = get_decoded_msg(ev.data)
meta = JSON.parse(get_decoded_msg(ev.data))
console.log("evdata string: ", deco.toString())
remaining = meta.nchunks
first = false
return
}
get_submit_btn.innerHTML = `Please Wait...<BR/>
${pctage(meta.nchunks, remaining)} Received`
console.log("got a chunk from the websocket")
handle_received_chunk(ev.data)
remaining--
console.log(remaining, meta.nchunks)
if (remaining <= 0) {
get_submit_btn.textContent = "Done!"
gen_download_link(meta.filename, received_file)
remaining = null
first = true
}
}
socket.addEventListener("message", handle_message)
const close = () => {
console.log("CONNECTION CLOSED")
}
socket.onclose = () => {
if (err) {
return false;
}
close();
}
}