|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WordPress\AiClient\Files\Utilities; |
| 6 | + |
| 7 | +/** |
| 8 | + * Utility class for MIME type operations. |
| 9 | + * |
| 10 | + * Provides static methods for working with MIME types, including |
| 11 | + * determining MIME types from file extensions. |
| 12 | + * |
| 13 | + * @since n.e.x.t |
| 14 | + */ |
| 15 | +class MimeTypeUtil |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Common MIME type mappings for file extensions. |
| 19 | + * |
| 20 | + * @var array<string, string> |
| 21 | + */ |
| 22 | + private static array $mimeTypes = [ |
| 23 | + // Text |
| 24 | + 'txt' => 'text/plain', |
| 25 | + 'html' => 'text/html', |
| 26 | + 'htm' => 'text/html', |
| 27 | + 'css' => 'text/css', |
| 28 | + 'js' => 'application/javascript', |
| 29 | + 'json' => 'application/json', |
| 30 | + 'xml' => 'application/xml', |
| 31 | + 'csv' => 'text/csv', |
| 32 | + 'md' => 'text/markdown', |
| 33 | + |
| 34 | + // Images |
| 35 | + 'jpg' => 'image/jpeg', |
| 36 | + 'jpeg' => 'image/jpeg', |
| 37 | + 'png' => 'image/png', |
| 38 | + 'gif' => 'image/gif', |
| 39 | + 'bmp' => 'image/bmp', |
| 40 | + 'webp' => 'image/webp', |
| 41 | + 'svg' => 'image/svg+xml', |
| 42 | + 'ico' => 'image/x-icon', |
| 43 | + |
| 44 | + // Documents |
| 45 | + 'pdf' => 'application/pdf', |
| 46 | + 'doc' => 'application/msword', |
| 47 | + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 48 | + 'xls' => 'application/vnd.ms-excel', |
| 49 | + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 50 | + 'ppt' => 'application/vnd.ms-powerpoint', |
| 51 | + 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
| 52 | + 'odt' => 'application/vnd.oasis.opendocument.text', |
| 53 | + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
| 54 | + |
| 55 | + // Archives |
| 56 | + 'zip' => 'application/zip', |
| 57 | + 'tar' => 'application/x-tar', |
| 58 | + 'gz' => 'application/gzip', |
| 59 | + 'rar' => 'application/x-rar-compressed', |
| 60 | + '7z' => 'application/x-7z-compressed', |
| 61 | + |
| 62 | + // Audio |
| 63 | + 'mp3' => 'audio/mpeg', |
| 64 | + 'wav' => 'audio/wav', |
| 65 | + 'ogg' => 'audio/ogg', |
| 66 | + 'flac' => 'audio/flac', |
| 67 | + 'm4a' => 'audio/m4a', |
| 68 | + |
| 69 | + // Video |
| 70 | + 'mp4' => 'video/mp4', |
| 71 | + 'avi' => 'video/x-msvideo', |
| 72 | + 'mov' => 'video/quicktime', |
| 73 | + 'wmv' => 'video/x-ms-wmv', |
| 74 | + 'flv' => 'video/x-flv', |
| 75 | + 'webm' => 'video/webm', |
| 76 | + 'mkv' => 'video/x-matroska', |
| 77 | + |
| 78 | + // Fonts |
| 79 | + 'ttf' => 'font/ttf', |
| 80 | + 'otf' => 'font/otf', |
| 81 | + 'woff' => 'font/woff', |
| 82 | + 'woff2' => 'font/woff2', |
| 83 | + |
| 84 | + // Other |
| 85 | + 'php' => 'application/x-httpd-php', |
| 86 | + 'sh' => 'application/x-sh', |
| 87 | + 'exe' => 'application/x-msdownload', |
| 88 | + ]; |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets the MIME type for a given file extension. |
| 92 | + * |
| 93 | + * @since n.e.x.t |
| 94 | + * |
| 95 | + * @param string $extension The file extension (without the dot). |
| 96 | + * @return string The MIME type, or 'text/plain' if unknown. |
| 97 | + */ |
| 98 | + public static function getMimeTypeForExtension(string $extension): string |
| 99 | + { |
| 100 | + $extension = strtolower($extension); |
| 101 | + |
| 102 | + return self::$mimeTypes[$extension] ?? 'text/plain'; |
| 103 | + } |
| 104 | +} |
0 commit comments