diff --git a/config/configuration.sample.yml b/config/configuration.sample.yml index 5e90137357..bc00f45118 100644 --- a/config/configuration.sample.yml +++ b/config/configuration.sample.yml @@ -81,6 +81,7 @@ main: client_secret: null 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 cf58d1a0cc..8de2f9c0e6 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -121,10 +121,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; @@ -151,8 +147,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; @@ -476,14 +470,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 18c80f2255..b03507ae19 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 445451e7c0..481d633047 100644 --- a/lib/conf.d/configuration.yml +++ b/lib/conf.d/configuration.yml @@ -84,6 +84,7 @@ debugger: allowed-ips: [] 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')); + } +}