31

I'm trying to find a package which would recognise file type. For example

final path = "/some/path/to/file/file.jpg";

should be recognised as image or

final path = "/some/path/to/file/file.doc";

should be recognised as document

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
delmin
  • 2,330
  • 5
  • 28
  • 54

4 Answers4

58

You can make use of the mime package from the Dart team to extract the MIME types from file names:

import 'package:mime/mime.dart';

final mimeType = lookupMimeType('/some/path/to/file/file.jpg'); // 'image/jpeg'

Helper functions

If you want to know whether a file path represents an image, you can create a function like this:

import 'package:mime/mime.dart';

bool isImage(String path) {
  final mimeType = lookupMimeType(path);

  return mimeType.startsWith('image/');
}

Likewise, if you want to know if a path represents a document, you can write a function like this:

import 'package:mime/mime.dart';

bool isDocument(String path) {
  final mimeType = lookupMimeType(path);

  return mimeType == 'application/msword';
}

You can find lists of MIME types at IANA or look at the extension map in the mime package.

From file headers

With the mime package, you can even check against header bytes of a file:

final mimeType = lookupMimeType('image_without_extension', headerBytes: [0xFF, 0xD8]); // jpeg
creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • 1
    oh man thank you very much.. actually that is main reason I asked about it.. I'm using mime in my backend and therefore I need this information in my front end... Thank you very much. Now I feel like an idiot not to at least try to look for that package – delmin Jun 13 '20 at 15:24
  • can you please explain how to actually use it in MultiPart http request.. I already asked about it with no answer.. It looks to me that with this package I don't really need to specify type and extension in MediaType.. Just can not figure out how to do it? https://stackoverflow.com/questions/62344958/how-to-get-type-of-uploaded-file-into-mediatype – delmin Jun 13 '20 at 15:45
  • 1
    whats the 0xFF, 0xD8 for? – Choy Sep 24 '21 at 08:33
  • 1
    It doesn't support a lot of mime types though, like HEIC or BMP. – Oliver Dixon Apr 10 '22 at 10:24
  • @OliverDixon you can contribute to a package :) – Behzod Faiziev Jan 15 '23 at 17:06
  • @creativecreatorormaybenot How can I find the mime type of pdf file that doesn't have a .pdf extension on the filename?? headerBytes: [0xFF, 0xD8] always return mime type as image/jpeg, so how is it possible to identify a document without any extension ?? – KJEjava48 May 23 '23 at 06:45
  • @KJEjava48 You will have to pass the actual header bytes of the file. `[0xFF, 0xD8]` is only applicable for the jpeg example. – creativecreatorormaybenot Jul 31 '23 at 10:43
8

There is no need of any extension. You can try below code snippet.

String getFileExtension(String fileName) {
 return "." + fileName.split('.').last;
}
Yash
  • 1,787
  • 1
  • 22
  • 23
1

If think you should take a look to path package, specially to extension method.
You can get file format without adding one more package to pubspec.yaml ;)

context.extension('foo.bar.dart.js', 2);   // -> '.dart.js
context.extension('foo.bar.dart.js', 3);   // -> '.bar.dart.js'
context.extension('foo.bar.dart.js', 10);  // -> '.bar.dart.js'
context.extension('path/to/foo.bar.dart.js', 2);  // -> '.dart.js'
ShomaEA
  • 61
  • 1
  • 4
-2

If the filename contains extension then use below code :

import 'package:mime/mime.dart';

File file = getFile();  // use any filepicker
final mimeType = lookupMimeType(file.path);

if the filename doesn't have an extension then use :

var dataHeader = await file.readAsBytes();
mimeType = lookupMimeType(file.path, headerBytes: dataHeader);

Full code :

String? mimeType = lookupMimeType(file!.path);
if(mimeType == null) {
   var dataHeader = await file.readAsBytes();
   mimeType = lookupMimeType(file!.path, headerBytes: dataHeader);
}
KJEjava48
  • 1,967
  • 7
  • 40
  • 69
  • It will read the entire file to get the `headerBytes`. Press F for the user's memory if they select a 10GB file. – Alex Rintt Jun 30 '23 at 14:52
  • @AlexRintt then what will be the best way to find the mimetype of a file( with or without the extension ) with minimum load ? – KJEjava48 Jul 08 '23 at 07:34
  • Use openRead() from File class. It returns a stream that you can manipulate to load the first kilobyte only. – Alex Rintt Jul 09 '23 at 20:34