Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
cache formats supported by imagick
turns out this can be quite slow

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Feb 13, 2023
commit 382432d4e1a9c83063ae7d2a2b33491540a3a84d
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
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