1(function() { 2 3 var server_max_size = 0, ws; 4 5 function san(s) 6 { 7 if (!s) 8 return ""; 9 10 return s.replace(/&/g, "&"). 11 replace(/\</g, "<"). 12 replace(/\>/g, ">"). 13 replace(/\"/g, """). 14 replace(/%/g, "%"); 15 } 16 17 function lws_urlencode(s) 18 { 19 return encodeURI(s).replace(/@/g, "%40"); 20 } 21 22 function trim(num) 23 { 24 var s = num.toString(); 25 26 if (!s.indexOf(".")) 27 return s; 28 29 while (s.length && s[s.length - 1] === "0") 30 s = s.substring(0, s.length - 1); 31 32 if (s[s.length - 1] === ".") 33 s = s.substring(0, s.length - 1); 34 35 return s; 36 } 37 38 function humanize(n) 39 { 40 if (n < 1024) 41 return n + "B"; 42 43 if (n < 1024 * 1024) 44 return trim((n / 1024).toFixed(2)) + "KiB"; 45 46 if (n < 1024 * 1024 * 1024) 47 return trim((n / (1024 * 1024)).toFixed(2)) + "MiB"; 48 49 return trim((n / (1024 * 1024 * 1024)).toFixed(2)) + "GiB"; 50 } 51 52 function da_enter(e) 53 { 54 var da = document.getElementById("da"); 55 56 e.preventDefault(); 57 da.classList.add("trot"); 58 } 59 60 function da_leave(e) 61 { 62 var da = document.getElementById("da"); 63 64 e.preventDefault(); 65 da.classList.remove("trot"); 66 } 67 68 function da_over(e) 69 { 70 var da = document.getElementById("da"); 71 72 e.preventDefault(); 73 da.classList.add("trot"); 74 } 75 76 function clear_errors() { 77 var n, t = document.getElementById("ongoing"); 78 79 for (n = 0; n < t.rows.length; n++) 80 if (t.rows[n].cells[0].classList.contains("err")) 81 t.deleteRow(n); 82 } 83 84 function do_upload(file) { 85 var formData = new FormData(); 86 var t = document.getElementById("ongoing"); 87 88 formData.append("file", file); 89 90 var row = t.insertRow(0), c1 = row.insertCell(0), 91 c2 = row.insertCell(1), c3 = row.insertCell(2); 92 93 c1.classList.add("ogn"); 94 c1.classList.add("r"); 95 96 if (file.size > server_max_size) { 97 c1.innerHTML = "Too Large"; 98 c1.classList.add("err"); 99 } else 100 c1.innerHTML = "<img class=\"working\">"; 101 102 c2.classList.add("ogn"); 103 c2.classList.add("r"); 104 c2.innerHTML = humanize(file.size); 105 106 c3.classList.add("ogn"); 107 c3.innerHTML = file.name; 108 109 if (file.size > server_max_size) 110 return; 111 112 fetch("upload/" + lws_urlencode(file.name), { 113 method: "POST", 114 body: formData 115 }) 116 .then((e) => { /* this just means we got a response code */ 117 var us = e.url.split("/"), ul = us[us.length - 1], n; 118 119 for (n = 0; n < t.rows.length; n++) 120 if (ul === lws_urlencode( 121 t.rows[n].cells[2].textContent)) { 122 if (e.ok === true) { 123 t.deleteRow(n); 124 } else { 125 t.rows[n].cells[0].textContent = 126 "Failed " + san(e.status.toString()); 127 t.rows[n].cells[0]. 128 classList.add("err"); 129 } 130 break; 131 } 132 }) 133 .catch((e) => { 134 var us = e.url.split("/"), ul = us[us.length - 1], n; 135 136 for (n = 0; n < t.rows.length; n++) 137 if (ul === lws_urlencode( 138 t.rows[n].cells[2].textContent)) { 139 t.rows[n].cells[0] = "FAIL"; 140 break; 141 } 142 }); 143 } 144 145 function da_drop(e) { 146 var da = document.getElementById("da"); 147 148 e.preventDefault(); 149 da.classList.remove("trot"); 150 151 clear_errors(); 152 153 ([...e.dataTransfer.files]).forEach(do_upload); 154 } 155 156 function upl_button(e) { 157 var fi = document.getElementById("file"); 158 159 clear_errors(); 160 e.preventDefault(); 161 162 ([...fi.files]).forEach(do_upload); 163 } 164 165 function body_drop(e) { 166 e.preventDefault(); 167 } 168 169 function inp() { 170 var fi = document.getElementById("file"), 171 upl = document.getElementById("upl"); 172 console.log("inp"); 173 upl.disabled = !fi.files.length; 174 } 175 176 function delfile(e) 177 { 178 e.stopPropagation(); 179 e.preventDefault(); 180 181 ws.send("{\"del\":\"" + decodeURI(e.target.getAttribute("file")) + 182 "\"}"); 183 } 184 185 function get_appropriate_ws_url(extra_url) 186 { 187 var pcol; 188 var u = document.URL; 189 190 /* 191 * We open the websocket encrypted if this page came on an 192 * https:// url itself, otherwise unencrypted 193 */ 194 195 if (u.substring(0, 5) === "https") { 196 pcol = "wss://"; 197 u = u.substr(8); 198 } else { 199 pcol = "ws://"; 200 if (u.substring(0, 4) === "http") 201 u = u.substr(7); 202 } 203 204 u = u.split("/"); 205 206 /* + "/xxx" bit is for IE10 workaround */ 207 208 return pcol + u[0] + "/" + extra_url; 209 } 210 211 function new_ws(urlpath, protocol) 212 { 213 return new WebSocket(urlpath, protocol); 214 } 215 216 document.addEventListener("DOMContentLoaded", function() { 217 var da = document.getElementById("da"), 218 fi = document.getElementById("file"), 219 upl = document.getElementById("upl"); 220 221 da.addEventListener("dragenter", da_enter, false); 222 da.addEventListener("dragleave", da_leave, false); 223 da.addEventListener("dragover", da_over, false); 224 da.addEventListener("drop", da_drop, false); 225 226 upl.addEventListener("click", upl_button, false); 227 fi.addEventListener("change", inp, false); 228 229 window.addEventListener("dragover", body_drop, false); 230 window.addEventListener("drop", body_drop, false); 231 232 ws = new_ws(get_appropriate_ws_url(""), "lws-deaddrop"); 233 try { 234 ws.onopen = function() { 235 var dd = document.getElementById("ddrop"), 236 da = document.getElementById("da"); 237 238 dd.classList.remove("noconn"); 239 da.classList.remove("disa"); 240 }; 241 242 ws.onmessage = function got_packet(msg) { 243 var j = JSON.parse(msg.data), s = "", n, 244 t = document.getElementById("dd-list"); 245 246 server_max_size = j.max_size; 247 document.getElementById("size").innerHTML = 248 "Server maximum file size " + 249 humanize(j.max_size); 250 251 s += "<table class=\"nb\">"; 252 for (n = 0; n < j.files.length; n++) { 253 var date = new Date(j.files[n].mtime * 1000); 254 s += "<tr><td class=\"dow r\">" + 255 humanize(j.files[n].size) + 256 "</td><td class=\"dow\">" + 257 date.toDateString() + " " + 258 date.toLocaleTimeString() + 259 "</td><td>"; 260 if (j.files[n].yours === 1) 261 s += "<img id=\"d" + n + 262 "\" class=\"delbtn\" file=\"" + 263 lws_urlencode(san(j.files[n].name)) + "\">"; 264 else 265 s += " "; 266 267 s += "</td><td class=\"ogn\"><a href=\"get/" + 268 lws_urlencode(san(j.files[n].name)) + 269 "\" download>" + 270 san(j.files[n].name) + "</a></td></tr>"; 271 } 272 s += "</table>"; 273 274 t.innerHTML = s; 275 276 for (n = 0; n < j.files.length; n++) { 277 var d = document.getElementById("d" + n); 278 if (d) 279 d.addEventListener("click", 280 delfile, false); 281 } 282 }; 283 284 ws.onclose = function() { 285 var dd = document.getElementById("ddrop"), 286 da = document.getElementById("da"); 287 288 dd.classList.add("noconn"); 289 da.classList.add("disa"); 290 }; 291 } catch(exception) { 292 alert("<p>Error " + exception); 293 } 294 295 }); 296}()); 297