/* * TODO: * check if files exist * check file sizes */ var running = false; var queue = []; function queue_work(){ let i = 0; for(; i < queue.length; i++){ item = queue[i]; if(!item.started){ console.log(item); console.log("Starting " + item.file.name); item.started = true; let req = new XMLHttpRequest(); let data = new FormData(); data.append("file", item.file); req.upload.addEventListener("progress", function(evt){ if(evt.lengthComputable){ console.log("Upload for " + item.file.name + " at " + evt.loaded / evt.total); item.node.childNodes[1].value = evt.loaded; } }); req.onloadstart = function(evt){ console.log("Upload for " + item.file.name + " started"); item.node.childNodes[1].max = item.file.size; item.node.childNodes[1].value = 0; }; req.onloadend = function(evt){ console.log("Upload for " + item.file.name + " ended"); queue_work(); }; req.onerror = function(evt){ console.log("Upload for " + item.file.name + " errored"); item.node.className += " errored"; }; req.onabort = function(evt){ console.log("Upload for " + item.file.name + " aborted"); item.node.className += " errored"; }; req.onreadystatechange = function(evt){ console.log("Upload for " + item.file.name + " state " + req.readyState); if(req.readyState === XMLHttpRequest.DONE && req.status != 200){ console.log("Upload for " + item.file.name + " failed with " + req.status); item.node.className += " errored"; } }; req.open("POST", 'upload'); req.send(data); return; } } console.log("Queue run done"); running = false; } function queue_run(){ if(running || queue.length == 0){ return; } running = true; console.log("Starting queue run"); queue_work(); } function element(id){ return document.getElementById(id); } function node(tag, style, text){ let new_node = document.createElement(tag); new_node.className = style; if(text){ new_node.textContent = text; } return new_node; } function upload_element(file){ let new_node = node("div", "queue-entry"); new_node.appendChild(node("span", "name", file.name)); new_node.appendChild(node("progress", "progress")); element("queue").appendChild(new_node); return new_node; } function upload_start(element){ for(let i = 0; i < element.target.files.length; i++){ console.log(element.target.files[i]); queue.push({"file": element.target.files[i], "node": upload_element(element.target.files[i]), "progress": 0, "started": false}); } queue_run(); } function listing_clear(){ element("dirlisting").innerHTML = ""; } function interface_update(access){ if(!access.includes("c")){ element("tab-upload-label").style.display = "none"; } if(!access.includes("r")){ element("tab-view-label").style.display = "none"; element("tab-upload").checked = "checked"; } } function listing_add(file, style = "default"){ let link = node("a", "listing-entry", file.name); link.href = "file/" + encodeURIComponent(file.name); link.setAttribute("data-filename", file.name) if(style == "gallery"){ let image = node("img", "preview"); image.src = "preview/" + encodeURIComponent(file.name); //link.insertBefore(image, link.firstChild); link.appendChild(image); } element("dirlisting").appendChild(link); } function listing_update(){ listing_clear(); let req = new XMLHttpRequest(); req.onload = function(evt){ let data = JSON.parse(req.response); interface_update(data.access); for(let i = 0; i < data.files.length; i++){ listing_add(data.files[i], data.display); } }; req.open("GET", "listing"); req.send(); } function download_all(ev){ let links = document.getElementsByClassName("listing-entry"); var current = 0; var interval = setInterval(function(){ if(current < links.length){ link = links[current]; link.download = link.getAttribute("data-filename"); link.click(); link.removeAttribute("download"); current++; } else{ clearInterval(interval); } }, 1000); } function init(){ element("file-submit").style.display = "none"; element("files").onchange = upload_start; element("download-all").onclick = download_all; listing_update(); } window.onload = init;