Skip to content

Commit

Permalink
cache formats supported by imagick
Browse files Browse the repository at this point in the history
turns out this can be quite slow

Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Feb 8, 2023
1 parent 6236235 commit cbf7e48
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
40 changes: 40 additions & 0 deletions lib/private/Preview/IMagickSupport.php
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 6 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -96,6 +99,7 @@ public function __construct(
$this->bootstrapCoordinator = $bootstrapCoordinator;
$this->container = $container;
$this->binaryFinder = $binaryFinder;
$this->imagickSupport = $imagickSupport;
}

/**
Expand Down Expand Up @@ -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],
Expand All @@ -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)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit cbf7e48

Please sign in to comment.