mime_type 0.2.4

  • Readme
  • Changelog
  • Installing
  • 92

MIME type #

Library to get MIME type from a file name or a file path. 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 two 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')

Both methods return null If MIME type for the file is not available.

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.

Changelog #

0.2.4 #

  • prepare for Uint8List SDK breaking change

0.2.3 #

  • forward compatible change in test/apache-types_includer.dart for upcoming SDK change

0.2.2 #

  • add dart environment information

0.2.1 #

  • Type woff2 added.

0.2.0 #

  • Included Apache-types.
  • Changed MIME type for extension 'dart' from 'application/dart' to 'application/vnd.dart'.

0.1.1 to 0.1.7 #

  • Several types added.

0.1.0 #

  • First publishing.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  mime_type: ^0.2.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:mime_type/mime_type.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
93
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
82
Overall:
Weighted score of the above. [more]
92
Learn more about scoring.

We analyzed this package on Oct 7, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:mime_type/mime_type.dart.

Health issues and suggestions

Document public APIs. (-1 points)

3 out of 3 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/mime_type.dart. (-0.50 points)

Analysis of lib/mime_type.dart reported 1 hint:

line 10 col 5: DO use curly braces for all flow control structures.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and mime_type.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

The package description is too short. (-8 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=0.8.10 <3.0.0