Ver Fonte

Merge pull request #263 from goto-bus-stop/feature/get-file

core: Add `getFile` method.
Artur Paikin há 7 anos atrás
pai
commit
2158a1c76b
3 ficheiros alterados com 17 adições e 17 exclusões
  1. 10 1
      src/core/Core.js
  2. 4 8
      src/plugins/Transloadit/index.js
  3. 3 8
      src/plugins/Tus10.js

+ 10 - 1
src/core/Core.js

@@ -293,6 +293,15 @@ class Uppy {
     })
   }
 
+  /**
+   * Get a file object.
+   *
+   * @param {string} fileID The ID of the file object to return.
+   */
+  getFile (fileID) {
+    return this.getState().files[fileID]
+  }
+
   removeFile (fileID) {
     const updatedFiles = Object.assign({}, this.getState().files)
     delete updatedFiles[fileID]
@@ -676,7 +685,7 @@ class Uppy {
 
       const waitingFileIDs = []
       Object.keys(this.state.files).forEach((fileID) => {
-        const file = this.state.files[fileID]
+        const file = this.getFile(fileID)
         // TODO: replace files[file].isRemote with some logic
         //
         // filter files that are now yet being uploaded / haven’t been uploaded

+ 4 - 8
src/plugins/Transloadit/index.js

@@ -78,7 +78,7 @@ module.exports = class Transloadit extends Plugin {
     const options = this.opts
     return Promise.all(
       fileIDs.map((fileID) => {
-        const file = this.getFile(fileID)
+        const file = this.core.getFile(fileID)
         const promise = Promise.resolve(options.getAssemblyOptions(file, options))
         return promise.then((assemblyOptions) => {
           this.validateParams(assemblyOptions.params)
@@ -255,10 +255,6 @@ module.exports = class Transloadit extends Plugin {
     })
   }
 
-  getFile (fileID) {
-    return this.core.state.files[fileID]
-  }
-
   prepareUpload (fileIDs) {
     fileIDs.forEach((fileID) => {
       this.core.emit('core:preprocess-progress', fileID, {
@@ -289,7 +285,7 @@ module.exports = class Transloadit extends Plugin {
     // If we don't have to wait for encoding metadata or results, we can close
     // the socket immediately and finish the upload.
     if (!this.shouldWait()) {
-      const file = this.getFile(fileID)
+      const file = this.core.getFile(fileID)
       const socket = this.socket[file.assembly]
       socket.close()
       return
@@ -304,7 +300,7 @@ module.exports = class Transloadit extends Plugin {
       })
 
       const onAssemblyFinished = (assembly) => {
-        const file = this.getFile(fileID)
+        const file = this.core.getFile(fileID)
         // An assembly for a different upload just finished. We can ignore it.
         if (assembly.assembly_id !== file.transloadit.assembly) {
           return
@@ -324,7 +320,7 @@ module.exports = class Transloadit extends Plugin {
       }
 
       const onAssemblyError = (assembly, error) => {
-        const file = this.getFile(fileID)
+        const file = this.core.getFile(fileID)
         // An assembly for a different upload just errored. We can ignore it.
         if (assembly.assembly_id !== file.transloadit.assembly) {
           return

+ 3 - 8
src/plugins/Tus10.js

@@ -266,16 +266,14 @@ module.exports = class Tus10 extends Plugin {
 
   onPauseAll (fileID, cb) {
     this.core.emitter.on('core:pause-all', () => {
-      const files = this.core.getState().files
-      if (!files[fileID]) return
+      if (!this.core.getFile(fileID)) return
       cb()
     })
   }
 
   onResumeAll (fileID, cb) {
     this.core.emitter.on('core:resume-all', () => {
-      const files = this.core.getState().files
-      if (!files[fileID]) return
+      if (!this.core.getFile(fileID)) return
       cb()
     })
   }
@@ -300,10 +298,7 @@ module.exports = class Tus10 extends Plugin {
     }
 
     this.core.log('Tus is uploading...')
-    const filesToUpload = fileIDs.map(getFile, this)
-    function getFile (fileID) {
-      return this.core.state.files[fileID]
-    }
+    const filesToUpload = fileIDs.map((fileID) => this.core.getFile(fileID))
 
     this.uploadFiles(filesToUpload)