diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php index a375001..8962393 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php @@ -937,6 +937,40 @@ public function addMapping($mimetype, $extension) { /** * {@inheritdoc} */ + public function removeMapping($extension) { + // Calls ::getMapping() to make sure mappings are removed from the + // ::$mapping array that has been altered by modules already. + $this->getMapping(); + + $extension = strtolower($extension); + if (isset($this->mapping['extensions'][$extension])) { + unset($this->mapping['extensions'][$extension]); + return TRUE; + } + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function removeMimeType($mimetype) { + // Calls ::getMapping() to make sure mappings are removed from the + // ::$mapping array that has been altered by modules already. + $this->getMapping(); + + if (!in_array($mimetype, $this->mapping['mimetypes'])) { + return FALSE; + } + foreach ($this->getExtensionsForMimeType($mimetype) as $extension) { + $this->removeMapping($extension); + } + unset($this->mapping['mimetypes'][$mimetype]); + return TRUE; + } + + /** + * {@inheritdoc} + */ public function getMimeTypes() { return array_values($this->getMapping()['mimetypes']); } diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php index 9b05e8c..6772cb2 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapperInterface.php @@ -12,18 +12,40 @@ interface MimeTypeMapperInterface { /** - * Adds a mapping between a mimetype and a extension. + * Adds a mapping between a MIME type and an extension. * * @param string $mimetype - * The mimetype the passed extension should map. + * The MIME type the passed extension should map. * @param string $extension - * The extension that should map to the passed mimetype. + * The extension that should map to the passed MIME type. * * @return $this */ public function addMapping($mimetype, $extension); /** + * Removes the mapping between a MIME type and an extension. + * + * @param string $extension + * The extension to be removed from the mapping. + * + * @return bool + * TRUE if the extension was present, FALSE otherwise. + */ + public function removeMapping($extension); + + /** + * Removes a MIME type and all its mapped extensions from the mapping. + * + * @param string $mimetype + * The MIME type to be removed from the mapping. + * + * @return bool + * TRUE if the MIME type was present, FALSE otherwise. + */ + public function removeMimeType($mimetype); + + /** * Returns known MIME types. * * @return string[] diff --git a/core/modules/system/src/Tests/File/MimeTypeTest.php b/core/modules/system/src/Tests/File/MimeTypeTest.php index 357fd77..ad8780f 100644 --- a/core/modules/system/src/Tests/File/MimeTypeTest.php +++ b/core/modules/system/src/Tests/File/MimeTypeTest.php @@ -113,5 +113,13 @@ public function testFileMimeTypeDetection() { $extensions = $mime_type_mapper->getExtensionsForMimeType($input); $this->assertIdentical($extensions, $expected, String::format('File extensions (using altered mappings) for %input are \'%output\' (expected: \'%expected\').', array('%input' => $input, '%output' => implode(', ', $extensions), '%expected' => implode(', ', $expected)))); } + + // Now test mapping removals. + $this->assertFalse($mime_type_mapper->removeMimeType('ready/made'), 'Unexisting MIME type not removed.'); + $this->assertTrue($mime_type_mapper->removeMimeType('madeup/file_test_1'), 'Existing MIME type removed.'); + $this->assertIdentical($mime_type_mapper->getExtensionsForMimeType('madeup/file_test_1'), [], 'No extensions mapped for removed MIME type.'); + $this->assertFalse($mime_type_mapper->removeMapping('readymade'), 'Unexisting extension not removed.'); + $this->assertTrue($mime_type_mapper->removeMapping('file_test_3'), 'Existing extension removed.'); + $this->assertIdentical($mime_type_mapper->getExtensionsForMimeType('madeup/file_test_2'), ['file_test_2'], 'Expected extensions mapped to MIME type after extension removal.'); } }