tag | 1c90a71f2e3ecc2d76fea6addbc89b86bc763d7f | |
---|---|---|
tagger | Kevin Moore <kevmoo@google.com> | Wed Jan 11 16:48:54 2023 -0800 |
object | 034471a60d5603a6988de6d9811bee1cb0657f70 |
commit | 034471a60d5603a6988de6d9811bee1cb0657f70 | [log] [tgz] |
---|---|---|
author | Kevin Moore <kevmoo@users.noreply.github.com> | Wed Jan 11 16:32:10 2023 -0800 |
committer | GitHub <noreply@github.com> | Wed Jan 11 16:32:10 2023 -0800 |
tree | 4b0a22d2c7292734078d857aa733974ad04c887c | |
parent | 273d454711487fe00849c2bf3dff189278fd0e6f [diff] |
Prepare to release v1.0.4 (#80)
Package for working with MIME type definitions and for processing streams of MIME multipart media types.
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() { print(lookupMimeType('test.html')); // 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
.
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. request .transform(new MimeMultipartTransformer(boundary)) .map((part) => part.fold(0, (p, d) => p + d)) .listen((length) => print('Part with length $length'));