Skip to content

Commit

Permalink
PartialTree - change the maxTotalFileSize error (#5203)
Browse files Browse the repository at this point in the history
* `restricter.validateAggregateRestrictions()` - report `"aggregateExceedsSize"` instead of reusing `"exceedsSize"` message

* tests - accommodate tests

* `locale.ts` - cherry-picking locale too
  • Loading branch information
lakesare committed May 30, 2024
1 parent fe0437a commit 31cc47f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
26 changes: 9 additions & 17 deletions packages/@uppy/core/src/Restricter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}),
)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/@uppy/core/src/Uppy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ describe('src/Core', () => {
it('should enforce the maxTotalFileSize rule', () => {
const core = new Core({
restrictions: {
maxTotalFileSize: 34000,
maxTotalFileSize: 20000,
},
})

Expand All @@ -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',
),
)
})

Expand Down
2 changes: 2 additions & 0 deletions packages/@uppy/core/src/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 31cc47f

Please sign in to comment.