パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.0.1

mime_type

Dart HTTP サーバー アプリケーション用の MIME タイプライブラリ。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
65.6万
いいね数
73
Pub ポイント
135

パッケージをインストール

flutter pub add mime_type

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Dart用の軽量なMIMEタイプライブラリで、拡張子またはコンテンツに基づいてファイルタイプを正確に検出および処理できるように設計されています。ファイル拡張子を対応するMIMEタイプにマッピングするための簡単なAPIを提供し、Webサービスにおける適切なコンテンツネゴシエーションを保証します。このパッケージは、モバイル、Web、デスクトップ環境を含む複数のプラットフォームでの使用に最適化されています。DartのHTTPサーバーフレームワークとシームレスに統合され、正しいContent-Typeヘッダーを期待するクライアントとの互換性を高めます。

用途

  • HTTPサーバー コンテンツタイプ管理
  • ファイルアップロードの処理
  • API応答ヘッダーの構成
  • 静的ファイルの提供
  • Webアプリケーションミドルウェア
  • コンテンツネゴシエーション

主な機能

  • 拡張子からMIMEタイプへのマッピング
  • クロスプラットフォーム互換性
  • 軽量で高速
  • HTTP サーバーとの簡単な統合
  • 一般的およびカスタム MIME タイプをサポートしています
  • 最小限の依存関係

適している対象

  • Dart HTTPサーバー開発者
  • Web API バックエンドプロジェクト
  • 静的ファイルサーバー
  • フルスタックDartアプリケーション
  • ファイル処理付きマイクロサービス

注意事項

  • MIMEタイプ検索のみに制限
  • ファイルの検証や解析なし
  • 手動での拡張処理が必要
  • クライアント側のMIME検出には適していません
  • 自動コンテンツスニ핑をサポートしていません

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

MIME type

Library to get MIME type from a file name or a file path and get the file extension from a MIME type. When a HTTP server sends a file to the client, MIME type of the file must be set to the Content-Type header of the response.

Only three methods are available:

  • String mime(String FileName) // gets the MIME type from a file name (such as 'Hello.dart') or a file path (such as '..\Hello.dart').

  • String mimeFromExtension(String extension) // gets the MIME type from an extension (such as 'dart')

  • String extensionFromMime(String mime) // gets extension from MIME type (returns null if there is no such mime type)

All methods return null if they cannot find a match.

Example

import 'package:mime_type/mime_type.dart';

sendFile(HttpRequest request, String fileName) {
  HttpResponse response = request.response;
  File file = new File(fileName);
  if (file.existsSync()) {
    String mimeType = mime(fileName);
    if (mimeType == null) mimeType = 'text/plain; charset=UTF-8';
      // you can change the default content type
      // or, you can choose to send error message
    response.headers.set('Content-Type', mimeType);
    RandomAccessFile openedFile = file.openSync();
    response.contentLength = openedFile.lengthSync();
    openedFile.closeSync();
    file.openRead().pipe(response);
  } else {
    // send 404 (Not Found) status to the client
  }
}

License

This library is licensed under MIT License.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト