fox-send/frontend/src/upload.js

353 lines
10 KiB
JavaScript
Raw Normal View History

2017-06-02 03:59:27 +00:00
const FileSender = require('./fileSender');
const { notify, gcmCompliant } = 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-07-07 14:37:10 +00:00
const Raven = window.Raven;
2017-06-06 21:24:51 +00:00
$(document).ready(function() {
gcmCompliant().catch(err => {
$('#page-one').hide();
2017-07-13 14:05:45 +00:00
$('#unsupported-browser').show();
2017-07-12 17:53:29 +00:00
});
2017-07-12 17:56:04 +00:00
$('#file-upload').change(onUpload);
2017-07-13 14:05:45 +00:00
$('body').on('dragover', allowDrop).on('drop', onUpload);
// reset copy button
2017-06-20 19:23:12 +00:00
const $copyBtn = $('#copy-btn');
$copyBtn.attr('disabled', false);
2017-07-13 14:05:45 +00:00
$('#link').attr('disabled', false);
$copyBtn.html('Copy to Clipboard');
2017-06-06 21:23:10 +00:00
$('#upload-progress').hide();
$('#share-link').hide();
2017-07-06 21:11:24 +00:00
$('#upload-error').hide();
2017-07-13 14:05:45 +00:00
$('#unsupported-browser').hide();
$('#compliance-error').hide();
2017-07-14 21:44:56 +00:00
$('#page-one').show();
2017-07-05 13:50:00 +00:00
if (localStorage.length === 0) {
toggleHeader();
} else {
for (let i = 0; i < localStorage.length; i++) {
const id = localStorage.key(i);
//check if file exists before adding to list
checkExistence(id, true);
}
2017-06-23 16:10:53 +00:00
}
// copy link to clipboard
2017-06-20 19:23:12 +00:00
$copyBtn.click(() => {
2017-06-24 00:05:41 +00:00
const aux = document.createElement('input');
2017-06-06 21:24:51 +00:00
aux.setAttribute('value', $('#link').attr('value'));
2017-06-06 21:23:10 +00:00
document.body.appendChild(aux);
aux.select();
2017-06-06 21:24:51 +00:00
document.execCommand('copy');
2017-06-06 21:23:10 +00:00
document.body.removeChild(aux);
2017-06-20 19:23:12 +00:00
//disable button for 3s
2017-06-20 19:52:01 +00:00
$copyBtn.attr('disabled', true);
2017-07-13 14:05:45 +00:00
$('#link').attr('disabled', true);
2017-07-14 21:44:56 +00:00
$copyBtn.html('<span class="icon-check"></span>');
2017-06-20 19:52:01 +00:00
window.setTimeout(() => {
2017-06-20 19:23:12 +00:00
$copyBtn.attr('disabled', false);
2017-07-13 14:05:45 +00:00
$('#link').attr('disabled', false);
$copyBtn.html('Copy to Clipboard');
2017-06-20 19:23:12 +00:00
}, 3000);
2017-06-06 21:23:10 +00:00
});
2017-07-13 14:05:45 +00: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 15:39:46 +00:00
startAngle: -Math.PI / 2,
2017-07-13 14:05:45 +00:00
fill: '#3B9DFF',
size: 158
});
// link back to home page
2017-06-06 21:24:51 +00:00
$('.send-new').click(() => {
2017-06-06 21:23:10 +00:00
$('#upload-progress').hide();
$('#share-link').hide();
2017-07-06 21:11:24 +00:00
$('#upload-error').hide();
2017-06-20 19:23:12 +00:00
$copyBtn.attr('disabled', false);
2017-07-13 14:05:45 +00:00
$('#link').attr('disabled', false);
$copyBtn.html('Copy to Clipboard');
2017-07-14 21:44:56 +00:00
$('.upload-window').removeClass('ondrag');
$('#page-one').show();
2017-06-06 21:23:10 +00:00
});
2017-07-13 14:05:45 +00:00
//cancel the upload
2017-07-13 15:39:46 +00:00
$('#cancel-upload').click(() => {});
// on file upload by browse or drag & drop
2017-07-12 17:56:04 +00:00
function onUpload(event) {
event.preventDefault();
2017-06-08 20:11:28 +00:00
let file = '';
2017-06-09 17:44:12 +00:00
if (event.type === 'drop') {
2017-07-12 17:56:04 +00:00
file = event.originalEvent.dataTransfer.files[0];
} else {
file = event.target.files[0];
}
2017-07-05 13:50:00 +00:00
const expiration = 24 * 60 * 60 * 1000; //will eventually come from a field
2017-06-20 19:23:12 +00:00
2017-06-23 16:10:53 +00:00
const fileSender = new FileSender(file);
2017-07-13 14:05:45 +00:00
fileSender.on('progress', progress => {
2017-06-23 16:10:53 +00:00
$('#page-one').hide();
2017-07-06 21:11:24 +00:00
$('#upload-error').hide();
2017-07-14 21:44:56 +00:00
$('#upload-progress').show();
2017-07-13 15:39:46 +00:00
const percent = progress[0] / progress[1];
2017-06-23 16:10:53 +00:00
// update progress bar
2017-07-13 15:39:46 +00:00
$('#ul-progress').circleProgress('value', percent);
2017-07-14 21:44:56 +00:00
$('#ul-progress').circleProgress().on('circle-animation-end', function() {
$('.percent-number').html(`${Math.floor(percent * 100)}`);
});
if (progress[1] < 1000000) {
$('.progress-text').html(
`${file.name} (${(progress[0] / 1000).toFixed(
1
)}KB of ${(progress[1] / 1000).toFixed(1)}KB)`
);
} else if (progress[1] < 1000000000) {
2017-07-13 15:39:46 +00:00
$('.progress-text').html(
`${file.name} (${(progress[0] / 1000000).toFixed(
1
)}MB of ${(progress[1] / 1000000).toFixed(1)}MB)`
);
2017-07-13 14:05:45 +00:00
} else {
2017-07-13 15:39:46 +00:00
$('.progress-text').html(
`${file.name} (${(progress[0] / 1000000).toFixed(
1
)}MB of ${(progress[1] / 1000000000).toFixed(1)}GB)`
);
2017-07-13 14:05:45 +00:00
}
2017-06-23 16:10:53 +00:00
});
fileSender.on('loading', isStillLoading => {
// The file is loading into Firefox at this stage
if (isStillLoading) {
2017-07-12 17:53:29 +00:00
console.log('Processing');
} else {
2017-07-12 17:53:29 +00:00
console.log('Finished processing');
}
2017-07-12 17:53:29 +00:00
});
fileSender.on('hashing', isStillHashing => {
// The file is being hashed
if (isStillHashing) {
console.log('Hashing');
} else {
2017-07-12 17:53:29 +00:00
console.log('Finished hashing');
}
2017-07-12 17:53:29 +00:00
});
fileSender.on('encrypting', isStillEncrypting => {
// The file is being encrypted
if (isStillEncrypting) {
console.log('Encrypting');
} else {
2017-07-12 17:53:29 +00:00
console.log('Finished encrypting');
}
2017-07-12 17:53:29 +00:00
});
2017-07-13 15:39:46 +00:00
let t = '';
2017-07-06 21:11:24 +00:00
fileSender
.upload()
.then(info => {
const fileData = {
name: file.name,
fileId: info.fileId,
url: info.url,
secretKey: info.secretKey,
deleteToken: info.deleteToken,
creationDate: new Date(),
expiry: expiration
};
localStorage.setItem(info.fileId, JSON.stringify(fileData));
2017-07-13 14:05:45 +00:00
$('#upload-filename').html('Ready to Send');
2017-07-13 15:39:46 +00:00
t = window.setTimeout(() => {
2017-07-13 14:05:45 +00:00
$('#page-one').hide();
$('#upload-progress').hide();
$('#upload-error').hide();
2017-07-14 21:44:56 +00:00
$('#share-link').show();
2017-07-13 14:05:45 +00:00
}, 2000);
2017-06-23 16:10:53 +00:00
2017-07-06 21:11:24 +00:00
populateFileList(JSON.stringify(fileData));
notify('Your upload has finished.');
})
.catch(err => {
2017-07-07 14:37:10 +00:00
Raven.captureException(err);
console.log(err);
2017-07-06 21:11:24 +00:00
$('#page-one').hide();
2017-07-13 14:05:45 +00:00
$('#upload-progress').hide();
2017-07-06 21:11:24 +00:00
$('#upload-error').show();
2017-07-13 14:05:45 +00:00
window.clearTimeout(t);
2017-07-06 21:11:24 +00:00
});
2017-07-12 17:56:04 +00:00
}
2017-06-23 16:10:53 +00:00
2017-07-12 17:56:04 +00:00
function allowDrop(ev) {
2017-06-23 16:10:53 +00:00
ev.preventDefault();
2017-07-12 17:56:04 +00:00
}
2017-06-23 16:10:53 +00:00
2017-06-29 23:08:57 +00:00
function checkExistence(id, populate) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
2017-06-29 23:11:33 +00:00
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
2017-06-29 23:08:57 +00:00
if (populate) {
populateFileList(localStorage.getItem(id));
}
2017-06-29 23:11:33 +00:00
} else if (xhr.status === 404) {
2017-06-29 23:08:57 +00:00
localStorage.removeItem(id);
}
}
};
xhr.open('get', '/exists/' + id, true);
xhr.send();
}
2017-06-23 16:10:53 +00:00
//update file table with current files in localStorage
function populateFileList(file) {
2017-06-24 00:05:41 +00:00
try {
file = JSON.parse(file);
2017-06-24 00:06:08 +00:00
} catch (e) {
return;
2017-06-24 00:05:41 +00:00
}
2017-06-09 17:44:12 +00:00
const row = document.createElement('tr');
const name = document.createElement('td');
const link = document.createElement('td');
const expiry = document.createElement('td');
const del = document.createElement('td');
const popupDiv = document.createElement('div');
2017-07-13 14:05:45 +00:00
const $popupText = $('<div>', { class: 'popuptext' });
2017-06-09 17:44:12 +00:00
const cellText = document.createTextNode(file.name);
2017-06-01 17:54:17 +00:00
2017-07-13 14:05:45 +00:00
const url = file.url.trim() + `#${file.secretKey}`.trim();
$('#link').attr('value', url);
2017-07-13 15:39:46 +00:00
$('#copy-text').html(
'Copy and share the link to send your file: ' + file.name
);
2017-07-13 14:05:45 +00:00
$popupText.attr('tabindex', '-1');
2017-06-06 21:23:10 +00:00
name.appendChild(cellText);
// create delete button
2017-07-14 21:44:56 +00:00
del.innerHTML = '<span class="icon-cancel-1" title="Delete"></span>';
2017-07-13 14:05:45 +00:00
2017-07-14 21:44:56 +00:00
link.innerHTML = '<span class="icon-docs" title="Copy URL"></span>';
link.style.color = '#0A8DFF';
2017-07-13 14:05:45 +00:00
//copy link to clipboard when icon clicked
$(link).click(function() {
const aux = document.createElement('input');
aux.setAttribute('value', url);
document.body.appendChild(aux);
aux.select();
document.execCommand('copy');
document.body.removeChild(aux);
2017-07-14 21:44:56 +00:00
link.innerHTML = 'Copied!';
window.setTimeout(() => {
link.innerHTML = '<span class="icon-docs" title="Copy URL"></span>';
}, 500);
2017-07-13 14:05:45 +00:00
});
2017-06-30 17:49:49 +00:00
2017-06-29 23:08:57 +00:00
file.creationDate = new Date(file.creationDate);
2017-06-29 23:11:33 +00:00
const future = new Date();
2017-06-29 23:08:57 +00:00
future.setTime(file.creationDate.getTime() + file.expiry);
let countdown = 0;
2017-06-29 23:11:33 +00:00
countdown = future.getTime() - new Date().getTime();
let minutes = Math.floor(countdown / 1000 / 60);
let hours = Math.floor(minutes / 60);
let seconds = Math.floor(countdown / 1000 % 60);
2017-06-29 23:08:57 +00:00
2017-06-30 17:49:49 +00:00
poll();
2017-06-29 23:08:57 +00:00
function poll() {
2017-06-29 23:11:33 +00:00
countdown = future.getTime() - new Date().getTime();
minutes = Math.floor(countdown / 1000 / 60);
hours = Math.floor(minutes / 60);
seconds = Math.floor(countdown / 1000 % 60);
2017-06-30 17:49:49 +00:00
let t;
2017-06-29 23:08:57 +00:00
2017-07-13 14:05:45 +00:00
if (hours >= 1) {
expiry.innerHTML = hours + 'h ' + minutes % 60 + 'm';
2017-06-30 17:49:49 +00:00
t = window.setTimeout(() => {
poll();
}, 60000);
} else if (hours === 0) {
2017-07-13 14:05:45 +00:00
expiry.innerHTML = minutes + 'm ' + seconds + 's';
2017-06-30 17:49:49 +00:00
t = window.setTimeout(() => {
poll();
}, 1000);
}
2017-06-29 23:08:57 +00:00
//remove from list when expired
2017-06-30 17:49:49 +00:00
if (countdown <= 0) {
2017-06-29 23:08:57 +00:00
localStorage.removeItem(file.fileId);
$(expiry).parents('tr').remove();
2017-06-30 17:49:49 +00:00
window.clearTimeout(t);
2017-07-13 14:05:45 +00:00
toggleHeader();
2017-06-29 23:08:57 +00:00
}
}
// create popup
popupDiv.classList.add('popup');
2017-06-08 20:11:28 +00:00
$popupText.html(
2017-07-14 21:44:56 +00:00
'<span class="del-file">Delete</span><span class="nvm" > Nevermind</span>'
2017-06-08 20:11:28 +00:00
);
2017-06-06 21:23:10 +00:00
2017-06-23 16:10:53 +00:00
// delete file
$popupText.find('.del-file').click(e => {
2017-06-23 19:17:47 +00:00
FileSender.delete(file.fileId, file.deleteToken).then(() => {
2017-06-23 16:10:53 +00:00
$(e.target).parents('tr').remove();
localStorage.removeItem(file.fileId);
2017-07-05 13:50:00 +00:00
toggleHeader();
2017-06-23 16:10:53 +00:00
});
});
2017-07-13 15:39:46 +00:00
document.getElementById('delete-file').onclick = () => {
2017-07-13 14:05:45 +00:00
FileSender.delete(file.fileId, file.deleteToken).then(() => {
localStorage.removeItem(file.fileId);
2017-07-14 21:44:56 +00:00
location.reload();
2017-07-13 14:05:45 +00:00
});
};
2017-06-23 16:10:53 +00:00
// add data cells to table row
2017-06-06 21:23:10 +00:00
row.appendChild(name);
row.appendChild(link);
row.appendChild(expiry);
$(popupDiv).append($popupText);
del.appendChild(popupDiv);
row.appendChild(del);
2017-06-06 21:23:10 +00:00
2017-06-23 16:10:53 +00:00
// show popup
2017-07-13 15:39:46 +00:00
del.addEventListener('click', function() {
2017-07-13 14:05:45 +00:00
$popupText.addClass('show');
$popupText.focus();
});
2017-06-23 16:10:53 +00:00
// hide popup
2017-06-23 19:17:47 +00:00
$popupText.find('.nvm').click(function(e) {
2017-06-23 17:35:17 +00:00
e.stopPropagation();
2017-07-13 14:05:45 +00:00
$popupText.removeClass('show');
2017-06-23 17:35:17 +00:00
});
2017-06-23 19:17:47 +00:00
$popupText.click(function(e) {
2017-06-23 17:35:17 +00:00
e.stopPropagation();
});
2017-07-13 14:05:45 +00:00
//close when popup loses focus
$popupText.blur(() => {
$popupText.removeClass('show');
});
2017-06-23 16:10:53 +00:00
$('tbody').append(row); //add row to table
2017-06-09 14:47:12 +00:00
2017-07-05 13:50:00 +00:00
toggleHeader();
}
function toggleHeader() {
//hide table header if empty list
if (document.querySelector('tbody').childNodes.length === 1) {
$('#file-list').hide();
} else {
$('#file-list').show();
}
2017-06-23 16:10:53 +00:00
}
2017-06-06 21:24:51 +00:00
});