drop.chapril.org-firefoxsend/frontend/src/fileSender.js

145 lines
4.0 KiB
JavaScript
Raw Normal View History

2017-06-02 05:59:27 +02:00
const EventEmitter = require('events');
2017-06-28 20:30:14 +02:00
const { arrayToHex } = 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;
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-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-06-02 05:59:27 +02:00
upload() {
const self = this;
self.emit('loading', true);
2017-06-02 05:59:27 +02:00
return Promise.all([
2017-06-29 19:30:08 +02:00
window.crypto.subtle
.generateKey(
{
name: 'AES-GCM',
length: 128
},
true,
['encrypt', 'decrypt']
),
2017-06-02 05:59:27 +02:00
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsArrayBuffer(this.file);
reader.onload = function(event) {
self.emit('loading', false);
self.emit('hashing', true);
2017-07-10 20:25:03 +02:00
const plaintext = new Uint8Array(this.result);
window.crypto.subtle.digest('SHA-256', plaintext).then(hash => {
self.emit('hashing', false);
self.emit('encrypting', true);
2017-07-12 19:53:29 +02:00
resolve({ plaintext: plaintext, hash: new Uint8Array(hash) });
});
2017-06-02 05:59:27 +02:00
};
2017-07-07 16:37:10 +02:00
reader.onerror = function(err) {
reject(err);
2017-07-06 23:11:24 +02:00
};
2017-06-02 05:59:27 +02:00
})
])
2017-07-10 20:25:03 +02:00
.then(([secretKey, file]) => {
2017-06-09 19:44:12 +02:00
return Promise.all([
2017-06-29 19:30:08 +02:00
window.crypto.subtle
.encrypt(
{
name: 'AES-GCM',
iv: this.iv,
2017-07-10 20:25:03 +02:00
additionalData: file.hash,
tagLength: 128
2017-06-29 19:30:08 +02:00
},
secretKey,
2017-07-10 20:25:03 +02:00
file.plaintext
2017-07-12 19:53:29 +02:00
)
.then(encrypted => {
self.emit('encrypting', false);
return new Promise((resolve, reject) => {
resolve(encrypted);
2017-07-12 19:53:29 +02:00
});
}),
2017-07-10 20:25:03 +02:00
window.crypto.subtle.exportKey('jwk', secretKey),
2017-07-12 19:53:29 +02:00
new Promise((resolve, reject) => {
resolve(file.hash);
})
2017-06-09 19:44:12 +02:00
]);
})
2017-07-10 20:25:03 +02:00
.then(([encrypted, keydata, hash]) => {
2017-06-09 19:44:12 +02:00
return new Promise((resolve, reject) => {
const file = this.file;
2017-06-28 20:30:14 +02:00
const fileId = arrayToHex(this.iv);
2017-06-09 19:44:12 +02: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 05:59:27 +02:00
2017-07-18 19:52:32 +02:00
const xhr = self.uploadXHR;
2017-06-02 05:59:27 +02:00
2017-06-09 19:44:12 +02:00
xhr.upload.addEventListener('progress', e => {
if (e.lengthComputable) {
2017-07-13 16:05:45 +02:00
self.emit('progress', [e.loaded, e.total]);
2017-06-09 19:44:12 +02:00
}
});
2017-06-02 05:59:27 +02:00
2017-06-09 19:44:12 +02:00
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
2017-07-20 21:50:20 +02:00
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);
2017-06-09 19:44:12 +02:00
}
};
xhr.open('post', '/upload', true);
2017-06-29 19:30:08 +02:00
xhr.setRequestHeader(
'X-File-Metadata',
JSON.stringify({
2017-07-10 20:25:03 +02:00
aad: arrayToHex(hash),
2017-07-10 21:45:20 +02:00
id: fileId,
2017-07-13 21:53:15 +02:00
filename: encodeURIComponent(file.name)
2017-06-29 19:30:08 +02:00
})
);
2017-06-09 19:44:12 +02:00
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;