dart-lang / tools

This repository is home to tooling related Dart packages.
BSD 3-Clause "New" or "Revised" License
29 stars 20 forks source link

Should can determine HEIC file by lookupMimeType #401

Open merry-mongoose opened 4 years ago

merry-mongoose commented 4 years ago

The original image format is HEIC on iOS. The mime of this file type has registered at https://www.iana.org/assignments/media-types/media-types.xhtml but lookupMimeType method returned null currently. Thanks

duzenko commented 4 years ago

Is this repo dead?

xbadal commented 3 years ago

Same issue!

ajain-bst commented 2 years ago

facing the same issue. Can anyone help?

jakobleck commented 2 years ago

Same. :)

ollyde commented 2 years ago

Test functions that work. You could also extend BMP to read the file bytes.

Platform file is the same as "File" from Dart IO.

bool isHeic(PlatformFile platformFile) {
  final heicBytes = [0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63];

  if (kIsWeb || platformFile.bytes != null) {
    final bytes = platformFile.bytes!;
    if (listEquals(heicBytes, [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]])) {
      return true;
    }
  }
  if (platformFile.path != null) {
    // Better to read path first, some platforms you can't load bytes.
    if (platformFile.path!.toLowerCase().contains("heic")) {
      return true;
    }
  }
  return false;
}

bool isBmp(PlatformFile platformFile) {
  final bmpBytes = [0x42, 0x4d];

  if (kIsWeb || platformFile.bytes != null) {
    final bytes = platformFile.bytes!;
    if (listEquals(bmpBytes, [bytes[0], bytes[1]])) {
      return true;
    }
  }
  return false;
}