diff options
author | Robin Appelman <robin@icewind.nl> | 2023-02-14 13:30:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-14 13:30:44 +0100 |
commit | c2cc3afd611e7fc73663e88abaf42546abdeb881 (patch) | |
tree | 68d7f5388b9b3480b0eb02fab57b079a8c588660 | |
parent | a705132c8d9eef7f4d0f030be76a2015a0b8ab6f (diff) | |
parent | 382432d4e1a9c83063ae7d2a2b33491540a3a84d (diff) | |
download | nextcloud-server-c2cc3afd611e7fc73663e88abaf42546abdeb881.tar.gz nextcloud-server-c2cc3afd611e7fc73663e88abaf42546abdeb881.zip |
Merge pull request #36603 from nextcloud/imagick-cache-formats
cache formats supported by imagick
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Preview/IMagickSupport.php | 40 | ||||
-rw-r--r-- | lib/private/PreviewManager.php | 14 | ||||
-rw-r--r-- | lib/private/Server.php | 4 |
5 files changed, 53 insertions, 7 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 6e0934c8fa1..8f4579bdd9c 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1391,6 +1391,7 @@ return array( '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 50aaa84ae77..21550e558a5 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 00000000000..e22ae93ab94 --- /dev/null +++ b/lib/private/Preview/IMagickSupport.php @@ -0,0 +1,40 @@ +<?php + +namespace OC\Preview; + +use OCP\ICache; +use OCP\ICacheFactory; + +class IMagickSupport { + private ICache $cache; + private ?\Imagick $imagick; + + public function __construct(ICacheFactory $cacheFactory) { + $this->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 367f0c1c057..dd6b6ba8ee1 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -33,6 +33,7 @@ namespace OC; 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 @@ class PreviewManager implements IPreview { ?string $userId, Coordinator $bootstrapCoordinator, IServerContainer $container, - IBinaryFinder $binaryFinder + IBinaryFinder $binaryFinder, + IMagickSupport $imagickSupport ) { $this->config = $config; $this->rootFolder = $rootFolder; @@ -96,6 +99,7 @@ class PreviewManager implements IPreview { $this->bootstrapCoordinator = $bootstrapCoordinator; $this->container = $container; $this->binaryFinder = $binaryFinder; + $this->imagickSupport = $imagickSupport; } /** @@ -368,9 +372,7 @@ class PreviewManager implements IPreview { $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 @@ class PreviewManager implements IPreview { 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 4ade6bf7c1c..35f63686457 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -127,6 +127,7 @@ use OC\Metadata\MetadataManager; 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; @@ -338,7 +339,8 @@ class Server extends ServerContainer implements IServerContainer { $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 */ |