FLUTTER ECOSYSTEM

dart-archive/mime

用於處理 MIME 類型定義以及處理 MIME 多部分媒體類型流的 Dart 套件。

mime 專案封面
Stars
128
Forks
49
最近推送(UTC)
2024年10月1日
專案狀態
已封存
Dart Archive GitHub avatar
GITHUB Organization

Dart Archive ↗

Legacy projects kept around for posterity – See github.com/dart-lang for current work

語言Dart

使用的依賴

依賴清單 2 項
  • dart_flutter_team_lints開發依賴^3.0.0
  • test開發依賴^1.16.6

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

展開 / 收合專案 README

[!IMPORTANT]
This repo has moved to https://github.com/dart-lang/tools/tree/main/pkgs/mime

Build Status Pub Package package publisher

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() {
  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.

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.
request
    .transform(new MimeMultipartTransformer(boundary))
    .map((part) => part.fold(0, (p, d) => p + d))
    .listen((length) => print('Part with length $length'));