Fix #6
This commit is contained in:
parent
d0f87384c9
commit
f66f268e20
|
@ -31,6 +31,16 @@ function base64ToArrayBuffer(base64) {
|
||||||
return bytes.buffer;
|
return bytes.buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Something's wring
|
||||||
|
function addAlert(msg) {
|
||||||
|
document.getElementById('please-wait').remove();
|
||||||
|
|
||||||
|
var pbd = document.getElementById('pbd');
|
||||||
|
pbd.setAttribute('class', 'alert alert-danger');
|
||||||
|
pbd.setAttribute('role', 'alert');
|
||||||
|
pbd.innerHTML = '<p>'+msg+'</p>';
|
||||||
|
}
|
||||||
|
|
||||||
// Spawn WebSocket
|
// Spawn WebSocket
|
||||||
function spawnWebsocket() {
|
function spawnWebsocket() {
|
||||||
var ws = new WebSocket(ws_url);
|
var ws = new WebSocket(ws_url);
|
||||||
|
@ -47,12 +57,7 @@ function spawnWebsocket() {
|
||||||
var data = JSON.parse(json);
|
var data = JSON.parse(json);
|
||||||
|
|
||||||
if (data.msg !== undefined) {
|
if (data.msg !== undefined) {
|
||||||
document.getElementById('please-wait').remove();
|
addAlert(data.msg);
|
||||||
|
|
||||||
var pbd = document.getElementById('pbd');
|
|
||||||
pbd.setAttribute('class', 'alert alert-danger');
|
|
||||||
pbd.setAttribute('role', 'alert');
|
|
||||||
pbd.innerHTML = '<p>'+data.msg+'</p>';
|
|
||||||
} else {
|
} else {
|
||||||
var slice = JSON.parse(res.shift());
|
var slice = JSON.parse(res.shift());
|
||||||
var percent = Math.round(100 * (data.part + 1)/data.total);
|
var percent = Math.round(100 * (data.part + 1)/data.total);
|
||||||
|
@ -60,23 +65,33 @@ function spawnWebsocket() {
|
||||||
pb.style.width = percent+'%';
|
pb.style.width = percent+'%';
|
||||||
pb.setAttribute('aria-valuenow', percent);
|
pb.setAttribute('aria-valuenow', percent);
|
||||||
document.getElementById('pbt').innerHTML = percent+'%';
|
document.getElementById('pbt').innerHTML = percent+'%';
|
||||||
window.a.push(base64ToArrayBuffer(sjcl.decrypt(window.key, slice)));
|
try {
|
||||||
if (data.part + 1 === data.total) {
|
var b64 = sjcl.decrypt(window.key, slice);
|
||||||
var blob = new File(a, data.name, {type: data.type});
|
window.a.push(base64ToArrayBuffer(b64));
|
||||||
|
if (data.part + 1 === data.total) {
|
||||||
|
var blob = new File(a, data.name, {type: data.type});
|
||||||
|
|
||||||
document.getElementById('please-wait').remove();
|
document.getElementById('please-wait').remove();
|
||||||
|
|
||||||
var pbd = document.getElementById('pbd');
|
var pbd = document.getElementById('pbd');
|
||||||
pbd.setAttribute('class', '');
|
pbd.setAttribute('class', '');
|
||||||
pbd.innerHTML = '<a href="'+URL.createObjectURL(blob)+'" class="btn btn-primary" download="'+data.name+'">'+i18n.download+'</a>';
|
pbd.innerHTML = '<a href="'+URL.createObjectURL(blob)+'" class="btn btn-primary" download="'+data.name+'">'+i18n.download+'</a>';
|
||||||
|
|
||||||
ws.send('{"ended":true}');
|
ws.send('{"ended":true}');
|
||||||
window.onbeforeunload = null;
|
window.onbeforeunload = null;
|
||||||
} else {
|
} else {
|
||||||
if (ws.readyState === 3) {
|
if (ws.readyState === 3) {
|
||||||
ws = spawnWebsocket();
|
ws = spawnWebsocket();
|
||||||
|
}
|
||||||
|
ws.send('{"part":'+(data.part + 1)+'}');
|
||||||
}
|
}
|
||||||
ws.send('{"part":'+(data.part + 1)+'}');
|
} catch(err) {
|
||||||
|
if (err.message === 'ccm: tag doesn\'t match') {
|
||||||
|
addAlert(i18n.badkey);
|
||||||
|
} else {
|
||||||
|
addAlert(err.message);
|
||||||
|
}
|
||||||
|
window.onbeforeunload = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,9 +104,13 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||||
window.a = new Array();
|
window.a = new Array();
|
||||||
window.key = pageKey();
|
window.key = pageKey();
|
||||||
|
|
||||||
// Set websocket
|
if (key !== '=') {
|
||||||
ws = spawnWebsocket();
|
// Set websocket
|
||||||
|
ws = spawnWebsocket();
|
||||||
|
|
||||||
// Prevent exiting page before full download
|
// Prevent exiting page before full download
|
||||||
window.onbeforeunload = confirmExit;
|
window.onbeforeunload = confirmExit;
|
||||||
|
} else {
|
||||||
|
addAlert(i18n.nokey);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -16,8 +16,10 @@
|
||||||
%= javascript begin
|
%= javascript begin
|
||||||
var ws_url = '<%= url_for('download')->to_abs() %>';
|
var ws_url = '<%= url_for('download')->to_abs() %>';
|
||||||
var i18n = {
|
var i18n = {
|
||||||
download: '<%= l('Download') %>',
|
badkey: '<%= l('It seems that the key in your URL is incorrect. Please, verify your URL') %>',
|
||||||
confirmExit: '<%= l('You have attempted to leave this page. The download will be canceled. Are you sure?') %>',
|
confirmExit: '<%= l('You have attempted to leave this page. The download will be canceled. Are you sure?') %>',
|
||||||
|
download: '<%= l('Download') %>',
|
||||||
|
nokey: '<%= l('You don\'t seem to have a key in your URL. You won\'t be able to decrypt the file. Download canceled.') %>',
|
||||||
}
|
}
|
||||||
% end
|
% end
|
||||||
%= javascript '/js/sjcl.js'
|
%= javascript '/js/sjcl.js'
|
||||||
|
|
Loading…
Reference in New Issue