diff options
Diffstat (limited to 'lib/private/PreviewManager.php')
-rw-r--r-- | lib/private/PreviewManager.php | 92 |
1 files changed, 51 insertions, 41 deletions
diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 21dd8f8a8ab..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,21 +49,22 @@ 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; public function __construct( - IConfig $config, - IRootFolder $rootFolder, - IAppData $appData, - IEventDispatcher $eventDispatcher, - GeneratorHelper $helper, - ?string $userId, - Coordinator $bootstrapCoordinator, - IServerContainer $container, - IBinaryFinder $binaryFinder, - IMagickSupport $imagickSupport + IConfig $config, + IRootFolder $rootFolder, + IAppData $appData, + IEventDispatcher $eventDispatcher, + GeneratorHelper $helper, + ?string $userId, + Coordinator $bootstrapCoordinator, + ContainerInterface $container, + IBinaryFinder $binaryFinder, + IMagickSupport $imagickSupport, ) { $this->config = $config; $this->rootFolder = $rootFolder; @@ -73,6 +76,7 @@ class PreviewManager implements IPreview { $this->container = $container; $this->binaryFinder = $binaryFinder; $this->imagickSupport = $imagickSupport; + $this->enablePreviews = $config->getSystemValueBool('enable_previews', true); } /** @@ -86,7 +90,7 @@ class PreviewManager implements IPreview { * @return void */ public function registerProvider($mimeTypeRegex, \Closure $callable): void { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + if (!$this->enablePreviews) { return; } @@ -101,7 +105,7 @@ class PreviewManager implements IPreview { * Get all providers */ public function getProviders(): array { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + if (!$this->enablePreviews) { return []; } @@ -134,34 +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) { + 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); } @@ -181,6 +178,7 @@ class PreviewManager implements IPreview { * @since 19.0.0 */ public function generatePreviews(File $file, array $specifications, $mimeType = null) { + $this->throwIfPreviewsDisabled($file, $mimeType); return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); } @@ -191,7 +189,7 @@ class PreviewManager implements IPreview { * @return boolean */ public function isMimeSupported($mimeType = '*') { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + if (!$this->enablePreviews) { return false; } @@ -215,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 { - if (!$this->config->getSystemValueBool('enable_previews', true)) { + 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; } @@ -231,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)) { @@ -339,9 +339,10 @@ class PreviewManager implements IPreview { $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); $this->registerCoreProvider(Preview\WebP::class, '/image\/webp/'); $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); - $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); + $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg$/'); $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); $this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes()); + $this->registerCoreProvider(Preview\ImaginaryPDF::class, Preview\ImaginaryPDF::supportedMimeTypes()); // SVG and Bitmap require imagick if ($this->imagickSupport->hasExtension()) { @@ -384,7 +385,7 @@ class PreviewManager implements IPreview { if (is_string($movieBinary)) { - $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ["movieBinary" => $movieBinary]); + $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ['movieBinary' => $movieBinary]); } } } @@ -452,4 +453,13 @@ class PreviewManager implements IPreview { }); } } + + /** + * @throws NotFoundException if preview generation is disabled + */ + private function throwIfPreviewsDisabled(File $file, ?string $mimeType = null): void { + if (!$this->isAvailable($file, $mimeType)) { + throw new NotFoundException('Previews disabled'); + } + } } |