浏览代码

@uppy/core: Fix `Restrictor` counts ghost files against `maxNumberOfFiles` (#4078)

Prevent core Restrictor class from including isGhost in maxNumberOfFiles count
Andrew McIntee 2 年之前
父节点
当前提交
7599f3abcb
共有 2 个文件被更改,包括 32 次插入2 次删除
  1. 5 2
      packages/@uppy/core/src/Restricter.js
  2. 27 0
      packages/@uppy/core/src/Uppy.test.js

+ 5 - 2
packages/@uppy/core/src/Restricter.js

@@ -33,8 +33,11 @@ class Restricter {
   validate (file, files) {
     const { maxFileSize, minFileSize, maxTotalFileSize, maxNumberOfFiles, allowedFileTypes } = this.getOpts().restrictions
 
-    if (maxNumberOfFiles && files.length + 1 > maxNumberOfFiles) {
-      throw new RestrictionError(`${this.i18n('youCanOnlyUploadX', { smart_count: maxNumberOfFiles })}`)
+    if (maxNumberOfFiles) {
+      const nonGhostFiles = files.filter(f => !f.isGhost)
+      if (nonGhostFiles.length + 1 > maxNumberOfFiles) {
+        throw new RestrictionError(`${this.i18n('youCanOnlyUploadX', { smart_count: maxNumberOfFiles })}`)
+      }
     }
 
     if (allowedFileTypes) {

+ 27 - 0
packages/@uppy/core/src/Uppy.test.js

@@ -1652,6 +1652,33 @@ describe('src/Core', () => {
       }
     })
 
+    it('should not enforce the maxNumberOfFiles rule for ghost files', () => {
+      const core = new Core({
+        restrictions: {
+          maxNumberOfFiles: 1,
+        },
+      })
+
+      expect(() => {
+        // add 1 ghost file
+        const fileId1 = core.addFile({
+          source: 'jest',
+          name: 'foo1.jpg',
+          type: 'image/jpeg',
+          data: new File([sampleImage], { type: 'image/jpeg' }),
+        })
+        core.setFileState(fileId1, { isGhost: true })
+
+        // add another file
+        core.addFile({
+          source: 'jest',
+          name: 'foo2.jpg',
+          type: 'image/jpeg',
+          data: new File([sampleImage], { type: 'image/jpeg' }),
+        })
+      }).not.toThrowError()
+    })
+
     xit('should enforce the minNumberOfFiles rule', () => { })
 
     it('should enforce the allowedFileTypes rule', () => {