Clone this repo:
  1. 0a75a41 Add repository to pubspec (#63) by Devon Carew · 5 months ago master v1.0.2
  2. c2c5ffd Add `audio/m4p` mimeType lookup by file path (#62) by Nils Reichardt · 6 months ago
  3. 7f4252d Add magic numbers for AIFF, WAVE and FLAC files (#53) by Hannes Braun · 8 months ago
  4. ca61196 Move to pkg:lints, tiny bumps in deps (#56) by Kevin Moore · 11 months ago v1.0.1
  5. 727d078 Add magic numbers for WebP (#54) by Rodinei Fagundes · 11 months ago

Pub Package Build Status

Package for working with MIME type definitions and for processing streams of MIME multipart media types.

Determining the MIME type for a file

The MimeTypeResolver class can be used to determine the MIME type of a file. It supports both using the extension of the file name and looking at magic bytes from the beginning of the file.

There is a builtin instance of MimeTypeResolver accessible through the top level function lookupMimeType. This builtin instance has the most common file name extensions and magic bytes registered.

import 'package:mime/mime.dart';

void main() {
  // text/html

  print(lookupMimeType('test', headerBytes: [0xFF, 0xD8]));
  // image/jpeg

  print(lookupMimeType('test.html', headerBytes: [0xFF, 0xD8]));
  // image/jpeg

You can build you own resolver by creating an instance of MimeTypeResolver and adding file name extensions and magic bytes using addExtension and addMagicNumber.

Processing MIME multipart media types

The class MimeMultipartTransformer is used to process a Stream of bytes encoded using a MIME multipart media types encoding. The transformer provides a new Stream of MimeMultipart objects each of which have the headers and the content of each part. The content of a part is provided as a stream of bytes.

Below is an example showing how to process an HTTP request and print the length of the content of each part.

// HTTP request with content type multipart/form-data.
HttpRequest request = ...;
// Determine the boundary form the content type header
String boundary = request.headers.contentType.parameters['boundary'];

// Process the body just calculating the length of each part.
    .transform(new MimeMultipartTransformer(boundary))
    .map((part) => part.fold(0, (p, d) => p + d))
    .listen((length) => print('Part with length $length'));

Take a look at the HttpBodyHandler in the http_server package for handling different content types in an HTTP request.

Features and bugs

Please file feature requests and bugs at the issue tracker.