Verified Commit a5b0f9cc authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3477346 by mondrake, smustgrave, larowlan:...

Issue #3477346 by mondrake, smustgrave, larowlan: ExtensionMimeTypeGuesser::guessMimeType returns less accurate MIME type when file extensions have multiple parts
parent db5dccd6
Loading
Loading
Loading
Loading
Loading
+6 −10
Original line number Diff line number Diff line
@@ -911,16 +911,12 @@ public function guessMimeType($path): ?string {
    $extension = '';
    $file_parts = explode('.', \Drupal::service('file_system')->basename($path));

    // Remove the first part: a full filename should not match an extension.
    array_shift($file_parts);

    // Iterate over the file parts, trying to find a match.
    // For my.awesome.image.jpeg, we try:
    // - jpeg
    // - image.jpeg, and
    // - awesome.image.jpeg
    while ($additional_part = array_pop($file_parts)) {
      $extension = strtolower($additional_part . ($extension ? '.' . $extension : ''));
    // Remove the first part: a full filename should not match an extension,
    // then iterate over the file parts, trying to find a match.
    // For 'my.awesome.image.jpeg', we try: 'awesome.image.jpeg', then
    // 'image.jpeg', then 'jpeg'.
    while (array_shift($file_parts)) {
      $extension = strtolower(implode('.', $file_parts));
      if (isset($this->mapping['extensions'][$extension])) {
        return $this->mapping['mimetypes'][$this->mapping['extensions'][$extension]];
      }
+8 −0
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@

use Drupal\file\Entity\File;

// cspell:ignore garply tarz

const FILE_URL_TEST_CDN_1 = 'http://cdn1.example.com';
const FILE_URL_TEST_CDN_2 = 'http://cdn2.example.com';

@@ -294,9 +296,15 @@ function file_test_file_mimetype_mapping_alter(&$mapping) {
  $mapping['mimetypes']['file_test_mimetype_1'] = 'made_up/file_test_1';
  $mapping['mimetypes']['file_test_mimetype_2'] = 'made_up/file_test_2';
  $mapping['mimetypes']['file_test_mimetype_3'] = 'made_up/doc';
  $mapping['mimetypes']['application-x-compress'] = 'application/x-compress';
  $mapping['mimetypes']['application-x-tarz'] = 'application/x-tarz';
  $mapping['mimetypes']['application-x-garply-waldo'] = 'application/x-garply-waldo';
  $mapping['extensions']['file_test_1'] = 'file_test_mimetype_1';
  $mapping['extensions']['file_test_2'] = 'file_test_mimetype_2';
  $mapping['extensions']['file_test_3'] = 'file_test_mimetype_2';
  $mapping['extensions']['z'] = 'application-x-compress';
  $mapping['extensions']['tar.z'] = 'application-x-tarz';
  $mapping['extensions']['garply.waldo'] = 'application-x-garply-waldo';
  // Override existing mapping.
  $mapping['extensions']['doc'] = 'file_test_mimetype_3';
}
+13 −1
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@

namespace Drupal\KernelTests\Core\File;

// cspell:ignore garply tarz

/**
 * Tests filename mimetype detection.
 *
@@ -30,13 +32,18 @@ public function testFileMimeTypeDetection(): void {
      'test.jar.jpg' => 'image/jpeg',
      'test.jpg.jar' => 'application/java-archive',
      'test.pcf.Z' => 'application/x-font',
      'pcf.z' => 'application/octet-stream',
      'test.garply.waldo' => 'application/x-garply-waldo',
      'pcf.z' => 'application/x-compress',
      'jar' => 'application/octet-stream',
      'garply.waldo' => 'application/octet-stream',
      'some.junk' => 'application/octet-stream',
      'foo.file_test_1' => 'made_up/file_test_1',
      'foo.file_test_2' => 'made_up/file_test_2',
      'foo.doc' => 'made_up/doc',
      'test.ogg' => 'audio/ogg',
      'foobar.z' => 'application/x-compress',
      'foobar.tar' => 'application/x-tar',
      'foobar.tar.z' => 'application/x-tarz',
    ];

    $guesser = $this->container->get('file.mime_type.guesser');
@@ -72,13 +79,18 @@ public function testFileMimeTypeDetection(): void {
      'test.jar.jpg' => 'image/jpeg',
      'test.jpg.jar' => 'application/java-archive',
      'test.pcf.z' => NULL,
      'test.garply.waldo' => NULL,
      'pcf.z' => NULL,
      'jar' => NULL,
      'garply.waldo' => NULL,
      'some.junk' => NULL,
      'foo.file_test_1' => NULL,
      'foo.file_test_2' => NULL,
      'foo.doc' => NULL,
      'test.ogg' => NULL,
      'foobar.z' => NULL,
      'foobar.tar' => NULL,
      'foobar.tar.z' => NULL,
    ];
    $extension_guesser = $this->container->get('file.mime_type.guesser.extension');
    $extension_guesser->setMapping($mapping);