byExtension static method

MimeType? byExtension(
  1. String? extension, {
  2. bool defaultAsApplication = true,
})

Creates an instance by file extension.

defaultAsApplication if true returns 'application/extension'.

Implementation

static MimeType? byExtension(String? extension,
    {bool defaultAsApplication = true}) {
  if (extension == null) return null;

  var idx = extension.lastIndexOf('.');
  if (idx >= 0) {
    extension = extension.substring(idx + 1);
  }
  extension = extension.trim().toLowerCase();

  switch (extension) {
    case 'zip':
      return MimeType.parse(zip);
    case 'gzip':
    case 'gz':
      return MimeType.parse(gzip);
    case 'png':
      return MimeType.parse(png);
    case 'jpeg':
    case 'jpg':
      return MimeType.parse(jpeg);
    case 'gif':
      return MimeType.parse(gif);
    case 'css':
      return MimeType.parse(css);
    case 'json':
      return MimeType.parse(json);
    case 'js':
    case 'javascript':
      return MimeType.parse(javaScript);
    case 'html':
    case 'htm':
      return MimeType.parse(html);
    case 'xhtml':
      return MimeType.parse(xhtml);
    case 'text':
    case 'txt':
      return MimeType.parse(text);
    case 'pdf':
      return MimeType.parse(pdf);
    case 'mp3':
      return MimeType.parse(mp3);
    case 'mpeg':
      return MimeType.parse(mpeg);
    case 'xml':
      return MimeType.parse(xml);
    case 'icon':
    case 'ico':
      return MimeType.parse(icon);
    case 'svg':
      return MimeType.parse(svg);

    case 'webp':
      return MimeType.parse(webp);
    case 'weba':
      return MimeType.parse(weba);
    case 'webm':
      return MimeType.parse(webm);

    case 'otf':
      return MimeType.parse(otf);
    case 'ttf':
      return MimeType.parse(ttf);
    case 'woff':
      return MimeType.parse(woff);
    case 'woff2':
      return MimeType.parse(woff2);
    case 'md':
      return MimeType.parse(markdown);
    case 'yaml':
    case 'yml':
      return MimeType.parse(yaml);
    case 'dart':
      return MimeType.parse(dart);
    default:
      {
        if (defaultAsApplication) {
          return MimeType.parse('application/$extension');
        }
        return null;
      }
  }
}