Browse Source

cache formats supported by imagick

turns out this can be quite slow

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/36603/head
Robin Appelman 3 years ago
parent
commit
382432d4e1
No known key found for this signature in database GPG Key ID: 42B69D8A64526EFB
  1. 1
      lib/composer/composer/autoload_classmap.php
  2. 1
      lib/composer/composer/autoload_static.php
  3. 40
      lib/private/Preview/IMagickSupport.php
  4. 14
      lib/private/PreviewManager.php
  5. 4
      lib/private/Server.php

1
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',

1
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',

40
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;
}
}

14
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)) {

4
lib/private/Server.php

@ -126,6 +126,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;
@ -336,7 +337,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 */

Loading…
Cancel
Save