Giter Club home page Giter Club logo

mime's Introduction

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'));

mime's People

Contributors

cedvdb avatar chalin avatar cordea avatar dependabot[bot] avatar devoncarew avatar dgrove avatar floitschg avatar franklinyow avatar hannesbraun avatar ivangolubykh avatar jakemac53 avatar keertip avatar kenzieschmoll avatar kevmoo avatar kwalrath avatar leafpetersen avatar literallylara avatar lrhn avatar mfrancis107 avatar mgenware avatar michaelrfairhurst avatar mkustermann avatar natebosch avatar nilsreichardt avatar rodineijf avatar sgjesse avatar simolus3 avatar szakarias avatar tomk9 avatar zi6xuan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mime's Issues

Add content type-to-extension conversion

Currently, the package only does file-to-content type conversion via the lookupMimeType function. As the extension maps already exist, it'd be easy to add both sides of the lookup, something like:

print(lookupExtension('text/html'));  // html
print(lookupExtension('application/pdf'));  // pdf

Incorrect mime type

Get one .xls file and rename it by removing extension and adding .doc extension.

Checking mime type with header return application/msword, but expected result is application/vnd.ms-excel

Code Example:

        final Uint8List imgData = imgFile.readAsBytesSync();
        final mimeType = lookupMimeType(imgFile.path, headerBytes: imgData);

video/quicktime in mp4 files

Hello

lookupMimeType(file.path)

In "file" is XFile (image_picker), return video/quicktime, but file is .mp4.

Using this Simulator for iOS.

How to resolve?

mime: 1.0.4
Flutter 3.7.12 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4d9e56e694 (4 weeks ago) • 2023-04-17 21:47:46 -0400
Engine • revision 1a65d409c7
Tools • Dart 2.19.6 • DevTools 2.20.1

lookupMimeType(path, headerBytes) returning TRUE on passing incorrect headerBytes as well

Action:

  • For a particular file, verify whether the MIME type coming from its extension name and header bytes are both same.
  • Task performed: lookupMimeType('file.jpeg', headerBytes: [0x89])

Expected behaviour:
FALSE

Actual result:
TRUE

Description:

  • As per the magic_number.dart, the magic bytes of JPEG file is [0xFF, 0xD8].
  • So, calling lookupMimeType('file.jpeg', headerBytes: [0x89]) should have returned FALSE, instead of TRUE.
  • I'm passing header bytes explicitly, so the match must fail if the header bytes don't match with the magic bytes.

webm is detected as audio/webm but should be video/webm

Thank you very much for your package which makes a lot of things in our app much easier. After some investigation we detected a minor problem:

If you check here: https://www.google.com/search?q=webm+file+mime+type&rlz=1C5CHFA_enDE983DE983&oq=webm+file+mime+type&aqs=chrome..69i57j33i160l2.4414j0j4&sourceid=chrome&ie=UTF-8 webm can be both video or audio. But this package defaults to audio which leads to a problem in my app that sent videos are displayed with an audioplayer. However I would default to video because then the worst case would be to play an audio file with a video player which would still work. What do you think?

await MimeMultipart blocking GUI

final parts = await transformer.bind(myBytes);
await for (final part in parts) {
// await MimeMultipart blocking GUI, is too slow.
  writePart(part);
}

Any solution?

add feature to query mimeType for it's top level type

It would be useful to have a method to match the top level type, like: bool isType(MimeTypeResolver.VIDEO, [filepath/file/mimeType]) such that it would return true in this example: isType(MimeTypeResolver.VIDEO, '/example.mp4') and false for: isType(MimeTypeResolver.AUDIO, '/example.txt')

Blob type always null

I've noticed that there is no a return type in the default_extension_map.dart for blob files, therefore I'm always getting null while bringing a blob file to lookupMimeType

Migrating to null safety

I was wondering if there are any plans to migrate this package to null safety?

I would be happy to have an attempt at null safety migration if there are not.

improve the detection of mp4 types

The first 8 bytes in magic_number.dart for video/mp4 are correct but the final 4 bytes make it more specific than it needs to be as they refer to the subtype. From a bit of experimenting it looks like dropping the last 4 elements from there (and the mask) would fix this. All the references that I can find list an 8 byte header.

lookupMimeType should check for a `null` filename

In http we just added mime as a dependency for support of multipart forms but are hitting an issue with the lookupMimeType.

When creating a MultipartFile there is an optional filename parameter. We also have a List<int> present which can be used for detecting magic number. This can result in a call to lookupMimeType where path is null.

Could lookupMimeType make both parameters optional and thus add a null check for path?

no events from MimeMultipart.listen() when called asynchronously

Originally opened as dart-lang/sdk#20140

This issue was originally filed by [email protected]


What steps will reproduce the problem?

  1. Create an HTTP server
  2. Transform requests with MimeMultipartTransformer
  3. listen() on the resulting MimeMultipart object after a short Timer callback

What is the expected output? What do you see instead?

