commit | 319815be54aeec408b7009e80c524f3c4a3dc896 | [log] [tgz] |
---|---|---|
author | Nate Bosch <nbosch@google.com> | Thu Nov 12 18:39:49 2020 -0800 |
committer | GitHub <noreply@github.com> | Thu Nov 12 18:39:49 2020 -0800 |
tree | 20f8ff85a135d747e1c721f621a738ef87ede7e6 | |
parent | 07635f7774447503248fbc6afb3911e9000a477e [diff] |
Cleanup in BoundMultipartStream (#35) Start to make the code a little easier to understand, and fix some style. - Initialize the `_controller` field in place and make it final. Set the callbacks in the constructor rather than instantiate in the constructor. This will prevent it from needing to be `late`. - Assign `boundaryPrefix`, `contentStartIndex`, and `byte` variables where they are declared rather than separate the declaration and initialization. Style fixes: - Use doc comments. - Remove unnecessary parens in while loop condition. - Remove unnecessary library directive.
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'));
Take a look at the HttpBodyHandler
in the http_server package for handling different content types in an HTTP request.
Please file feature requests and bugs at the issue tracker.