send/frontend/src/upload.js

223 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-06-02 03:59:27 +00:00
const FileSender = require('./fileSender');
const { notify } = require('./utils');
2017-06-08 20:45:28 +00:00
const $ = require('jquery');
2017-06-06 21:24:51 +00:00
$(document).ready(function() {
// reset copy button
2017-06-20 19:23:12 +00:00
const $copyBtn = $('#copy-btn');
$copyBtn.attr('disabled', false);
$copyBtn.html('Copy');
2017-06-06 21:23:10 +00:00
$('#page-one').show();
2017-06-23 16:10:53 +00:00
$('#file-list').show();
2017-06-06 21:23:10 +00:00
$('#upload-progress').hide();
$('#share-link').hide();
2017-06-23 19:17:47 +00:00
for (let i = 0; i < localStorage.length; i++) {
2017-06-24 00:05:41 +00:00
const id = localStorage.key(i);
2017-06-29 23:08:57 +00:00
//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-06-20 19:23:12 +00:00
$copyBtn.html('Copied!');
2017-06-20 19:52:01 +00:00
window.setTimeout(() => {
2017-06-20 19:23:12 +00:00
$copyBtn.attr('disabled', false);
$copyBtn.html('Copy');
}, 3000);
2017-06-06 21:23:10 +00:00
});
// link back to home page
2017-06-06 21:24:51 +00:00
$('.send-new').click(() => {
2017-06-06 21:23:10 +00:00
$('#page-one').show();
$('#file-list').show();
$('#upload-progress').hide();
$('#share-link').hide();
2017-06-20 19:23:12 +00:00
$copyBtn.attr('disabled', false);
$copyBtn.html('Copy');
2017-06-06 21:23:10 +00:00
});
2017-06-01 22:12:30 +00:00
// on file upload by browse or drag & drop
window.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') {
file = event.dataTransfer.files[0];
} else {
file = event.target.files[0];
}
2017-06-29 23:11:33 +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);
fileSender.on('progress', percentComplete => {
$('#page-one').hide();
$('#file-list').hide();
$('#upload-progress').show();
$('#upload-filename').innerHTML += file.name;
// update progress bar
document
.querySelector('#progress-bar')
.style.setProperty('--progress', percentComplete + '%');
$('#progress-text').html(`${percentComplete}%`);
});
fileSender.upload().then(info => {
const url = info.url.trim() + `#${info.secretKey}`.trim();
$('#link').attr('value', url);
const fileData = {
name: file.name,
fileId: info.fileId,
url: info.url,
secretKey: info.secretKey,
2017-06-29 23:08:57 +00:00
deleteToken: info.deleteToken,
creationDate: new Date(),
expiry: expiration
};
localStorage.setItem(info.fileId, JSON.stringify(fileData));
2017-06-23 16:10:53 +00:00
$('#page-one').hide();
$('#file-list').hide();
$('#upload-progress').hide();
$('#share-link').show();
populateFileList(JSON.stringify(fileData));
2017-06-24 00:05:41 +00:00
notify('Your upload has finished.');
2017-06-23 16:10:53 +00:00
});
};
window.allowDrop = function(ev) {
ev.preventDefault();
};
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');
2017-06-20 19:23:12 +00:00
del.setAttribute('align', 'center');
2017-06-09 17:44:12 +00:00
const btn = document.createElement('button');
const popupDiv = document.createElement('div');
const $popupText = $('<span>', { class: 'popuptext' });
const cellText = document.createTextNode(file.name);
2017-06-01 17:54:17 +00:00
2017-06-06 21:23:10 +00:00
name.appendChild(cellText);
// create delete button
btn.innerHTML = 'x';
btn.classList.add('delete-btn');
2017-06-29 23:08:57 +00:00
2017-06-23 19:17:47 +00:00
link.innerHTML = file.url.trim() + `#${file.secretKey}`.trim();
2017-06-29 23:11:33 +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
if (hours > 0) {
expiry.innerHTML = hours + 'h';
window.setInterval(() => {
poll();
expiry.innerHTML = hours + 'h';
}, 3600000);
2017-06-29 23:11:33 +00:00
} else if (hours === 0) {
2017-06-29 23:08:57 +00:00
expiry.innerHTML = minutes + 'm' + seconds + 's';
window.setInterval(() => {
poll();
expiry.innerHTML = minutes + 'm' + seconds + 's';
}, 1000);
}
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-29 23:08:57 +00:00
//remove from list when expired
2017-06-29 23:11:33 +00:00
if (hours === 0 && minutes === 0 && seconds === 0) {
2017-06-29 23:08:57 +00:00
localStorage.removeItem(file.fileId);
$(expiry).parents('tr').remove();
}
}
// create popup
popupDiv.classList.add('popup');
2017-06-08 20:11:28 +00:00
$popupText.html(
2017-06-09 17:44:12 +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);
});
});
// add data cells to table row
2017-06-06 21:23:10 +00:00
row.appendChild(name);
row.appendChild(link);
row.appendChild(expiry);
popupDiv.appendChild(btn);
$(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
del.addEventListener('click', toggleShow);
// 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();
toggleShow();
});
2017-06-23 19:17:47 +00:00
$popupText.click(function(e) {
2017-06-23 17:35:17 +00:00
e.stopPropagation();
});
2017-06-20 19:23:12 +00:00
2017-06-23 16:10:53 +00:00
$('tbody').append(row); //add row to table
2017-06-09 14:47:12 +00:00
2017-06-09 17:44:12 +00:00
function toggleShow() {
2017-06-09 13:45:06 +00:00
$popupText.toggleClass('show');
}
2017-06-23 16:10:53 +00:00
}
2017-06-06 21:24:51 +00:00
});