zdsValidator function

Future<FilePickerException?> zdsValidator(
  1. ZdsFilePickerController controller,
  2. FilePickerConfig config,
  3. FileWrapper wrapper,
  4. FilePickerOptions option
)

Default file validator

Implementation

Future<FilePickerException?> zdsValidator(
  ZdsFilePickerController controller,
  FilePickerConfig config,
  FileWrapper wrapper,
  FilePickerOptions option,
) async {
  final dynamic file = wrapper.content;
  if (file is! XFile) return null;

  //file type check if [useLiveMediaOnly] is true and
  //option will be [FilePickerOptions.FILE]
  if (config.useLiveMediaOnly && option == FilePickerOptions.FILE) {
    final allowedFileTypes = getAllowedFileBrowserTypes(
      useLiveMediaOnly: config.useLiveMediaOnly,
      allowedFileTypes: config.allowedExtensions,
    );
    if (allowedFileTypes.isNotEmpty && !allowedFileTypes.contains(wrapper.extension)) {
      return FilePickerException(PickerExceptionType.unsupportedFile);
    }
  }

  // File type check
  if (config.allowedExtensions.isNotEmpty && !config.allowedExtensions.contains(wrapper.extension)) {
    return FilePickerException(PickerExceptionType.unsupportedFile);
  }

  // Check if file is already selected
  if (controller.items.contains(wrapper)) {
    return FilePickerException(PickerExceptionType.duplicateFile);
  }

  // check if the file size is within the limit
  if (!(wrapper.isImage() || wrapper.isVideo()) && config.maxFileSize > 0 && config.maxFileSize < await file.length()) {
    return FilePickerException(PickerExceptionType.maxFileSize, args: <String>[fileSizeWithUnit(config.maxFileSize)]);
  }

  return null;
}