X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Ffilefield_sources%2Fsrc%2FFile%2FMimeType%2FExtensionMimeTypeGuesser.php;fp=web%2Fmodules%2Fcontrib%2Ffilefield_sources%2Fsrc%2FFile%2FMimeType%2FExtensionMimeTypeGuesser.php;h=2c44e97ff95c7676c218ec883ade4d1cd41b04f9;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/filefield_sources/src/File/MimeType/ExtensionMimeTypeGuesser.php b/web/modules/contrib/filefield_sources/src/File/MimeType/ExtensionMimeTypeGuesser.php new file mode 100644 index 000000000..2c44e97ff --- /dev/null +++ b/web/modules/contrib/filefield_sources/src/File/MimeType/ExtensionMimeTypeGuesser.php @@ -0,0 +1,75 @@ +checkDefaultMapping(); + + $mime_key = array_search($mimetype, $this->mapping['mimetypes']); + $extension = array_search($mime_key, $this->mapping['extensions']); + + return $extension; + } + + /** + * Convert mime type to most common extension. + * + * @param string $mimetype + * Mime type. + * + * @return string|bool + * Return extension if found, FALSE otherwise. + */ + public function convertMimeTypeToMostCommonExtension($mimetype) { + $this->checkDefaultMapping(); + + $extension = FALSE; + if (isset($mimetype)) { + // See if this matches a known MIME type. + $mime_key = array_search($mimetype, $this->mapping['mimetypes']); + if ($mime_key !== FALSE) { + // If we have a match, get this list of likely extensions. For some + // reason Drupal lists the "most common" extension last for most file + // types including php, jpg, and doc. + if ($extensions = array_keys($this->mapping['extensions'], $mime_key)) { + $extension = end($extensions); + } + } + } + return $extension; + } + + /** + * Check for default mapping. + */ + private function checkDefaultMapping() { + if ($this->mapping === NULL) { + $mapping = $this->defaultMapping; + // Allow modules to alter the default mapping. + $this->moduleHandler->alter('file_mimetype_mapping', $mapping); + $this->mapping = $mapping; + } + } + +}