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

188 lines
5.6 KiB
JavaScript
Raw Normal View History

require('./common');
2017-06-02 21:38:05 +02:00
const FileReceiver = require('./fileReceiver');
const { notify, findMetric, sendEvent } = require('./utils');
2017-07-20 21:50:20 +02:00
const bytes = require('bytes');
const Storage = require('./storage');
const storage = new Storage(localStorage);
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-22 23:50:57 +02:00
const Raven = window.Raven;
2017-07-21 00:16:00 +02:00
2017-06-06 23:24:51 +02:00
$(document).ready(function() {
//link back to homepage
$('.send-new').attr('href', window.location.origin);
2017-07-21 00:16:00 +02:00
$('.send-new').click(function(target) {
target.preventDefault();
sendEvent('recipient', 'restarted', {
cd2: 'completed'
2017-07-22 02:01:26 +02:00
}).then(() => {
location.href = target.currentTarget.href;
});
2017-07-22 02:01:26 +02:00
});
2017-07-21 00:16:00 +02:00
$('.legal-links a, .social-links a, #dl-firefox').click(function(target) {
target.preventDefault();
const metric = findMetric(target.currentTarget.href);
// record exited event by recipient
sendEvent('recipient', 'exited', {
cd3: metric
2017-07-22 02:01:26 +02:00
}).then(() => {
location.href = target.currentTarget.href;
});
2017-07-22 02:01:26 +02:00
});
2017-07-21 00:16:00 +02:00
2017-07-24 07:26:06 +02:00
const filename = $('#dl-filename').text();
const bytelength = Number($('#dl-bytelength').text());
const timeToExpiry = Number($('#dl-ttl').text());
2017-07-14 23:44:56 +02:00
2017-07-13 16:05:45 +02:00
//initiate progress bar
$('#dl-progress').circleProgress({
value: 0.0,
2017-07-13 17:39:46 +02:00
startAngle: -Math.PI / 2,
2017-07-24 07:42:40 +02:00
fill: '#3B9DFF',
size: 158,
animation: { duration: 300 }
2017-07-13 16:05:45 +02:00
});
2017-07-12 19:56:04 +02:00
$('#download-btn').click(download);
function download() {
storage.totalDownloads += 1;
2017-06-06 23:23:10 +02:00
const fileReceiver = new FileReceiver();
const unexpiredFiles = storage.numFiles;
2017-07-13 16:05:45 +02:00
fileReceiver.on('progress', progress => {
2017-07-21 00:16:00 +02:00
window.onunload = function() {
storage.referrer = 'cancelled-download';
2017-07-21 00:16:00 +02:00
// record download-stopped (cancelled by tab close or reload)
2017-07-21 22:25:08 +02:00
sendEvent('recipient', 'download-stopped', {
cm1: bytelength,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd2: 'cancelled'
2017-07-22 02:01:26 +02:00
});
};
2017-07-21 00:16:00 +02:00
2017-07-19 21:48:39 +02:00
$('#download-page-one').attr('hidden', true);
$('#download-progress').removeAttr('hidden');
2017-07-13 17:39:46 +02:00
const percent = progress[0] / progress[1];
2017-06-20 21:23:12 +02:00
// update progress bar
2017-07-13 17:39:46 +02:00
$('#dl-progress').circleProgress('value', percent);
2017-07-24 07:26:06 +02:00
$('.percent-number').text(`${Math.floor(percent * 100)}`);
2017-07-22 02:01:26 +02:00
$('.progress-text').text(
`${filename} (${bytes(progress[0], {
decimalPlaces: 1,
fixedDecimals: true
})} of ${bytes(progress[1], { decimalPlaces: 1 })})`
);
2017-06-06 23:23:10 +02:00
});
2017-07-21 00:16:00 +02:00
let downloadEnd;
fileReceiver.on('decrypting', isStillDecrypting => {
// The file is being decrypted
if (isStillDecrypting) {
2017-07-24 07:26:06 +02:00
fileReceiver.removeAllListeners('progress');
window.onunload = null;
document.l10n.formatValue('decryptingFile').then(decryptingFile => {
$('.progress-text').text(decryptingFile);
});
} else {
downloadEnd = Date.now();
}
2017-07-12 19:53:29 +02: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-24 07:26:06 +02:00
document.l10n.formatValue('verifyingFile').then(verifyingFile => {
$('.progress-text').text(verifyingFile);
});
} else {
2017-07-24 07:26:06 +02:00
$('.progress-text').text(' ');
document.l10n
.formatValues('downloadNotification', 'downloadFinish')
.then(translated => {
notify(translated[0]);
$('.title').text(translated[1]);
});
}
2017-07-12 19:53:29 +02:00
});
const startTime = Date.now();
2017-07-21 00:16:00 +02:00
// record download-started by recipient
2017-07-21 22:25:08 +02:00
sendEvent('recipient', 'download-started', {
cm1: bytelength,
cm4: timeToExpiry,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads
});
2017-07-21 00:16:00 +02:00
2017-06-06 23:24:51 +02:00
fileReceiver
2017-06-20 21:52:01 +02:00
.download()
2017-07-21 00:16:00 +02:00
.catch(err => {
// record download-stopped (errored) by recipient
2017-07-21 22:25:08 +02:00
sendEvent('recipient', 'download-stopped', {
cm1: bytelength,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd2: 'errored',
cd6: err
});
2017-07-21 00:16:00 +02:00
if (err.message === 'notfound') {
location.reload();
} else {
2017-07-25 20:28:33 +02:00
document.l10n.formatValue('errorPageHeader').then(translated => {
$('.title').text(translated);
});
$('#download-btn').attr('hidden', true);
$('#expired-img').removeAttr('hidden');
}
2017-08-03 00:53:52 +02:00
throw err;
2017-06-20 21:52:01 +02:00
})
.then(([decrypted, fname]) => {
const endTime = Date.now();
2017-07-21 00:16:00 +02:00
const totalTime = endTime - startTime;
const downloadTime = endTime - downloadEnd;
const downloadSpeed = bytelength / (downloadTime / 1000);
storage.referrer = 'completed-download';
2017-07-21 00:16:00 +02:00
// record download-stopped (completed) by recipient
2017-07-21 22:25:08 +02:00
sendEvent('recipient', 'download-stopped', {
cm1: bytelength,
cm2: totalTime,
cm3: downloadSpeed,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd2: 'completed'
});
2017-07-21 00:16:00 +02:00
2017-06-20 21:52:01 +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();
2017-06-22 23:50:57 +02:00
})
2017-06-23 21:17:47 +02:00
.catch(err => {
2017-06-22 23:50:57 +02:00
Raven.captureException(err);
return Promise.reject(err);
2017-06-20 21:52:01 +02:00
});
2017-07-12 19:56:04 +02:00
}
2017-06-06 23:23:10 +02:00
});