FLUTTER ECOSYSTEM

mitsuoka/mime_type

Dart HTTP 서버 애플리케이션용 MIME 유형 라이브러리.

mime_type 프로젝트 이미지
Stars
16
Forks
15
최근 푸시(UTC)
2024. 8. 27.
프로젝트 상태
활성
Teruyoshi Mitsuoka GitHub avatar
GITHUB User

Teruyoshi Mitsuoka ↗

Chigasaki, Japan
언어Dart

이 저장소가 배포한 패키지

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

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.