@@ -18,25 +18,52 @@ const DropZone: FC<DropZoneProps> = ({callback, messages}) => {
1818 const { uploadUrl, group, accept} = useContext ( MediaManagerContext ) ;
1919
2020 const handleClose = ( ) => {
21- setAlert ( '' )
22- }
21+ setAlert ( '' ) ;
22+ } ;
23+
24+ // Проверка, разрешён ли тип файла
25+ const isFileAllowed = ( file : File ) : boolean => {
26+ const fileType = file . type ;
27+ const fileName = file . name ;
28+
29+ // Проверяем по MIME-типу или расширению
30+ return accept . some ( type => {
31+ if ( type . startsWith ( '.' ) ) {
32+ return fileName . toLowerCase ( ) . endsWith ( type . toLowerCase ( ) ) ;
33+ } else if ( type . endsWith ( '/*' ) ) {
34+ const mainType = type . slice ( 0 , - 1 ) ; // 'image/*' -> 'image'
35+ return fileType . startsWith ( mainType ) ;
36+ } else {
37+ return fileType === type ;
38+ }
39+ } ) ;
40+ } ;
2341
2442 const onDrop = async ( e : React . DragEvent < HTMLDivElement > ) => {
2543 e . preventDefault ( ) ;
2644 e . stopPropagation ( ) ;
2745 setLoading ( true ) ;
2846
29- if ( e . dataTransfer . files ) {
30- for ( const file of e . dataTransfer . files ) {
31- await upload ( file ) ;
47+ const files = Array . from ( e . dataTransfer . files ) ;
48+ for ( const file of files ) {
49+ if ( ! isFileAllowed ( file ) ) {
50+ setLoading ( false ) ;
51+ setAlert ( messages [ `File type not allowed` ] || `File type not allowed: ${ file . name } ` ) ;
52+ return ; // Останавливаем загрузку
3253 }
54+ await upload ( file ) ;
3355 }
3456 } ;
3557
3658 const onLoad = async ( e : React . ChangeEvent < HTMLInputElement > ) => {
3759 e . stopPropagation ( ) ;
3860 if ( e . target . files ) {
39- for ( const file of e . target . files ) {
61+ const files = Array . from ( e . target . files ) ;
62+ for ( const file of files ) {
63+ if ( ! isFileAllowed ( file ) ) {
64+ setAlert ( `${ messages [ `File type are not supported` ] } .${ file . name . split ( '.' ) . pop ( ) ?. toLowerCase ( ) } ` ) ;
65+ return ; // Останавливаем загрузку
66+ }
4067 setLoading ( true ) ;
4168 await upload ( file ) ;
4269 }
0 commit comments