Index: branches/extensionless-files/includes/MimeMagic.php |
— | — | @@ -313,6 +313,19 @@ |
314 | 314 | return $r; |
315 | 315 | } |
316 | 316 | |
| 317 | + |
| 318 | + /** |
| 319 | + * returns the preferred file extension for a given mime type |
| 320 | + */ |
| 321 | + function getPreferredExtensionForType( $mime ) { |
| 322 | + $ext = explode( ' ', $this->getExtensionsForType( $mime ) ); |
| 323 | + // assume the first one listed is preferred, unless normalizeExtension |
| 324 | + // decides otherwise |
| 325 | + $ext = File::normalizeExtension( $ext[0] ); |
| 326 | + |
| 327 | + return $ext; |
| 328 | + } |
| 329 | + |
317 | 330 | /** returns a list of mime types for a given file extension |
318 | 331 | * as a space separated string. |
319 | 332 | */ |
Index: branches/extensionless-files/includes/filerepo/File.php |
— | — | @@ -91,6 +91,19 @@ |
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
| 95 | + * Given a file name, normalize its extension extension to the common form, |
| 96 | + * and ensure it's clean. |
| 97 | + * |
| 98 | + * @param $ext string (without the .) |
| 99 | + * @return string |
| 100 | + */ |
| 101 | + function getNormalizedExtensionFromName( $name ) { |
| 102 | + $n = strrpos( $name, '.' ); |
| 103 | + return File::normalizeExtension( $n ? substr( $name, $n + 1 ) |
| 104 | + : '' ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
95 | 108 | * Checks if file extensions are compatible |
96 | 109 | * |
97 | 110 | * @param $old File Old file |
— | — | @@ -98,9 +111,8 @@ |
99 | 112 | */ |
100 | 113 | static function checkExtensionCompatibility( File $old, $new ) { |
101 | 114 | $oldMime = $old->getMimeType(); |
102 | | - $n = strrpos( $new, '.' ); |
103 | | - $newExt = self::normalizeExtension( |
104 | | - $n ? substr( $new, $n + 1 ) : '' ); |
| 115 | + $newExt = self::getNormalizedExtensionFromName( $new ); |
| 116 | + |
105 | 117 | $mimeMagic = MimeMagic::singleton(); |
106 | 118 | return $mimeMagic->isMatchingExtension( $newExt, $oldMime ); |
107 | 119 | } |