2017-06-02 19:38:05 +00:00
|
|
|
const FileReceiver = require('./fileReceiver');
|
2017-07-20 23:06:06 +00:00
|
|
|
const { notify, findMetric, isFile } = require('./utils');
|
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-01 22:10:00 +00:00
|
|
|
|
2017-06-22 21:50:57 +00:00
|
|
|
const Raven = window.Raven;
|
2017-07-20 22:16:00 +00:00
|
|
|
|
|
|
|
if (!localStorage.hasOwnProperty('totalDownloads')) {
|
|
|
|
localStorage.setItem('totalDownloads', 0);
|
|
|
|
}
|
|
|
|
|
2017-06-06 21:24:51 +00:00
|
|
|
$(document).ready(function() {
|
2017-07-18 21:16:07 +00:00
|
|
|
//link back to homepage
|
|
|
|
$('.send-new').attr('href', window.location.origin);
|
2017-07-20 22:16:00 +00:00
|
|
|
|
|
|
|
if (location.pathname.toString().includes('download')) {
|
|
|
|
$('.send-new').click(function(target) {
|
|
|
|
target.preventDefault();
|
|
|
|
window.analytics
|
|
|
|
.sendEvent('recipient', 'restarted', {
|
|
|
|
cd2: 'completed'
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
location.href = target.currentTarget.href;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
$('.legal-links a, .social-links a, #dl-firefox').click(function(target) {
|
|
|
|
target.preventDefault();
|
2017-07-20 22:20:52 +00:00
|
|
|
const metric = findMetric(target.currentTarget.href);
|
2017-07-20 22:16:00 +00:00
|
|
|
// record exited event by recipient
|
|
|
|
window.analytics
|
|
|
|
.sendEvent('recipient', 'exited', {
|
|
|
|
cd3: metric
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
location.href = target.currentTarget.href;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
$('#expired-send-new').click(function() {
|
|
|
|
localStorage.setItem('referrer', 'errored-download');
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2017-07-18 21:16:07 +00:00
|
|
|
|
2017-07-14 21:44:56 +00:00
|
|
|
const filename = $('#dl-filename').html();
|
2017-07-20 22:16:00 +00:00
|
|
|
const bytelength = $('#dl-bytelength').html();
|
|
|
|
const timeToExpiry = $('#dl-ttl').html();
|
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-13 14:05:45 +00:00
|
|
|
fill: '#00C8D7',
|
2017-07-20 13:25:04 +00:00
|
|
|
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() {
|
2017-07-20 22:20:52 +00:00
|
|
|
const totalDownloads = localStorage.getItem('totalDownloads');
|
2017-07-20 22:16:00 +00:00
|
|
|
localStorage.setItem('totalDownloads', Number(totalDownloads) + 1);
|
|
|
|
|
2017-06-06 21:23:10 +00:00
|
|
|
const fileReceiver = new FileReceiver();
|
2017-07-20 22:16:00 +00:00
|
|
|
let unexpiredFiles = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < localStorage.length; i++) {
|
|
|
|
const id = localStorage.key(i);
|
2017-07-20 23:06:06 +00:00
|
|
|
if (isFile(id)) {
|
2017-07-20 22:16:00 +00:00
|
|
|
unexpiredFiles += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let totalUploads = 0;
|
|
|
|
if (localStorage.hasOwnProperty('totalUploads')) {
|
|
|
|
totalUploads = localStorage.getItem('totalUploads');
|
|
|
|
}
|
2017-06-06 21:23:10 +00:00
|
|
|
|
2017-07-13 14:05:45 +00:00
|
|
|
fileReceiver.on('progress', progress => {
|
2017-07-20 22:16:00 +00:00
|
|
|
|
|
|
|
window.onunload = function() {
|
|
|
|
localStorage.setItem('referrer', 'cancelled-download');
|
|
|
|
// record download-stopped (cancelled by tab close or reload)
|
|
|
|
window.analytics
|
|
|
|
.sendEvent('recipient', 'download-stopped', {
|
|
|
|
cm1: bytelength,
|
|
|
|
cm5: totalUploads,
|
|
|
|
cm6: unexpiredFiles,
|
|
|
|
cm7: localStorage.getItem('totalDownloads'),
|
|
|
|
cd2: 'cancelled'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$('.percent-number').html(`${Math.floor(percent * 100)}`);
|
2017-07-14 21:44:56 +00:00
|
|
|
if (progress[1] < 1000000) {
|
|
|
|
$('.progress-text').html(
|
2017-07-18 14:50:53 +00:00
|
|
|
`${filename} (${(progress[0] / 1000).toFixed(1)}KB of
|
2017-07-17 23:22:43 +00:00
|
|
|
${(progress[1] / 1000).toFixed(1)}KB)`
|
2017-07-14 21:44:56 +00:00
|
|
|
);
|
|
|
|
} else if (progress[1] < 1000000000) {
|
|
|
|
$('.progress-text').html(
|
2017-07-18 14:50:53 +00:00
|
|
|
`${filename} (${(progress[0] / 1000000).toFixed(1)}MB of ${(progress[1] / 1000000).toFixed(1)}MB)`
|
2017-07-14 21:44:56 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$('.progress-text').html(
|
2017-07-18 14:50:53 +00:00
|
|
|
`${filename} (${(progress[0] / 1000000).toFixed(1)}MB of ${(progress[1] / 1000000000).toFixed(1)}GB)`
|
2017-07-14 21:44:56 +00:00
|
|
|
);
|
|
|
|
}
|
2017-06-20 19:23:12 +00:00
|
|
|
//on complete
|
2017-07-13 14:05:45 +00:00
|
|
|
if (percent === 1) {
|
2017-06-06 21:23:10 +00:00
|
|
|
fileReceiver.removeAllListeners('progress');
|
2017-07-19 16:04:27 +00:00
|
|
|
document.l10n.formatValues('downloadNotification', 'downloadFinish')
|
|
|
|
.then(translated => {
|
|
|
|
notify(translated[0]);
|
|
|
|
$('.title').html(translated[1]);
|
2017-07-19 19:48:39 +00:00
|
|
|
});
|
2017-07-20 22:16:00 +00:00
|
|
|
window.onunload = null;
|
2017-06-06 21:23:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-20 22:16:00 +00:00
|
|
|
let downloadEnd;
|
2017-07-11 20:30:25 +00:00
|
|
|
fileReceiver.on('decrypting', isStillDecrypting => {
|
|
|
|
// The file is being decrypted
|
|
|
|
if (isStillDecrypting) {
|
2017-07-12 17:53:29 +00:00
|
|
|
console.log('Decrypting');
|
2017-07-11 20:30:25 +00:00
|
|
|
} else {
|
2017-07-12 17:53:29 +00:00
|
|
|
console.log('Done decrypting');
|
2017-07-20 22:16:00 +00:00
|
|
|
downloadEnd = new Date().getTime();
|
2017-07-11 20:30:25 +00:00
|
|
|
}
|
2017-07-12 17:53:29 +00:00
|
|
|
});
|
2017-07-11 20:30:25 +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-12 17:53:29 +00:00
|
|
|
console.log('Checking file integrity');
|
2017-07-11 20:30:25 +00:00
|
|
|
} else {
|
2017-07-12 17:53:29 +00:00
|
|
|
console.log('Integrity check done');
|
2017-07-11 20:30:25 +00:00
|
|
|
}
|
2017-07-12 17:53:29 +00:00
|
|
|
});
|
2017-07-11 20:30:25 +00:00
|
|
|
|
2017-07-20 22:16:00 +00:00
|
|
|
const startTime = new Date().getTime();
|
|
|
|
|
|
|
|
// record download-started by recipient
|
|
|
|
window.analytics
|
|
|
|
.sendEvent('recipient', 'download-started', {
|
|
|
|
cm1: bytelength,
|
|
|
|
cm4: timeToExpiry,
|
|
|
|
cm5: totalUploads,
|
|
|
|
cm6: unexpiredFiles,
|
|
|
|
cm7: localStorage.getItem('totalDownloads')
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
window.analytics
|
|
|
|
.sendEvent('recipient', 'download-stopped', {
|
|
|
|
cm1: bytelength,
|
|
|
|
cm5: totalUploads,
|
|
|
|
cm6: unexpiredFiles,
|
|
|
|
cm7: localStorage.getItem('totalDownloads'),
|
|
|
|
cd2: 'errored',
|
|
|
|
cd6: err
|
|
|
|
});
|
|
|
|
|
2017-07-19 16:04:27 +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]) => {
|
2017-07-20 22:16:00 +00:00
|
|
|
const endTime = new Date().getTime();
|
|
|
|
const totalTime = endTime - startTime;
|
|
|
|
const downloadTime = endTime - downloadEnd;
|
|
|
|
const downloadSpeed = bytelength / (downloadTime / 1000);
|
|
|
|
|
|
|
|
localStorage.setItem('referrer', 'completed-download');
|
|
|
|
// record download-stopped (completed) by recipient
|
|
|
|
window.analytics
|
|
|
|
.sendEvent('recipient', 'download-stopped', {
|
|
|
|
cm1: bytelength,
|
|
|
|
cm2: totalTime,
|
|
|
|
cm3: downloadSpeed,
|
|
|
|
cm5: totalUploads,
|
|
|
|
cm6: unexpiredFiles,
|
|
|
|
cm7: localStorage.getItem('totalDownloads'),
|
|
|
|
cd2: 'completed'
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|