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

561 lines
17 KiB
JavaScript
Raw Normal View History

/* global MAXFILESIZE EXPIRE_SECONDS */
require('./common');
2017-06-02 05:59:27 +02:00
const FileSender = require('./fileSender');
2017-07-22 02:01:26 +02:00
const {
notify,
gcmCompliant,
findMetric,
sendEvent,
ONE_DAY_IN_MS
} = 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-07-07 16:37:10 +02:00
const Raven = window.Raven;
if (storage.has('referrer')) {
window.referrer = storage.referrer;
storage.remove('referrer');
2017-07-21 00:16:00 +02:00
} else {
window.referrer = 'external';
}
2017-06-06 23:24:51 +02:00
$(document).ready(function() {
gcmCompliant().catch(err => {
2017-07-19 21:48:39 +02:00
$('#page-one').attr('hidden', true);
2017-07-22 02:01:26 +02:00
sendEvent('sender', 'unsupported', {
cd6: err
}).then(() => {
location.replace('/unsupported');
2017-07-22 02:01:26 +02:00
});
2017-07-12 19:53:29 +02:00
});
2017-07-12 19:56:04 +02:00
$('#file-upload').change(onUpload);
$('.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('sender', '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
});
$('#send-new-completed').click(function(target) {
target.preventDefault();
// record restarted event
sendEvent('sender', 'restarted', {
cd2: 'completed'
2017-07-22 02:01:26 +02:00
}).then(() => {
storage.referrer = 'completed-upload';
location.href = target.currentTarget.href;
});
2017-07-22 02:01:26 +02:00
});
$('#send-new-error').click(function(target) {
target.preventDefault();
// record restarted event
sendEvent('sender', 'restarted', {
cd2: 'errored'
2017-07-22 02:01:26 +02:00
}).then(() => {
storage.referrer = 'errored-upload';
location.href = target.currentTarget.href;
});
2017-07-22 02:01:26 +02:00
});
2017-07-13 16:05:45 +02:00
$('body').on('dragover', allowDrop).on('drop', onUpload);
// reset copy button
2017-06-20 21:23:12 +02:00
const $copyBtn = $('#copy-btn');
$copyBtn.attr('disabled', false);
2017-07-13 16:05:45 +02:00
$('#link').attr('disabled', false);
$copyBtn.attr('data-l10n-id', 'copyUrlFormButton');
const files = storage.files;
if (files.length === 0) {
2017-07-05 15:50:00 +02:00
toggleHeader();
} else {
2017-07-27 16:46:28 +02:00
for (let index in files) {
const id = files[index].fileId;
//check if file still exists before adding to list
checkExistence(id, files[index], true);
2017-07-05 15:50:00 +02:00
}
2017-06-23 18:10:53 +02:00
}
// copy link to clipboard
2017-06-20 21:23:12 +02:00
$copyBtn.click(() => {
// record copied event from success screen
sendEvent('sender', 'copied', {
cd4: 'success-screen'
2017-07-21 00:16:00 +02:00
});
2017-06-24 02:05:41 +02:00
const aux = document.createElement('input');
2017-06-06 23:24:51 +02:00
aux.setAttribute('value', $('#link').attr('value'));
2017-06-06 23:23:10 +02:00
document.body.appendChild(aux);
aux.select();
2017-06-06 23:24:51 +02:00
document.execCommand('copy');
2017-06-06 23:23:10 +02:00
document.body.removeChild(aux);
2017-06-20 21:23:12 +02:00
//disable button for 3s
2017-06-20 21:52:01 +02:00
$copyBtn.attr('disabled', true);
2017-07-13 16:05:45 +02:00
$('#link').attr('disabled', true);
2017-07-22 02:01:26 +02:00
$copyBtn.html(
'<img src="/resources/check-16.svg" class="icon-check"></img>'
);
2017-06-20 21:52:01 +02:00
window.setTimeout(() => {
2017-06-20 21:23:12 +02:00
$copyBtn.attr('disabled', false);
2017-07-13 16:05:45 +02:00
$('#link').attr('disabled', false);
$copyBtn.attr('data-l10n-id', 'copyUrlFormButton');
2017-06-20 21:23:12 +02:00
}, 3000);
2017-06-06 23:23:10 +02:00
});
2017-07-13 16:05:45 +02:00
$('.upload-window').on('dragover', () => {
$('.upload-window').addClass('ondrag');
});
$('.upload-window').on('dragleave', () => {
$('.upload-window').removeClass('ondrag');
});
//initiate progress bar
$('#ul-progress').circleProgress({
value: 0.0,
2017-07-13 17:39:46 +02:00
startAngle: -Math.PI / 2,
2017-07-13 16:05:45 +02:00
fill: '#3B9DFF',
2017-07-19 23:11:46 +02:00
size: 158,
animation: { duration: 300 }
2017-07-13 16:05:45 +02:00
});
//link back to homepage
$('.send-new').attr('href', window.location);
2017-07-18 19:52:32 +02:00
// on file upload by browse or drag & drop
2017-07-12 19:56:04 +02:00
function onUpload(event) {
event.preventDefault();
// don't allow upload if not on upload page
2017-07-25 21:08:37 +02:00
if ($('#page-one').attr('hidden')) {
return;
}
storage.totalUploads += 1;
2017-07-21 00:16:00 +02:00
2017-06-08 22:11:28 +02:00
let file = '';
2017-06-09 19:44:12 +02:00
if (event.type === 'drop') {
2017-07-24 21:42:16 +02:00
if (!event.originalEvent.dataTransfer.files[0]) {
$('.upload-window').removeClass('ondrag');
return;
}
2017-07-22 02:01:26 +02:00
if (
event.originalEvent.dataTransfer.files.length > 1 ||
event.originalEvent.dataTransfer.files[0].size === 0
) {
$('.upload-window').removeClass('ondrag');
2017-07-22 02:01:26 +02:00
document.l10n.formatValue('uploadPageMultipleFilesAlert').then(str => {
alert(str);
});
return;
}
2017-07-12 19:56:04 +02:00
file = event.originalEvent.dataTransfer.files[0];
} else {
file = event.target.files[0];
}
2017-07-20 21:50:20 +02:00
if (file.size > MAXFILESIZE) {
2017-07-22 02:01:26 +02:00
return document.l10n
.formatValue('fileTooBig', { size: bytes(MAXFILESIZE) })
.then(alert);
2017-07-20 21:50:20 +02:00
}
$('#page-one').attr('hidden', true);
$('#upload-error').attr('hidden', true);
$('#upload-progress').removeAttr('hidden');
2017-07-24 07:26:06 +02:00
document.l10n.formatValue('importingFile').then(importingFile => {
$('.progress-text').text(importingFile);
});
//don't allow drag and drop when not on page-one
$('body').off('drop', onUpload);
2017-06-20 21:23:12 +02:00
2017-06-23 18:10:53 +02:00
const fileSender = new FileSender(file);
2017-07-18 19:52:32 +02:00
$('#cancel-upload').click(() => {
fileSender.cancel();
storage.referrer = 'cancelled-upload';
2017-07-21 00:16:00 +02:00
// record upload-stopped (cancelled) by sender
2017-07-21 22:25:08 +02:00
sendEvent('sender', 'upload-stopped', {
cm1: file.size,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd1: event.type === 'drop' ? 'drop' : 'click',
cd2: 'cancelled'
});
2017-07-26 20:24:58 +02:00
location.reload();
2017-07-18 19:52:32 +02:00
});
2017-07-13 16:05:45 +02:00
fileSender.on('progress', progress => {
2017-07-13 17:39:46 +02:00
const percent = progress[0] / progress[1];
2017-06-23 18:10:53 +02:00
// update progress bar
2017-07-13 17:39:46 +02:00
$('#ul-progress').circleProgress('value', percent);
2017-07-14 23:44:56 +02:00
$('#ul-progress').circleProgress().on('circle-animation-end', function() {
2017-07-24 07:26:06 +02:00
$('.percent-number').text(`${Math.floor(percent * 100)}`);
2017-07-14 23:44:56 +02:00
});
2017-07-22 02:01:26 +02:00
$('.progress-text').text(
`${file.name} (${bytes(progress[0], {
decimalPlaces: 1,
fixedDecimals: true
})} of ${bytes(progress[1], { decimalPlaces: 1 })})`
);
2017-07-12 19:53:29 +02:00
});
fileSender.on('hashing', isStillHashing => {
// The file is being hashed
if (isStillHashing) {
2017-07-24 07:26:06 +02:00
document.l10n.formatValue('verifyingFile').then(verifyingFile => {
$('.progress-text').text(verifyingFile);
});
} else {
2017-07-12 19:53:29 +02:00
console.log('Finished hashing');
}
2017-07-12 19:53:29 +02:00
});
2017-07-21 00:16:00 +02:00
let uploadStart;
fileSender.on('encrypting', isStillEncrypting => {
// The file is being encrypted
if (isStillEncrypting) {
2017-07-24 07:26:06 +02:00
document.l10n.formatValue('encryptingFile').then(encryptingFile => {
$('.progress-text').text(encryptingFile);
});
} else {
2017-07-12 19:53:29 +02:00
console.log('Finished encrypting');
uploadStart = Date.now();
}
2017-07-12 19:53:29 +02:00
});
2017-07-21 00:16:00 +02:00
let t;
const startTime = Date.now();
const unexpiredFiles = storage.numFiles + 1;
2017-07-21 00:16:00 +02:00
// record upload-started event by sender
2017-07-21 22:25:08 +02:00
sendEvent('sender', 'upload-started', {
cm1: file.size,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd1: event.type === 'drop' ? 'drop' : 'click',
cd5: window.referrer
});
2017-07-21 00:16:00 +02:00
2017-07-24 07:26:06 +02:00
// For large files we need to give the ui a tick to breathe and update
// before we kick off the FileSender
setTimeout(() => {
fileSender
.upload()
.then(info => {
const endTime = Date.now();
const totalTime = endTime - startTime;
const uploadTime = endTime - uploadStart;
const uploadSpeed = file.size / (uploadTime / 1000);
const expiration = EXPIRE_SECONDS * 1000;
2017-07-24 07:26:06 +02:00
// record upload-stopped (completed) by sender
sendEvent('sender', 'upload-stopped', {
cm1: file.size,
cm2: totalTime,
cm3: uploadSpeed,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd1: event.type === 'drop' ? 'drop' : 'click',
cd2: 'completed'
});
const fileData = {
name: file.name,
size: file.size,
fileId: info.fileId,
url: info.url,
secretKey: info.secretKey,
deleteToken: info.deleteToken,
creationDate: new Date(),
expiry: expiration,
totalTime: totalTime,
typeOfUpload: event.type === 'drop' ? 'drop' : 'click',
uploadSpeed: uploadSpeed
};
storage.addFile(info.fileId, fileData);
$('#upload-filename').attr(
'data-l10n-id',
'uploadSuccessConfirmHeader'
);
t = window.setTimeout(() => {
$('#page-one').attr('hidden', true);
$('#upload-progress').attr('hidden', true);
$('#upload-error').attr('hidden', true);
$('#share-link').removeAttr('hidden');
}, 1000);
populateFileList(fileData);
document.l10n.formatValue('notifyUploadDone').then(str => {
notify(str);
});
})
.catch(err => {
2017-07-25 16:35:09 +02:00
// err is 0 when coming from a cancel upload event
if (err === 0) {
return;
}
// only show error page when the error is anything other than user cancelling the upload
2017-07-24 07:26:06 +02:00
Raven.captureException(err);
2017-07-19 21:48:39 +02:00
$('#page-one').attr('hidden', true);
$('#upload-progress').attr('hidden', true);
2017-07-24 07:26:06 +02:00
$('#upload-error').removeAttr('hidden');
window.clearTimeout(t);
// record upload-stopped (errored) by sender
sendEvent('sender', 'upload-stopped', {
cm1: file.size,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd1: event.type === 'drop' ? 'drop' : 'click',
cd2: 'errored',
cd6: err
});
2017-07-21 22:25:08 +02:00
});
2017-07-24 07:26:06 +02:00
}, 10);
2017-07-12 19:56:04 +02:00
}
2017-06-23 18:10:53 +02:00
2017-07-12 19:56:04 +02:00
function allowDrop(ev) {
2017-06-23 18:10:53 +02:00
ev.preventDefault();
2017-07-12 19:56:04 +02:00
}
2017-06-23 18:10:53 +02:00
function checkExistence(id, file, populate) {
2017-06-30 01:08:57 +02:00
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
2017-06-30 01:11:33 +02:00
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
2017-06-30 01:08:57 +02:00
if (populate) {
populateFileList(file);
2017-06-30 01:08:57 +02:00
}
2017-06-30 01:11:33 +02:00
} else if (xhr.status === 404) {
storage.remove(id);
if (storage.numFiles === 0) {
toggleHeader();
}
2017-06-30 01:08:57 +02:00
}
}
};
xhr.open('get', '/exists/' + id, true);
xhr.send();
}
//update file table with current files in storage
function populateFileList(file) {
2017-06-09 19:44:12 +02:00
const row = document.createElement('tr');
const name = document.createElement('td');
const link = document.createElement('td');
2017-07-22 02:01:26 +02:00
const $copyIcon = $('<img>', {
src: '/resources/copy-16.svg',
class: 'icon-copy',
'data-l10n-id': 'copyUrlHover'
});
2017-06-09 19:44:12 +02:00
const expiry = document.createElement('td');
const del = document.createElement('td');
2017-07-22 02:01:26 +02:00
const $delIcon = $('<img>', {
src: '/resources/close-16.svg',
class: 'icon-delete',
'data-l10n-id': 'deleteButtonHover'
});
2017-06-09 19:44:12 +02:00
const popupDiv = document.createElement('div');
2017-07-13 16:05:45 +02:00
const $popupText = $('<div>', { class: 'popuptext' });
2017-06-09 19:44:12 +02:00
const cellText = document.createTextNode(file.name);
2017-06-01 19:54:17 +02:00
2017-07-13 16:05:45 +02:00
const url = file.url.trim() + `#${file.secretKey}`.trim();
2017-07-13 16:05:45 +02:00
$('#link').attr('value', url);
2017-07-22 02:01:26 +02:00
$('#copy-text').attr('data-l10n-args', '{"filename": "' + file.name + '"}');
$('#copy-text').attr('data-l10n-id', 'copyUrlFormLabelWithName');
2017-07-13 16:05:45 +02:00
$popupText.attr('tabindex', '-1');
2017-06-06 23:23:10 +02:00
name.appendChild(cellText);
// create delete button
2017-07-13 16:05:45 +02:00
const delSpan = document.createElement('span');
$(delSpan).addClass('icon-cancel-1');
$(delSpan).attr('data-l10n-id', 'deleteButtonHover');
del.appendChild(delSpan);
const linkSpan = document.createElement('span');
$(linkSpan).addClass('icon-docs');
$(linkSpan).attr('data-l10n-id', 'copyUrlHover');
link.appendChild(linkSpan);
2017-07-14 23:44:56 +02:00
link.style.color = '#0A8DFF';
2017-07-13 16:05:45 +02:00
//copy link to clipboard when icon clicked
$copyIcon.click(function() {
2017-07-21 00:16:00 +02:00
// record copied event from upload list
2017-07-21 22:25:08 +02:00
sendEvent('sender', 'copied', {
cd4: 'upload-list'
});
2017-07-13 16:05:45 +02:00
const aux = document.createElement('input');
aux.setAttribute('value', url);
document.body.appendChild(aux);
aux.select();
document.execCommand('copy');
document.body.removeChild(aux);
2017-07-22 02:01:26 +02:00
document.l10n.formatValue('copiedUrl').then(translated => {
link.innerHTML = translated;
});
2017-07-14 23:44:56 +02:00
window.setTimeout(() => {
const linkImg = document.createElement('img');
$(linkImg).addClass('icon-copy');
$(linkImg).attr('data-l10n-id', 'copyUrlHover');
$(linkImg).attr('src', '/resources/copy-16.svg');
$(link).html(linkImg);
2017-07-14 23:44:56 +02:00
}, 500);
2017-07-13 16:05:45 +02:00
});
2017-06-30 19:49:49 +02:00
2017-06-30 01:08:57 +02:00
file.creationDate = new Date(file.creationDate);
2017-06-30 01:11:33 +02:00
const future = new Date();
2017-06-30 01:08:57 +02:00
future.setTime(file.creationDate.getTime() + file.expiry);
let countdown = 0;
countdown = future.getTime() - Date.now();
2017-06-30 01:11:33 +02:00
let minutes = Math.floor(countdown / 1000 / 60);
let hours = Math.floor(minutes / 60);
let seconds = Math.floor(countdown / 1000 % 60);
2017-06-30 01:08:57 +02:00
2017-06-30 19:49:49 +02:00
poll();
2017-06-30 01:08:57 +02:00
function poll() {
countdown = future.getTime() - Date.now();
2017-06-30 01:11:33 +02:00
minutes = Math.floor(countdown / 1000 / 60);
hours = Math.floor(minutes / 60);
seconds = Math.floor(countdown / 1000 % 60);
2017-06-30 19:49:49 +02:00
let t;
2017-06-30 01:08:57 +02:00
2017-07-13 16:05:45 +02:00
if (hours >= 1) {
expiry.innerHTML = hours + 'h ' + minutes % 60 + 'm';
2017-06-30 19:49:49 +02:00
t = window.setTimeout(() => {
poll();
}, 60000);
} else if (hours === 0) {
2017-07-13 16:05:45 +02:00
expiry.innerHTML = minutes + 'm ' + seconds + 's';
2017-06-30 19:49:49 +02:00
t = window.setTimeout(() => {
poll();
}, 1000);
}
2017-06-30 01:08:57 +02:00
//remove from list when expired
2017-06-30 19:49:49 +02:00
if (countdown <= 0) {
storage.remove(file.fileId);
2017-06-30 01:08:57 +02:00
$(expiry).parents('tr').remove();
2017-06-30 19:49:49 +02:00
window.clearTimeout(t);
2017-07-13 16:05:45 +02:00
toggleHeader();
2017-06-30 01:08:57 +02:00
}
}
// create popup
popupDiv.classList.add('popup');
2017-07-24 21:43:19 +02:00
const $popupMessage = $('<div>', { class: 'popup-message' });
$popupMessage.attr('data-l10n-id', 'deletePopupText');
const $popupDelSpan = $('<span>', { class: 'popup-yes' });
$popupDelSpan.attr('data-l10n-id', 'deletePopupYes');
const $popupNvmSpan = $('<span>', { class: 'popup-no' });
$popupNvmSpan.attr('data-l10n-id', 'deletePopupCancel');
2017-06-06 23:23:10 +02:00
2017-07-25 21:08:37 +02:00
$popupText.html([$popupMessage, $popupDelSpan, $popupNvmSpan]);
2017-06-06 23:23:10 +02:00
// add data cells to table row
row.appendChild(name);
$(link).append($copyIcon);
row.appendChild(link);
row.appendChild(expiry);
$(popupDiv).append($popupText);
$(del).append($delIcon);
del.appendChild(popupDiv);
row.appendChild(del);
$('tbody').append(row); //add row to table
const unexpiredFiles = storage.numFiles;
2017-07-21 00:16:00 +02:00
2017-06-23 18:10:53 +02:00
// delete file
2017-07-24 21:43:19 +02:00
$popupText.find('.popup-yes').click(e => {
2017-06-23 21:17:47 +02:00
FileSender.delete(file.fileId, file.deleteToken).then(() => {
2017-06-23 18:10:53 +02:00
$(e.target).parents('tr').remove();
2017-07-22 02:01:26 +02:00
const timeToExpiry =
ONE_DAY_IN_MS - (Date.now() - file.creationDate.getTime());
2017-07-21 00:16:00 +02:00
// record upload-deleted from file list
2017-07-21 22:25:08 +02:00
sendEvent('sender', 'upload-deleted', {
cm1: file.size,
cm2: file.totalTime,
cm3: file.uploadSpeed,
cm4: timeToExpiry,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd1: file.typeOfUpload,
cd4: 'upload-list'
2017-07-22 02:01:26 +02:00
}).then(() => {
2017-07-21 22:25:08 +02:00
storage.remove(file.fileId);
2017-07-22 02:01:26 +02:00
});
2017-07-05 15:50:00 +02:00
toggleHeader();
2017-06-23 18:10:53 +02:00
});
});
2017-07-21 00:16:00 +02:00
2017-07-13 17:39:46 +02:00
document.getElementById('delete-file').onclick = () => {
2017-07-13 16:05:45 +02:00
FileSender.delete(file.fileId, file.deleteToken).then(() => {
2017-07-22 02:01:26 +02:00
const timeToExpiry =
ONE_DAY_IN_MS - (Date.now() - file.creationDate.getTime());
2017-07-21 00:16:00 +02:00
// record upload-deleted from success screen
2017-07-21 22:25:08 +02:00
sendEvent('sender', 'upload-deleted', {
cm1: file.size,
cm2: file.totalTime,
cm3: file.uploadSpeed,
cm4: timeToExpiry,
cm5: storage.totalUploads,
cm6: unexpiredFiles,
cm7: storage.totalDownloads,
cd1: file.typeOfUpload,
cd4: 'success-screen'
2017-07-22 02:01:26 +02:00
}).then(() => {
2017-07-21 22:25:08 +02:00
storage.remove(file.fileId);
location.reload();
2017-07-22 02:01:26 +02:00
});
2017-07-13 16:05:45 +02:00
});
};
2017-06-23 18:10:53 +02:00
// show popup
$delIcon.click(function() {
2017-07-13 16:05:45 +02:00
$popupText.addClass('show');
$popupText.focus();
});
2017-06-23 18:10:53 +02:00
// hide popup
2017-07-24 21:43:19 +02:00
$popupText.find('.popup-no').click(function(e) {
2017-06-23 19:35:17 +02:00
e.stopPropagation();
2017-07-13 16:05:45 +02:00
$popupText.removeClass('show');
2017-06-23 19:35:17 +02:00
});
2017-06-23 21:17:47 +02:00
$popupText.click(function(e) {
2017-06-23 19:35:17 +02:00
e.stopPropagation();
});
2017-07-13 16:05:45 +02:00
//close when popup loses focus
$popupText.blur(() => {
$popupText.removeClass('show');
});
2017-07-05 15:50:00 +02:00
toggleHeader();
}
function toggleHeader() {
//hide table header if empty list
if (document.querySelector('tbody').childNodes.length === 1) {
2017-07-19 21:48:39 +02:00
$('#file-list').attr('hidden', true);
2017-07-05 15:50:00 +02:00
} else {
2017-07-19 21:48:39 +02:00
$('#file-list').removeAttr('hidden');
2017-07-05 15:50:00 +02:00
}
2017-06-23 18:10:53 +02:00
}
2017-06-06 23:24:51 +02:00
});