From 50193b7df33d90cf682d15cf5aee745ca22bb6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Thu, 21 Sep 2017 14:21:00 +0200 Subject: [PATCH] restore: Omit completed uploads from saved file state This makes sure that if an upload has finished, and the user refreshes the page, the old files won't be sitting there in the completed state. Files that have finished uploading but were part of a "batch" of files that hasn't finished uploading are also kept. --- src/plugins/RestoreFiles/index.js | 55 ++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/src/plugins/RestoreFiles/index.js b/src/plugins/RestoreFiles/index.js index c8cf39258..970d01abe 100644 --- a/src/plugins/RestoreFiles/index.js +++ b/src/plugins/RestoreFiles/index.js @@ -46,12 +46,57 @@ module.exports = class RestoreFiles extends Plugin { } } - saveFilesStateToLocalStorage () { - const files = JSON.stringify({ - currentUploads: this.core.state.currentUploads, - files: this.core.state.files + /** + * Get file objects that are currently waiting: they've been selected, + * but aren't yet being uploaded. + */ + getWaitingFiles () { + const waitingFiles = {} + + const allFiles = this.core.state.files + Object.keys(allFiles).forEach((fileID) => { + const file = this.core.getFile(fileID) + if (!file.progress || !file.progress.uploadStarted) { + waitingFiles[fileID] = file + } }) - localStorage.setItem(`uppyState:${this.core.opts.id}`, files) + + return waitingFiles + } + + /** + * Get file objects that are currently being uploaded. If a file has finished + * uploading, but the other files in the same batch have not, the finished + * file is also returned. + */ + getUploadingFiles () { + const uploadingFiles = {} + + const { currentUploads } = this.core.state + if (currentUploads) { + const uploadIDs = Object.keys(currentUploads) + uploadIDs.forEach((uploadID) => { + const filesInUpload = currentUploads[uploadID].fileIDs + filesInUpload.forEach((fileID) => { + uploadingFiles[fileID] = this.core.getFile(fileID) + }) + }) + } + + return uploadingFiles + } + + saveFilesStateToLocalStorage () { + const filesToSave = Object.assign( + this.getWaitingFiles(), + this.getUploadingFiles() + ) + + const state = JSON.stringify({ + currentUploads: this.core.state.currentUploads, + files: filesToSave + }) + localStorage.setItem(`uppyState:${this.core.opts.id}`, state) } loadFileBlobsFromServiceWorker () {