2017-06-01 22:10:00 +00:00
|
|
|
const EventEmitter = require('events');
|
2017-06-01 22:12:30 +00:00
|
|
|
const UIWrapper = require('./ui').UIWrapper;
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let onChange = event => {
|
2017-06-01 22:10:00 +00:00
|
|
|
let file = event.target.files[0];
|
|
|
|
let reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
|
|
|
|
let random_iv = window.crypto.getRandomValues(new Uint8Array(16));
|
|
|
|
let hex = ivToStr(random_iv);
|
|
|
|
|
|
|
|
reader.onload = function(event) {
|
|
|
|
let self = this;
|
2017-06-01 22:12:30 +00:00
|
|
|
window.crypto.subtle
|
|
|
|
.generateKey(
|
|
|
|
{
|
|
|
|
name: 'AES-CBC',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
)
|
|
|
|
.then(key => {
|
|
|
|
let arrayBuffer = self.result;
|
|
|
|
let array = new Uint8Array(arrayBuffer);
|
|
|
|
|
|
|
|
window.crypto.subtle
|
|
|
|
.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-CBC',
|
|
|
|
iv: random_iv
|
|
|
|
},
|
|
|
|
key,
|
|
|
|
array
|
|
|
|
)
|
|
|
|
.then(uploadFile.bind(null, file, hex, key))
|
|
|
|
.catch(err => console.error(err));
|
|
|
|
})
|
|
|
|
.catch(err => console.error(err));
|
2017-06-01 22:10:00 +00:00
|
|
|
};
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
window.onChange = onChange;
|
|
|
|
|
|
|
|
let uploadFile = (file, hex, key, encrypted) => {
|
|
|
|
let dataView = new DataView(encrypted);
|
|
|
|
let blob = new Blob([dataView], { type: file.type });
|
2017-06-01 22:12:30 +00:00
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
let fd = new FormData();
|
2017-06-01 22:12:30 +00:00
|
|
|
fd.append('fname', file.name);
|
|
|
|
fd.append('data', blob, file.name);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.open('post', '/upload/' + hex, true);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
let listelem = setupUI();
|
|
|
|
listelem.emit('name', file.name);
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.upload.addEventListener('progress', updateProgress.bind(null, listelem));
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.onreadystatechange = () => {
|
2017-06-01 22:10:00 +00:00
|
|
|
if (xhr.readyState == XMLHttpRequest.DONE) {
|
2017-06-01 22:12:30 +00:00
|
|
|
window.crypto.subtle.exportKey('jwk', key).then(keydata => {
|
2017-06-01 22:10:00 +00:00
|
|
|
localStorage.setItem(hex, xhr.responseText);
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
listelem.emit(
|
|
|
|
'link',
|
|
|
|
'http://localhost:3000/download/' + hex + '/#' + keydata.k
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
'Share this link with a friend: http://localhost:3000/download/' +
|
|
|
|
hex +
|
|
|
|
'/#' +
|
|
|
|
keydata.k
|
|
|
|
);
|
|
|
|
});
|
2017-06-01 22:10:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(fd);
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
let updateProgress = (UIelem, e) => {
|
|
|
|
if (e.lengthComputable) {
|
2017-06-01 22:12:30 +00:00
|
|
|
let percentComplete = Math.floor(e.loaded / e.total * 100);
|
|
|
|
UIelem.emit('progress', 'Progress: ' + percentComplete + '%');
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
if (percentComplete === 100) {
|
2017-06-01 22:12:30 +00:00
|
|
|
let btn = document.createElement('button');
|
|
|
|
btn.innerText = 'Delete from server';
|
|
|
|
btn.addEventListener('click', () => {
|
|
|
|
let segments = UIelem.link.innerText.split('/');
|
2017-06-01 22:10:00 +00:00
|
|
|
let key = segments[segments.length - 2];
|
|
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.open('post', '/delete/' + key, true);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
2017-06-01 22:10:00 +00:00
|
|
|
if (!localStorage.getItem(key)) return;
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.send(JSON.stringify({ delete_token: localStorage.getItem(key) }));
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
2017-06-01 22:12:30 +00:00
|
|
|
document.getElementById('uploaded_files').removeChild(UIelem.li);
|
2017-06-01 22:10:00 +00:00
|
|
|
localStorage.removeItem(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xhr.status === 200) {
|
2017-06-01 22:12:30 +00:00
|
|
|
console.log('The file was successfully deleted.');
|
2017-06-01 22:10:00 +00:00
|
|
|
} else {
|
2017-06-01 22:12:30 +00:00
|
|
|
console.log('The file has expired, or has already been deleted.');
|
2017-06-01 22:10:00 +00:00
|
|
|
}
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
});
|
|
|
|
UIelem.li.appendChild(btn);
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
let setupUI = () => {
|
2017-06-01 22:12:30 +00:00
|
|
|
let li = document.createElement('li');
|
|
|
|
let name = document.createElement('p');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(name);
|
2017-06-01 22:12:30 +00:00
|
|
|
|
|
|
|
let link = document.createElement('a');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(link);
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let progress = document.createElement('p');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(progress);
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
document.getElementById('uploaded_files').appendChild(li);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
return new UIWrapper(li, name, link, progress);
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let ivToStr = iv => {
|
|
|
|
let hexStr = '';
|
2017-06-01 22:10:00 +00:00
|
|
|
for (let i in iv) {
|
|
|
|
if (iv[i] < 16) {
|
2017-06-01 22:12:30 +00:00
|
|
|
hexStr += '0' + iv[i].toString(16);
|
2017-06-01 22:10:00 +00:00
|
|
|
} else {
|
|
|
|
hexStr += iv[i].toString(16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.hexStr = hexStr;
|
|
|
|
return hexStr;
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let strToIv = str => {
|
2017-06-01 22:10:00 +00:00
|
|
|
let iv = new Uint8Array(16);
|
|
|
|
for (let i = 0; i < str.length; i += 2) {
|
2017-06-01 22:12:30 +00:00
|
|
|
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
2017-06-01 22:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return iv;
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|