Skip to content

Commit 21b7e9b

Browse files
authored
Refactor: Group mp4 and heic with other offset-based types (#28)
* Refactor: Group mp4 and heic with other offset-based types Moves the `mp4` and `heic` cases in the `get` method to group them with other file types that use an `offset` match type. This change improves code readability and organization without altering functionality. * feat: Use byRange matching for mp4 and heic Moves `FileMagicNumberType.mp4` and `FileMagicNumberType.heic` to use the `byRange` matching strategy instead of `exact`. This change groups them with other formats like `webp`, `wav`, and `avi` that already use range-based magic number matching. * feat: Add test case for combined file types Adds a new test to verify that `detectFileTypeFromBytes` correctly identifies a file type based on its initial magic number, even when it contains another file type embedded within it. Specifically, the test uses a byte slice representing a PDF that contains a JP2 file signature and confirms that it is correctly detected as a PDF. * feat: Add test case for combined file types Adds a new test to verify that `detectFileTypeFromBytes` correctly identifies a file type based on its initial magic number, even when it contains another file type embedded within it. Specifically, the test uses a byte slice representing a PDF that contains a JP2 file signature and confirms that it is correctly detected as a PDF. * fix: Correctly identify PDFs containing JP2 images Bumps the version to 1.4.1. This patch addresses an issue where a PDF file containing a JPEG 2000 (JP2) image was incorrectly identified as a JP2 file. Changes include: - Corrected the file type detection logic to properly distinguish between a standalone JP2 file and a PDF embedding a JP2. - Added a new test case to verify the fix and prevent future regressions for PDFs that contain other file types. * Fix: Update CHANGELOG for 1.4.1 Adds the issue number to the changelog entry for version 1.4.1, referencing the fix for an issue where a PDF containing a JP2 image was incorrectly identified as a JP2 file.
1 parent 019fc10 commit 21b7e9b

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.4.1
2+
* Fixed an error that confused a PDF with a JP2 image inside, with a JP2 file [#27](https://github.com/vicajilau/file_magic_number/issues/27).
3+
* Added new test about pdfs with another files inside (ej: pdfs with jp2)
4+
15
## 1.4.0
26
* Fixed an issue where JPEG files were detected as TIFF files [#17](https://github.com/vicajilau/file_magic_number/issues/17).
37
* Added webp support.

lib/src/file_magic_number_match_type.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ enum FileMagicNumberMatchType {
1919
/// This method is used internally by the library during file type detection.
2020
static FileMagicNumberMatchType get(FileMagicNumberType type) {
2121
switch (type) {
22-
case FileMagicNumberType.mp4:
23-
case FileMagicNumberType.heic:
2422
case FileMagicNumberType.pdf:
2523
return offset;
2624
case FileMagicNumberType.webp:
2725
case FileMagicNumberType.wav:
2826
case FileMagicNumberType.avi:
2927
return byRange;
28+
case FileMagicNumberType.mp4:
29+
case FileMagicNumberType.heic:
3030
default:
3131
return exact;
3232
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: file_magic_number
22
description: "A Flutter package to detect file types based on their magic number instead of MIME types. Supports Flutter on mobile, desktop, and web without native code."
3-
version: 1.4.0
3+
version: 1.4.1
44
homepage: https://github.com/vicajilau/file_magic_number
55

66
environment:

test/file_magic_number_bytes_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,51 @@ void main() {
9090
expect(result, FileMagicNumberType.wav);
9191
});
9292

93+
test('Detects combined files (e.g., PDF containing a JP2)', () {
94+
// A PDF file can contain other file types. The detection should identify
95+
// it as PDF based on the initial bytes, not the embedded content.
96+
// PDF magic number: %PDF (0x25 0x50 0x44 0x46)
97+
// JP2 magic number: ....jP J.. (0x00 0x00 0x00 0x0C 0x6A 0x50 0x20 0x20 0x0D 0x0A)
98+
final bytes = Uint8List.fromList([
99+
0x25, 0x50, 0x44, 0x46, // PDF header
100+
0x2D, 0x31, 0x2E, 0x37, // PDF version
101+
0x0A, 0x25, 0xC4, 0xE5, 0xF2, 0xE5, 0xEB, 0xA7, // Some more PDF data
102+
0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A // Embedded JP2 signature
103+
]);
104+
final result = FileMagicNumber.detectFileTypeFromBytes(bytes);
105+
expect(result, FileMagicNumberType.pdf);
106+
});
107+
108+
test('Detects combined files (e.g., PDF containing an image)', () {
109+
// A PDF file can contain other file types. The detection should identify
110+
// it as PDF based on the initial bytes, not the embedded content.
111+
// PDF magic number: %PDF (0x25 0x50 0x44 0x46)
112+
// PNG magic number: 0x89 0x50 0x4E 0x47
113+
final bytes = Uint8List.fromList([
114+
0x25, 0x50, 0x44, 0x46, // PDF header
115+
0x2D, 0x31, 0x2E, 0x37, // PDF version
116+
0x0A, 0x31, 0x20, 0x30, 0x20, 0x6F, 0x62, 0x6A, // Some PDF objects
117+
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A // Embedded PNG signature
118+
]);
119+
final result = FileMagicNumber.detectFileTypeFromBytes(bytes);
120+
expect(result, FileMagicNumberType.pdf);
121+
});
122+
123+
test('Detects combined files (e.g., PDF containing an MP4)', () {
124+
// A PDF file can contain other file types. The detection should identify
125+
// it as PDF based on the initial bytes, not the embedded content.
126+
// PDF magic number: %PDF (0x25 0x50 0x44 0x46)
127+
// MP4 magic number (ftypisom): 0x66 0x74 0x79 0x70 0x69 0x73 0x6F 0x6D
128+
final bytes = Uint8List.fromList([
129+
0x25, 0x50, 0x44, 0x46, // PDF header
130+
0x2D, 0x31, 0x2E, 0x37, // PDF version
131+
0x0A, 0x31, 0x20, 0x30, 0x20, 0x6F, 0x62, 0x6A, // Some PDF objects
132+
0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6F, 0x6D // Embedded MP4 signature
133+
]);
134+
final result = FileMagicNumber.detectFileTypeFromBytes(bytes);
135+
expect(result, FileMagicNumberType.pdf);
136+
});
137+
93138
test('Detects WebP file', () {
94139
// RIFF....WEBP (offset 0: "RIFF", offset 8: "WEBP")
95140
final bytes = Uint8List.fromList([

0 commit comments

Comments
 (0)