瀏覽代碼

tus: Save upload URL early on

Renée Kooi 7 年之前
父節點
當前提交
2dff730404
共有 1 個文件被更改,包括 21 次插入0 次删除
  1. 21 0
      src/plugins/Tus10.js

+ 21 - 0
src/plugins/Tus10.js

@@ -131,6 +131,7 @@ module.exports = class Tus10 extends Plugin {
       }
 
       optsTus.onProgress = (bytesUploaded, bytesTotal) => {
+        this.onReceiveUploadUrl(file, upload.url)
         this.core.emitter.emit('core:upload-progress', {
           uploader: this,
           id: file.id,
@@ -249,6 +250,26 @@ module.exports = class Tus10 extends Plugin {
     })
   }
 
+  getFile (fileID) {
+    return this.core.state.files[fileID]
+  }
+
+  onReceiveUploadUrl (file, uploadURL) {
+    const currentFile = this.getFile(file.id)
+    // Only do the update if we didn't have an upload URL yet.
+    if (!currentFile.tus || currentFile.tus.uploadUrl !== uploadURL) {
+      const newFile = Object.assign({}, currentFile, {
+        tus: Object.assign({}, currentFile.tus, {
+          uploadUrl: uploadURL
+        })
+      })
+      const files = Object.assign({}, this.core.state.files, {
+        [currentFile.id]: newFile
+      })
+      this.core.setState({ files })
+    }
+  }
+
   onFileRemove (fileID, cb) {
     this.core.emitter.on('core:file-remove', (targetFileID) => {
       if (fileID === targetFileID) cb()