Skip to content
Snippets Groups Projects
Commit fa31ac08 authored by catch's avatar catch
Browse files

Issue #2387443 by alexpott, dawehner: BinaryFileResponse can fail because the...

Issue #2387443 by alexpott, dawehner: BinaryFileResponse can fail because the core MIME guessing is not added to the MimeType singleton
parent 9fc078cc
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
use Drupal\Core\DependencyInjection\ServiceProviderInterface; use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\Core\DependencyInjection\YamlFileLoader; use Drupal\Core\DependencyInjection\YamlFileLoader;
use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\File\MimeType\MimeTypeGuesser;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\PageCache\RequestPolicyInterface; use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\PhpStorage\PhpStorageFactory; use Drupal\Core\PhpStorage\PhpStorageFactory;
...@@ -458,6 +459,9 @@ public function preHandle(Request $request) { ...@@ -458,6 +459,9 @@ public function preHandle(Request $request) {
$allowed_protocols = array('http', 'https'); $allowed_protocols = array('http', 'https');
} }
UrlHelper::setAllowedProtocols($allowed_protocols); UrlHelper::setAllowedProtocols($allowed_protocols);
// Override of Symfony's mime type guesser singleton.
MimeTypeGuesser::registerWithSymfonyGuesser($this->container);
} }
/** /**
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace Drupal\Core\File\MimeType; namespace Drupal\Core\File\MimeType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser as SymfonyMimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
/** /**
...@@ -88,4 +90,18 @@ protected function sortGuessers() { ...@@ -88,4 +90,18 @@ protected function sortGuessers() {
return $sorted; return $sorted;
} }
/**
* A helper function to register with Symfony's singleton mime type guesser.
*
* Symfony's default mimetype guessers have dependencies on PHP's fileinfo
* extension or being able to run the system command file. Drupal's guesser
* does not have these dependencies.
*
* @see \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
*/
public static function registerWithSymfonyGuesser(ContainerInterface $container) {
$singleton = SymfonyMimeTypeGuesser::getInstance();
$singleton->register($container->get('file.mime_type.guesser'));
}
} }
<?php
/**
* @file
* Contains \Drupal\Tests\Core\File\MimeTypeGuesserTest.
*/
namespace Drupal\Tests\Core\File;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\File\MimeType\MimeTypeGuesser;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser as SymfonyMimeTypeGuesser;
/**
* @coversDefaultClass \Drupal\Core\File\MimeType\MimeTypeGuesser
* @group DrupalKernel
*/
class MimeTypeGuesserTest extends UnitTestCase {
/**
* @covers ::registerWithSymfonyGuesser
*
* @see Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
*/
public function testSymfonyGuesserRegistration() {
// Make the guessers property accessible on Symfony's MimeTypeGuesser.
$symfony_guesser = SymfonyMimeTypeGuesser::getInstance();
// Test that the Drupal mime type guess is not being used before the
// override method is called. It is possible that the test environment does
// not support the default guessers.
$guessers = $this->readAttribute($symfony_guesser, 'guessers');
if (count($guessers)) {
$this->assertNotInstanceOf('Drupal\Core\File\MimeType\MimeTypeGuesser', $guessers[0]);
}
$container = new ContainerBuilder();
$container->set('file.mime_type.guesser', new MimeTypeGuesser());
MimeTypeGuesser::registerWithSymfonyGuesser($container);
$guessers = $this->readAttribute($symfony_guesser, 'guessers');
$this->assertInstanceOf('Drupal\Core\File\MimeType\MimeTypeGuesser', $guessers[0]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment