2017-08-10 19:02:13 +02:00
|
|
|
import EventEmitter from 'events';
|
|
|
|
import { arrayToHex } from './utils';
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-08-10 19:02:13 +02:00
|
|
|
export default class FileSender extends EventEmitter {
|
2017-06-02 05:59:27 +02:00
|
|
|
constructor(file) {
|
|
|
|
super();
|
|
|
|
this.file = file;
|
2017-06-20 22:03:04 +02:00
|
|
|
this.iv = window.crypto.getRandomValues(new Uint8Array(12));
|
2017-07-18 19:52:32 +02:00
|
|
|
this.uploadXHR = new XMLHttpRequest();
|
2017-08-14 21:04:03 +02:00
|
|
|
this.key = window.crypto.subtle.generateKey(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt']
|
|
|
|
);
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify({ delete_token: token }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-18 19:52:32 +02:00
|
|
|
cancel() {
|
|
|
|
this.uploadXHR.abort();
|
|
|
|
}
|
|
|
|
|
2017-08-14 21:04:03 +02:00
|
|
|
readFile() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(this.file);
|
|
|
|
reader.onload = function(event) {
|
|
|
|
const plaintext = new Uint8Array(this.result);
|
|
|
|
resolve(plaintext);
|
|
|
|
};
|
|
|
|
reader.onerror = function(err) {
|
|
|
|
reject(err);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-08-14 21:04:03 +02:00
|
|
|
uploadFile(encrypted, keydata) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const file = this.file;
|
|
|
|
const fileId = arrayToHex(this.iv);
|
|
|
|
const dataView = new DataView(encrypted);
|
|
|
|
const blob = new Blob([dataView], { type: file.type });
|
|
|
|
const fd = new FormData();
|
|
|
|
fd.append('data', blob, file.name);
|
2017-06-02 05:59:27 +02:00
|
|
|
|
2017-08-14 21:04:03 +02:00
|
|
|
const xhr = this.uploadXHR;
|
2017-06-02 21:49:56 +02:00
|
|
|
|
2017-08-14 21:04:03 +02:00
|
|
|
xhr.upload.addEventListener('progress', e => {
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
this.emit('progress', [e.loaded, e.total]);
|
|
|
|
}
|
2017-06-02 05:59:27 +02:00
|
|
|
});
|
2017-08-14 21:04:03 +02:00
|
|
|
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
const responseObj = JSON.parse(xhr.responseText);
|
|
|
|
return resolve({
|
|
|
|
url: responseObj.url,
|
|
|
|
fileId: responseObj.id,
|
|
|
|
secretKey: keydata.k,
|
|
|
|
deleteToken: responseObj.delete
|
|
|
|
});
|
|
|
|
}
|
|
|
|
reject(xhr.status);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.open('post', '/upload', true);
|
|
|
|
xhr.setRequestHeader(
|
|
|
|
'X-File-Metadata',
|
|
|
|
JSON.stringify({
|
|
|
|
id: fileId,
|
|
|
|
filename: encodeURIComponent(file.name)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
xhr.send(fd);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async upload() {
|
|
|
|
this.emit('loading');
|
|
|
|
const key = await this.key;
|
|
|
|
const plaintext = await this.readFile();
|
|
|
|
this.emit('encrypting');
|
|
|
|
const encrypted = await window.crypto.subtle.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: this.iv,
|
|
|
|
tagLength: 128
|
|
|
|
},
|
|
|
|
key,
|
|
|
|
plaintext
|
|
|
|
);
|
|
|
|
const keydata = await window.crypto.subtle.exportKey('jwk', key);
|
|
|
|
return this.uploadFile(encrypted, keydata);
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
|
|
|
}
|