diff --git a/packages/@uppy/core/src/Restricter.ts b/packages/@uppy/core/src/Restricter.ts index a8000363d6..663e8cb6de 100644 --- a/packages/@uppy/core/src/Restricter.ts +++ b/packages/@uppy/core/src/Restricter.ts @@ -98,25 +98,17 @@ class Restricter { } 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), + }), + ) } } } diff --git a/packages/@uppy/core/src/Uppy.test.ts b/packages/@uppy/core/src/Uppy.test.ts index b71229a8c9..e0195c18ba 100644 --- a/packages/@uppy/core/src/Uppy.test.ts +++ b/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', + ), ) }) diff --git a/packages/@uppy/core/src/locale.ts b/packages/@uppy/core/src/locale.ts index 92674805d6..90a9974499 100644 --- a/packages/@uppy/core/src/locale.ts +++ b/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: