OLD | NEW |
(Empty) | |
| 1 #MIME type package |
| 2 |
| 3 Package for working with MIME type definitions and for processing |
| 4 streams of MIME multipart media types. |
| 5 |
| 6 ##Determining the MIME type for a file |
| 7 |
| 8 The `MimeTypeResolver` class can be used to determine the MIME type of |
| 9 a file. It supports both using the extension of the file name and |
| 10 looking at magic bytes from the begining of the file. |
| 11 |
| 12 There is a builtin instance of `MimeTypeResolver` accessible through |
| 13 the top level function `lookupMimeType`. This builtin instance has |
| 14 the most common file name extensions and magic bytes registered. |
| 15 |
| 16 print(lookupMimeType('test.html')); // Will print text/html |
| 17 print(lookupMimeType('test', [0xFF, 0xD8])); // Will print image/jpeg |
| 18 print(lookupMimeType('test.html', [0xFF, 0xD8])); // Will print image/jpeg |
| 19 |
| 20 You can build you own resolver by creating an instance of |
| 21 `MimeTypeResolver` and adding file name extensions and magic bytes |
| 22 using `addExtension` and `addMagicNumber`. |
| 23 |
| 24 ##Processing MIME multipart media types |
| 25 |
| 26 The class `MimeMultipartTransformer` is used to process a `Stream` of |
| 27 bytes encoded using a MIME multipart media types encoding. The |
| 28 transformer provides a new `Stream` of `MimeMultipart` objects each of |
| 29 which have the headers and the content of each part. The content of a |
| 30 part is provided as a stream of bytes. |
| 31 |
| 32 Below is an example showing how to process an HTTP request and print |
| 33 the length of the content of each part. |
| 34 |
| 35 // HTTP request with content type multipart/form-data. |
| 36 HttpRequest request = ...; |
| 37 // Determine the boundary form the content type header |
| 38 String boundary = request.headers.contentType.parameters['boundary']; |
| 39 |
| 40 // Process the body just calculating the length of each part. |
| 41 request.transform(new MimeMultipartTransformer(boundary)) |
| 42 .map((part) => part.fold(0, (p, d) => p + d)) |
| 43 .listen((length) => print('Part with length $length')); |
| 44 |
| 45 Take a look at the `HttpBodyHandler` in the [http_server][1] package for |
| 46 handling different content types in a HTTP request. |
| 47 |
| 48 [1]: https://pub.dartlang.org/packages/http_server |
OLD | NEW |