|
3 | 3 |
|
4 | 4 | // Check if we are being given a mime.types file or if we should use the
|
5 | 5 | // default URL.
|
6 |
| -$source = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : 'https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types'; |
| 6 | +$source = count($argv) > 1 ? $argv[1] : 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.44.0/db.json'; |
7 | 7 |
|
8 | 8 | // See if we can actually load it.
|
9 |
| -$types = @file($source); |
10 |
| -if ($types === false) { |
| 9 | +$data = @file_get_contents($source); |
| 10 | +if ($data === false) { |
11 | 11 | fprintf(STDERR, "Error: unable to read $source\n");
|
12 | 12 | exit(1);
|
13 | 13 | }
|
14 | 14 |
|
15 |
| -// Remove comments and flip into an extensions array. |
| 15 | +// Source preference from lowest to highest |
| 16 | +$prefs = ['nginx' => 1, 'apache' => 2, 'custom' => 3, 'iana' => 4]; |
| 17 | + |
16 | 18 | $extensions = [];
|
17 |
| -array_walk($types, function ($line) use (&$extensions) { |
18 |
| - $line = trim($line); |
19 |
| - if ($line && $line[0] != '#') { |
20 |
| - $fields = preg_split('/\s+/', $line); |
21 |
| - if (count($fields) > 1) { |
22 |
| - $mime = array_shift($fields); |
23 |
| - foreach ($fields as $extension) { |
24 |
| - $extensions[$extension] = $mime; |
| 19 | +$types = json_decode($data, true); |
| 20 | +foreach ($types as $mime => $info) { |
| 21 | + $source = $info['source'] ?? 'custom'; |
| 22 | + $pref = $prefs[$source]; |
| 23 | + foreach ($info['extensions'] ?? [] as $extension) { |
| 24 | + if (isset($extensions[$extension])) { |
| 25 | + // Use same preference rules as jshttp/mime-types |
| 26 | + [$oldMime, $oldPref] = $extensions[$extension]; |
| 27 | + if ($oldMime !== 'application/octet-stream' |
| 28 | + && ($oldPref > $pref |
| 29 | + || ($oldPref === $pref && substr($oldMime, 0, 12) === 'application/'))) { |
| 30 | + continue; |
25 | 31 | }
|
26 | 32 | }
|
| 33 | + |
| 34 | + $extensions[$extension] = [$mime, $pref]; |
27 | 35 | }
|
28 |
| -}); |
| 36 | +} |
| 37 | + |
| 38 | +// Only keep the MIME type. |
| 39 | +$extensions = array_map(function($x) { return $x[0]; }, $extensions); |
29 | 40 |
|
30 | 41 | $additional_mime_maps = [
|
31 |
| - "map" => "application/json", // from commit: a0d62f08ae8cbebc88e5c92e08fca8d0cdc7309d |
32 | 42 | "jsm" => "application/javascript",
|
33 | 43 | ];
|
34 | 44 |
|
|
0 commit comments