Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Changed sniffer to arrays

AuthorDave Jarvis <email>
Date2026-02-08 19:13:22 GMT-0800
Commit19a0389ccf5bee2727831f696d2fb19919cc2e0e
Parent370c327
new/MediaTypeSniffer.php
private const FORMATS = [
// Images
- [0x3C, 0x73, 0x76, 0x67, 0x20] => 'image/svg+xml',
- [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] => 'image/png',
- [0xFF, 0xD8, 0xFF, 0xE0] => 'image/jpeg',
- [0xFF, 0xD8, 0xFF, 0xEE] => 'image/jpeg',
- [0xFF, 0xD8, 0xFF, 0xE1, self::ANY, self::ANY, 0x45, 0x78, 0x69, 0x66, 0x00] =>
- 'image/jpeg',
- [0x47, 0x49, 0x46, 0x38] => 'image/gif',
- [0x42, 0x4D] => 'image/bmp',
- [0x49, 0x49, 0x2A, 0x00] => 'image/tiff',
- [0x4D, 0x4D, 0x00, 0x2A] => 'image/tiff',
- [0x52, 0x49, 0x46, 0x46, self::ANY, self::ANY, self::ANY, self::ANY,
- 0x57, 0x45, 0x42, 0x50] => 'image/webp',
- [0x38, 0x42, 0x50, 0x53, 0x00, 0x01] => 'image/vnd.adobe.photoshop',
- [0x8A, 0x4D, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] => 'video/x-mng',
- [0x23, 0x64, 0x65, 0x66] => 'image/x-xbitmap',
- [0x21, 0x20, 0x58, 0x50, 0x4D, 0x32] => 'image/x-xpixmap',
+ [[0x3C, 0x73, 0x76, 0x67, 0x20], 'image/svg+xml'],
+ [[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], 'image/png'],
+ [[0xFF, 0xD8, 0xFF, 0xE0], 'image/jpeg'],
+ [[0xFF, 0xD8, 0xFF, 0xEE], 'image/jpeg'],
+ [[0xFF, 0xD8, 0xFF, 0xE1, self::ANY, self::ANY, 0x45, 0x78, 0x69, 0x66, 0x00], 'image/jpeg'],
+ [[0x47, 0x49, 0x46, 0x38], 'image/gif'],
+ [[0x42, 0x4D], 'image/bmp'],
+ [[0x49, 0x49, 0x2A, 0x00], 'image/tiff'],
+ [[0x4D, 0x4D, 0x00, 0x2A], 'image/tiff'],
+ [[0x52, 0x49, 0x46, 0x46, self::ANY, self::ANY, self::ANY, self::ANY, 0x57, 0x45, 0x42, 0x50], 'image/webp'],
+ [[0x38, 0x42, 0x50, 0x53, 0x00, 0x01], 'image/vnd.adobe.photoshop'],
+ [[0x8A, 0x4D, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], 'video/x-mng'],
+ [[0x23, 0x64, 0x65, 0x66], 'image/x-xbitmap'],
+ [[0x21, 0x20, 0x58, 0x50, 0x4D, 0x32], 'image/x-xpixmap'],
// Documents/Text
- [0x3C, 0x21] => 'text/html',
- [0x3C, 0x68, 0x74, 0x6D, 0x6C] => 'text/html',
- [0x3C, 0x68, 0x65, 0x61, 0x64] => 'text/html',
- [0x3C, 0x62, 0x6F, 0x64, 0x79] => 'text/html',
- [0x3C, 0x48, 0x54, 0x4D, 0x4C] => 'text/html',
- [0x3C, 0x48, 0x45, 0x41, 0x44] => 'text/html',
- [0x3C, 0x42, 0x4F, 0x44, 0x59] => 'text/html',
- [0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20] => 'text/xml',
- [0xFE, 0xFF, 0x00, 0x3C, 0x00, 0x3f, 0x00, 0x78] => 'text/xml',
- [0xFF, 0xFE, 0x3C, 0x00, 0x3F, 0x00, 0x78, 0x00] => 'text/xml',
- [0x25, 0x50, 0x44, 0x46, 0x2D] => 'application/pdf',
- [0x25, 0x21, 0x50, 0x53, 0x2D, 0x41, 0x64, 0x6F, 0x62, 0x65, 0x2D] =>
- 'application/postscript',
- [0x25, 0x21, 0x50, 0x53] => 'application/postscript',
+ [[0x3C, 0x21], 'text/html'],
+ [[0x3C, 0x68, 0x74, 0x6D, 0x6C], 'text/html'],
+ [[0x3C, 0x68, 0x65, 0x61, 0x64], 'text/html'],
+ [[0x3C, 0x62, 0x6F, 0x64, 0x79], 'text/html'],
+ [[0x3C, 0x48, 0x54, 0x4D, 0x4C], 'text/html'],
+ [[0x3C, 0x48, 0x45, 0x41, 0x44], 'text/html'],
+ [[0x3C, 0x42, 0x4F, 0x44, 0x59], 'text/html'],
+ [[0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20], 'text/xml'],
+ [[0xFE, 0xFF, 0x00, 0x3C, 0x00, 0x3f, 0x00, 0x78], 'text/xml'],
+ [[0xFF, 0xFE, 0x3C, 0x00, 0x3F, 0x00, 0x78, 0x00], 'text/xml'],
+ [[0x25, 0x50, 0x44, 0x46, 0x2D], 'application/pdf'],
+ [[0x25, 0x21, 0x50, 0x53, 0x2D, 0x41, 0x64, 0x6F, 0x62, 0x65, 0x2D], 'application/postscript'],
+ [[0x25, 0x21, 0x50, 0x53], 'application/postscript'],
// Audio/Video
- [0xFF, 0xFB, self::ANY] => 'audio/mpeg',
- [0x49, 0x44, 0x33] => 'audio/mpeg',
- [0x2E, 0x73, 0x6E, 0x64] => 'audio/basic',
- [0x64, 0x6E, 0x73, 0x2E] => 'audio/basic',
- [0x52, 0x49, 0x46, 0x46, self::ANY, self::ANY, self::ANY, self::ANY,
- 0x57, 0x41, 0x56, 0x45] => 'audio/wav',
+ [[0xFF, 0xFB, self::ANY], 'audio/mpeg'],
+ [[0x49, 0x44, 0x33], 'audio/mpeg'],
+ [[0x2E, 0x73, 0x6E, 0x64], 'audio/basic'],
+ [[0x64, 0x6E, 0x73, 0x2E], 'audio/basic'],
+ [[0x52, 0x49, 0x46, 0x46, self::ANY, self::ANY, self::ANY, self::ANY, 0x57, 0x41, 0x56, 0x45], 'audio/wav'],
// Archives/Binaries
- [0x50, 0x4B, 0x03, 0x04] => 'application/zip',
- [0x50, 0x4B, 0x05, 0x06] => 'application/zip',
- [0x50, 0x4B, 0x07, 0x08] => 'application/zip',
- [0x1F, 0x8B, 0x08] => 'application/gzip',
- [0x42, 0x5A, 0x68] => 'application/x-bzip2',
- [0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00] => 'application/x-xz',
- [0x52, 0x61, 0x72, 0x21, 0x1A, 0x07] => 'application/vnd.rar',
- [0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C] => 'application/x-7z-compressed',
+ [[0x50, 0x4B, 0x03, 0x04], 'application/zip'],
+ [[0x50, 0x4B, 0x05, 0x06], 'application/zip'],
+ [[0x50, 0x4B, 0x07, 0x08], 'application/zip'],
+ [[0x1F, 0x8B, 0x08], 'application/gzip'],
+ [[0x42, 0x5A, 0x68], 'application/x-bzip2'],
+ [[0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00], 'application/x-xz'],
+ [[0x52, 0x61, 0x72, 0x21, 0x1A, 0x07], 'application/vnd.rar'],
+ [[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C], 'application/x-7z-compressed'],
// Executables/System
- [0x41, 0x43, self::ANY, self::ANY, self::ANY, self::ANY, 0x00, 0x00,
- 0x00, 0x00, 0x00] => 'application/acad',
- [0xCA, 0xFE, 0xBA, 0xBE] => 'application/java-vm',
- [0xAC, 0xED] => 'application/x-java-serialized-object',
- [0x4D, 0x5A] => 'application/x-msdownload',
- [0x7F, 0x45, 0x4C, 0x46] => 'application/x-elf',
- [0xCE, 0xFA, 0xED, 0xFE] => 'application/x-mach-binary',
- [0xCF, 0xFA, 0xED, 0xFE] => 'application/x-mach-binary',
- [0xFE, 0xED, 0xFA, 0xCE] => 'application/x-mach-binary',
- [0xFE, 0xED, 0xFA, 0xCF] => 'application/x-mach-binary',
+ [[0x41, 0x43, self::ANY, self::ANY, self::ANY, self::ANY, 0x00, 0x00, 0x00, 0x00, 0x00], 'application/acad'],
+ [[0xCA, 0xFE, 0xBA, 0xBE], 'application/java-vm'],
+ [[0xAC, 0xED], 'application/x-java-serialized-object'],
+ [[0x4D, 0x5A], 'application/x-msdownload'],
+ [[0x7F, 0x45, 0x4C, 0x46], 'application/x-elf'],
+ [[0xCE, 0xFA, 0xED, 0xFE], 'application/x-mach-binary'],
+ [[0xCF, 0xFA, 0xED, 0xFE], 'application/x-mach-binary'],
+ [[0xFE, 0xED, 0xFA, 0xCE], 'application/x-mach-binary'],
+ [[0xFE, 0xED, 0xFA, 0xCF], 'application/x-mach-binary'],
];
/**
* Sniffs the media type based on magic bytes (the first few bytes)
- * of the data. This internal method is the primary detection.
+ * of the data.
*
* @param string $data The raw binary data (a string of bytes).
}
- foreach( self::FORMATS as $pattern => $type ) {
+ // Updated to destructure the list of arrays [pattern, type]
+ foreach( self::FORMATS as [$pattern, $type] ) {
$patternLength = count( $pattern );
private static function getMediaTypeByExtension( $filePath ): string {
$extension = strtolower( pathinfo( $filePath, PATHINFO_EXTENSION ) );
-
return self::EXTENSION_MAP[$extension] ?? 'application/octet-stream';
}
}
?>
+
Delta53 lines added, 57 lines removed, 4-line decrease