소스 검색

PartialTree - change the `maxTotalFileSize` error (#5203)

* `restricter.validateAggregateRestrictions()` - report `"aggregateExceedsSize"` instead of reusing `"exceedsSize"` message

* tests - accommodate tests

* `locale.ts` - cherry-picking locale too
Evgenia Karunus 10 달 전
부모
커밋
31cc47f3fb
3개의 변경된 파일15개의 추가작업 그리고 19개의 파일을 삭제
  1. 9 17
      packages/@uppy/core/src/Restricter.ts
  2. 4 2
      packages/@uppy/core/src/Uppy.test.ts
  3. 2 0
      packages/@uppy/core/src/locale.ts

+ 9 - 17
packages/@uppy/core/src/Restricter.ts

@@ -98,25 +98,17 @@ class Restricter<M extends Meta, B extends Body> {
     }
 
     if (maxTotalFileSize) {
-      let totalFilesSize = existingFiles.reduce(
-        (total, f) => (total + (f.size ?? 0)) as number,
+      const totalFilesSize = [...existingFiles, ...addingFiles].reduce(
+        (total, f) => total + (f.size ?? 0),
         0,
       )
-
-      for (const addingFile of addingFiles) {
-        if (addingFile.size != null) {
-          // We can't check maxTotalFileSize if the size is unknown.
-          totalFilesSize += addingFile.size
-
-          if (totalFilesSize > maxTotalFileSize) {
-            throw new RestrictionError(
-              this.getI18n()('exceedsSize', {
-                size: prettierBytes(maxTotalFileSize),
-                file: addingFile.name,
-              }),
-            )
-          }
-        }
+      if (totalFilesSize > maxTotalFileSize) {
+        throw new RestrictionError(
+          this.getI18n()('aggregateExceedsSize', {
+            sizeAllowed: prettierBytes(maxTotalFileSize),
+            size: prettierBytes(totalFilesSize),
+          }),
+        )
       }
     }
   }

+ 4 - 2
packages/@uppy/core/src/Uppy.test.ts

@@ -2159,7 +2159,7 @@ describe('src/Core', () => {
     it('should enforce the maxTotalFileSize rule', () => {
       const core = new Core({
         restrictions: {
-          maxTotalFileSize: 34000,
+          maxTotalFileSize: 20000,
         },
       })
 
@@ -2178,7 +2178,9 @@ describe('src/Core', () => {
           data: testImage,
         })
       }).toThrowError(
-        new Error('foo1.jpg exceeds maximum allowed size of 33 KB'),
+        new Error(
+          'You selected 34 KB of files, but maximum allowed size is 20 KB',
+        ),
       )
     })
 

+ 2 - 0
packages/@uppy/core/src/locale.ts

@@ -12,6 +12,8 @@ export default {
       0: 'You have to select at least %{smart_count} file',
       1: 'You have to select at least %{smart_count} files',
     },
+    aggregateExceedsSize:
+      'You selected %{size} of files, but maximum allowed size is %{sizeAllowed}',
     exceedsSize: '%{file} exceeds maximum allowed size of %{size}',
     missingRequiredMetaField: 'Missing required meta fields',
     missingRequiredMetaFieldOnFile: