2017-06-02 19:38:05 +00:00
|
|
|
const EventEmitter = require('events');
|
2017-06-20 21:33:28 +00:00
|
|
|
const { strToIv, strToUintArr } = require('./utils');
|
2017-06-02 19:38:05 +00:00
|
|
|
|
2017-06-22 21:50:57 +00:00
|
|
|
const Raven = window.Raven;
|
|
|
|
|
2017-06-02 19:38:05 +00:00
|
|
|
class FileReceiver extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.salt = strToIv(location.pathname.slice(10, -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
download() {
|
|
|
|
return Promise.all([
|
|
|
|
new Promise((resolve, reject) => {
|
2017-06-09 17:44:12 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-06-02 19:49:56 +00:00
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
xhr.onprogress = event => {
|
|
|
|
if (event.lengthComputable) {
|
|
|
|
const percentComplete = Math.floor(
|
|
|
|
event.loaded / event.total * 100
|
|
|
|
);
|
2017-06-02 19:38:05 +00:00
|
|
|
this.emit('progress', percentComplete);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-09 23:56:25 +00:00
|
|
|
xhr.onload = function(event) {
|
2017-06-02 21:11:54 +00:00
|
|
|
if (xhr.status === 404) {
|
2017-06-06 21:24:51 +00:00
|
|
|
reject(
|
|
|
|
new Error('The file has expired, or has already been deleted.')
|
|
|
|
);
|
2017-06-02 21:54:50 +00:00
|
|
|
return;
|
2017-06-02 21:11:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 17:44:12 +00:00
|
|
|
const blob = new Blob([this.response]);
|
|
|
|
const fileReader = new FileReader();
|
2017-06-02 19:38:05 +00:00
|
|
|
fileReader.onload = function() {
|
|
|
|
resolve({
|
|
|
|
data: this.result,
|
2017-06-20 21:33:28 +00:00
|
|
|
aad: xhr.getResponseHeader('Additional-Data'),
|
2017-06-02 19:49:56 +00:00
|
|
|
fname: xhr
|
|
|
|
.getResponseHeader('Content-Disposition')
|
2017-06-06 17:23:37 +00:00
|
|
|
.match(/=(.+)/)[1]
|
2017-06-02 19:38:05 +00:00
|
|
|
});
|
2017-06-02 19:49:56 +00:00
|
|
|
};
|
2017-06-02 19:38:05 +00:00
|
|
|
|
|
|
|
fileReader.readAsArrayBuffer(blob);
|
2017-06-02 19:49:56 +00:00
|
|
|
};
|
|
|
|
|
2017-06-02 19:38:05 +00:00
|
|
|
xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
|
|
|
|
xhr.responseType = 'blob';
|
|
|
|
xhr.send();
|
|
|
|
}),
|
2017-06-02 19:49:56 +00:00
|
|
|
window.crypto.subtle.importKey(
|
|
|
|
'jwk',
|
|
|
|
{
|
|
|
|
kty: 'oct',
|
|
|
|
k: location.hash.slice(1),
|
2017-06-20 20:03:04 +00:00
|
|
|
alg: 'A256GCM',
|
2017-06-02 19:49:56 +00:00
|
|
|
ext: true
|
|
|
|
},
|
|
|
|
{
|
2017-06-20 20:03:04 +00:00
|
|
|
name: 'AES-GCM'
|
2017-06-02 19:49:56 +00:00
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
)
|
2017-06-20 20:03:04 +00:00
|
|
|
]).then(([fdata, key]) => {
|
|
|
|
const salt = this.salt;
|
2017-06-20 21:33:28 +00:00
|
|
|
console.log(strToUintArr(fdata.aad));
|
|
|
|
|
2017-06-20 20:03:04 +00:00
|
|
|
return Promise.all([
|
|
|
|
window.crypto.subtle.decrypt(
|
|
|
|
{
|
|
|
|
name: 'AES-GCM',
|
|
|
|
iv: salt,
|
2017-06-20 21:33:28 +00:00
|
|
|
tagLength: 128,
|
|
|
|
additionalData: strToUintArr(fdata.aad)
|
2017-06-20 20:03:04 +00:00
|
|
|
},
|
|
|
|
key,
|
|
|
|
fdata.data
|
|
|
|
),
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
resolve(fdata.fname);
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
});
|
2017-06-02 19:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 19:49:56 +00:00
|
|
|
module.exports = FileReceiver;
|