From 421dd30c9d0bf7b1b886db1788ad6e6227bda728 Mon Sep 17 00:00:00 2001 From: Daniela Arcese Date: Thu, 29 Jun 2017 16:08:57 -0700 Subject: [PATCH 1/3] remove expired uploads --- frontend/src/upload.js | 64 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/frontend/src/upload.js b/frontend/src/upload.js index 6d902b0d..b957ff82 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -15,7 +15,8 @@ $(document).ready(function() { for (let i = 0; i < localStorage.length; i++) { const id = localStorage.key(i); - populateFileList(localStorage.getItem(id)); + //check if file exists before adding to list + checkExistence(id, true); } // copy link to clipboard @@ -54,6 +55,7 @@ $(document).ready(function() { } else { file = event.target.files[0]; } + let expiration = 24*60*60*1000; //will eventually come from a field const fileSender = new FileSender(file); fileSender.on('progress', percentComplete => { @@ -75,7 +77,9 @@ $(document).ready(function() { fileId: info.fileId, url: info.url, secretKey: info.secretKey, - deleteToken: info.deleteToken + deleteToken: info.deleteToken, + creationDate: new Date(), + expiry: expiration }; localStorage.setItem(info.fileId, JSON.stringify(fileData)); @@ -93,6 +97,23 @@ $(document).ready(function() { ev.preventDefault(); }; + function checkExistence(id, populate) { + const xhr = new XMLHttpRequest(); + xhr.onreadystatechange = () => { + if (xhr.readyState === XMLHttpRequest.DONE){ + if (xhr.status == 200) { + if (populate) { + populateFileList(localStorage.getItem(id)); + } + } else if (xhr.status == 404){ + localStorage.removeItem(id); + } + } + }; + xhr.open('get', '/exists/' + id, true); + xhr.send(); + } + //update file table with current files in localStorage function populateFileList(file) { try { @@ -117,7 +138,46 @@ $(document).ready(function() { // create delete button btn.innerHTML = 'x'; btn.classList.add('delete-btn'); + link.innerHTML = file.url.trim() + `#${file.secretKey}`.trim(); + //file.expiry = new Date(file.expiry); + file.creationDate = new Date(file.creationDate); + + let future = new Date(); + future.setTime(file.creationDate.getTime() + file.expiry); + + let countdown = 0; + 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); + + if (hours > 0) { + expiry.innerHTML = hours + 'h'; + window.setInterval(() => { + poll(); + expiry.innerHTML = hours + 'h'; + }, 3600000); + } else if (hours == 0){ + expiry.innerHTML = minutes + 'm' + seconds + 's'; + window.setInterval(() => { + poll(); + expiry.innerHTML = minutes + 'm' + seconds + 's'; + }, 1000); + } + + function poll() { + countdown = future.getTime() - (new Date().getTime()); + minutes = Math.floor(countdown/1000/60); + hours = Math.floor(minutes/60); + seconds = Math.floor((countdown/1000)%60); + + //remove from list when expired + if (hours == 0 && minutes == 0 && seconds == 0){ + localStorage.removeItem(file.fileId); + $(expiry).parents('tr').remove(); + } + } // create popup popupDiv.classList.add('popup'); From 9026702e7b61c0e946309e65c62472db7b24f6c1 Mon Sep 17 00:00:00 2001 From: Daniela Arcese Date: Thu, 29 Jun 2017 16:11:33 -0700 Subject: [PATCH 2/3] lint --- frontend/src/upload.js | 32 ++++++++++++++++---------------- server/log.js | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/frontend/src/upload.js b/frontend/src/upload.js index b957ff82..39a7437c 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -55,7 +55,7 @@ $(document).ready(function() { } else { file = event.target.files[0]; } - let expiration = 24*60*60*1000; //will eventually come from a field + const expiration = 24 * 60 * 60 * 1000; //will eventually come from a field const fileSender = new FileSender(file); fileSender.on('progress', percentComplete => { @@ -100,12 +100,12 @@ $(document).ready(function() { function checkExistence(id, populate) { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = () => { - if (xhr.readyState === XMLHttpRequest.DONE){ - if (xhr.status == 200) { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 200) { if (populate) { populateFileList(localStorage.getItem(id)); } - } else if (xhr.status == 404){ + } else if (xhr.status === 404) { localStorage.removeItem(id); } } @@ -140,17 +140,17 @@ $(document).ready(function() { btn.classList.add('delete-btn'); link.innerHTML = file.url.trim() + `#${file.secretKey}`.trim(); - //file.expiry = new Date(file.expiry); + file.creationDate = new Date(file.creationDate); - let future = new Date(); + const future = new Date(); future.setTime(file.creationDate.getTime() + file.expiry); let countdown = 0; - 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); + 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); if (hours > 0) { expiry.innerHTML = hours + 'h'; @@ -158,7 +158,7 @@ $(document).ready(function() { poll(); expiry.innerHTML = hours + 'h'; }, 3600000); - } else if (hours == 0){ + } else if (hours === 0) { expiry.innerHTML = minutes + 'm' + seconds + 's'; window.setInterval(() => { poll(); @@ -167,13 +167,13 @@ $(document).ready(function() { } function poll() { - countdown = future.getTime() - (new Date().getTime()); - minutes = Math.floor(countdown/1000/60); - hours = Math.floor(minutes/60); - seconds = Math.floor((countdown/1000)%60); + countdown = future.getTime() - new Date().getTime(); + minutes = Math.floor(countdown / 1000 / 60); + hours = Math.floor(minutes / 60); + seconds = Math.floor(countdown / 1000 % 60); //remove from list when expired - if (hours == 0 && minutes == 0 && seconds == 0){ + if (hours === 0 && minutes === 0 && seconds === 0) { localStorage.removeItem(file.fileId); $(expiry).parents('tr').remove(); } diff --git a/server/log.js b/server/log.js index 0042b6bc..27919e8d 100644 --- a/server/log.js +++ b/server/log.js @@ -1,6 +1,6 @@ const conf = require('./config.js'); -const isProduction = conf.env === 'production' +const isProduction = conf.env === 'production'; const mozlog = require('mozlog')({ app: 'FirefoxFileshare', From 1908ce084d6569311cc17212dfa961f3652d9f05 Mon Sep 17 00:00:00 2001 From: Daniela Arcese Date: Fri, 30 Jun 2017 10:49:49 -0700 Subject: [PATCH 3/3] fix polling function --- frontend/src/upload.js | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/frontend/src/upload.js b/frontend/src/upload.js index 39a7437c..ae63be42 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -55,7 +55,7 @@ $(document).ready(function() { } else { file = event.target.files[0]; } - const expiration = 24 * 60 * 60 * 1000; //will eventually come from a field + const expiration = 2 * 60 * 1000; //will eventually come from a field const fileSender = new FileSender(file); fileSender.on('progress', percentComplete => { @@ -140,7 +140,7 @@ $(document).ready(function() { btn.classList.add('delete-btn'); link.innerHTML = file.url.trim() + `#${file.secretKey}`.trim(); - + file.creationDate = new Date(file.creationDate); const future = new Date(); @@ -152,30 +152,36 @@ $(document).ready(function() { let hours = Math.floor(minutes / 60); let seconds = Math.floor(countdown / 1000 % 60); - if (hours > 0) { - expiry.innerHTML = hours + 'h'; - window.setInterval(() => { - poll(); - expiry.innerHTML = hours + 'h'; - }, 3600000); - } else if (hours === 0) { - expiry.innerHTML = minutes + 'm' + seconds + 's'; - window.setInterval(() => { - poll(); - expiry.innerHTML = minutes + 'm' + seconds + 's'; - }, 1000); - } + poll(); function poll() { countdown = future.getTime() - new Date().getTime(); minutes = Math.floor(countdown / 1000 / 60); hours = Math.floor(minutes / 60); seconds = Math.floor(countdown / 1000 % 60); + let t; + if (hours > 1) { + expiry.innerHTML = hours + 'h'; + t = window.setTimeout(() => { + poll(); + }, 3600000); + } else if (hours === 1) { + expiry.innerHTML = hours + 'h'; + t = window.setTimeout(() => { + poll(); + }, 60000); + } else if (hours === 0) { + expiry.innerHTML = minutes + 'm' + seconds + 's'; + t = window.setTimeout(() => { + poll(); + }, 1000); + } //remove from list when expired - if (hours === 0 && minutes === 0 && seconds === 0) { + if (countdown <= 0) { localStorage.removeItem(file.fileId); $(expiry).parents('tr').remove(); + window.clearTimeout(t); } }