Skip to content

Switch to mime-db as source of extension => MIME map #5764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions sapi/cli/generate_mime_type_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,42 @@

// Check if we are being given a mime.types file or if we should use the
// default URL.
$source = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : 'https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types';
$source = count($argv) > 1 ? $argv[1] : 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@v1.44.0/db.json';

// See if we can actually load it.
$types = @file($source);
if ($types === false) {
$data = @file_get_contents($source);
if ($data === false) {
fprintf(STDERR, "Error: unable to read $source\n");
exit(1);
}

// Remove comments and flip into an extensions array.
// Source preference from lowest to highest
$prefs = ['nginx' => 1, 'apache' => 2, 'custom' => 3, 'iana' => 4];

$extensions = [];
array_walk($types, function ($line) use (&$extensions) {
$line = trim($line);
if ($line && $line[0] != '#') {
$fields = preg_split('/\s+/', $line);
if (count($fields) > 1) {
$mime = array_shift($fields);
foreach ($fields as $extension) {
$extensions[$extension] = $mime;
$types = json_decode($data, true);
foreach ($types as $mime => $info) {
$source = $info['source'] ?? 'custom';
$pref = $prefs[$source];
foreach ($info['extensions'] ?? [] as $extension) {
if (isset($extensions[$extension])) {
// Use same preference rules as jshttp/mime-types
[$oldMime, $oldPref] = $extensions[$extension];
if ($oldMime !== 'application/octet-stream'
&& ($oldPref > $pref
|| ($oldPref === $pref && substr($oldMime, 0, 12) === 'application/'))) {
continue;
}
}

$extensions[$extension] = [$mime, $pref];
}
});
}

// Only keep the MIME type.
$extensions = array_map(function($x) { return $x[0]; }, $extensions);

$additional_mime_maps = [
"map" => "application/json", // from commit: a0d62f08ae8cbebc88e5c92e08fca8d0cdc7309d
"jsm" => "application/javascript",
];

Expand Down
Loading