send/frontend/src/download.js

194 lines
5.8 KiB
JavaScript
Raw Normal View History

require('./common');
2017-06-02 19:38:05 +00:00
const FileReceiver = require('./fileReceiver');
const { notify, findMetric, gcmCompliant, sendEvent } = require('./utils');
2017-07-20 19:50:20 +00:00
const bytes = require('bytes');
const Storage = require('./storage');
const storage = new Storage(localStorage);
2017-07-21 20:36:26 +00:00
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-07-20 22:16:00 +00:00
2017-06-06 21:24:51 +00:00
$(document).ready(function() {
gcmCompliant().catch(err => {
$('#download').attr('hidden', true);
sendEvent('recipient', 'unsupported', {
cd6: err
}).then(() => {
location.replace('/unsupported');
});
});
//link back to homepage
$('.send-new').attr('href', window.location.origin);
2017-07-20 22:16:00 +00:00
$('.send-new').click(function(target) {
target.preventDefault();
sendEvent('recipient', 'restarted', {
cd2: 'completed'
2017-07-22 00:01:26 +00:00
}).then(() => {
location.href = target.currentTarget.href;
});
2017-07-22 00:01:26 +00:00
});
2017-07-20 22:16:00 +00: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 00:01:26 +00:00
}).then(() => {
location.href = target.currentTarget.href;
});
2017-07-22 00:01:26 +00:00
});
2017-07-20 22:16:00 +00:00
2017-07-24 05:26:06 +00:00
const filename = $('#dl-filename').text();
const bytelength = Number($('#dl-bytelength').text());
const timeToExpiry = Number($('#dl-ttl').text());
2017-07-14 21:44:56 +00:00
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-24 05:42:40 +00:00
fill: '#3B9DFF',
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() {
storage.totalDownloads += 1;
2017-06-06 21:23:10 +00:00
const fileReceiver = new FileReceiver();
const unexpiredFiles = storage.numFiles;
2017-07-13 14:05:45 +00:00
fileReceiver.on('progress', progress => {
2017-07-20 22:16:00 +00:00
window.onunload = function() {
storage.referrer = 'cancelled-download';
2017-07-20 22:16:00 +00:00
// record download-stopped (cancelled by tab close or reload)
2017-07-21 20:25:08 +00:00
sendEvent('recipient', 'download-stopped', {
cm1: bytelength,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd2: 'cancelled'
2017-07-22 00:01:26 +00:00
});
};
2017-07-20 22:16:00 +00:00
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);
2017-07-24 05:26:06 +00:00
$('.percent-number').text(`${Math.floor(percent * 100)}`);
2017-07-22 00:01:26 +00:00
$('.progress-text').text(
`${filename} (${bytes(progress[0], {
decimalPlaces: 1,
fixedDecimals: true
})} of ${bytes(progress[1], { decimalPlaces: 1 })})`
);
2017-06-06 21:23:10 +00:00
});
2017-07-20 22:16:00 +00:00
let downloadEnd;
fileReceiver.on('decrypting', isStillDecrypting => {
// The file is being decrypted
if (isStillDecrypting) {
2017-07-24 05:26:06 +00:00
fileReceiver.removeAllListeners('progress');
window.onunload = null;
document.l10n.formatValue('decryptingFile').then(decryptingFile => {
$('.progress-text').text(decryptingFile);
});
} else {
2017-07-12 17:53:29 +00:00
console.log('Done decrypting');
downloadEnd = Date.now();
}
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-24 05:26:06 +00:00
document.l10n.formatValue('verifyingFile').then(verifyingFile => {
$('.progress-text').text(verifyingFile);
});
} else {
2017-07-24 05:26:06 +00:00
$('.progress-text').text(' ');
document.l10n
.formatValues('downloadNotification', 'downloadFinish')
.then(translated => {
notify(translated[0]);
$('.title').text(translated[1]);
});
}
2017-07-12 17:53:29 +00:00
});
const startTime = Date.now();
2017-07-20 22:16:00 +00:00
// record download-started by recipient
2017-07-21 20:25:08 +00:00
sendEvent('recipient', 'download-started', {
cm1: bytelength,
cm4: timeToExpiry,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads
});
2017-07-20 22:16:00 +00:00
2017-06-06 21:24:51 +00:00
fileReceiver
2017-06-20 19:52:01 +00:00
.download()
2017-07-20 22:16:00 +00:00
.catch(err => {
// record download-stopped (errored) by recipient
2017-07-21 20:25:08 +00:00
sendEvent('recipient', 'download-stopped', {
cm1: bytelength,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd2: 'errored',
cd6: err
});
2017-07-20 22:16:00 +00:00
2017-07-22 00:01:26 +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 endTime = Date.now();
2017-07-20 22:16:00 +00:00
const totalTime = endTime - startTime;
const downloadTime = endTime - downloadEnd;
const downloadSpeed = bytelength / (downloadTime / 1000);
storage.referrer = 'completed-download';
2017-07-20 22:16:00 +00:00
// record download-stopped (completed) by recipient
2017-07-21 20:25:08 +00:00
sendEvent('recipient', 'download-stopped', {
cm1: bytelength,
cm2: totalTime,
cm3: downloadSpeed,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd2: 'completed'
});
2017-07-20 22:16:00 +00:00
2017-06-20 19:52:01 +00: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 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
});