fox-send/app/fileSender.js

147 lines
3.6 KiB
JavaScript
Raw Normal View History

import Nanobus from 'nanobus';
import { arrayToHex, bytes } from './utils';
2017-06-02 03:59:27 +00:00
export default class FileSender extends Nanobus {
2017-06-02 03:59:27 +00:00
constructor(file) {
super('FileSender');
2017-06-02 03:59:27 +00:00
this.file = file;
this.msg = 'importingFile';
this.progress = [0, 1];
this.cancelled = false;
2017-06-20 20:03:04 +00:00
this.iv = window.crypto.getRandomValues(new Uint8Array(12));
2017-07-18 17:52:32 +00:00
this.uploadXHR = new XMLHttpRequest();
2017-08-14 19:04:03 +00:00
this.key = window.crypto.subtle.generateKey(
{
name: 'AES-GCM',
length: 128
},
true,
['encrypt']
);
2017-06-02 03:59:27 +00:00
}
static delete(id, token) {
2017-06-02 03:59:27 +00:00
return new Promise((resolve, reject) => {
if (!id || !token) {
2017-06-09 13:45:06 +00:00
return reject();
2017-06-02 03:59:27 +00:00
}
2017-06-09 17:44:12 +00:00
const xhr = new XMLHttpRequest();
xhr.open('POST', `/api/delete/${id}`);
2017-06-02 03:59:27 +00:00
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
resolve();
}
};
xhr.send(JSON.stringify({ delete_token: token }));
});
}
get progressRatio() {
return this.progress[0] / this.progress[1];
}
get sizes() {
return {
partialSize: bytes(this.progress[0]),
totalSize: bytes(this.progress[1])
};
}
2017-07-18 17:52:32 +00:00
cancel() {
this.cancelled = true;
if (this.msg === 'fileSizeProgress') {
this.uploadXHR.abort();
}
2017-07-18 17:52:32 +00:00
}
2017-08-14 19:04:03 +00: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 03:59:27 +00:00
2017-08-14 19:04:03 +00:00
uploadFile(encrypted, keydata) {
return new Promise((resolve, reject) => {
const file = this.file;
const id = arrayToHex(this.iv);
2017-08-14 19:04:03 +00:00
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 03:59:27 +00:00
2017-08-14 19:04:03 +00:00
const xhr = this.uploadXHR;
2017-08-14 19:04:03 +00:00
xhr.upload.addEventListener('progress', e => {
if (e.lengthComputable) {
this.progress = [e.loaded, e.total];
this.emit('progress', this.progress);
2017-08-14 19:04:03 +00:00
}
2017-06-02 03:59:27 +00:00
});
2017-08-14 19:04:03 +00:00
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
this.progress = [1, 1];
this.msg = 'notifyUploadDone';
2017-08-14 19:04:03 +00:00
const responseObj = JSON.parse(xhr.responseText);
return resolve({
url: responseObj.url,
id: responseObj.id,
2017-08-14 19:04:03 +00:00
secretKey: keydata.k,
deleteToken: responseObj.delete
});
}
this.msg = 'errorPageHeader';
reject(new Error(xhr.status));
2017-08-14 19:04:03 +00:00
}
};
xhr.open('post', '/api/upload', true);
2017-08-14 19:04:03 +00:00
xhr.setRequestHeader(
'X-File-Metadata',
JSON.stringify({
id: id,
2017-08-14 19:04:03 +00:00
filename: encodeURIComponent(file.name)
})
);
xhr.send(fd);
this.msg = 'fileSizeProgress';
2017-08-14 19:04:03 +00:00
});
}
async upload() {
const key = await this.key;
const plaintext = await this.readFile();
if (this.cancelled) {
throw new Error(0);
}
this.msg = 'encryptingFile';
2017-08-14 19:04:03 +00:00
this.emit('encrypting');
const encrypted = await window.crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: this.iv,
tagLength: 128
},
key,
plaintext
);
if (this.cancelled) {
throw new Error(0);
}
2017-08-14 19:04:03 +00:00
const keydata = await window.crypto.subtle.exportKey('jwk', key);
return this.uploadFile(encrypted, keydata);
2017-06-02 03:59:27 +00:00
}
}