Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PartialTree - change the maxTotalFileSize error #5203

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
lakesare marked this conversation as resolved.
Show resolved Hide resolved
'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
Loading