From f80c0b5b1d4be56a62478c142b71aec27b7dbbd6 Mon Sep 17 00:00:00 2001 From: Abhinav Adduri Date: Fri, 2 Jun 2017 14:11:54 -0700 Subject: [PATCH] changed to handle 404 during download, also removing progress listener once percent completion is 100 to avoid manipulating DOM twice --- frontend/src/download.js | 10 +++++++++- frontend/src/fileReceiver.js | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/frontend/src/download.js b/frontend/src/download.js index 9f233f81..c7a4937b 100644 --- a/frontend/src/download.js +++ b/frontend/src/download.js @@ -15,6 +15,8 @@ let download = () => { progress.innerText = `Progress: ${percentComplete}%`; if (percentComplete === 100) { + fileReceiver.removeAllListeners('progress'); + let finished = document.createElement('p'); finished.innerText = 'Your download has finished.'; li.appendChild(finished); @@ -28,7 +30,13 @@ let download = () => { } }); - fileReceiver.download().then(([decrypted, fname]) => { + fileReceiver.download() + .catch((err) => { + console.log('The file has expired, or has already been deleted.'); + document.getElementById('downloaded_files').removeChild(li); + return; + }) + .then(([decrypted, fname]) => { name.innerText = fname; let dataView = new DataView(decrypted); let blob = new Blob([dataView]); diff --git a/frontend/src/fileReceiver.js b/frontend/src/fileReceiver.js index 5c5ae918..20c1e108 100644 --- a/frontend/src/fileReceiver.js +++ b/frontend/src/fileReceiver.js @@ -20,6 +20,11 @@ class FileReceiver extends EventEmitter { }; xhr.onload = function(e) { + + if (xhr.status === 404) { + reject(new Error('The file has expired, or has already been deleted.')); + } + let blob = new Blob([this.response]); let fileReader = new FileReader(); fileReader.onload = function() { @@ -52,7 +57,8 @@ class FileReceiver extends EventEmitter { true, ['encrypt', 'decrypt'] ) - ]).then(([fdata, key]) => { + ]) + .then(([fdata, key]) => { let salt = this.salt; return Promise.all([ window.crypto.subtle.decrypt(