Commit 6b3c7aa5 authored by catch's avatar catch
Browse files

Issue #3172550 by bradjones1, DuaelFr, alexpott, smustgrave, longwave:...

Issue #3172550 by bradjones1, DuaelFr, alexpott, smustgrave, longwave: Register Drupal's mime type guesser the Symfony MimeTypes service

(cherry picked from commit 63602ae6d1de2667c118b42f1812724d238518d4)
parent 142c8a67
Loading
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
use Drupal\Core\DependencyInjection\YamlFileLoader;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\File\MimeType\MimeTypeGuesser;
use Drupal\Core\Http\TrustedHostsRequestFactory;
use Drupal\Core\Installer\InstallerKernel;
use Drupal\Core\Installer\InstallerRedirectTrait;
@@ -625,6 +626,9 @@ public function preHandle(Request $request) {
    // Set the allowed protocols.
    UrlHelper::setAllowedProtocols($this->container->getParameter('filter_protocols'));

    // Override of Symfony's MIME type guesser singleton.
    MimeTypeGuesser::registerWithSymfonyGuesser($this->container);

    $this->prepared = TRUE;
  }

+2 −0
Original line number Diff line number Diff line
@@ -153,6 +153,8 @@ class ExtensionMimeTypeGuesser implements MimeTypeGuesserInterface {
      129 => 'application/x-iphone',
      130 => 'application/x-iso9660-image',
      131 => 'application/x-java-jnlp-file',
      // Per RFC 9239, text/javascript is preferred over application/javascript.
      // @see https://www.rfc-editor.org/rfc/rfc9239
      132 => 'text/javascript',
      133 => 'application/x-jmol',
      134 => 'application/x-kchart',
+17 −0
Original line number Diff line number Diff line
@@ -3,7 +3,9 @@
namespace Drupal\Core\File\MimeType;

use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypes;

/**
 * Defines a MIME type guesser that also supports stream wrapper paths.
@@ -111,4 +113,19 @@ protected function sortGuessers() {
    return array_merge(...$this->guessers);
  }

  /**
   * 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\Mime\MimeTypes
   */
  public static function registerWithSymfonyGuesser(ContainerInterface $container) {
    $guesser = new MimeTypes();
    $guesser->registerGuesser($container->get('file.mime_type.guesser'));
    MimeTypes::setDefault($guesser);
  }

}
+5 −1
Original line number Diff line number Diff line
@@ -23,7 +23,11 @@ class SettingsConfigValidationTest extends KernelTestBase {
  public function testPreviewImagePathIsValidated(): void {
    $this->installConfig('image');

    $file = sys_get_temp_dir() . '/fake_image.png';
    // Drupal does not have a hard dependency on the fileinfo extension and
    // implements an extension-based mimetype guesser. Therefore, we must use
    // an incorrect extension here instead of writing text to a supposed PNG
    // file and depending on a check of the file contents.
    $file = sys_get_temp_dir() . '/fake_image.png.txt';
    file_put_contents($file, 'Not an image!');

    $this->expectException(SchemaIncompleteException::class);
+0 −3
Original line number Diff line number Diff line
@@ -119,9 +119,6 @@ public function deliver(Request $request, string $file_name) {
    if (file_exists($uri)) {
      return new BinaryFileResponse($uri, 200, [
        'Cache-control' => static::CACHE_CONTROL,
        // @todo: remove the explicit setting of Content-Type once this is
        // fixed in https://www.drupal.org/project/drupal/issues/3172550.
        'Content-Type' => $this->contentType,
      ]);
    }

Loading