diff --git a/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php b/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php index c6f5eaf..9119048 100644 --- a/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php +++ b/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php @@ -35,7 +35,6 @@ public function __construct(MimeTypeMapperInterface $mapper) { * {@inheritdoc} */ public function guess($path) { - $mapping = $this->mapper->getMapping(); $extension = ''; $file_parts = explode('.', drupal_basename($path)); @@ -49,8 +48,9 @@ public function guess($path) { // - awesome.image.jpeg while ($additional_part = array_pop($file_parts)) { $extension = strtolower($additional_part . ($extension ? '.' . $extension : '')); - if (isset($mapping['extensions'][$extension])) { - return $mapping['mimetypes'][$mapping['extensions'][$extension]]; + $mime_type = $this->mapper->getMimeTypeForExtension($extension); + if ($mime_type) { + return $mime_type; } } diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php index 1f0299d..01e75b9 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php @@ -865,24 +865,30 @@ class MimeTypeMapper implements MimeTypeMapperInterface { /** * {@inheritdoc} */ - public function getMapping() { - return $this->mapping; + public function addMapping($mimetype, $extension) { + if (!in_array($mimetype, $this->mapping['mimetypes'])) { + $this->mapping['mimetypes'][] = $mimetype; + } + $key = array_search($mimetype, $this->mapping['mimetypes']); + $this->mapping['extensions'][$extension] = $key; + + return TRUE; } /** * {@inheritdoc} */ public function getMimeTypes() { - list($mimetypes,) = $this->getMapping(); - return $mimetypes; + return array_values($this->mapping['mimetypes']); } /** * {@inheritdoc} */ public function getMimeTypeForExtension($extension) { - list($mimetypes, $extensions) = $this->getMapping(); $extension = strtolower($extension); - return isset($extensions[$extension]) ? $mimetypes[$extensions[$extension]] : NULL; + $extensions = $this->mapping['extensions']; + return isset($extensions[$extension]) ? $this->mapping['mimetypes'][$extensions[$extension]] : NULL; } + } diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php index c348193..8618a86 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php @@ -10,25 +10,27 @@ * Provides a sensible mapping between filename extensions and MIME types. */ interface MimeTypeMapperInterface { + /** - * Returns known MIME types. + * Adds a mapping between a mimetype and a extension. * - * @return string[] - * An indexed array of MIME types. + * @param string $mimetype + * The mimetype the passed extension should map. + * @param string $extension + * The extension that should map to the passed mimetype. + * + * @return bool + * Boolean TRUE if the mapping was added successfully, FALSE otherwise. */ - public function getMimeTypes(); + public function addMapping($mimetype, $extension); /** - * Returns a mapping from file extensions to appropriate MIME types. + * Returns known MIME types. * - * @return array - * An array consisting of two arrays: - * - mimetypes: MIME types, keyed by a unique number. - * - extensions: an associative array with the MIME type key numbers as - * values. The keys are file extensions, in lower case and without any - * preceding dot. + * @return string[] + * An array of MIME types. */ - public function getMapping(); + public function getMimeTypes(); /** * Returns the appropriate MIME type for a given file extension. @@ -40,4 +42,5 @@ public function getMapping(); * A matching MIME type, or NULL if no MIME type matches the extension. */ public function getMimeTypeForExtension($extension); + }