getFileMimeType function

MimeType? getFileMimeType(
  1. File file, [
  2. String accept = ''
])

Returns the file MimeType.

accept the accept attribute when file was selected.

Implementation

MimeType? getFileMimeType(File file, [String accept = '']) {
  var mimeType = MimeType.byExtension(file.name, defaultAsApplication: false);
  if (mimeType != null) return mimeType;

  var fileExtension = getPathExtension(file.name) ?? '';
  fileExtension = fileExtension.toLowerCase().trim();

  if (fileExtension == 'jpg') fileExtension = 'jpeg';

  accept = accept.toLowerCase();

  var mediaType = '';

  if (accept.contains('image')) {
    mediaType = 'image/$fileExtension';
  } else if (accept.contains('video')) {
    mediaType = 'video/$fileExtension';
  } else if (accept.contains('audio')) {
    mediaType = 'audio/$fileExtension';
  } else if (accept.contains('json')) {
    mediaType = 'application/json';
  } else {
    mediaType = fileExtension;
  }

  return MimeType.parse(mediaType);
}