Procházet zdrojové kódy

xhr-upload: Throw an error when trying to upload a remote file with `bundle: true` (#1769)

* Warn that we can’t bundle remote files when uploading in xhr-upload

* allow for `error` type logs in `onError` callback

* refactor error handling

* tweak error copy

* add Promise.reject

* throw always

* update docs to reflect that we now send global metadata with xhr, and that bundle: true is not possible with remote uploads
Artur Paikin před 5 roky
rodič
revize
ff7771b2cd

+ 11 - 9
packages/@uppy/core/src/index.js

@@ -1277,19 +1277,12 @@ class Uppy {
    * @returns {Promise}
    */
   upload () {
-    const onError = (err) => {
-      const message = typeof err === 'object' ? err.message : err
-      const details = (typeof err === 'object' && err.details) ? err.details : ''
-      this.log(`${message} ${details}`)
-      this.info({ message: message, details: details }, 'error', 5000)
-      throw (typeof err === 'object' ? err : new Error(err))
-    }
-
     if (!this.plugins.uploader) {
       this.log('No uploader type plugins are used', 'warning')
     }
 
     let files = this.getState().files
+
     const onBeforeUploadResult = this.opts.onBeforeUpload(files)
 
     if (onBeforeUploadResult === false) {
@@ -1320,10 +1313,19 @@ class Uppy {
         return this._runUpload(uploadID)
       })
       .catch((err) => {
+        const message = typeof err === 'object' ? err.message : err
+        const details = (typeof err === 'object' && err.details) ? err.details : ''
+
         if (err.isRestriction) {
           this.emit('restriction-failed', null, err)
+          this.log(`${message} ${details}`, 'info')
+          this.info({ message: message, details: details }, 'info', 5000)
+        } else {
+          this.log(`${message} ${details}`, 'error')
+          this.info({ message: message, details: details }, 'error', 5000)
         }
-        onError(err)
+
+        throw (typeof err === 'object' ? err : new Error(err))
       })
   }
 }

+ 6 - 0
packages/@uppy/xhr-upload/src/index.js

@@ -537,6 +537,12 @@ module.exports = class XHRUpload extends Plugin {
     const files = fileIDs.map((fileID) => this.uppy.getFile(fileID))
 
     if (this.opts.bundle) {
+      // if bundle: true, we don’t support remote uploads
+      const isSomeFileRemote = files.some(file => file.isRemote)
+      if (isSomeFileRemote) {
+        throw new Error('Can’t upload remote files when bundle: true option is set')
+      }
+
       return this.uploadBundle(files)
     }
 

+ 3 - 1
website/src/docs/xhrupload.md

@@ -90,7 +90,9 @@ headers: {
 
 Send all files in a single multipart request. When `bundle` is set to `true`, `formData` must also be set to `true`.
 
-> Note: When `bundle` is set to `true`, file metadata is **not** sent to the endpoint. This is because it is not obvious how metadata should be sent when there are multiple files in a single request. If you need this, please open an issue and we will try to figure it out together.
+⚠️ Only use `bundle: true` with local uploads (drag-drop, browse, webcam), Uppy won’t be able to bundle remote files (from Google Drive or Instagram), and will throw an error in this case.
+
+> Note: When `bundle` is set to `true`, [global uppy metadata](https://uppy.io/docs/uppy/#meta), the one set via `meta` options property, is sent to the endpoint. Individual per-file metadata is ignored.
 
 All files will be appended to the provided `fieldName` field in the request. To upload files on different fields, use [`uppy.setFileState()`](/docs/uppy#uppy-setFileState-fileID-state) to set the `xhrUpload.fieldName` property on the file: