import { FileWithPath } from 'file-selector' ; import { BYTES_PER_MEGABYTE } from '../../constants/bytesPerMegabyte' ; import { UseFilePickerConfig } from '../../interfaces' ; import { Validator } from '../validatorInterface' ; export default class FileSizeValidator implements Validator { validateBeforeParsing ( config , plainFiles ) { const { minFileSize , maxFileSize } = config ; for ( const file of plainFiles ) { if ( minFileSize || maxFileSize ) { return checkFileSize ( { minFileSize , maxFileSize , fileSize : file . size } ) ; } } return Promise . resolve ( ) ; } async validateAfterParsing ( _config , _file ) { return Promise . resolve ( ) ; } } const checkFileSize = ( { fileSize , maxFileSize , minFileSize } ) => new Promise < void > ( ( resolve , reject ) => { if ( minFileSize ) { const minBytes = minFileSize * BYTES_PER_MEGABYTE ; if ( fileSize < minBytes ) { reject ( { fileSizeTooSmall : true } ) ; } } if ( maxFileSize ) { const maxBytes = maxFileSize * BYTES_PER_MEGABYTE ; if ( fileSize > maxBytes ) { reject ( { fileSizeToolarge : true } ) ; } } resolve ( ) ; } ) ;