Created
April 11, 2018 17:55
-
-
Save teohhanhui/90bbc4a61888ad2e92a160d8459db300 to your computer and use it in GitHub Desktop.
Fix SVG mime type in Symfony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use AppBundle\HttpFoundation\File\MimeType\ErrataMimeTypeGuesser; | |
use Symfony\Component\Config\Loader\LoaderInterface; | |
use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser; | |
use Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser; | |
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; | |
use Symfony\Component\HttpKernel\Kernel; | |
class AppKernel extends Kernel | |
{ | |
private $errataMimeTypeGuesserRegistered = false; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function boot(): void | |
{ | |
$this->registerErrataMimeTypeGuesser(); | |
parent::boot(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function registerBundles(): array | |
{ | |
// ... | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function registerContainerConfiguration(LoaderInterface $loader): void | |
{ | |
// ... | |
} | |
private function registerErrataMimeTypeGuesser(): void | |
{ | |
if ($this->errataMimeTypeGuesserRegistered) { | |
return; | |
} | |
$decorated = null; | |
if ( | |
FileBinaryMimeTypeGuesser::isSupported() && | |
null !== shell_exec('command -v file') // https://github.com/symfony/symfony/pull/26886 | |
) { | |
$decorated = new FileBinaryMimeTypeGuesser(); | |
} elseif (FileinfoMimeTypeGuesser::isSupported()) { | |
$decorated = new FileinfoMimeTypeGuesser(); | |
} | |
if (null !== $decorated) { | |
MimeTypeGuesser::getInstance()->register(new ErrataMimeTypeGuesser($decorated)); | |
} | |
$this->errataMimeTypeGuesserRegistered = true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace AppBundle\HttpFoundation\File\MimeType; | |
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; | |
class ErrataMimeTypeGuesser implements MimeTypeGuesserInterface | |
{ | |
private $decorated; | |
public function __construct(MimeTypeGuesserInterface $decorated) | |
{ | |
$this->decorated = $decorated; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function guess($path): ?string | |
{ | |
$mimeType = $this->decorated->guess($path); | |
if ('image/svg' === $mimeType) { | |
$mimeType = 'image/svg+xml'; | |
} | |
return $mimeType; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment