send/frontend/src/upload.js

157 lines
4 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events');
2017-06-01 22:12:30 +00:00
const UIWrapper = require('./ui').UIWrapper;
2017-06-01 22:12:30 +00:00
let onChange = event => {
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:12:30 +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
let fd = new FormData();
2017-06-01 22:12:30 +00:00
fd.append('fname', file.name);
fd.append('data', blob, file.name);
let xhr = new XMLHttpRequest();
2017-06-01 22:12:30 +00:00
xhr.open('post', '/upload/' + hex, true);
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:12:30 +00:00
xhr.onreadystatechange = () => {
if (xhr.readyState == XMLHttpRequest.DONE) {
2017-06-01 22:12:30 +00:00
window.crypto.subtle.exportKey('jwk', key).then(keydata => {
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
);
});
}
};
xhr.send(fd);
2017-06-01 22:12:30 +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 + '%');
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('/');
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');
if (!localStorage.getItem(key)) return;
2017-06-01 22:12:30 +00:00
xhr.send(JSON.stringify({ delete_token: localStorage.getItem(key) }));
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
2017-06-01 22:12:30 +00:00
document.getElementById('uploaded_files').removeChild(UIelem.li);
localStorage.removeItem(key);
}
if (xhr.status === 200) {
2017-06-01 22:12:30 +00:00
console.log('The file was successfully deleted.');
} else {
2017-06-01 22:12:30 +00:00
console.log('The file has expired, or has already been deleted.');
}
2017-06-01 22:12:30 +00:00
};
});
UIelem.li.appendChild(btn);
}
}
2017-06-01 22:12:30 +00:00
};
let setupUI = () => {
2017-06-01 22:12:30 +00:00
let li = document.createElement('li');
let name = document.createElement('p');
li.appendChild(name);
2017-06-01 22:12:30 +00:00
let link = document.createElement('a');
li.appendChild(link);
2017-06-01 22:12:30 +00:00
let progress = document.createElement('p');
li.appendChild(progress);
2017-06-01 22:12:30 +00:00
document.getElementById('uploaded_files').appendChild(li);
return new UIWrapper(li, name, link, progress);
2017-06-01 22:12:30 +00:00
};
2017-06-01 22:12:30 +00:00
let ivToStr = iv => {
let hexStr = '';
for (let i in iv) {
if (iv[i] < 16) {
2017-06-01 22:12:30 +00:00
hexStr += '0' + iv[i].toString(16);
} else {
hexStr += iv[i].toString(16);
}
}
window.hexStr = hexStr;
return hexStr;
2017-06-01 22:12:30 +00:00
};
2017-06-01 22:12:30 +00:00
let strToIv = str => {
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);
}
return iv;
2017-06-01 22:12:30 +00:00
};