send/frontend/src/download.js

109 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-06-02 19:38:05 +00:00
const FileReceiver = require('./fileReceiver');
2017-06-21 20:23:36 +00:00
const { notify } = require('./utils');
2017-06-08 20:45:28 +00:00
const $ = require('jquery');
2017-07-13 14:05:45 +00:00
require('jquery-circle-progress');
2017-06-22 21:50:57 +00:00
const Raven = window.Raven;
2017-06-06 21:24:51 +00:00
$(document).ready(function() {
//link back to homepage
$('.send-new').attr('href', window.location.origin);
2017-07-14 21:44:56 +00:00
const filename = $('#dl-filename').html();
2017-07-13 14:05:45 +00:00
//initiate progress bar
$('#dl-progress').circleProgress({
value: 0.0,
2017-07-13 15:39:46 +00:00
startAngle: -Math.PI / 2,
2017-07-13 14:05:45 +00:00
fill: '#00C8D7',
size: 158,
animation: { duration: 300 }
2017-07-13 14:05:45 +00:00
});
2017-07-12 17:56:04 +00:00
$('#download-btn').click(download);
function download() {
2017-06-06 21:23:10 +00:00
const fileReceiver = new FileReceiver();
2017-07-13 14:05:45 +00:00
fileReceiver.on('progress', progress => {
2017-07-19 19:48:39 +00:00
$('#download-page-one').attr('hidden', true);
$('#download-progress').removeAttr('hidden');
2017-07-13 15:39:46 +00:00
const percent = progress[0] / progress[1];
2017-06-20 19:23:12 +00:00
// update progress bar
2017-07-13 15:39:46 +00:00
$('#dl-progress').circleProgress('value', percent);
$('.percent-number').html(`${Math.floor(percent * 100)}`);
2017-07-14 21:44:56 +00:00
if (progress[1] < 1000000) {
$('.progress-text').html(
2017-07-18 14:50:53 +00:00
`${filename} (${(progress[0] / 1000).toFixed(1)}KB of
2017-07-17 23:22:43 +00:00
${(progress[1] / 1000).toFixed(1)}KB)`
2017-07-14 21:44:56 +00:00
);
} else if (progress[1] < 1000000000) {
$('.progress-text').html(
2017-07-18 14:50:53 +00:00
`${filename} (${(progress[0] / 1000000).toFixed(1)}MB of ${(progress[1] / 1000000).toFixed(1)}MB)`
2017-07-14 21:44:56 +00:00
);
} else {
$('.progress-text').html(
2017-07-18 14:50:53 +00:00
`${filename} (${(progress[0] / 1000000).toFixed(1)}MB of ${(progress[1] / 1000000000).toFixed(1)}GB)`
2017-07-14 21:44:56 +00:00
);
}
2017-06-20 19:23:12 +00:00
//on complete
2017-07-13 14:05:45 +00:00
if (percent === 1) {
2017-06-06 21:23:10 +00:00
fileReceiver.removeAllListeners('progress');
2017-07-19 16:04:27 +00:00
document.l10n.formatValues('downloadNotification', 'downloadFinish')
.then(translated => {
notify(translated[0]);
$('.title').html(translated[1]);
2017-07-19 19:48:39 +00:00
});
2017-06-06 21:23:10 +00:00
}
});
fileReceiver.on('decrypting', isStillDecrypting => {
// The file is being decrypted
if (isStillDecrypting) {
2017-07-12 17:53:29 +00:00
console.log('Decrypting');
} else {
2017-07-12 17:53:29 +00:00
console.log('Done decrypting');
}
2017-07-12 17:53:29 +00:00
});
fileReceiver.on('hashing', isStillHashing => {
// The file is being hashed to make sure a malicious user hasn't tampered with it
if (isStillHashing) {
2017-07-12 17:53:29 +00:00
console.log('Checking file integrity');
} else {
2017-07-12 17:53:29 +00:00
console.log('Integrity check done');
}
2017-07-12 17:53:29 +00:00
});
2017-06-06 21:24:51 +00:00
fileReceiver
2017-06-20 19:52:01 +00:00
.download()
.catch(() => {
2017-07-19 16:04:27 +00:00
document.l10n.formatValue('expiredPageHeader')
.then(translated => {
$('.title').text(translated);
2017-07-19 19:48:39 +00:00
});
$('#download-btn').attr('hidden', true);
$('#expired-img').removeAttr('hidden');
2017-06-20 19:52:01 +00:00
console.log('The file has expired, or has already been deleted.');
2017-06-20 19:23:12 +00:00
return;
2017-06-20 19:52:01 +00:00
})
.then(([decrypted, fname]) => {
const dataView = new DataView(decrypted);
const blob = new Blob([dataView]);
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadUrl;
if (window.navigator.msSaveBlob) {
// if we are in microsoft edge or IE
window.navigator.msSaveBlob(blob, fname);
return;
}
a.download = fname;
document.body.appendChild(a);
a.click();
2017-06-22 21:50:57 +00:00
})
2017-06-23 19:17:47 +00:00
.catch(err => {
2017-06-22 21:50:57 +00:00
Raven.captureException(err);
return Promise.reject(err);
2017-06-20 19:52:01 +00:00
});
2017-07-12 17:56:04 +00:00
}
2017-06-06 21:23:10 +00:00
});