getMimeType function

String? getMimeType(
  1. String? path, {
  2. List<int>? headerBytes,
  3. bool? isExtension,
  4. bool autoUtf8 = true,
})

Retrieves the mime type from the given path and, optional, header bytes (headerBytes). It returns null if not fund.

For example,

getMimeType("home.js");
getMimeType("alpha/home.js");
getMimeType("js");
getMimeType("alpha/home.js?foo");
  //returns "text/javascript; charset=utf-8";
  • isExtension whether path is an extension, e.g., "js". If not specified (i.e., null), extension is assumed if it doesn't contain '.' nor '/'/
  • autoUtf8 whether to append "; charset=utf-8" when detecing a text mime type.

Implementation

String? getMimeType(String? path,
    {List<int>? headerBytes, bool? isExtension, bool autoUtf8 = true}) {
  if (path != null) {
    final i = path.lastIndexOf('?');
    if (i >= 0) path = path.substring(0, i);

    if (isExtension == null) isExtension = !_rePath.hasMatch(path);
    if (isExtension) path = '.$path';

    final mime = _mimeResolver.lookup(path, headerBytes: headerBytes);
    if (mime != null)
      return autoUtf8 && _isTextType(mime) ? "$mime; charset=utf-8": mime;
  }
}