From 382432d4e1a9c83063ae7d2a2b33491540a3a84d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 8 Feb 2023 11:48:33 +0100 Subject: [PATCH] cache formats supported by imagick turns out this can be quite slow Signed-off-by: Robin Appelman --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Preview/IMagickSupport.php | 40 +++++++++++++++++++++ lib/private/PreviewManager.php | 14 ++++---- lib/private/Server.php | 4 ++- 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 lib/private/Preview/IMagickSupport.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 6e0934c8fa1e4..8f4579bdd9ca4 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1391,6 +1391,7 @@ 'OC\\Preview\\Generator' => $baseDir . '/lib/private/Preview/Generator.php', 'OC\\Preview\\GeneratorHelper' => $baseDir . '/lib/private/Preview/GeneratorHelper.php', 'OC\\Preview\\HEIC' => $baseDir . '/lib/private/Preview/HEIC.php', + 'OC\\Preview\\IMagickSupport' => $baseDir . '/lib/private/Preview/IMagickSupport.php', 'OC\\Preview\\Illustrator' => $baseDir . '/lib/private/Preview/Illustrator.php', 'OC\\Preview\\Image' => $baseDir . '/lib/private/Preview/Image.php', 'OC\\Preview\\Imaginary' => $baseDir . '/lib/private/Preview/Imaginary.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 50aaa84ae77c3..21550e558a5dc 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1424,6 +1424,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Preview\\Generator' => __DIR__ . '/../../..' . '/lib/private/Preview/Generator.php', 'OC\\Preview\\GeneratorHelper' => __DIR__ . '/../../..' . '/lib/private/Preview/GeneratorHelper.php', 'OC\\Preview\\HEIC' => __DIR__ . '/../../..' . '/lib/private/Preview/HEIC.php', + 'OC\\Preview\\IMagickSupport' => __DIR__ . '/../../..' . '/lib/private/Preview/IMagickSupport.php', 'OC\\Preview\\Illustrator' => __DIR__ . '/../../..' . '/lib/private/Preview/Illustrator.php', 'OC\\Preview\\Image' => __DIR__ . '/../../..' . '/lib/private/Preview/Image.php', 'OC\\Preview\\Imaginary' => __DIR__ . '/../../..' . '/lib/private/Preview/Imaginary.php', diff --git a/lib/private/Preview/IMagickSupport.php b/lib/private/Preview/IMagickSupport.php new file mode 100644 index 0000000000000..e22ae93ab940e --- /dev/null +++ b/lib/private/Preview/IMagickSupport.php @@ -0,0 +1,40 @@ +cache = $cacheFactory->createLocal('imagick'); + + if (extension_loaded('imagick')) { + $this->imagick = new \Imagick(); + } else { + $this->imagick = null; + } + } + + public function hasExtension(): bool { + return !is_null($this->imagick); + } + + public function supportsFormat(string $format): bool { + if (is_null($this->imagick)) { + return false; + } + + $cached = $this->cache->get($format); + if (!is_null($cached)) { + return $cached; + } + + $formatSupported = count($this->imagick->queryFormats($format)) === 1; + $this->cache->set($format, $cached); + return $formatSupported; + } +} diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 367f0c1c0571c..dd6b6ba8ee112 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -33,6 +33,7 @@ use OC\AppFramework\Bootstrap\Coordinator; use OC\Preview\Generator; use OC\Preview\GeneratorHelper; +use OC\Preview\IMagickSupport; use OCP\AppFramework\QueryException; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\File; @@ -73,6 +74,7 @@ class PreviewManager implements IPreview { private array $loadedBootstrapProviders = []; private IServerContainer $container; private IBinaryFinder $binaryFinder; + private IMagickSupport $imagickSupport; public function __construct( IConfig $config, @@ -84,7 +86,8 @@ public function __construct( ?string $userId, Coordinator $bootstrapCoordinator, IServerContainer $container, - IBinaryFinder $binaryFinder + IBinaryFinder $binaryFinder, + IMagickSupport $imagickSupport ) { $this->config = $config; $this->rootFolder = $rootFolder; @@ -96,6 +99,7 @@ public function __construct( $this->bootstrapCoordinator = $bootstrapCoordinator; $this->container = $container; $this->binaryFinder = $binaryFinder; + $this->imagickSupport = $imagickSupport; } /** @@ -368,9 +372,7 @@ protected function registerCoreProviders() { $this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes()); // SVG, Office and Bitmap require imagick - if (extension_loaded('imagick')) { - $checkImagick = new \Imagick(); - + if ($this->imagickSupport->hasExtension()) { $imagickProviders = [ 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], @@ -390,12 +392,12 @@ protected function registerCoreProviders() { continue; } - if (count($checkImagick->queryFormats($queryFormat)) === 1) { + if ($this->imagickSupport->supportsFormat($queryFormat)) { $this->registerCoreProvider($class, $provider['mimetype']); } } - if (count($checkImagick->queryFormats('PDF')) === 1) { + if ($this->imagickSupport->supportsFormat('PDF')) { // Office requires openoffice or libreoffice $officeBinary = $this->config->getSystemValue('preview_libreoffice_path', null); if (!is_string($officeBinary)) { diff --git a/lib/private/Server.php b/lib/private/Server.php index bd33cdf58bd80..2e7d641c2047d 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -126,6 +126,7 @@ use OC\Notification\Manager; use OC\OCS\DiscoveryService; use OC\Preview\GeneratorHelper; +use OC\Preview\IMagickSupport; use OC\Remote\Api\ApiFactory; use OC\Remote\InstanceFactory; use OC\RichObjectStrings\Validator; @@ -336,7 +337,8 @@ public function __construct($webRoot, \OC\Config $config) { $c->get(ISession::class)->get('user_id'), $c->get(Coordinator::class), $c->get(IServerContainer::class), - $c->get(IBinaryFinder::class) + $c->get(IBinaryFinder::class), + $c->get(IMagickSupport::class) ); }); /** @deprecated 19.0.0 */