I expect to get data events the way I should from listen(). Instead, I usually get zero callbacks. If I was uploading a large file, I sometimes get one empty array. It is not very impressive.

What version of the product are you using?

The mime library version 0.9.0+3, Dart version 1.5.3.

On what operating system?

Mac OS X

What browser (if applicable)?

N/A

Please provide any additional information below.

I have recreated this bug in an isolated project here: https://github.com/unixpickle/dart-multipart-bug. Additionally, that repository includes a patch which fixes the issue. Somebody else should look over my patch before verifying it, but it's pretty small.

Please publish a new version

The version pinned in sdk/DEPS is above the last version published to pub and tagged. Please make a new version and publish.

mime is HEIC,but my image suffix is jpeg,return mime type is image/jpeg

[✓] Flutter (Channel stable, 3.3.10, on macOS 14.1.1 23B81 darwin-arm, locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
✗ cmdline-tools component is missing
Run path/to/sdkmanager --install "cmdline-tools;latest"
See https://developer.android.com/studio/command-line for more details.
✗ Android license status unknown.
Run flutter doctor --android-licenses to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
[✓] Chrome - develop for the web
[!] Android Studio (version 2022.3)
✗ Unable to find bundled Java version.
[✓] IntelliJ IDEA Ultimate Edition (version 2023.2.1)
[✓] Connected device (4 available)
[✓] HTTP Host Availability

image

but when i use lookupMimeType to get mime,return image/jpeg

image:

6b8d367ed637ccb5a7e4c4fd343b45a4

extension map - out-of-date

Can extension map in this library be updated to allow IOC or provide a better way of keeping the extension out of the code base itself via filesystem resource?

Incorrect parsing of file content in latin1 encoding

It seems that if I try to read an image file in order to retrieve magic numbers from it so I could better detect the mime type of the file, iOS doesn't appear to be friendly about it.

I am using the following code to extract the first 20 hex codes from the file:

List<int> fileBytes = File(path).readAsBytesSync().toList();
List<int> header = [];
int i = 0;
String output = '';
fileBytes.forEach((element){
  if(element == 0) return;
  if (i > 20) return;
  output += element.toRadixString(16) + " ";
  header.add(element);
  i++;
});

In addition to that, I am also trying to read the file content into a string, with the code below:

String output = File(path).readAsStringSync(encoding: latin1);

In order to replicate the issue, I am using this image as an example.

On Android the header variable gives me the following hex:

52 49 46 46 b0 5b 57 45 42 50 56 50 38 20 a4 5b 10 c6 1 9d 1

Looking at the list of magic numbers this hex codes is for the WebP image file, which is correct.

Even the file content (the content variable) starts with:
android

However, running the same code on iOS, produces this for hex:

89 50 4e 47 d a 1a a d 49 48 44 52 2 bc 1 90 8 6 4c 3c

This corresponds to the PNG image format, which is wrong. Even the file content is incorrect, as it start as the code below:
ios

I can say for certain that the problem is not with the iOS emulator as the same issue appears on a physical device. It is also appearing on iOS 14+ version (as I know iOS just recently introduced WebP support). Regardless, I don't believe the issue is with the image format support.

I am having a hard time understanding why the same file is being read so differently on iOS. On android, it is read exactly as it's expected, while on iOS it's like the OS is converting the file to a format it understand and gives that to the flutter app to use.

So, if there's anyone who can give me a better explanation on why this happens, I would be extremely grateful. I have also submitted this issue on the flutter bug tracker.

.XLS | .XLSX | .CSV in lookupMimeType

lookupMimeType is it possible to add support for extensions .XLS | .XLSX | .CSV ?

String mimeType = lookupMimeType('test', headerBytes: file.take(defaultMagicNumbersMaxLength).toList()) ?? '';

mimeType return null

Support for m4a filetypes

Similar to the mp4 video codec, would be helpful to have audio only codec: m4a supported under lookupMimeType.
Thanks.

lookupMimeType returns null for .txt files

Hello.

'lookupMimeType' returns null for .txt files.

File file = new File('${Directory.current.path}/test/assets/file.txt')
Uint8List value = file.readAsBytesSync();
String mimeType = lookupMimeType('no-file', headerBytes: value);
print(mimeType);

Have a good day ! :)

Not strong mode clean

Running the latest analyzer with dartanalyzer --strong reveals several strong mode issues in bound_multipart_stream.dart (unsound implicit cast).

mime: please add 'text/x-markdown' for markdown file extensions (md, markdown) to the _defaultExtensionMap

Originally opened as dart-lang/sdk#12857

This issue was originally filed by [email protected]


0.6.21_r26639

As markdown is quite common these days, and this change does not conflict with anything currently in the _defaultExtensionMap, I think it is reasonable to add it?

'md':'text/x-markdown',
'markdown':'text/x-markdown',

The mime type 'text/x-markdown' seems to be what is most commonly used, for example:

node-mime: https://github.com/broofa/node-mime/pull/48/files
CodeMirror: http://codemirror.net/mode/markdown/

thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.