Created
September 14, 2012 21:25
-
-
Save lavoiesl/3724957 to your computer and use it in GitHub Desktop.
Generate Nginx mime types using /usr/share/mime/globs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !/usr/bin/env php | |
| <?php | |
| /** | |
| * Parses mime-type globs, as listed in /usr/share/mime/globs | |
| * Only parses group/id:*.extension | |
| * Usage: cat /usr/share/mime/globs | convert-mime-globs-to-nginx.php > /etc/nginx/mime.types | |
| */ | |
| $stdin = fopen('php://stdin', 'r'); | |
| if (!$stdin) { | |
| file_put_contents('Cannot open stdin', 'php://stderr'); | |
| exit(1); | |
| } | |
| $mimes = array(); | |
| $extensions = array(); | |
| $mime_part = '[a-z0-9_\-\.\+]+'; | |
| while ($line = trim(fgets($stdin))) { | |
| if (preg_match($m = "!^($mime_part)/($mime_part)\s*:\*\.($mime_part)$!i", $line, $matches)) { | |
| // Make sure we have only one id per extension, case insensitive | |
| $ext = strtolower($matches[3]); | |
| if (isset($extensions[$ext])) { | |
| file_put_contents("$ext cannot be registered as ${matches[1]}/${matches[2]}, it is already registered as ${extensions[$ext]}\n", 'php://stderr'); | |
| } else { | |
| $mimes[$matches[1]][$matches[2]][] = $ext; | |
| $extensions[$ext] = "${matches[1]}/${matches[2]}"; | |
| } | |
| } | |
| } | |
| fclose($stdin); | |
| // Sort by mime group | |
| ksort($mimes); | |
| // Pad all mime-types to be this long | |
| $pad = 40; | |
| echo "types {"; | |
| foreach ($mimes as $group => $ids) { | |
| echo "\n"; | |
| // Sort by extension | |
| asort($ids); | |
| foreach ($ids as $id => $types) { | |
| echo ' ' . str_pad("$group/$id", $pad) . implode(' ', $types) . ";\n"; | |
| } | |
| } | |
| echo "}\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment