Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Detect HEIC/HEIF format by checking header bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
CORDEA committed Dec 2, 2023
1 parent 8ebf946 commit dfb5a6a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 1.0.5-wip

* Update video/mp4 mimeType lookup by header bytes.
* Add image/heic mimeType lookup by header bytes.
* Add image/heif mimeType lookup by header bytes.
* Add m4b mimeType lookup by extension.
* Add `text/markdown` mimeType lookup by extension.
* Require Dart 3.0.0.
Expand Down
95 changes: 95 additions & 0 deletions lib/src/magic_number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,99 @@ const List<MagicNumber> initialMagicNumbers = [
]),

MagicNumber('font/woff2', [0x77, 0x4f, 0x46, 0x32]),

/// High Efficiency Image File Format (ISO/IEC 23008-12).
/// -> 4 bytes indicating the ftyp box length.
/// -> 4 bytes have the ASCII characters 'f' 't' 'y' 'p'.
/// -> 4 bytes have the ASCII characters 'h' 'e' 'i' 'c'.
/// https://www.iana.org/assignments/media-types/image/heic
MagicNumber('image/heic', [
0x00,
0x00,
0x00,
0x00,
0x66,
0x74,
0x79,
0x70,
0x68,
0x65,
0x69,
0x63
], mask: [
0x00,
0x00,
0x00,
0x00,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF
]),

/// -> 4 bytes indicating the ftyp box length.
/// -> 4 bytes have the ASCII characters 'f' 't' 'y' 'p'.
/// -> 4 bytes have the ASCII characters 'h' 'e' 'i' 'x'.
MagicNumber('image/heic', [
0x00,
0x00,
0x00,
0x00,
0x66,
0x74,
0x79,
0x70,
0x68,
0x65,
0x69,
0x78
], mask: [
0x00,
0x00,
0x00,
0x00,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF
]),

/// -> 4 bytes indicating the ftyp box length.
/// -> 4 bytes have the ASCII characters 'f' 't' 'y' 'p'.
/// -> 4 bytes have the ASCII characters 'm' 'i' 'f' '1'.
MagicNumber('image/heif', [
0x00,
0x00,
0x00,
0x00,
0x66,
0x74,
0x79,
0x70,
0x6D,
0x69,
0x66,
0x31
], mask: [
0x00,
0x00,
0x00,
0x00,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF
]),
];
47 changes: 47 additions & 0 deletions test/mime_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void main() {
_expectMimeType('file.toml', 'application/toml');
_expectMimeType('file.md', 'text/markdown');
_expectMimeType('file.markdown', 'text/markdown');
_expectMimeType('file.heif', 'image/heif');
_expectMimeType('file.heic', 'image/heic');
});

test('unknown-mime-type', () {
Expand Down Expand Up @@ -233,6 +235,51 @@ void main() {
0x56,
0x45
]);
_expectMimeType('file', 'image/heic', headerBytes: [
0x00,
0x00,
0x00,
0x18,
0x66,
0x74,
0x79,
0x70,
0x68,
0x65,
0x69,
0x63,
0x00
]);
_expectMimeType('file', 'image/heic', headerBytes: [
0x00,
0x00,
0x00,
0x18,
0x66,
0x74,
0x79,
0x70,
0x68,
0x65,
0x69,
0x78,
0x00
]);
_expectMimeType('file', 'image/heif', headerBytes: [
0x00,
0x00,
0x00,
0x18,
0x66,
0x74,
0x79,
0x70,
0x6D,
0x69,
0x66,
0x31,
0x00
]);
});
});

Expand Down

0 comments on commit dfb5a6a

Please sign in to comment.