2017-06-02 19:38:05 +00:00
|
|
|
const EventEmitter = require('events');
|
|
|
|
const { strToIv } = require('./utils');
|
|
|
|
|
|
|
|
class FileReceiver extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.salt = strToIv(location.pathname.slice(10, -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
download() {
|
|
|
|
return Promise.all([
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
let xhr = new XMLHttpRequest();
|
2017-06-02 19:49:56 +00:00
|
|
|
|
2017-06-02 19:38:05 +00:00
|
|
|
xhr.onprogress = e => {
|
|
|
|
if (e.lengthComputable) {
|
|
|
|
let percentComplete = Math.floor(e.loaded / e.total * 100);
|
|
|
|
this.emit('progress', percentComplete);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onload = function(e) {
|
2017-06-02 21:11:54 +00:00
|
|
|
|
|
|
|
if (xhr.status === 404) {
|
|
|
|
reject(new Error('The file has expired, or has already been deleted.'));
|
|
|
|
}
|
|
|
|
|
2017-06-02 19:38:05 +00:00
|
|
|
let blob = new Blob([this.response]);
|
|
|
|
let fileReader = new FileReader();
|
|
|
|
fileReader.onload = function() {
|
|
|
|
resolve({
|
|
|
|
data: this.result,
|
2017-06-02 19:49:56 +00:00
|
|
|
fname: xhr
|
|
|
|
.getResponseHeader('Content-Disposition')
|
|
|
|
.match(/filename="(.+)"/)[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),
|
|
|
|
alg: 'A128CBC',
|
|
|
|
ext: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'AES-CBC'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
)
|
2017-06-02 21:11:54 +00:00
|
|
|
])
|
|
|
|
.then(([fdata, key]) => {
|
2017-06-02 19:49:56 +00:00
|
|
|
let salt = this.salt;
|
|
|
|
return Promise.all([
|
|
|
|
window.crypto.subtle.decrypt(
|
2017-06-02 19:38:05 +00:00
|
|
|
{
|
|
|
|
name: 'AES-CBC',
|
|
|
|
iv: salt
|
2017-06-02 19:49:56 +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;
|