diff options
Diffstat (limited to 'lib/private/PreviewManager.php')
-rw-r--r-- | lib/private/PreviewManager.php | 56 |
1 files changed, 26 insertions, 30 deletions
diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 8417554566e..0bb0280406c 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -21,8 +21,10 @@ use OCP\Files\SimpleFS\ISimpleFile; use OCP\IBinaryFinder; use OCP\IConfig; use OCP\IPreview; -use OCP\IServerContainer; use OCP\Preview\IProviderV2; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; + use function array_key_exists; class PreviewManager implements IPreview { @@ -47,7 +49,7 @@ class PreviewManager implements IPreview { * @psalm-var array<string, null> */ private array $loadedBootstrapProviders = []; - private IServerContainer $container; + private ContainerInterface $container; private IBinaryFinder $binaryFinder; private IMagickSupport $imagickSupport; private bool $enablePreviews; @@ -60,7 +62,7 @@ class PreviewManager implements IPreview { GeneratorHelper $helper, ?string $userId, Coordinator $bootstrapCoordinator, - IServerContainer $container, + ContainerInterface $container, IBinaryFinder $binaryFinder, IMagickSupport $imagickSupport, ) { @@ -136,35 +138,27 @@ class PreviewManager implements IPreview { $this->rootFolder, $this->config ), - $this->eventDispatcher + $this->eventDispatcher, + $this->container->get(LoggerInterface::class), ); } return $this->generator; } - /** - * Returns a preview of a file - * - * The cache is searched first and if nothing usable was found then a preview is - * generated by one of the providers - * - * @param File $file - * @param int $width - * @param int $height - * @param bool $crop - * @param string $mode - * @param string $mimeType - * @return ISimpleFile - * @throws NotFoundException - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) - * @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(); + public function getPreview( + File $file, + $width = -1, + $height = -1, + $crop = false, + $mode = IPreview::MODE_FILL, + $mimeType = null, + bool $cacheResult = true, + ): ISimpleFile { + $this->throwIfPreviewsDisabled($file, $mimeType); $previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all'); $sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency); try { - $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); + $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType, $cacheResult); } finally { Generator::unguardWithSemaphore($sem); } @@ -184,7 +178,7 @@ class PreviewManager implements IPreview { * @since 19.0.0 */ public function generatePreviews(File $file, array $specifications, $mimeType = null) { - $this->throwIfPreviewsDisabled(); + $this->throwIfPreviewsDisabled($file, $mimeType); return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); } @@ -219,13 +213,15 @@ class PreviewManager implements IPreview { /** * Check if a preview can be generated for a file */ - public function isAvailable(\OCP\Files\FileInfo $file): bool { + public function isAvailable(\OCP\Files\FileInfo $file, ?string $mimeType = null): bool { if (!$this->enablePreviews) { return false; } + $fileMimeType = $mimeType ?? $file->getMimeType(); + $this->registerCoreProviders(); - if (!$this->isMimeSupported($file->getMimetype())) { + if (!$this->isMimeSupported($fileMimeType)) { return false; } @@ -235,7 +231,7 @@ class PreviewManager implements IPreview { } foreach ($this->providers as $supportedMimeType => $providers) { - if (preg_match($supportedMimeType, $file->getMimetype())) { + if (preg_match($supportedMimeType, $fileMimeType)) { foreach ($providers as $providerClosure) { $provider = $this->helper->getProvider($providerClosure); if (!($provider instanceof IProviderV2)) { @@ -461,8 +457,8 @@ class PreviewManager implements IPreview { /** * @throws NotFoundException if preview generation is disabled */ - private function throwIfPreviewsDisabled(): void { - if (!$this->enablePreviews) { + private function throwIfPreviewsDisabled(File $file, ?string $mimeType = null): void { + if (!$this->isAvailable($file, $mimeType)) { throw new NotFoundException('Previews disabled'); } } |