From 4dadec5ae9fe56955015f0be404c61c2965ecd5c Mon Sep 17 00:00:00 2001 From: Daniela Arcese Date: Fri, 23 Jun 2017 12:10:53 -0400 Subject: [PATCH] fix conflicts --- frontend/src/upload.js | 166 ++++++++++++++++++++++++---------------- server/portal_server.js | 17 ++++ views/index.handlebars | 19 +++-- 3 files changed, 128 insertions(+), 74 deletions(-) diff --git a/frontend/src/upload.js b/frontend/src/upload.js index 984787f4..1ef072e6 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -11,13 +11,18 @@ $(document).ready(function() { $copyBtn.html('Copy'); $('#page-one').show(); - $('#file-list').hide(); + $('#file-list').show(); $('#upload-progress').hide(); $('#share-link').hide(); + for(let i=0; i { - const aux = document.createElement('input'); + var aux = document.createElement('input'); aux.setAttribute('value', $('#link').attr('value')); document.body.appendChild(aux); aux.select(); @@ -52,39 +57,6 @@ $(document).ready(function() { file = event.target.files[0]; } - const $fileList = $('#uploaded-files'); - 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'); - del.setAttribute('align', 'center'); - const btn = document.createElement('button'); - const popupDiv = document.createElement('div'); - const $popupText = $('', { class: 'popuptext' }); - const cellText = document.createTextNode(file.name); - - name.appendChild(cellText); - - // create delete button - btn.innerHTML = 'x'; - btn.classList.add('delete-btn'); - - // create popup - popupDiv.classList.add('popup'); - $popupText.html( - 'Delete Nevermind' - ); - - // add data cells to table row - row.appendChild(name); - row.appendChild(link); - row.appendChild(expiry); - popupDiv.appendChild(btn); - $(popupDiv).append($popupText); - del.appendChild(popupDiv); - row.appendChild(del); - const fileSender = new FileSender(file); fileSender.on('progress', percentComplete => { $('#page-one').hide(); @@ -96,53 +68,111 @@ $(document).ready(function() { .querySelector('#progress-bar') .style.setProperty('--progress', percentComplete + '%'); $('#progress-text').html(`${percentComplete}%`); - if (percentComplete === 100) { - notify('Your upload has finished.'); - } + }); fileSender.upload().then(info => { const url = info.url.trim() + `#${info.secretKey}`.trim(); $('#link').attr('value', url); - link.innerHTML = url; + localStorage.setItem(info.fileId, info.deleteToken); - // delete file - $popupText.find('.del-file').click(e => { - FileSender.delete( - info.fileId, - localStorage.getItem(info.fileId) - ).then(() => { - $(e.target).parents('tr').remove(); - localStorage.removeItem(info.fileId); - }) - .catch(err => { - Raven.captureException(err); - return Promise.reject(err); - }); - }); - - // show popup - del.addEventListener('click', toggleShow); - // hide popup - $popupText.find('.nvm').click(toggleShow); - - $fileList.append(row); //add row to table $('#page-one').hide(); $('#file-list').hide(); $('#upload-progress').hide(); $('#share-link').show(); - }) - .catch(err => { - Raven.captureException(err); - return Promise.reject(err); - }); - function toggleShow() { - $popupText.toggleClass('show'); - } + checkUploads(info.fileId, url); + }); }; window.allowDrop = function(ev) { ev.preventDefault(); }; + + //load previous uploads + function checkUploads(id, url='') { + return new Promise ((resolve, reject) => { + const xhr = new XMLHttpRequest(); + xhr.responseType = 'json'; + xhr.onreadystatechange = () => { + if (xhr.readyState == 4 && xhr.status == 200) { + resolve(xhr.response); + } + else if (xhr.readyState == 4 && xhr.status == 404) { + reject('error code: ' + xhr.status); + } + }; + xhr.onerror = () => { + reject('There was a network error.'); + }; + xhr.open('get', '/file/' + id, true); + xhr.send(); + }).then (response => { + populateFileList(response, url); + }, error => { + console.log(error); + }); + } + + //update file table with current files in localStorage + function populateFileList(file, url) { + console.log(file); + const $fileList = $('#uploaded-files'); + 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'); + del.setAttribute('align', 'center'); + const btn = document.createElement('button'); + const popupDiv = document.createElement('div'); + const $popupText = $('', { class: 'popuptext' }); + const cellText = document.createTextNode(file.name); + const progress = document.createElement('p'); + + name.appendChild(cellText); + + // create delete button + btn.innerHTML = 'x'; + btn.classList.add('delete-btn'); + link.innerHTML = url; + + // create popup + popupDiv.classList.add('popup'); + $popupText.html( + 'Delete Nevermind' + ); + + // delete file + $popupText.find('.del-file').click(e => { + FileSender.delete( + file.fileId, + localStorage.getItem(file.fileId) + ).then(() => { + $(e.target).parents('tr').remove(); + localStorage.removeItem(file.fileId); + }); + }); + + // add data cells to table row + row.appendChild(name); + row.appendChild(link); + row.appendChild(expiry); + popupDiv.appendChild(btn); + $(popupDiv).append($popupText); + del.appendChild(popupDiv); + row.appendChild(del); + + // show popup + del.addEventListener('click', toggleShow); + // hide popup + $popupText.find('.nvm').click(toggleShow); + + $('tbody').append(row); //add row to table + + function toggleShow() { + $popupText.toggleClass('show'); + } + } + }); diff --git a/server/portal_server.js b/server/portal_server.js index 9dc2cf01..6fba066a 100644 --- a/server/portal_server.js +++ b/server/portal_server.js @@ -50,6 +50,23 @@ app.get('/exists/:id', (req, res) => { }).catch(err => res.sendStatus(404)); }); +app.get('/file/:id', (req, res) => { + let id = req.params.id; + storage.filename(id).then(filename => { + storage + .length(id) + .then(contentLength => { + res.json({ + name: filename, + filesize: bytes(contentLength), + fileId: id + }) + }).catch(() => { + console.log('error retrieving id ' + id); + }); + }) +}); + app.get('/download/:id', (req, res) => { const id = req.params.id; storage.filename(id).then(filename => { diff --git a/views/index.handlebars b/views/index.handlebars index ed765aed..4ef0788b 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -38,12 +38,19 @@
- - - - - - + + + + + + + + + + + + +
FileCopy URLExpires inDelete
FileCopy URLExpires inDelete