lookup method

String lookup (
  1. String path,
  2. {List<int> headerBytes}
)

Extract the extension from path and use that for MIME-type lookup.

If no matching MIME-type was found, null is returned.

If headerBytes is present, a match for known magic-numbers will be performed first. This allows the correct mime-type to be found, even though a file have been saved using the wrong file-name extension. If less than magicNumbersMaxLength bytes was provided, some magic-numbers won't be matched against.

Implementation

String lookup(String path, {List<int> headerBytes}) {
  String result;
  if (headerBytes != null) {
    result = _matchMagic(headerBytes, _magicNumbers);
    if (result != null) return result;
    if (_useDefault) {
      result = _matchMagic(headerBytes, DEFAULT_MAGIC_NUMBERS);
      if (result != null) return result;
    }
  }
  var ext = _ext(path);
  result = _extensionMap[ext];
  if (result != null) return result;
  if (_useDefault) {
    result = defaultExtensionMap[ext];
    if (result != null) return result;
  }
  return null;
}