2017-08-05 21:23:58 +02:00
|
|
|
const { Raven } = require('./common');
|
2017-06-02 21:38:05 +02:00
|
|
|
const FileReceiver = require('./fileReceiver');
|
2017-08-05 18:40:57 +02:00
|
|
|
const { notify, gcmCompliant } = require('./utils');
|
2017-07-20 21:50:20 +02:00
|
|
|
const bytes = require('bytes');
|
2017-07-21 21:44:55 +02:00
|
|
|
const Storage = require('./storage');
|
|
|
|
const storage = new Storage(localStorage);
|
2017-08-05 18:40:57 +02:00
|
|
|
const links = require('./links');
|
2017-08-05 21:23:58 +02:00
|
|
|
const metrics = require('./metrics');
|
2017-07-21 22:36:26 +02:00
|
|
|
|
2017-06-08 22:45:28 +02:00
|
|
|
const $ = require('jquery');
|
2017-07-13 16:05:45 +02:00
|
|
|
require('jquery-circle-progress');
|
2017-06-02 00:10:00 +02:00
|
|
|
|
2017-08-04 22:41:06 +02:00
|
|
|
$(() => {
|
2017-08-03 23:07:22 +02:00
|
|
|
gcmCompliant()
|
2017-08-04 22:41:06 +02:00
|
|
|
.then(() => {
|
|
|
|
const $downloadBtn = $('#download-btn');
|
|
|
|
const $dlProgress = $('#dl-progress');
|
|
|
|
const $progressText = $('.progress-text');
|
|
|
|
const $title = $('.title');
|
|
|
|
|
2017-08-03 23:07:22 +02:00
|
|
|
const filename = $('#dl-filename').text();
|
2017-08-05 21:23:58 +02:00
|
|
|
const size = Number($('#dl-size').text());
|
|
|
|
const ttl = Number($('#dl-ttl').text());
|
2017-08-03 23:07:22 +02:00
|
|
|
|
|
|
|
//initiate progress bar
|
2017-08-04 22:41:06 +02:00
|
|
|
$dlProgress.circleProgress({
|
2017-08-03 23:07:22 +02:00
|
|
|
value: 0.0,
|
|
|
|
startAngle: -Math.PI / 2,
|
|
|
|
fill: '#3B9DFF',
|
|
|
|
size: 158,
|
|
|
|
animation: { duration: 300 }
|
|
|
|
});
|
2017-08-04 22:41:06 +02:00
|
|
|
|
|
|
|
const download = () => {
|
2017-08-05 06:08:32 +02:00
|
|
|
// Disable the download button to avoid accidental double clicks.
|
2017-08-04 22:41:06 +02:00
|
|
|
$downloadBtn.attr('disabled', 'disabled');
|
2017-08-05 18:40:57 +02:00
|
|
|
links.setOpenInNewTab(true);
|
|
|
|
|
2017-08-03 23:07:22 +02:00
|
|
|
const fileReceiver = new FileReceiver();
|
|
|
|
|
|
|
|
fileReceiver.on('progress', progress => {
|
|
|
|
window.onunload = function() {
|
2017-08-05 21:23:58 +02:00
|
|
|
metrics.cancelledDownload({ size });
|
2017-08-03 23:07:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$('#download-page-one').attr('hidden', true);
|
|
|
|
$('#download-progress').removeAttr('hidden');
|
|
|
|
const percent = progress[0] / progress[1];
|
|
|
|
// update progress bar
|
2017-08-04 22:41:06 +02:00
|
|
|
$dlProgress.circleProgress('value', percent);
|
2017-08-03 23:07:22 +02:00
|
|
|
$('.percent-number').text(`${Math.floor(percent * 100)}`);
|
2017-08-04 22:41:06 +02:00
|
|
|
$progressText.text(
|
2017-08-03 23:07:22 +02:00
|
|
|
`${filename} (${bytes(progress[0], {
|
|
|
|
decimalPlaces: 1,
|
|
|
|
fixedDecimals: true
|
|
|
|
})} of ${bytes(progress[1], { decimalPlaces: 1 })})`
|
|
|
|
);
|
2017-07-24 07:26:06 +02:00
|
|
|
});
|
2017-07-11 22:30:25 +02:00
|
|
|
|
2017-08-03 23:07:22 +02:00
|
|
|
let downloadEnd;
|
|
|
|
fileReceiver.on('decrypting', isStillDecrypting => {
|
|
|
|
// The file is being decrypted
|
|
|
|
if (isStillDecrypting) {
|
|
|
|
fileReceiver.removeAllListeners('progress');
|
|
|
|
window.onunload = null;
|
|
|
|
document.l10n.formatValue('decryptingFile').then(decryptingFile => {
|
2017-08-04 22:41:06 +02:00
|
|
|
$progressText.text(decryptingFile);
|
2017-08-03 23:07:22 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
downloadEnd = Date.now();
|
|
|
|
}
|
2017-07-24 07:26:06 +02:00
|
|
|
});
|
2017-07-11 22:30:25 +02:00
|
|
|
|
2017-08-03 23:07:22 +02:00
|
|
|
fileReceiver.on('hashing', isStillHashing => {
|
|
|
|
// The file is being hashed to make sure a malicious user hasn't tampered with it
|
|
|
|
if (isStillHashing) {
|
|
|
|
document.l10n.formatValue('verifyingFile').then(verifyingFile => {
|
2017-08-04 22:41:06 +02:00
|
|
|
$progressText.text(verifyingFile);
|
2017-08-03 23:07:22 +02:00
|
|
|
});
|
|
|
|
} else {
|
2017-08-04 22:41:06 +02:00
|
|
|
$progressText.text(' ');
|
2017-08-03 23:07:22 +02:00
|
|
|
document.l10n
|
|
|
|
.formatValues('downloadNotification', 'downloadFinish')
|
|
|
|
.then(translated => {
|
|
|
|
notify(translated[0]);
|
2017-08-04 22:41:06 +02:00
|
|
|
$title.text(translated[1]);
|
2017-08-03 23:07:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-07-21 00:16:00 +02:00
|
|
|
|
2017-08-03 23:07:22 +02:00
|
|
|
const startTime = Date.now();
|
2017-07-21 00:16:00 +02:00
|
|
|
|
2017-08-05 21:23:58 +02:00
|
|
|
metrics.startedDownload({ size, ttl });
|
2017-07-21 00:16:00 +02:00
|
|
|
|
2017-08-03 23:07:22 +02:00
|
|
|
fileReceiver
|
|
|
|
.download()
|
|
|
|
.catch(err => {
|
2017-08-05 21:23:58 +02:00
|
|
|
metrics.stoppedDownload({ size, err });
|
2017-08-03 23:07:22 +02:00
|
|
|
|
|
|
|
if (err.message === 'notfound') {
|
|
|
|
location.reload();
|
|
|
|
} else {
|
|
|
|
document.l10n.formatValue('errorPageHeader').then(translated => {
|
2017-08-04 22:41:06 +02:00
|
|
|
$title.text(translated);
|
2017-08-03 23:07:22 +02:00
|
|
|
});
|
2017-08-04 22:41:06 +02:00
|
|
|
$downloadBtn.attr('hidden', true);
|
2017-08-03 23:07:22 +02:00
|
|
|
$('#expired-img').removeAttr('hidden');
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
})
|
|
|
|
.then(([decrypted, fname]) => {
|
|
|
|
const endTime = Date.now();
|
2017-08-05 21:23:58 +02:00
|
|
|
const time = endTime - startTime;
|
2017-08-03 23:07:22 +02:00
|
|
|
const downloadTime = endTime - downloadEnd;
|
2017-08-05 21:23:58 +02:00
|
|
|
const speed = size / (downloadTime / 1000);
|
|
|
|
storage.totalDownloads += 1;
|
|
|
|
metrics.completedDownload({ size, time, speed });
|
2017-08-03 23:07:22 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
Raven.captureException(err);
|
|
|
|
return Promise.reject(err);
|
2017-08-05 18:40:57 +02:00
|
|
|
})
|
|
|
|
.then(() => links.setOpenInNewTab(false));
|
2017-08-05 21:23:58 +02:00
|
|
|
};
|
2017-08-04 22:41:06 +02:00
|
|
|
|
|
|
|
$downloadBtn.on('click', download);
|
2017-08-03 23:07:22 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2017-08-05 21:23:58 +02:00
|
|
|
metrics.unsupported({ err }).then(() => {
|
2017-08-03 23:07:22 +02:00
|
|
|
location.replace('/unsupported/gcm');
|
2017-06-20 21:52:01 +02:00
|
|
|
});
|
2017-08-03 23:07:22 +02:00
|
|
|
});
|
2017-06-06 23:23:10 +02:00
|
|
|
});
|