2017-06-01 22:12:30 +00:00
|
|
|
const UIWrapper = require('./ui').UIWrapper;
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
let download = () => {
|
|
|
|
let xhr = new XMLHttpRequest();
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
|
|
|
|
xhr.responseType = 'blob';
|
|
|
|
|
2017-06-01 22:10:00 +00:00
|
|
|
let listelem = setupUI();
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.addEventListener('progress', updateProgress.bind(null, listelem));
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
xhr.onload = function(e) {
|
2017-06-01 22:10:00 +00:00
|
|
|
// maybe send a separate request before this one to get the filename?
|
|
|
|
|
|
|
|
// maybe render the html itself with the filename, since it's generated server side
|
|
|
|
// after a get request with the unique id
|
2017-06-01 22:12:30 +00:00
|
|
|
listelem.emit(
|
|
|
|
'name',
|
|
|
|
xhr.getResponseHeader('Content-Disposition').match(/filename="(.+)"/)[1]
|
|
|
|
);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
if (this.status == 200) {
|
|
|
|
let self = this;
|
|
|
|
let blob = new Blob([this.response]);
|
|
|
|
let arrayBuffer;
|
|
|
|
let fileReader = new FileReader();
|
|
|
|
fileReader.onload = function() {
|
|
|
|
arrayBuffer = this.result;
|
|
|
|
let array = new Uint8Array(arrayBuffer);
|
|
|
|
salt = strToIv(location.pathname.slice(10, -1));
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
window.crypto.subtle
|
|
|
|
.importKey(
|
|
|
|
'jwk',
|
|
|
|
{
|
|
|
|
kty: 'oct',
|
2017-06-01 22:10:00 +00:00
|
|
|
k: location.hash.slice(1),
|
2017-06-01 22:12:30 +00:00
|
|
|
alg: 'A128CBC',
|
|
|
|
ext: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'AES-CBC'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
)
|
|
|
|
.then(key => {
|
|
|
|
return window.crypto.subtle.decrypt(
|
2017-06-01 22:10:00 +00:00
|
|
|
{
|
2017-06-01 22:12:30 +00:00
|
|
|
name: 'AES-CBC',
|
|
|
|
iv: salt
|
2017-06-01 22:10:00 +00:00
|
|
|
},
|
|
|
|
key,
|
2017-06-01 22:12:30 +00:00
|
|
|
array
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(decrypted => {
|
|
|
|
let dataView = new DataView(decrypted);
|
|
|
|
let blob = new Blob([dataView]);
|
|
|
|
let downloadUrl = URL.createObjectURL(blob);
|
|
|
|
let a = document.createElement('a');
|
|
|
|
a.href = downloadUrl;
|
|
|
|
a.download = xhr
|
|
|
|
.getResponseHeader('Content-Disposition')
|
|
|
|
.match(/filename="(.+)"/)[1];
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
alert(
|
|
|
|
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
|
|
|
);
|
|
|
|
console.error(err);
|
|
|
|
});
|
2017-06-01 22:10:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fileReader.readAsArrayBuffer(blob);
|
|
|
|
} else {
|
2017-06-01 22:12:30 +00:00
|
|
|
alert(
|
|
|
|
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
|
|
|
);
|
2017-06-01 22:10:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
window.download = download;
|
|
|
|
|
|
|
|
let setupUI = () => {
|
2017-06-01 22:12:30 +00:00
|
|
|
let li = document.createElement('li');
|
|
|
|
let name = document.createElement('p');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(name);
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let progress = document.createElement('p');
|
2017-06-01 22:10:00 +00:00
|
|
|
li.appendChild(progress);
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
document.getElementById('downloaded_files').appendChild(li);
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
return new UIWrapper(li, name, null, progress);
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let ivToStr = iv => {
|
|
|
|
let hexStr = '';
|
2017-06-01 22:10:00 +00:00
|
|
|
for (let i in iv) {
|
|
|
|
if (iv[i] < 16) {
|
2017-06-01 22:12:30 +00:00
|
|
|
hexStr += '0' + iv[i].toString(16);
|
2017-06-01 22:10:00 +00:00
|
|
|
} else {
|
|
|
|
hexStr += iv[i].toString(16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.hexStr = hexStr;
|
|
|
|
return hexStr;
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let strToIv = str => {
|
2017-06-01 22:10:00 +00:00
|
|
|
let iv = new Uint8Array(16);
|
|
|
|
for (let i = 0; i < str.length; i += 2) {
|
2017-06-01 22:12:30 +00:00
|
|
|
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
2017-06-01 22:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return iv;
|
2017-06-01 22:12:30 +00:00
|
|
|
};
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
let updateProgress = (UIelem, e) => {
|
2017-06-01 22:12:30 +00:00
|
|
|
if (e.lengthComputable) {
|
|
|
|
let percentComplete = Math.floor(e.loaded / e.total * 100);
|
|
|
|
UIelem.emit('progress', 'Progress: ' + percentComplete + '%');
|
2017-06-01 22:10:00 +00:00
|
|
|
|
|
|
|
if (percentComplete === 100) {
|
2017-06-01 22:12:30 +00:00
|
|
|
let finished = document.createElement('p');
|
|
|
|
finished.innerText = 'Your download has finished.';
|
2017-06-01 22:10:00 +00:00
|
|
|
UIelem.li.appendChild(finished);
|
|
|
|
|
2017-06-01 22:12:30 +00:00
|
|
|
let close = document.createElement('button');
|
|
|
|
close.innerText = 'Ok';
|
|
|
|
close.addEventListener('click', () => {
|
|
|
|
document.getElementById('downloaded_files').removeChild(UIelem.li);
|
2017-06-01 22:10:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
UIelem.li.appendChild(close);
|
|
|
|
}
|
2017-06-01 22:12:30 +00:00
|
|
|
}
|
|
|
|
};
|