Browse Source

tus,xhrupload: Reject upload() if all file uploads failed.

Renée Kooi 7 years ago
parent
commit
ef3480ac26
3 changed files with 42 additions and 22 deletions
  1. 16 1
      src/core/Utils.js
  2. 13 6
      src/plugins/Tus10.js
  3. 13 15
      src/plugins/XHRUpload.js

+ 16 - 1
src/core/Utils.js

@@ -487,6 +487,20 @@ function _emitSocketProgress (uploader, progressData, file) {
 
 const emitSocketProgress = throttle(_emitSocketProgress, 300, {leading: true, trailing: true})
 
+function rejectIfAllRejected (promiseArray) {
+  const rejections = []
+  promiseArray.forEach((result) => {
+    if (!result.isFulfilled()) {
+      rejections.push(result.reason())
+    }
+  })
+
+  if (rejections.length > 0) {
+    return Promise.reject(rejections[0])
+  }
+  return promiseArray
+}
+
 module.exports = {
   generateFileID,
   toArray,
@@ -512,5 +526,6 @@ module.exports = {
   findDOMElement,
   findAllDOMElements,
   getSocketHost,
-  emitSocketProgress
+  emitSocketProgress,
+  rejectIfAllRejected
 }

+ 13 - 6
src/plugins/Tus10.js

@@ -2,7 +2,11 @@ const Plugin = require('./Plugin')
 const tus = require('tus-js-client')
 const settle = require('promise-settle')
 const UppySocket = require('../core/UppySocket')
-const Utils = require('../core/Utils')
+const {
+  emitSocketProgress,
+  getSocketHost,
+  rejectIfAllRejected
+} = require('../core/Utils')
 require('whatwg-fetch')
 
 // Extracted from https://github.com/tus/tus-js-client/blob/master/lib/upload.js#L13
@@ -142,7 +146,8 @@ module.exports = class Tus10 extends Plugin {
       optsTus.onError = (err) => {
         this.core.log(err)
         this.core.emit('core:upload-error', file.id, err)
-        reject(new Error('Failed because: ' + err))
+        err.message = `Failed because: ${err.message}`
+        reject(err)
       }
 
       optsTus.onProgress = (bytesUploaded, bytesTotal) => {
@@ -249,7 +254,7 @@ module.exports = class Tus10 extends Plugin {
 
   connectToServerSocket (file) {
     const token = file.serverToken
-    const host = Utils.getSocketHost(file.remote.host)
+    const host = getSocketHost(file.remote.host)
     const socket = new UppySocket({ target: `${host}/api/${token}` })
 
     this.onFileRemove(file.id, () => socket.send('pause', {}))
@@ -261,7 +266,7 @@ module.exports = class Tus10 extends Plugin {
     this.onPauseAll(file.id, () => socket.send('pause', {}))
     this.onResumeAll(file.id, () => socket.send('resume', {}))
 
-    socket.on('progress', (progressData) => Utils.emitSocketProgress(this, progressData, file))
+    socket.on('progress', (progressData) => emitSocketProgress(this, progressData, file))
 
     socket.on('success', (data) => {
       this.core.emitter.emit('core:upload-success', file.id, data, data.url)
@@ -324,7 +329,7 @@ module.exports = class Tus10 extends Plugin {
   }
 
   uploadFiles (files) {
-    return settle(files.map((file, index) => {
+    const promises = files.map((file, index) => {
       const current = parseInt(index, 10) + 1
       const total = files.length
 
@@ -333,7 +338,9 @@ module.exports = class Tus10 extends Plugin {
       } else {
         return this.uploadRemote(file, current, total)
       }
-    }))
+    })
+
+    return settle(promises).then(rejectIfAllRejected)
   }
 
   handleUpload (fileIDs) {

+ 13 - 15
src/plugins/XHRUpload.js

@@ -1,7 +1,11 @@
 const Plugin = require('./Plugin')
 const settle = require('promise-settle')
 const UppySocket = require('../core/UppySocket')
-const Utils = require('../core/Utils')
+const {
+  emitSocketProgress,
+  getSocketHost,
+  rejectIfAllRejected
+} = require('../core/Utils')
 
 module.exports = class XHRUpload extends Plugin {
   constructor (core, opts) {
@@ -161,10 +165,10 @@ module.exports = class XHRUpload extends Plugin {
 
         res.json().then((data) => {
           const token = data.token
-          const host = Utils.getSocketHost(file.remote.host)
+          const host = getSocketHost(file.remote.host)
           const socket = new UppySocket({ target: `${host}/api/${token}` })
 
-          socket.on('progress', (progressData) => Utils.emitSocketProgress(this, progressData, file))
+          socket.on('progress', (progressData) => emitSocketProgress(this, progressData, file))
 
           socket.on('success', (data) => {
             this.core.emit('core:upload-success', file.id, data, data.url)
@@ -176,8 +180,8 @@ module.exports = class XHRUpload extends Plugin {
     })
   }
 
-  selectForUpload (files) {
-    return settle(files.map((file, i) => {
+  uploadFiles (files) {
+    const promises = files.map((file, i) => {
       const current = parseInt(i, 10) + 1
       const total = files.length
 
@@ -186,15 +190,9 @@ module.exports = class XHRUpload extends Plugin {
       } else {
         return this.upload(file, current, total)
       }
-    }))
-
-    //   if (this.opts.bundle) {
-    //     uploaders.push(this.upload(files, 0, files.length))
-    //   } else {
-    //     for (let i in files) {
-    //       uploaders.push(this.upload(files, i, files.length))
-    //     }
-    //   }
+    })
+
+    return settle(promises).then(rejectIfAllRejected)
   }
 
   handleUpload (fileIDs) {
@@ -209,7 +207,7 @@ module.exports = class XHRUpload extends Plugin {
       return this.core.state.files[fileID]
     }
 
-    return this.selectForUpload(files).then(() => null)
+    return this.uploadFiles(files).then(() => null)
   }
 
   install () {