diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php index f218061..627b458 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php @@ -902,7 +902,19 @@ public function getMapping() { /** * {@inheritdoc} */ - public function setMapping(array $mapping = NULL) { - $this->mapping = $mapping; + public function getMimeTypes() { + // Fire alter hooks. + list($mimetypes,) = $this->getMapping(); + return $mimetypes; + } + + /** + * {@inheritdoc} + */ + public function getMimeTypeForExtension($extension) { + // Fire alter hooks. + list($mimetypes, $extensions) = $this->getMapping(); + $extension = strtolower($extension); + return isset($extensions[$extension]) ? $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 90718cb..c348193 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php @@ -11,20 +11,16 @@ */ interface MimeTypeMapperInterface { /** - * Replaces the mimetypes/extension mapping. + * Returns known MIME types. * - * @param array|null $mapping - * Passing a NULL mapping will reset to the default mapping. + * @return string[] + * An indexed array of MIME types. */ - public function setMapping(array $mapping = NULL); + public function getMimeTypes(); /** * Returns a mapping from file extensions to appropriate MIME types. * - * Unless the mapping has been replaced by setMapping(), this will use a - * default mapping and invoke hook_file_mimetype_mapping_alter() before - * returning the result. - * * @return array * An array consisting of two arrays: * - mimetypes: MIME types, keyed by a unique number. @@ -33,4 +29,15 @@ public function setMapping(array $mapping = NULL); * preceding dot. */ public function getMapping(); + + /** + * Returns the appropriate MIME type for a given file extension. + * + * @param string $extension + * A file extension, without leading dot. + * + * @return string|null + * A matching MIME type, or NULL if no MIME type matches the extension. + */ + public function getMimeTypeForExtension($extension); } diff --git a/core/modules/system/src/Tests/File/MimeTypeTest.php b/core/modules/system/src/Tests/File/MimeTypeTest.php index 33db96a..3d21b16 100644 --- a/core/modules/system/src/Tests/File/MimeTypeTest.php +++ b/core/modules/system/src/Tests/File/MimeTypeTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\system\Tests\File\MimeTypeTest. + * Contains Drupal\system\Tests\File\MimeTypeTest. */ namespace Drupal\system\Tests\File; @@ -55,40 +55,5 @@ public function testFileMimeTypeDetection() { $output = $guesser->guess($input); $this->assertIdentical($output, $expected, format_string('Mimetype (using default mappings) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected))); } - - // Now test the extension gusser by passing in a custom mapping. - $mapping = array( - 'mimetypes' => array( - 0 => 'application/java-archive', - 1 => 'image/jpeg', - ), - 'extensions' => array( - 'jar' => 0, - 'jpg' => 1, - ) - ); - - $test_case = array( - 'test.jar' => 'application/java-archive', - 'test.jpeg' => 'application/octet-stream', - 'test.jpg' => 'image/jpeg', - 'test.jar.jpg' => 'image/jpeg', - 'test.jpg.jar' => 'application/java-archive', - 'test.pcf.z' => 'application/octet-stream', - 'pcf.z' => 'application/octet-stream', - 'jar' => 'application/octet-stream', - 'some.junk' => 'application/octet-stream', - 'foo.file_test_1' => 'application/octet-stream', - 'foo.file_test_2' => 'application/octet-stream', - 'foo.doc' => 'application/octet-stream', - 'test.ogg' => 'application/octet-stream', - ); - $extension_guesser = $this->container->get('file.mime_type.guesser.extension'); - $this->container->get('file.mime_type.mapper')->setMapping($mapping); - - foreach ($test_case as $input => $expected) { - $output = $extension_guesser->guess($input); - $this->assertIdentical($output, $expected, format_string('Mimetype (using passed-in mappings) for %input is %output (expected: %expected).', array('%input' => $input, '%output' => $output, '%expected' => $expected))); - } } }