Skip to content

Commit d60279d

Browse files
authored
Merge pull request #286 from DevinoSolutions/remove-10mb-limit
Add Support for Custom maxFileSize Upload Limit
2 parents eb06431 + 4d0fece commit d60279d

File tree

7 files changed

+33
-1132
lines changed

7 files changed

+33
-1132
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "upup-react-file-uploader",
33
"author": "Devino Solutions",
44
"license": "MIT",
5-
"version": "1.5.0",
5+
"version": "1.5.1",
66
"publishConfig": {
77
"access": "public"
88
},

pnpm-lock.yaml

Lines changed: 0 additions & 1112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/backend/lib/files/file-validate-params.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import checkFileType from '../../../shared/lib/checkFileType'
22
import { UploadError, UploadErrorType } from '../../../shared/types'
33
import { FileParams } from '../../types'
44

5-
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024 // 10MB;
6-
75
export default function fileValidateParams(file: FileParams) {
86
// Validate required file params
97
const requiredFileParams = ['name', 'type', 'size'] as const
@@ -20,7 +18,7 @@ export default function fileValidateParams(file: FileParams) {
2018
type: fileType,
2119
accept = '*',
2220
size,
23-
maxFileSize = DEFAULT_MAX_FILE_SIZE,
21+
maxFileSize = file.maxFileSize,
2422
} = file
2523

2624
// Validate file type against accept pattern
@@ -33,7 +31,7 @@ export default function fileValidateParams(file: FileParams) {
3331
)
3432

3533
// Validate file size
36-
if (size > maxFileSize)
34+
if (maxFileSize && size > maxFileSize)
3735
throw new UploadError(
3836
`File size: ${size} exceeds maximum limit of ${
3937
maxFileSize / (1024 * 1024)

src/frontend/components/AdapterSelector.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ export default function AdapterSelector() {
122122
},
123123
)}
124124
>
125-
Max {maxFileSize.size} {maxFileSize.unit} file
126-
{limit > 1 ? 's are ' : ' is '} allowed
125+
{maxFileSize?.size && maxFileSize?.unit && (
126+
<>
127+
Max {maxFileSize.size} {maxFileSize.unit} file
128+
{limit > 1 ? 's are ' : ' is '}allowed
129+
</>
130+
)}
127131
</p>
128132
</div>
129133
</div>

src/frontend/context/RootContext.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type ContextProps = Required<
4141
| 'isProcessing'
4242
| 'allowPreview'
4343
| 'mini'
44-
| 'maxFileSize'
4544
| 'onFileClick'
4645
| 'onIntegrationClick'
4746
| 'onFilesDragOver'
@@ -52,10 +51,11 @@ type ContextProps = Required<
5251
| 'classNames'
5352
| 'icons'
5453
>
55-
> & {
56-
multiple: boolean
57-
icons: Required<UpupUploaderPropsIcons>
58-
}
54+
> &
55+
Pick<UpupUploaderProps, 'maxFileSize'> & {
56+
multiple: boolean
57+
icons: Required<UpupUploaderPropsIcons>
58+
}
5959

6060
export interface IRootContext {
6161
inputRef: RefObject<HTMLInputElement | null>

src/frontend/hooks/useRootProvider.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function useRootProvider({
3636
limit: propLimit = 1,
3737
isProcessing = false,
3838
allowPreview = true,
39-
maxFileSize = { size: 10, unit: 'MB' },
39+
maxFileSize,
4040
shouldCompress = false,
4141
uploadAdapters = [UploadAdapter.INTERNAL, UploadAdapter.LINK],
4242
onError: errorHandler,
@@ -151,7 +151,11 @@ export default function useRootProvider({
151151
if (!checkFileType(accept, file)) {
152152
onError(`${file.name} has an unsupported type!`)
153153
onFileTypeMismatch(file, accept)
154-
} else if (!checkFileSize(file, maxFileSize))
154+
} else if (
155+
maxFileSize?.size &&
156+
maxFileSize?.unit &&
157+
!checkFileSize(file, maxFileSize)
158+
)
155159
onError(
156160
`${file.name} is larger than ${maxFileSize.size} ${maxFileSize.unit}!`,
157161
)
@@ -240,10 +244,13 @@ export default function useRootProvider({
240244
constraints: {
241245
multiple,
242246
accept,
243-
maxFileSize: sizeToBytes(
244-
maxFileSize.size,
245-
maxFileSize.unit,
246-
),
247+
maxFileSize:
248+
maxFileSize?.size && maxFileSize?.unit
249+
? sizeToBytes(
250+
maxFileSize.size,
251+
maxFileSize.unit,
252+
)
253+
: undefined,
247254
},
248255
customProps,
249256
enableAutoCorsConfig,
@@ -296,8 +303,8 @@ export default function useRootProvider({
296303
tokenEndpoint,
297304
multiple,
298305
accept,
299-
maxFileSize.size,
300-
maxFileSize.unit,
306+
maxFileSize?.size,
307+
maxFileSize?.unit,
301308
customProps,
302309
enableAutoCorsConfig,
303310
onFilesUploadComplete,

stories/UploaderWithButton.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ const meta = {
5353
onFilesSelected={files => {
5454
console.log(files)
5555
}}
56+
maxFileSize={{
57+
size: 1,
58+
unit: 'KB',
59+
}}
5660
onError={error => {
5761
console.error('UpupUploader Error:', error)
5862
}}

0 commit comments

Comments
 (0)