패키지, 저장소 및 앱 카테고리 검색

패키지 상세v1.0.1

mime_type

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

AndroidiOSWebmacOSWindowsLinux
30일 다운로드
65.6만
좋아요
73
Pub 점수
135

패키지 설치

flutter pub add mime_type

패키지 개요

공개 정보를 바탕으로 생성한 선택 가이드입니다.

확장자 또는 콘텐츠 기반으로 파일 유형을 정확하게 탐지하고 처리할 수 있도록 설계된 Dart용 가벼운 MIME 유형 라이브러리입니다. 파일 확장자를 해당 MIME 유형에 매핑하는 간단한 API를 제공하여 웹 서비스에서 적절한 콘텐츠 협상이 가능하도록 합니다. 이 패키지는 모바일, 웹, 데스크톱 환경을 포함한 다양한 플랫폼에서 사용하도록 최적화되어 있으며, Dart의 HTTP 서버 프레임워크와 원활하게 통합되어 올바른 Content-Type 헤더를 기대하는 클라이언트와의 호환성을 향상시킵니다.

사용 사례

  • HTTP 서버 콘텐츠 유형 관리
  • 파일 업로드 처리
  • API 응답 헤더 구성
  • 정적 파일 제공
  • 웹 애플리케이션 미들웨어
  • 콘텐츠 협상

주요 기능

  • 확장자에서 MIME 매핑
  • 크로스 플랫폼 호환성
  • 가볍고 빠름
  • HTTP 서버와의 간편한 통합
  • 일반 및 사용자 지정 MIME 유형을 지원합니다
  • 최소 종속성

적합한 대상

  • 다트 HTTP 서버 개발자
  • 웹 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.

카테고리

소스 저장소

이 패키지를 사용하는 프로젝트