1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Defr\PhpMimeType;
13:
14: use Symfony\Component\HttpFoundation\Response;
15: use Symfony\Component\HttpFoundation\ResponseHeaderBag;
16:
17: 18: 19:
20: class MimeType
21: {
22: 23: 24:
25: public static $mimeTypes = [
26: 'txt' => 'text/plain',
27: 'csv' => 'text/csv',
28: 'htm' => 'text/html',
29: 'html' => 'text/html',
30: 'php' => 'text/html',
31: 'css' => 'text/css',
32: 'js' => 'application/javascript',
33: 'json' => 'application/json',
34: 'xml' => 'application/xml',
35: 'swf' => 'application/x-shockwave-flash',
36: 'flv' => 'video/x-flv',
37:
38: 'png' => 'image/png',
39: 'jpe' => 'image/jpeg',
40: 'jpeg' => 'image/jpeg',
41: 'jpg' => 'image/jpeg',
42: 'gif' => 'image/gif',
43: 'bmp' => 'image/bmp',
44: 'ico' => 'image/vnd.microsoft.icon',
45: 'tiff' => 'image/tiff',
46: 'tif' => 'image/tiff',
47: 'svg' => 'image/svg+xml',
48: 'svgz' => 'image/svg+xml',
49:
50: 'zip' => 'application/zip',
51: 'rar' => 'application/x-rar-compressed',
52: 'exe' => 'application/x-msdownload',
53: 'msi' => 'application/x-msdownload',
54: 'cab' => 'application/vnd.ms-cab-compressed',
55:
56: 'mpg' => 'audio/mpeg',
57: 'mp2' => 'audio/mpeg',
58: 'mp3' => 'audio/mpeg',
59: 'mp4' => 'audio/mp4',
60: 'qt' => 'video/quicktime',
61: 'mov' => 'video/quicktime',
62: 'ogg' => 'audio/ogg',
63: 'oga' => 'audio/ogg',
64: 'wav' => 'audio/wav',
65: 'webm' => 'audio/webm',
66: 'aac' => 'audio/aac',
67:
68: 'pdf' => 'application/pdf',
69: 'psd' => 'image/vnd.adobe.photoshop',
70: 'ai' => 'application/postscript',
71: 'eps' => 'application/postscript',
72: 'ps' => 'application/postscript',
73:
74: 'doc' => 'application/msword',
75: 'dot' => 'application/msword',
76: 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
77: 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
78: 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
79: 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
80: 'odt' => 'application/vnd.oasis.opendocument.text',
81: 'rtf' => 'application/rtf',
82: 'xls' => 'application/vnd.ms-excel',
83: 'xlt' => 'application/vnd.ms-excel',
84: 'xla' => 'application/vnd.ms-excel',
85: 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
86: 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
87: 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
88: 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
89: 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
90: 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
91: 'ppt' => 'application/vnd.ms-powerpoint',
92: 'pot' => 'application/vnd.ms-powerpoint',
93: 'pps' => 'application/vnd.ms-powerpoint',
94: 'ppa' => 'application/vnd.ms-powerpoint',
95: 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
96: 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
97: 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
98: 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
99: 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
100: 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
101: 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
102: 'mdb' => 'application/vnd.ms-access',
103: 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
104: ];
105:
106: 107: 108:
109: public static $fa = [
110:
111: 'image' => 'fa-file-image-o',
112: 'audio' => 'fa-file-audio-o',
113: 'video' => 'fa-file-video-o',
114:
115: 'application/pdf' => 'fa-file-pdf-o',
116: 'pdf' => 'fa-file-pdf-o',
117: 'application/msword' => 'fa-file-word-o',
118: 'application/vnd.ms-word' => 'fa-file-word-o',
119: 'application/vnd.oasis.opendocument.text' => 'fa-file-word-o',
120: 'application/vnd.openxmlformats-officedocument.wordprocessingml' => 'fa-file-word-o',
121: 'application/vnd.ms-excel' => 'fa-file-excel-o',
122: 'application/vnd.openxmlformats-officedocument.spreadsheetml' => 'fa-file-excel-o',
123: 'application/application/vnd.oasis.opendocument.spreadsheet' => 'fa-file-excel-o',
124: 'application/vnd.oasis.opendocument.spreadsheet' => 'fa-file-excel-o',
125: 'csv' => 'fa-file-excel-o',
126: 'application/vnd.ms-powerpoint' => 'fa-file-powerpoint-o',
127: 'application/vnd.openxmlformats-officedocument.presentationml' => 'fa-file-powerpoint-o',
128: 'application/vnd.oasis.opendocument.presentation' => 'fa-file-powerpoint-o',
129:
130: 'text/plain' => 'fa-file-text-o',
131: 'text/html' => 'fa-file-code-o',
132: 'application/json' => 'fa-file-code-o',
133:
134: 'application/gzip' => 'fa-file-archive-o',
135: 'application/zip' => 'fa-file-archive-o',
136: ];
137:
138: const MIME_TYPE_IF_UNKNOWN = 'application/octet-stream';
139:
140: 141: 142: 143: 144:
145: public static function get(
146: $file
147: ) {
148: if (is_string($file)) {
149: $file = new \SplFileInfo($file);
150: }
151:
152: $extension = mb_strtolower($file->getExtension());
153: if ('' === $extension) {
154: return static::MIME_TYPE_IF_UNKNOWN;
155: }
156:
157: if (array_key_exists($extension, static::$mimeTypes)) {
158: return static::$mimeTypes[$extension];
159: }
160:
161: if (function_exists('finfo_open') && $file->isFile()) {
162: $path = $file->getPath();
163:
164: $fileInfo = finfo_open(FILEINFO_MIME);
165: $mimeType = finfo_file($fileInfo, $path);
166: finfo_close($fileInfo);
167:
168: return $mimeType;
169: }
170:
171: return static::MIME_TYPE_IF_UNKNOWN;
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: public static function multiple(
182: array $files
183: ) {
184: $out = [];
185: foreach ($files as $file) {
186: $out[] = static::info($file);
187: }
188:
189: return $out;
190: }
191:
192: 193: 194: 195: 196: 197: 198:
199: public static function info(
200: $file
201: ) {
202: return new MimeTypeInfo($file, static::get($file));
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212: 213:
214: public static function response(
215: $file,
216: $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT,
217: $fileName = null
218: ) {
219: if (!class_exists('Symfony\\Component\\HttpFoundation\\Response')) {
220: throw new MimeTypeException('HttpFoundation component not found, install it.');
221: }
222:
223: if (
224: !in_array(
225: $disposition,
226: [ResponseHeaderBag::DISPOSITION_INLINE, ResponseHeaderBag::DISPOSITION_ATTACHMENT],
227: true
228: )
229: ) {
230: $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT;
231: }
232:
233: $info = static::info($file);
234: $spl = $info->getSplFileObject();
235:
236: $response = new Response($spl->fread($spl->getSize()));
237:
238: $response->headers->set('Content-Type', $info->getMimeType());
239: $disposition = $response->headers->makeDisposition(
240: $disposition,
241: null === $fileName ? $spl->getFileName() : $fileName
242: );
243: $response->headers->set('Content-Disposition', $disposition);
244:
245: return $response;
246: }
247:
248: public static function getFontAwesomeIcon($file, $fixedWidth = false)
249: {
250: $fileMimeType = self::get($file);
251:
252: $foundIcon = 'fa-file-o';
253: foreach (self::$fa as $mimeType => $icon) {
254: if (false !== mb_strpos($fileMimeType, $mimeType)) {
255: $foundIcon = $icon;
256: }
257: }
258:
259: return 'fa '.$foundIcon.($fixedWidth ? ' fa-fw' : null);
260: }
261: }
262: