os_mime_type

os_mime_type is a Dart package that aids in the conversion between MIME types and file extensions. With this package, you can easily determine the MIME type of a file based on its extension or filename, and also derive the file extension from a MIME type.

Functions

This package provides the following functions:

  • mimeFromExtension({required String extension}): This function takes a file extension and returns the corresponding MIME type. If no matching MIME type is found, it returns "unknown".

  • mimeFromFileName({required String fileName}): This function takes a filename and returns the corresponding MIME type based on its extension.

  • extensionFromMime({required String mime}): This function takes a MIME type and returns the corresponding file extension. If no matching file extension is found, it returns null.

Usage Example

import 'package:os_mime_type/os_mime_type.dart';

void main() {
  String mime = mimeFromExtension(extension: ".png");
  print(mime);  // Output: "image/png"

  mime = mimeFromFileName(fileName: "image.jpg");
  print(mime);  // Output: "image/jpeg"

  String? ext = extensionFromMime(mime: "application/json");
  print(ext);  // Output: ".json"
}