2017-05-31 18:19:59 +00:00
|
|
|
function download() {
|
|
|
|
var xhr = new XMLHttpRequest();
|
2017-06-01 19:41:29 +00:00
|
|
|
xhr.open('get', '/assets' + location.pathname.slice(0, -1), true);
|
|
|
|
xhr.responseType = 'blob';
|
2017-05-31 18:19:59 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
var li = document.createElement('li');
|
|
|
|
var progress = document.createElement('p');
|
2017-06-01 16:55:47 +00:00
|
|
|
li.appendChild(progress);
|
2017-06-01 19:41:29 +00:00
|
|
|
document.getElementById('downloaded_files').appendChild(li);
|
|
|
|
|
|
|
|
xhr.addEventListener('progress', returnBindedLI(li, progress));
|
2017-06-01 17:05:09 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
xhr.onload = function(e) {
|
2017-06-01 17:05:09 +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 19:41:29 +00:00
|
|
|
var name = document.createElement('p');
|
|
|
|
name.innerHTML = xhr
|
|
|
|
.getResponseHeader('Content-Disposition')
|
|
|
|
.match(/filename="(.+)"/)[1];
|
2017-06-01 17:05:09 +00:00
|
|
|
li.insertBefore(name, li.firstChild);
|
|
|
|
|
2017-05-31 18:19:59 +00:00
|
|
|
if (this.status == 200) {
|
|
|
|
let self = this;
|
|
|
|
var blob = new Blob([this.response]);
|
|
|
|
var arrayBuffer;
|
|
|
|
var fileReader = new FileReader();
|
|
|
|
fileReader.onload = function() {
|
|
|
|
arrayBuffer = this.result;
|
|
|
|
var array = new Uint8Array(arrayBuffer);
|
|
|
|
salt = strToIv(location.pathname.slice(10, -1));
|
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
window.crypto.subtle
|
|
|
|
.importKey(
|
|
|
|
'jwk',
|
|
|
|
{
|
|
|
|
kty: 'oct',
|
2017-05-31 18:19:59 +00:00
|
|
|
k: location.hash.slice(1),
|
2017-06-01 19:41:29 +00:00
|
|
|
alg: 'A128CBC',
|
|
|
|
ext: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'AES-CBC'
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
['encrypt', 'decrypt']
|
|
|
|
)
|
|
|
|
.then(function(key) {
|
|
|
|
window.crypto.subtle
|
|
|
|
.decrypt(
|
2017-05-31 18:19:59 +00:00
|
|
|
{
|
2017-06-01 19:41:29 +00:00
|
|
|
name: 'AES-CBC',
|
|
|
|
iv: salt
|
2017-05-31 18:19:59 +00:00
|
|
|
},
|
|
|
|
key,
|
2017-06-01 19:41:29 +00:00
|
|
|
array
|
|
|
|
)
|
|
|
|
.then(function(decrypted) {
|
|
|
|
var dataView = new DataView(decrypted);
|
|
|
|
var blob = new Blob([dataView]);
|
|
|
|
var downloadUrl = URL.createObjectURL(blob);
|
|
|
|
var a = document.createElement('a');
|
|
|
|
a.href = downloadUrl;
|
|
|
|
a.download = xhr
|
|
|
|
.getResponseHeader('Content-Disposition')
|
|
|
|
.match(/filename="(.+)"/)[1];
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
alert(
|
|
|
|
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
|
|
|
);
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
2017-05-31 18:19:59 +00:00
|
|
|
console.error(err);
|
2017-06-01 19:41:29 +00:00
|
|
|
});
|
2017-05-31 18:19:59 +00:00
|
|
|
};
|
|
|
|
fileReader.readAsArrayBuffer(blob);
|
|
|
|
} else {
|
2017-06-01 19:41:29 +00:00
|
|
|
alert(
|
|
|
|
'This link is either invalid or has expired, or the uploader has deleted the file.'
|
|
|
|
);
|
2017-05-31 18:19:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
function ivToStr(iv) {
|
2017-06-01 19:41:29 +00:00
|
|
|
let hexStr = '';
|
2017-05-31 18:19:59 +00:00
|
|
|
for (var i in iv) {
|
|
|
|
if (iv[i] < 16) {
|
2017-06-01 19:41:29 +00:00
|
|
|
hexStr += '0' + iv[i].toString(16);
|
2017-05-31 18:19:59 +00:00
|
|
|
} else {
|
|
|
|
hexStr += iv[i].toString(16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.hexStr = hexStr;
|
|
|
|
return hexStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function strToIv(str) {
|
|
|
|
var iv = new Uint8Array(16);
|
|
|
|
for (var i = 0; i < str.length; i += 2) {
|
2017-06-01 19:41:29 +00:00
|
|
|
iv[i / 2] = parseInt(str.charAt(i) + str.charAt(i + 1), 16);
|
2017-05-31 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return iv;
|
|
|
|
}
|
|
|
|
|
2017-06-01 16:55:47 +00:00
|
|
|
function returnBindedLI(li, progress) {
|
|
|
|
return function updateProgress(e) {
|
2017-06-01 19:41:29 +00:00
|
|
|
if (e.lengthComputable) {
|
|
|
|
var percentComplete = Math.floor(e.loaded / e.total * 100);
|
|
|
|
progress.innerHTML = 'Progress: ' + percentComplete + '%';
|
|
|
|
}
|
2017-06-01 16:55:47 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
if (percentComplete === 100) {
|
|
|
|
var finished = document.createElement('p');
|
|
|
|
finished.innerHTML = 'Your download has finished.';
|
|
|
|
li.appendChild(finished);
|
2017-06-01 16:55:47 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
var close = document.createElement('button');
|
|
|
|
close.innerHTML = 'Ok';
|
|
|
|
close.addEventListener('click', function() {
|
|
|
|
document.getElementById('downloaded_files').removeChild(li);
|
|
|
|
});
|
2017-06-01 16:55:47 +00:00
|
|
|
|
2017-06-01 19:41:29 +00:00
|
|
|
li.appendChild(close);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|