refactoring code
This commit is contained in:
parent
e3611ded17
commit
c539ed6282
5 changed files with 225 additions and 95 deletions
184
public/upload.js
184
public/upload.js
|
@ -1,101 +1,105 @@
|
|||
function onChange(event) {
|
||||
|
||||
var file = event.target.files[0];
|
||||
var reader = new FileReader();
|
||||
reader.readAsArrayBuffer(file);
|
||||
|
||||
let random_iv = window.crypto.getRandomValues(new Uint8Array(16));
|
||||
var hex = ivToStr(random_iv);
|
||||
|
||||
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((key) => {
|
||||
let arrayBuffer = self.result;
|
||||
let array = new Uint8Array(arrayBuffer);
|
||||
|
||||
var random_iv = window.crypto.getRandomValues(new Uint8Array(16));
|
||||
window.crypto.subtle.encrypt({
|
||||
name: "AES-CBC",
|
||||
iv: random_iv },
|
||||
key,
|
||||
array)
|
||||
.then(uploadFile.bind(null, file, hex, key))
|
||||
.catch((err) => console.error(err));
|
||||
|
||||
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);
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
var hex = ivToStr(random_iv);
|
||||
xhr.open('post', '/upload/' + hex, true);
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
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) {
|
||||
console.error(err);
|
||||
});
|
||||
}).catch((err) => console.error(err));
|
||||
};
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
let uploadFile = (file, hex, key, 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);
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.open("post", "/upload/" + hex, true);
|
||||
|
||||
let prog = new window.ProgressEmitter(file.name, hex, file.type);
|
||||
|
||||
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);
|
||||
|
||||
prog.link = "http://localhost:3000/download/" + hex + "/#" + keydata.k;
|
||||
prog.emit();
|
||||
|
||||
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);
|
||||
// setupLI(file.name);
|
||||
|
||||
}
|
||||
|
||||
function setupLI(emitter) {
|
||||
var li = document.createElement("li");
|
||||
var name = document.createElement("p");
|
||||
name.innerHTML = emitter;
|
||||
li.appendChild(name);
|
||||
|
||||
var link = document.createElement("a");
|
||||
link.addEventListener('NotifyProgress', (progress_event) => {
|
||||
console.log(progress_event);
|
||||
});
|
||||
li.appendChild(link);
|
||||
|
||||
var progress = document.createElement("p");
|
||||
li.appendChild(progress);
|
||||
document.getElementById("uploaded_files").appendChild(li);
|
||||
}
|
||||
|
||||
function ivToStr(iv) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue