Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
14 / 17
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileExt
82.35% covered (warning)
82.35%
14 / 17
66.67% covered (warning)
66.67%
2 / 3
5.14
0.00% covered (danger)
0.00%
0 / 1
 getMimeType
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4
5/**
6 * Why don't we use PHP's mime_content_type-function (https://www.php.net/manual/en/function.mime-content-type.php)?
7 * Because it gives inappropriate results: a css file results in "text/css" which makes it unloadable as resource
8 * from other CSS file with @include, since some browsers (firefox coff coff) say "wrong mime type".
9 * So we use our on list.
10 *
11 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
12 */
13class FileExt {
14  const knownTypes = [
15    "aac" => "audio/aac",
16    "abw" => "application/x-abiword",
17    "arc" => "application/x-freearc",
18    "avif" => "image/avif",
19    "avi" => "video/x-msvideo",
20    "azw" => "application/vnd.amazon.ebook",
21    "bin" => "application/octet-stream",
22    "bmp" => "image/bmp",
23    "bz" => "application/x-bzip",
24    "bz2" => "application/x-bzip2",
25    "cda" => "application/x-cdf",
26    "csh" => "application/x-csh",
27    "css" => "text/css",
28    "csv" => "text/csv",
29    "doc" => "application/msword",
30    "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
31    "eot" => "application/vnd.ms-fontobject",
32    "epub" => "application/epub+zip",
33    "gz" => "application/gzip",
34    "gif" => "image/gif",
35    "htm" => "text/html",
36    "html" => "text/html",
37    "ico" => "image/vnd.microsoft.icon",
38    "ics" => "text/calendar",
39    "jar" => "application/java-archive",
40    "jpeg" => "image/jpeg",
41    "jpg" => "image/jpeg",
42    "js" => "text/javascript",
43    "json" => "application/json",
44    "jsonld" => "application/ld+json",
45    "mid" => "audio/midi",
46    "midi" => "audio/midi",
47    "mjs" => "text/javascript",
48    "mp3" => "audio/mpeg",
49    "mp4" => "video/mp4",
50    "mpeg" => "video/mpeg",
51    "mpkg" => "application/vnd.apple.installer+xml",
52    "odp" => "application/vnd.oasis.opendocument.presentation",
53    "ods" => "application/vnd.oasis.opendocument.spreadsheet",
54    "odt" => "application/vnd.oasis.opendocument.text",
55    "oga" => "audio/ogg",
56    "ogv" => "video/ogg",
57    "ogx" => "application/ogg",
58    "opus" => "audio/opus",
59    "otf" => "font/otf",
60    "png" => "image/png",
61    "pdf" => "application/pdf",
62    "php" => "application/x-httpd-php",
63    "ppt" => "application/vnd.ms-powerpoint",
64    "pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
65    "rar" => "application/vnd.rar",
66    "rtf" => "application/rtf",
67    "sh" => "application/x-sh",
68    "svg" => "image/svg+xml",
69    "swf" => "application/x-shockwave-flash",
70    "tar" => "application/x-tar",
71    "tif" => "image/tiff",
72    "tiff" => "image/tiff",
73    "ts" => "video/mp2t",
74    "ttf" => "font/ttf",
75    "txt" => "text/plain",
76    "vsd" => "application/vnd.visio",
77    "wav" => "audio/wav",
78    "weba" => "audio/webm",
79    "webm" => "video/webm",
80    "webp" => "image/webp",
81    "woff" => "font/woff",
82    "woff2" => "font/woff2",
83    "xhtml" => "application/xhtml+xml",
84    "xls" => "application/vnd.ms-excel",
85    "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
86    "xml" => "application/xml",
87    "xul" => "application/vnd.mozilla.xul+xml",
88    "zip" => "application/zip",
89    "3gp" => "video/3gpp",
90    "3g2" => "video/3gpp2",
91    "7z" => "application/x-7z-compressed"
92  ];
93
94  static function getMimeType($filePath): string {
95    if (!file_exists($filePath)) {
96      return "";
97    }
98
99//    echo "\n#############" . pathinfo($filePath, PATHINFO_EXTENSION) . '##########';
100    return FileExt::knownTypes[strtolower(pathinfo($filePath, PATHINFO_EXTENSION))] ?? 'application/octet-stream';
101  }
102
103  static function get($filePath, int $maxDots = 0): string {
104    if (substr_count($filePath, '.') <= $maxDots) {
105      return '';
106    }
107
108    return implode(
109      '.',
110      array_slice(
111        explode(
112          '.',
113          basename($filePath)
114        ),
115        ($maxDots + 1) * -1,
116        ($maxDots + 1)
117      )
118    );
119  }
120
121  static function has(string $filePath, string $ext): bool {
122    return strtoupper(FileExt::get($filePath, substr_count($ext, '.'))) == strtoupper($ext);
123  }
124}