@@ -23,7 +23,8 @@ const i18nDefaults = {
2323 browseImages : 'Add from gallery' ,
2424 insertUrl : 'Insert from URL' ,
2525 urlInput : 'Type or insert URL' ,
26- errorTitle : 'Error: ' ,
26+ errorTitle : 'Upload error: ' ,
27+ fileTooLarge : 'Image file is too large' ,
2728 wrongFileType : ( accept ) =>
2829 `This file is not one of the accepted file types, ${ accept . join ( ', ' ) } ` ,
2930} ;
@@ -32,6 +33,8 @@ const propTypes = {
3233 accept : PropTypes . arrayOf ( PropTypes . string ) ,
3334 onBrowseClick : PropTypes . func ,
3435 onUpload : PropTypes . func ,
36+ /** the maximum file size in bytes */
37+ maxFileSizeInBytes : PropTypes . number ,
3538 hasInsertFromUrl : PropTypes . bool ,
3639 i18n : PropTypes . shape ( {
3740 dropContainerLabelText : PropTypes . string ,
@@ -41,11 +44,13 @@ const propTypes = {
4144 browseImages : PropTypes . string ,
4245 insertUrl : PropTypes . string ,
4346 urlInput : PropTypes . string ,
47+ fileTooLarge : PropTypes . string ,
4448 } ) ,
4549} ;
4650
4751const defaultProps = {
4852 accept : [ 'APNG' , 'AVIF' , 'GIF' , 'JPEG' , 'JPG' , 'PNG' , 'WebP' ] ,
53+ maxFileSizeInBytes : 1048576 ,
4954 onBrowseClick : ( ) => { } ,
5055 onUpload : ( ) => { } ,
5156 hasInsertFromUrl : false ,
@@ -58,6 +63,7 @@ const ImageUploader = ({
5863 i18n,
5964 onUpload,
6065 accept,
66+ maxFileSizeInBytes,
6167 ...other
6268} ) => {
6369 const [ fromURL , setFromURL ] = useState ( false ) ;
@@ -96,7 +102,9 @@ const ImageUploader = ({
96102
97103 const handleOnChange = ( _event , files ) => {
98104 const acceptedFiles = accept . map ( ( i ) => i . toLowerCase ( ) ) ;
99- if (
105+ if ( files . addedFiles [ 0 ] . size > maxFileSizeInBytes ) {
106+ setError ( i18n . fileTooLarge ) ;
107+ } else if (
100108 acceptedFiles . includes (
101109 files . addedFiles [ 0 ] . name
102110 . match ( / ( [ ^ / . ] * ?) (? = \? | # | $ ) / || [ ] ) [ 0 ]
0 commit comments