2017-06-02 05:59:27 +02:00
|
|
|
const EventEmitter = require('events');
|
2017-06-20 21:18:14 +02:00
|
|
|
const { ivToStr } = require('./utils');
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-06-22 23:50:57 +02:00
|
|
|
const Raven = window.Raven;
|
|
|
|
|
2017-06-02 05:59:27 +02:00
|
|
|
class FileSender extends EventEmitter {
|
|
|
|
constructor(file) {
|
|
|
|
super();
|
|
|
|
this.file = file;
|
|
|
|
this.iv = window.crypto.getRandomValues(new Uint8Array(16));
|
|
|
|
}
|
|
|
|
|
|
|
|
static delete(fileId, token) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!fileId || !token) {
|
2017-06-09 15:45:06 +02:00
|
|
|
return reject();
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
2017-06-09 19:44:12 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-06-02 05:59:27 +02:00
|
|
|
xhr.open('post', '/delete/' + fileId, true);
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xhr.status === 200) {
|
2017-06-21 23:31:21 +02:00
|
|
|
console.log('The file was successfully deleted.');
|
2017-06-02 05:59:27 +02:00
|
|
|
} else {
|
|
|
|
console.log('The file has expired, or has already been deleted.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify({ delete_token: token }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
upload() {
|
|
|
|
return Promise.all([
|
|
|
|
window.crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: 'AES-CBC',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
),
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(this.file);
|
|
|
|
reader.onload = function(event) {
|
|
|
|
resolve(new Uint8Array(this.result));
|
|
|
|
};
|
|
|
|
})
|
|
|
|
])
|
2017-06-09 19:44:12 +02:00
|
|
|
.then(([secretKey, plaintext]) => {
|
|
|
|
return Promise.all([
|
|
|
|
window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-CBC',
|
|
|
|
iv: this.iv
|
|
|
|
},
|
|
|
|
secretKey,
|
|
|
|
plaintext
|
|
|
|
),
|
|
|
|
window.crypto.subtle.exportKey('jwk', secretKey)
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
.then(([encrypted, keydata]) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const file = this.file;
|
|
|
|
const fileId = ivToStr(this.iv);
|
|
|
|
const dataView = new DataView(encrypted);
|
|
|
|
const blob = new Blob([dataView], { type: file.type });
|
|
|
|
const fd = new FormData();
|
|
|
|
fd.append('fname', file.name);
|
|
|
|
fd.append('data', blob, file.name);
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-06-09 19:44:12 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-06-09 19:44:12 +02:00
|
|
|
xhr.upload.addEventListener('progress', e => {
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
const percentComplete = Math.floor(e.loaded / e.total * 100);
|
|
|
|
this.emit('progress', percentComplete);
|
|
|
|
}
|
|
|
|
});
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-06-09 19:44:12 +02:00
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
// uuid field and url field
|
|
|
|
const responseObj = JSON.parse(xhr.responseText);
|
|
|
|
resolve({
|
|
|
|
url: responseObj.url,
|
|
|
|
fileId: fileId,
|
|
|
|
secretKey: keydata.k,
|
|
|
|
deleteToken: responseObj.uuid
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2017-06-02 21:49:56 +02:00
|
|
|
|
2017-06-09 19:44:12 +02:00
|
|
|
xhr.open('post', '/upload/' + fileId, true);
|
|
|
|
xhr.send(fd);
|
|
|
|
});
|
2017-06-22 23:50:57 +02:00
|
|
|
})
|
2017-06-23 21:17:47 +02:00
|
|
|
.catch(err => {
|
|
|
|
Raven.captureException(err);
|
2017-06-22 23:50:57 +02:00
|
|
|
return Promise.reject(err);
|
2017-06-02 05:59:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FileSender;
|