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-06 03:06:43 +02:00
|
|
|
const { bytes, notify, gcmCompliant } = require('./utils');
|
2017-07-21 21:44:55 +02:00
|
|
|
const Storage = require('./storage');
|
2017-08-10 03:16:10 +02:00
|
|
|
const storage = new Storage();
|
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-08-06 03:06:43 +02:00
|
|
|
const progress = require('./progress');
|
2017-06-08 22:45:28 +02:00
|
|
|
const $ = require('jquery');
|
2017-06-02 00:10:00 +02:00
|
|
|
|
2017-08-06 03:06:43 +02:00
|
|
|
function onUnload(size) {
|
|
|
|
metrics.cancelledDownload({ size });
|
|
|
|
}
|
|
|
|
|
|
|
|
function download() {
|
|
|
|
const $downloadBtn = $('#download-btn');
|
|
|
|
const $title = $('.title');
|
|
|
|
const $file = $('#dl-file');
|
|
|
|
const size = Number($file.attr('data-size'));
|
|
|
|
const ttl = Number($file.attr('data-ttl'));
|
|
|
|
const unloadHandler = onUnload.bind(null, size);
|
|
|
|
const startTime = Date.now();
|
|
|
|
const fileReceiver = new FileReceiver();
|
|
|
|
|
|
|
|
$downloadBtn.attr('disabled', 'disabled');
|
|
|
|
$('#download-page-one').attr('hidden', true);
|
|
|
|
$('#download-progress').removeAttr('hidden');
|
|
|
|
metrics.startedDownload({ size, ttl });
|
|
|
|
links.setOpenInNewTab(true);
|
|
|
|
window.addEventListener('unload', unloadHandler);
|
|
|
|
|
|
|
|
fileReceiver.on('progress', data => {
|
|
|
|
progress.setProgress({ complete: data[0], total: data[1] });
|
|
|
|
});
|
|
|
|
|
|
|
|
let downloadEnd;
|
|
|
|
fileReceiver.on('decrypting', () => {
|
|
|
|
downloadEnd = Date.now();
|
|
|
|
window.removeEventListener('unload', unloadHandler);
|
|
|
|
fileReceiver.removeAllListeners('progress');
|
|
|
|
document.l10n.formatValue('decryptingFile').then(progress.setText);
|
|
|
|
});
|
|
|
|
|
|
|
|
fileReceiver
|
|
|
|
.download()
|
|
|
|
.catch(err => {
|
|
|
|
metrics.stoppedDownload({ size, err });
|
2017-07-11 22:30:25 +02:00
|
|
|
|
2017-08-06 03:06:43 +02:00
|
|
|
if (err.message === 'notfound') {
|
|
|
|
location.reload();
|
|
|
|
} else {
|
|
|
|
document.l10n.formatValue('errorPageHeader').then(translated => {
|
|
|
|
$title.text(translated);
|
2017-07-24 07:26:06 +02:00
|
|
|
});
|
2017-08-06 03:06:43 +02:00
|
|
|
$downloadBtn.attr('hidden', true);
|
|
|
|
$('#expired-img').removeAttr('hidden');
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
})
|
2017-08-09 21:56:32 +02:00
|
|
|
.then(([decrypted, file]) => {
|
|
|
|
const fname = file.name;
|
2017-08-06 03:06:43 +02:00
|
|
|
const endTime = Date.now();
|
|
|
|
const time = endTime - startTime;
|
|
|
|
const downloadTime = endTime - downloadEnd;
|
|
|
|
const speed = size / (downloadTime / 1000);
|
|
|
|
storage.totalDownloads += 1;
|
|
|
|
metrics.completedDownload({ size, time, speed });
|
|
|
|
progress.setText(' ');
|
|
|
|
document.l10n
|
|
|
|
.formatValues('downloadNotification', 'downloadFinish')
|
|
|
|
.then(translated => {
|
|
|
|
notify(translated[0]);
|
|
|
|
$title.text(translated[1]);
|
2017-08-03 23:07:22 +02:00
|
|
|
});
|
2017-07-21 00:16:00 +02:00
|
|
|
|
2017-08-06 03:06:43 +02:00
|
|
|
const dataView = new DataView(decrypted);
|
2017-08-09 21:56:32 +02:00
|
|
|
const blob = new Blob([dataView], { type: file.type });
|
2017-08-06 03:06:43 +02:00
|
|
|
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-08-10 03:16:10 +02:00
|
|
|
URL.revokeObjectURL(downloadUrl);
|
2017-08-06 03:06:43 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
Raven.captureException(err);
|
|
|
|
return Promise.reject(err);
|
|
|
|
})
|
|
|
|
.then(() => links.setOpenInNewTab(false));
|
|
|
|
}
|
2017-08-03 23:07:22 +02:00
|
|
|
|
2017-08-06 03:06:43 +02:00
|
|
|
$(() => {
|
|
|
|
const $file = $('#dl-file');
|
|
|
|
const filename = $file.attr('data-filename');
|
|
|
|
const b = Number($file.attr('data-size'));
|
|
|
|
const size = bytes(b);
|
|
|
|
document.l10n
|
|
|
|
.formatValue('downloadFileSize', { size })
|
|
|
|
.then(str => $('#dl-filesize').text(str));
|
|
|
|
document.l10n
|
|
|
|
.formatValue('downloadingPageProgress', { filename, size })
|
|
|
|
.then(str => $('#dl-title').text(str));
|
2017-08-04 22:41:06 +02:00
|
|
|
|
2017-08-06 03:06:43 +02:00
|
|
|
gcmCompliant()
|
|
|
|
.then(() => {
|
|
|
|
$('#download-btn').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
|
|
|
});
|