mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-23 18:29:09 +00:00
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.
This commit is contained in:
parent
c2a2c5f677
commit
50193b7df3
1 changed files with 50 additions and 5 deletions
|
|
@ -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 () {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue