From 9aa4e7abd87100d6adbd23aeeaede6736807f543 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Thu, 13 Feb 2014 12:20:41 +0100 Subject: [PATCH] Fix #1680 : Add support for custom extension to mime-type mapping --- config/configuration.sample.yml | 1 + lib/Alchemy/Phrasea/Application.php | 14 ----- .../Phrasea/Border/CustomExtensionGuesser.php | 36 +++++++++++++ .../Border/MimeGuesserConfiguration.php | 52 +++++++++++++++++++ .../Provider/BorderManagerServiceProvider.php | 6 +++ lib/conf.d/configuration.yml | 1 + .../Border/CustomExtensionGuesserTest.php | 19 +++++++ 7 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Border/CustomExtensionGuesser.php create mode 100644 lib/Alchemy/Phrasea/Border/MimeGuesserConfiguration.php create mode 100644 tests/Alchemy/Tests/Phrasea/Border/CustomExtensionGuesserTest.php diff --git a/config/configuration.sample.yml b/config/configuration.sample.yml index 6ffefd4003..daa3746f08 100644 --- a/config/configuration.sample.yml +++ b/config/configuration.sample.yml @@ -52,6 +52,7 @@ binaries: unoconv_timeout: 60 border-manager: enabled: true + extension-mapping: { } checkers: - type: Checker\Sha256 diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 428b9392ba..1198d35c61 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -109,10 +109,6 @@ use Monolog\Logger; use Monolog\Processor\IntrospectionProcessor; use Neutron\Silex\Provider\ImagineServiceProvider; use MediaVorus\MediaVorusServiceProvider; -use MediaVorus\Utils\RawImageMimeTypeGuesser; -use MediaVorus\Utils\PostScriptMimeTypeGuesser; -use MediaVorus\Utils\AudioMimeTypeGuesser; -use MediaVorus\Utils\VideoMimeTypeGuesser; use MediaAlchemyst\MediaAlchemystServiceProvider; use Monolog\Handler\NullHandler; use MP4Box\MP4BoxServiceProvider; @@ -136,8 +132,6 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser; -use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Form\FormFactory; @@ -412,14 +406,6 @@ class Application extends SilexApplication $this->mount('/permalink/', new Permalink()); $this->mount('/lightbox/', new Lightbox()); - $guesser = MimeTypeGuesser::getInstance(); - - $guesser->register(new FileBinaryMimeTypeGuesser()); - $guesser->register(new RawImageMimeTypeGuesser()); - $guesser->register(new PostScriptMimeTypeGuesser()); - $guesser->register(new AudioMimeTypeGuesser()); - $guesser->register(new VideoMimeTypeGuesser()); - $app['plugins.directory'] = $app->share(function () { $dir = __DIR__ . '/../../../plugins'; diff --git a/lib/Alchemy/Phrasea/Border/CustomExtensionGuesser.php b/lib/Alchemy/Phrasea/Border/CustomExtensionGuesser.php new file mode 100644 index 0000000000..551e2cacf7 --- /dev/null +++ b/lib/Alchemy/Phrasea/Border/CustomExtensionGuesser.php @@ -0,0 +1,36 @@ +mapping = $mapping; + } + + /** + * {@inheritdoc} + */ + public function guess($path) + { + $extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); + + if (isset($this->mapping[$extension])) { + return $this->mapping[$extension]; + } + } +} diff --git a/lib/Alchemy/Phrasea/Border/MimeGuesserConfiguration.php b/lib/Alchemy/Phrasea/Border/MimeGuesserConfiguration.php new file mode 100644 index 0000000000..6129d281cc --- /dev/null +++ b/lib/Alchemy/Phrasea/Border/MimeGuesserConfiguration.php @@ -0,0 +1,52 @@ +conf = $conf; + } + + /** + * Registers mime type guessers given the configuration + */ + public function register() + { + $guesser = MimeTypeGuesser::getInstance(); + + $guesser->register(new FileBinaryMimeTypeGuesser()); + $guesser->register(new RawImageMimeTypeGuesser()); + $guesser->register(new PostScriptMimeTypeGuesser()); + $guesser->register(new AudioMimeTypeGuesser()); + $guesser->register(new VideoMimeTypeGuesser()); + + if ($this->conf->isSetup()) { + $conf = $this->conf->getConfig(); + + if (isset($conf['border-manager']['extension-mapping']) && is_array($conf['border-manager']['extension-mapping'])) { + $guesser->register(new CustomExtensionGuesser($conf['border-manager']['extension-mapping'])); + } + } + } +} diff --git a/lib/Alchemy/Phrasea/Core/Provider/BorderManagerServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/BorderManagerServiceProvider.php index 0527601a3e..37791357fd 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/BorderManagerServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/BorderManagerServiceProvider.php @@ -12,6 +12,7 @@ namespace Alchemy\Phrasea\Core\Provider; use Alchemy\Phrasea\Border\Manager; +use Alchemy\Phrasea\Border\MimeGuesserConfiguration; use Silex\Application; use Silex\ServiceProviderInterface; use XPDF\Exception\BinaryNotFoundException; @@ -97,9 +98,14 @@ class BorderManagerServiceProvider implements ServiceProviderInterface return $borderManager; }); + + $app['border-manager.mime-guesser-configuration'] = $app->share(function (Application $app) { + return new MimeGuesserConfiguration($app['phraseanet.configuration']); + }); } public function boot(Application $app) { + $app['border-manager.mime-guesser-configuration']->register(); } } diff --git a/lib/conf.d/configuration.yml b/lib/conf.d/configuration.yml index 174ae96d36..2e3eb81901 100644 --- a/lib/conf.d/configuration.yml +++ b/lib/conf.d/configuration.yml @@ -55,6 +55,7 @@ binaries: unoconv_timeout: 60 border-manager: enabled: true + extension-mapping: { } checkers: - type: Checker\Sha256 diff --git a/tests/Alchemy/Tests/Phrasea/Border/CustomExtensionGuesserTest.php b/tests/Alchemy/Tests/Phrasea/Border/CustomExtensionGuesserTest.php new file mode 100644 index 0000000000..296d6bfaa6 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Border/CustomExtensionGuesserTest.php @@ -0,0 +1,19 @@ + 'video/x-romain-neutron', + ); + + $guesser = new CustomExtensionGuesser($conf); + $this->assertNull($guesser->guess(__FILE__)); + $this->assertEquals('video/x-romain-neutron', $guesser->guess('/path/to/video.mpeg')); + } +}