2017-06-02 03:59:27 +00:00
|
|
|
const EventEmitter = require('events');
|
2017-06-28 18:30:14 +00:00
|
|
|
const { arrayToHex } = require('./utils');
|
2017-06-02 03:59:27 +00:00
|
|
|
|
2017-06-22 21:50:57 +00:00
|
|
|
const Raven = window.Raven;
|
|
|
|
|
2017-06-02 03:59:27 +00:00
|
|
|
class FileSender extends EventEmitter {
|
|
|
|
constructor(file) {
|
|
|
|
super();
|
|
|
|
this.file = file;
|
2017-06-20 20:03:04 +00:00
|
|
|
this.iv = window.crypto.getRandomValues(new Uint8Array(12));
|
2017-06-02 03:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static delete(fileId, token) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!fileId || !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();
|
2017-06-02 03:59:27 +00: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 21:31:21 +00:00
|
|
|
console.log('The file was successfully deleted.');
|
2017-06-02 03:59:27 +00:00
|
|
|
} else {
|
|
|
|
console.log('The file has expired, or has already been deleted.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify({ delete_token: token }));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
upload() {
|
2017-07-11 20:30:25 +00:00
|
|
|
const self = this;
|
|
|
|
self.emit('loading', true);
|
2017-06-02 03:59:27 +00:00
|
|
|
return Promise.all([
|
2017-06-29 17:30:08 +00:00
|
|
|
window.crypto.subtle
|
|
|
|
.generateKey(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
length: 128
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
)
|
|
|
|
.catch(err =>
|
|
|
|
console.log('There was an error generating a crypto key')
|
|
|
|
),
|
2017-06-02 03:59:27 +00:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.readAsArrayBuffer(this.file);
|
|
|
|
reader.onload = function(event) {
|
2017-07-11 20:30:25 +00:00
|
|
|
self.emit('loading', false);
|
|
|
|
self.emit('hashing', true);
|
2017-07-10 18:25:03 +00:00
|
|
|
const plaintext = new Uint8Array(this.result);
|
|
|
|
window.crypto.subtle.digest('SHA-256', plaintext).then(hash => {
|
2017-07-11 20:30:25 +00:00
|
|
|
self.emit('hashing', false);
|
|
|
|
self.emit('encrypting', true);
|
2017-07-12 17:53:29 +00:00
|
|
|
resolve({ plaintext: plaintext, hash: new Uint8Array(hash) });
|
|
|
|
});
|
2017-06-02 03:59:27 +00:00
|
|
|
};
|
2017-07-07 14:37:10 +00:00
|
|
|
reader.onerror = function(err) {
|
|
|
|
reject(err);
|
2017-07-06 21:11:24 +00:00
|
|
|
};
|
2017-06-02 03:59:27 +00:00
|
|
|
})
|
|
|
|
])
|
2017-07-10 18:25:03 +00:00
|
|
|
.then(([secretKey, file]) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
return Promise.all([
|
2017-06-29 17:30:08 +00:00
|
|
|
window.crypto.subtle
|
|
|
|
.encrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: this.iv,
|
2017-07-10 18:25:03 +00:00
|
|
|
additionalData: file.hash,
|
2017-07-07 21:59:42 +00:00
|
|
|
tagLength: 128
|
2017-06-29 17:30:08 +00:00
|
|
|
},
|
|
|
|
secretKey,
|
2017-07-10 18:25:03 +00:00
|
|
|
file.plaintext
|
2017-07-12 17:53:29 +00:00
|
|
|
)
|
|
|
|
.then(encrypted => {
|
2017-07-11 20:30:25 +00:00
|
|
|
self.emit('encrypting', false);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(encrypted);
|
2017-07-12 17:53:29 +00:00
|
|
|
});
|
2017-07-11 20:30:25 +00:00
|
|
|
}),
|
2017-07-10 18:25:03 +00:00
|
|
|
window.crypto.subtle.exportKey('jwk', secretKey),
|
2017-07-12 17:53:29 +00:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
resolve(file.hash);
|
|
|
|
})
|
2017-06-09 17:44:12 +00:00
|
|
|
]);
|
|
|
|
})
|
2017-07-10 18:25:03 +00:00
|
|
|
.then(([encrypted, keydata, hash]) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const file = this.file;
|
2017-06-28 18:30:14 +00:00
|
|
|
const fileId = arrayToHex(this.iv);
|
2017-06-09 17:44:12 +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-06-09 17:44:12 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-06-02 03:59:27 +00:00
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
xhr.upload.addEventListener('progress', e => {
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
const percentComplete = Math.floor(e.loaded / e.total * 100);
|
2017-07-11 20:30:25 +00:00
|
|
|
self.emit('progress', percentComplete);
|
2017-06-09 17:44:12 +00:00
|
|
|
}
|
|
|
|
});
|
2017-06-02 03:59:27 +00:00
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
|
|
|
// uuid field and url field
|
|
|
|
const responseObj = JSON.parse(xhr.responseText);
|
|
|
|
resolve({
|
|
|
|
url: responseObj.url,
|
2017-07-10 19:19:20 +00:00
|
|
|
fileId: responseObj.id,
|
2017-06-09 17:44:12 +00:00
|
|
|
secretKey: keydata.k,
|
2017-07-12 16:01:31 +00:00
|
|
|
deleteToken: responseObj.delete
|
2017-06-09 17:44:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2017-06-02 19:49:56 +00:00
|
|
|
|
2017-07-10 19:19:20 +00:00
|
|
|
xhr.open('post', '/upload', true);
|
2017-06-29 17:30:08 +00:00
|
|
|
xhr.setRequestHeader(
|
|
|
|
'X-File-Metadata',
|
|
|
|
JSON.stringify({
|
2017-07-10 18:25:03 +00:00
|
|
|
aad: arrayToHex(hash),
|
2017-07-10 19:45:20 +00:00
|
|
|
id: fileId,
|
2017-06-29 17:30:08 +00:00
|
|
|
filename: file.name
|
|
|
|
})
|
|
|
|
);
|
2017-06-09 17:44:12 +00:00
|
|
|
xhr.send(fd);
|
|
|
|
});
|
2017-06-22 21:50:57 +00:00
|
|
|
})
|
2017-06-23 19:17:47 +00:00
|
|
|
.catch(err => {
|
|
|
|
Raven.captureException(err);
|
2017-06-22 21:50:57 +00:00
|
|
|
return Promise.reject(err);
|
2017-06-02 03:59:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = FileSender;
|