added prettier for js formatting. use 'npm run format'
This commit is contained in:
parent
0f64dcad85
commit
57461ead7f
5 changed files with 754 additions and 224 deletions
|
@ -1,26 +1,24 @@
|
|||
function download() {
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("get", "/assets" + location.pathname.slice(0, -1), true);
|
||||
xhr.responseType = "blob";
|
||||
xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
|
||||
xhr.responseType = 'blob';
|
||||
|
||||
var li = document.createElement("li");
|
||||
var progress = document.createElement("p");
|
||||
var li = document.createElement('li');
|
||||
var progress = document.createElement('p');
|
||||
li.appendChild(progress);
|
||||
document.getElementById("downloaded_files").appendChild(li);
|
||||
|
||||
xhr.addEventListener("progress", returnBindedLI(li, progress));
|
||||
|
||||
|
||||
|
||||
xhr.onload = function(e) {
|
||||
document.getElementById('downloaded_files').appendChild(li);
|
||||
|
||||
xhr.addEventListener('progress', returnBindedLI(li, progress));
|
||||
|
||||
xhr.onload = function(e) {
|
||||
// maybe send a separate request before this one to get the filename?
|
||||
|
||||
// maybe render the html itself with the filename, since it's generated server side
|
||||
// after a get request with the unique id
|
||||
var name = document.createElement("p");
|
||||
name.innerHTML = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1];
|
||||
var name = document.createElement('p');
|
||||
name.innerHTML = xhr
|
||||
.getResponseHeader('Content-Disposition')
|
||||
.match(/filename="(.+)"/)[1];
|
||||
li.insertBefore(name, li.firstChild);
|
||||
|
||||
if (this.status == 200) {
|
||||
|
@ -33,59 +31,69 @@ function download() {
|
|||
var array = new Uint8Array(arrayBuffer);
|
||||
salt = strToIv(location.pathname.slice(10, -1));
|
||||
|
||||
window.crypto.subtle.importKey(
|
||||
"jwk",
|
||||
{
|
||||
kty: "oct",
|
||||
window.crypto.subtle
|
||||
.importKey(
|
||||
'jwk',
|
||||
{
|
||||
kty: 'oct',
|
||||
k: location.hash.slice(1),
|
||||
alg: "A128CBC",
|
||||
ext: true,
|
||||
},
|
||||
{
|
||||
name: "AES-CBC",
|
||||
},
|
||||
true,
|
||||
["encrypt", "decrypt"])
|
||||
.then(function(key){
|
||||
window.crypto.subtle.decrypt(
|
||||
alg: 'A128CBC',
|
||||
ext: true
|
||||
},
|
||||
{
|
||||
name: 'AES-CBC'
|
||||
},
|
||||
true,
|
||||
['encrypt', 'decrypt']
|
||||
)
|
||||
.then(function(key) {
|
||||
window.crypto.subtle
|
||||
.decrypt(
|
||||
{
|
||||
name: "AES-CBC",
|
||||
iv: salt,
|
||||
name: 'AES-CBC',
|
||||
iv: salt
|
||||
},
|
||||
key,
|
||||
array)
|
||||
.then(function(decrypted){
|
||||
var dataView = new DataView(decrypted);
|
||||
var blob = new Blob([dataView]);
|
||||
var downloadUrl = URL.createObjectURL(blob);
|
||||
var a = document.createElement("a");
|
||||
a.href = downloadUrl;
|
||||
a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="(.+)"/)[1];
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
})
|
||||
.catch(function(err){
|
||||
alert("This link is either invalid or has expired, or the uploader has deleted the file.");
|
||||
console.error(err);
|
||||
});
|
||||
})
|
||||
.catch(function(err){
|
||||
array
|
||||
)
|
||||
.then(function(decrypted) {
|
||||
var dataView = new DataView(decrypted);
|
||||
var blob = new Blob([dataView]);
|
||||
var downloadUrl = URL.createObjectURL(blob);
|
||||
var a = document.createElement('a');
|
||||
a.href = downloadUrl;
|
||||
a.download = xhr
|
||||
.getResponseHeader('Content-Disposition')
|
||||
.match(/filename="(.+)"/)[1];
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
})
|
||||
.catch(function(err) {
|
||||
alert(
|
||||
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
||||
);
|
||||
console.error(err);
|
||||
});
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
fileReader.readAsArrayBuffer(blob);
|
||||
} else {
|
||||
alert("This link is either invalid or has expired, or the uploader has deleted the file.")
|
||||
alert(
|
||||
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
||||
);
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function ivToStr(iv) {
|
||||
let hexStr = "";
|
||||
let hexStr = '';
|
||||
for (var i in iv) {
|
||||
if (iv[i] < 16) {
|
||||
hexStr += "0" + iv[i].toString(16);
|
||||
hexStr += '0' + iv[i].toString(16);
|
||||
} else {
|
||||
hexStr += iv[i].toString(16);
|
||||
}
|
||||
|
@ -97,7 +105,7 @@ function ivToStr(iv) {
|
|||
function strToIv(str) {
|
||||
var iv = new Uint8Array(16);
|
||||
for (var i = 0; i < str.length; i += 2) {
|
||||
iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16);
|
||||
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
||||
}
|
||||
|
||||
return iv;
|
||||
|
@ -105,24 +113,23 @@ function strToIv(str) {
|
|||
|
||||
function returnBindedLI(li, progress) {
|
||||
return function updateProgress(e) {
|
||||
if (e.lengthComputable) {
|
||||
var percentComplete = Math.floor((e.loaded / e.total) * 100);
|
||||
progress.innerHTML = "Progress: " + percentComplete + "%";
|
||||
}
|
||||
if (e.lengthComputable) {
|
||||
var percentComplete = Math.floor(e.loaded / e.total * 100);
|
||||
progress.innerHTML = 'Progress: ' + percentComplete + '%';
|
||||
}
|
||||
|
||||
if (percentComplete === 100) {
|
||||
var finished = document.createElement("p");
|
||||
finished.innerHTML = "Your download has finished.";
|
||||
li.appendChild(finished);
|
||||
if (percentComplete === 100) {
|
||||
var finished = document.createElement('p');
|
||||
finished.innerHTML = 'Your download has finished.';
|
||||
li.appendChild(finished);
|
||||
|
||||
var close = document.createElement("button");
|
||||
close.innerHTML = "Ok";
|
||||
close.addEventListener("click", function() {
|
||||
document.getElementById("downloaded_files").removeChild(li);
|
||||
});
|
||||
var close = document.createElement('button');
|
||||
close.innerHTML = 'Ok';
|
||||
close.addEventListener('click', function() {
|
||||
document.getElementById('downloaded_files').removeChild(li);
|
||||
});
|
||||
|
||||
li.appendChild(close);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
li.appendChild(close);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
201
public/upload.js
201
public/upload.js
|
@ -3,85 +3,106 @@ function onChange(event) {
|
|||
var reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
let self = this;
|
||||
window.crypto.subtle.generateKey({
|
||||
name: "AES-CBC",
|
||||
length: 128
|
||||
},
|
||||
true,
|
||||
["encrypt", "decrypt"])
|
||||
.then(function(key){
|
||||
var arrayBuffer = self.result;
|
||||
var array = new Uint8Array(arrayBuffer);
|
||||
window.crypto.subtle
|
||||
.generateKey(
|
||||
{
|
||||
name: 'AES-CBC',
|
||||
length: 128
|
||||
},
|
||||
true,
|
||||
['encrypt', 'decrypt']
|
||||
)
|
||||
.then(function(key) {
|
||||
var arrayBuffer = self.result;
|
||||
var array = new Uint8Array(arrayBuffer);
|
||||
|
||||
var random_iv = window.crypto.getRandomValues(new Uint8Array(16));
|
||||
var random_iv = window.crypto.getRandomValues(new Uint8Array(16));
|
||||
|
||||
window.crypto.subtle.encrypt({
|
||||
name: "AES-CBC",
|
||||
iv: random_iv },
|
||||
key,
|
||||
array)
|
||||
.then(function(encrypted){
|
||||
var dataView = new DataView(encrypted);
|
||||
var blob = new Blob([dataView], { type: file.type });
|
||||
|
||||
var fd = new FormData();
|
||||
fd.append("fname", file.name);
|
||||
fd.append("data", blob, file.name);
|
||||
window.crypto.subtle
|
||||
.encrypt(
|
||||
{
|
||||
name: 'AES-CBC',
|
||||
iv: random_iv
|
||||
},
|
||||
key,
|
||||
array
|
||||
)
|
||||
.then(function(encrypted) {
|
||||
var dataView = new DataView(encrypted);
|
||||
var blob = new Blob([dataView], { type: file.type });
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
var hex = ivToStr(random_iv);
|
||||
xhr.open("post", "/upload/" + hex, true);
|
||||
var fd = new FormData();
|
||||
fd.append('fname', file.name);
|
||||
fd.append('data', blob, file.name);
|
||||
|
||||
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 xhr = new XMLHttpRequest();
|
||||
var hex = ivToStr(random_iv);
|
||||
xhr.open('post', '/upload/' + hex, true);
|
||||
|
||||
var progress = document.createElement("p");
|
||||
li.appendChild(progress);
|
||||
document.getElementById("uploaded_files").appendChild(li);
|
||||
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);
|
||||
|
||||
xhr.upload.addEventListener("progress", returnBindedLI(progress, name, link, li));
|
||||
var progress = document.createElement('p');
|
||||
li.appendChild(progress);
|
||||
document.getElementById('uploaded_files').appendChild(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);
|
||||
xhr.upload.addEventListener(
|
||||
'progress',
|
||||
returnBindedLI(progress, name, link, li)
|
||||
);
|
||||
|
||||
link.innerHTML = "http://localhost:3000/download/" + hex + "/#" + keydata.k;
|
||||
link.setAttribute("href", "http://localhost:3000/download/" + hex + "/#" + keydata.k);
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
||||
window.crypto.subtle
|
||||
.exportKey('jwk', key)
|
||||
.then(function(keydata) {
|
||||
var curr_name = localStorage.getItem(file.name);
|
||||
|
||||
console.log("Share this link with a friend: http://localhost:3000/download/" + hex + "/#" + keydata.k);
|
||||
})
|
||||
}
|
||||
};
|
||||
localStorage.setItem(hex, xhr.responseText);
|
||||
|
||||
xhr.send(fd);
|
||||
})
|
||||
.catch(function(err){
|
||||
console.error(err);
|
||||
});
|
||||
link.innerHTML =
|
||||
'http://localhost:3000/download/' +
|
||||
hex +
|
||||
'/#' +
|
||||
keydata.k;
|
||||
link.setAttribute(
|
||||
'href',
|
||||
'http://localhost:3000/download/' + hex + '/#' + keydata.k
|
||||
);
|
||||
|
||||
console.log(
|
||||
'Share this link with a friend: http://localhost:3000/download/' +
|
||||
hex +
|
||||
'/#' +
|
||||
keydata.k
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(fd);
|
||||
})
|
||||
.catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
})
|
||||
.catch(function(err){
|
||||
.catch(function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
function ivToStr(iv) {
|
||||
let hexStr = "";
|
||||
let hexStr = '';
|
||||
for (var i in iv) {
|
||||
if (iv[i] < 16) {
|
||||
hexStr += "0" + iv[i].toString(16);
|
||||
hexStr += '0' + iv[i].toString(16);
|
||||
} else {
|
||||
hexStr += iv[i].toString(16);
|
||||
}
|
||||
|
@ -93,7 +114,7 @@ function ivToStr(iv) {
|
|||
function strToIv(str) {
|
||||
var iv = new Uint8Array(16);
|
||||
for (var i = 0; i < str.length; i += 2) {
|
||||
iv[i/2] = parseInt((str.charAt(i) + str.charAt(i + 1)), 16);
|
||||
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
||||
}
|
||||
|
||||
return iv;
|
||||
|
@ -101,41 +122,39 @@ function strToIv(str) {
|
|||
|
||||
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 (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];
|
||||
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;
|
||||
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.send(JSON.stringify({ delete_token: localStorage.getItem(key) }));
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
document.getElementById("uploaded_files").removeChild(li);
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
document.getElementById('uploaded_files').removeChild(li);
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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