drop.chapril.org-firefoxsend/frontend/src/download.js

121 lines
3.6 KiB
JavaScript
Raw Normal View History

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');
const Storage = require('./storage');
const storage = new Storage();
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-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-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-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();
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-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-06 03:06:43 +02:00
gcmCompliant()
.then(() => {
$('#download-btn').on('click', download);
})
.catch(err => {
2017-08-05 21:23:58 +02:00
metrics.unsupported({ err }).then(() => {
location.replace('/unsupported/gcm');
2017-06-20 21:52:01 +02:00
});
});
2017-06-06 23:23:10 +02:00
});