diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 9ae11dcce45ae..09f83a0162710 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -73,6 +73,7 @@ class PreviewManager implements IPreview { private IServerContainer $container; private IBinaryFinder $binaryFinder; private IMagickSupport $imagickSupport; + private bool $enablePreviews; public function __construct( IConfig $config, @@ -96,6 +97,7 @@ public function __construct( $this->container = $container; $this->binaryFinder = $binaryFinder; $this->imagickSupport = $imagickSupport; + $this->enablePreviews = $config->getSystemValueBool('enable_previews', true); } /** @@ -109,7 +111,7 @@ public function __construct( * @return void */ public function registerProvider($mimeTypeRegex, \Closure $callable): void { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + if (!$this->enablePreviews) { return; } @@ -124,7 +126,7 @@ public function registerProvider($mimeTypeRegex, \Closure $callable): void { * Get all providers */ public function getProviders(): array { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + if (!$this->enablePreviews) { return []; } @@ -181,6 +183,7 @@ private function getGenerator(): Generator { * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 */ public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { + $this->throwIfPreviewsDisabled(); $previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all'); $sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency); try { @@ -204,6 +207,7 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false, * @since 19.0.0 */ public function generatePreviews(File $file, array $specifications, $mimeType = null) { + $this->throwIfPreviewsDisabled(); return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); } @@ -214,7 +218,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType = * @return boolean */ public function isMimeSupported($mimeType = '*') { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + if (!$this->enablePreviews) { return false; } @@ -239,7 +243,7 @@ public function isMimeSupported($mimeType = '*') { * Check if a preview can be generated for a file */ public function isAvailable(\OCP\Files\FileInfo $file): bool { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + if (!$this->enablePreviews) { return false; } @@ -475,4 +479,13 @@ private function registerBootstrapProviders(): void { }); } } + + /** + * @throws NotFoundException if preview generation is disabled + */ + private function throwIfPreviewsDisabled(): void { + if (!$this->enablePreviews) { + throw new NotFoundException('Previews disabled'); + } + } }