implemented deleting from server. multiple uploads by uploader handled and displayed on DOM
This commit is contained in:
parent
c25c97fbaa
commit
895c4d364f
5 changed files with 118 additions and 27 deletions
|
@ -31,12 +31,40 @@ function onChange(event) {
|
|||
var xhr = new XMLHttpRequest();
|
||||
var hex = ivToStr(random_iv);
|
||||
xhr.open("post", "/upload/" + hex, true);
|
||||
xhr.addEventListener("progress", updateProgress);
|
||||
xhr.upload.addEventListener("progress", updateProgress);
|
||||
|
||||
var li = document.createElement("li");
|
||||
var name = document.createElement("p");
|
||||
name.innerHTML = file.name;
|
||||
li.appendChild(name);
|
||||
|
||||
var link = document.createElement("a");
|
||||
li.appendChild(link);
|
||||
|
||||
var progress = document.createElement("p");
|
||||
li.appendChild(progress);
|
||||
document.getElementById("uploaded_files").appendChild(li);
|
||||
|
||||
|
||||
xhr.upload.addEventListener("progress", returnBindedLI(progress, name, link, li));
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
||||
window.crypto.subtle.exportKey("jwk", key).then(function(keydata) {
|
||||
var curr_name = localStorage.getItem(file.name);
|
||||
|
||||
localStorage.setItem(hex, xhr.responseText);
|
||||
|
||||
link.innerHTML = "http://localhost:3000/download/" + hex + "/#" + keydata.k;
|
||||
link.setAttribute("href", "http://localhost:3000/download/" + hex + "/#" + keydata.k);
|
||||
|
||||
// if (curr_name) {
|
||||
// localStorage.setItem(file.name, curr_name + "," + hex);
|
||||
// } else {
|
||||
// localStorage.setItem(file.name, hex)
|
||||
// }
|
||||
|
||||
|
||||
|
||||
console.log("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k);
|
||||
alert("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k);
|
||||
})
|
||||
|
@ -80,9 +108,42 @@ function strToIv(str) {
|
|||
return iv;
|
||||
}
|
||||
|
||||
function updateProgress(e) {
|
||||
if (e.lengthComputable) {
|
||||
var percentComplete = Math.floor((e.loaded / e.total) * 100);
|
||||
document.getElementById("downloadProgress").innerHTML = "Progress: " + percentComplete + "%";
|
||||
}
|
||||
}
|
||||
function returnBindedLI(a_element, name, link, li) {
|
||||
return function updateProgress(e) {
|
||||
if (e.lengthComputable) {
|
||||
var percentComplete = Math.floor((e.loaded / e.total) * 100);
|
||||
a_element.innerHTML = "Progress: " + percentComplete + "%";
|
||||
|
||||
if (percentComplete === 100) {
|
||||
var btn = document.createElement("button");
|
||||
btn.innerHTML = "Delete from server";
|
||||
btn.addEventListener("click", function() {
|
||||
var segments = link.innerHTML.split("/");
|
||||
var key = segments[segments.length - 2];
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("post", "/delete/" + key, true);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
if (!localStorage.getItem(key)) return;
|
||||
|
||||
xhr.send(JSON.stringify({delete_token: localStorage.getItem(key)}));
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
document.getElementById("uploaded_files").removeChild(li);
|
||||
}
|
||||
|
||||
if (xhr.status === 200) {
|
||||
console.log("The file was successfully deleted.");
|
||||
} else {
|
||||
console.log("The file has expired, or has already been deleted.");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
li.appendChild(btn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue