diff --git a/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php b/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php index b0c716a..53712ca 100644 --- a/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php +++ b/core/lib/Drupal/Core/File/MimeType/ExtensionMimeTypeGuesser.php @@ -11,7 +11,7 @@ use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; /** - * Makes possible to guess the MIME type of a stream wrapper path. + * Makes possible to guess the MIME type of a file using its extension. */ class ExtensionMimeTypeGuesser implements MimeTypeGuesserInterface { @@ -883,19 +883,16 @@ class ExtensionMimeTypeGuesser implements MimeTypeGuesserInterface { * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. - * @param array $mapping. - * (optional) The mapping to use. If empty self::defaultMapping is used. */ - public function __construct(ModuleHandlerInterface $module_handler, array $mapping = array()) { + public function __construct(ModuleHandlerInterface $module_handler) { $this->moduleHandler = $module_handler; - $this->mapping = $mapping; } /** * {@inheritdoc} */ public function guess($path) { - if (empty($this->mapping)) { + if ($this->mapping === NULL) { $mapping = $this->defaultMapping; // Allow modules to alter the default mapping. $this->moduleHandler->alter('file_mimetype_mapping', $mapping); @@ -923,4 +920,14 @@ public function guess($path) { return 'application/octet-stream'; } + /** + * Sets the mimetypes/extension mapping to use when guessing mimetype. + * + * @param array|null $mapping + * Passing a NULL mapping will cause guess() to use self::$defaultMapping. + */ + public function setMapping(array $mapping = NULL) { + $this->mapping = $mapping; + } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php b/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php index 8efc05f..06ef0fb 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/MimeTypeTest.php @@ -64,7 +64,7 @@ public function testFileMimeTypeDetection() { $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 passing in the map. + // Now test the extension gusser by passing in a custom mapping. $mapping = array( 'mimetypes' => array( 0 => 'application/java-archive', @@ -91,10 +91,11 @@ public function testFileMimeTypeDetection() { 'foo.doc' => 'application/octet-stream', 'test.ogg' => 'application/octet-stream', ); - $guesser = new ExtensionMimeTypeGuesser($this->container->get('module_handler'), $mapping); + $extension_guesser = $this->container->get('file.mime_type.guesser.extension'); + $extension_guesser->setMapping($mapping); foreach ($test_case as $input => $expected) { - $output = $guesser->guess($input, $mapping); + $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))); } }