diff options
Diffstat (limited to 'lib/private/Preview/Generator.php')
-rw-r--r-- | lib/private/Preview/Generator.php | 350 |
1 files changed, 129 insertions, 221 deletions
diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index 47f2952c6e6..4a7341896ef 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -1,31 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Elijah Martin-Merrill <elijah@nyp-itsours.com> - * @author J0WI <J0WI@users.noreply.github.com> - * @author John Molakvoæ <skjnldsv@protonmail.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Scott Dutton <scott@exussum.co.uk> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Preview; @@ -35,6 +12,7 @@ use OCP\Files\IAppData; use OCP\Files\InvalidPathException; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\InMemoryFile; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; @@ -44,40 +22,20 @@ use OCP\IStreamImage; use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Preview\IProviderV2; use OCP\Preview\IVersionedPreviewFile; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; +use Psr\Log\LoggerInterface; class Generator { public const SEMAPHORE_ID_ALL = 0x0a11; public const SEMAPHORE_ID_NEW = 0x07ea; - /** @var IPreview */ - private $previewManager; - /** @var IConfig */ - private $config; - /** @var IAppData */ - private $appData; - /** @var GeneratorHelper */ - private $helper; - /** @var EventDispatcherInterface */ - private $legacyEventDispatcher; - /** @var IEventDispatcher */ - private $eventDispatcher; - public function __construct( - IConfig $config, - IPreview $previewManager, - IAppData $appData, - GeneratorHelper $helper, - EventDispatcherInterface $legacyEventDispatcher, - IEventDispatcher $eventDispatcher + private IConfig $config, + private IPreview $previewManager, + private IAppData $appData, + private GeneratorHelper $helper, + private IEventDispatcher $eventDispatcher, + private LoggerInterface $logger, ) { - $this->config = $config; - $this->previewManager = $previewManager; - $this->appData = $appData; - $this->helper = $helper; - $this->legacyEventDispatcher = $legacyEventDispatcher; - $this->eventDispatcher = $eventDispatcher; } /** @@ -86,17 +44,19 @@ class Generator { * 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) */ - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { + public function getPreview( + File $file, + int $width = -1, + int $height = -1, + bool $crop = false, + string $mode = IPreview::MODE_FILL, + ?string $mimeType = null, + bool $cacheResult = true, + ): ISimpleFile { $specification = [ 'width' => $width, 'height' => $height, @@ -104,31 +64,39 @@ class Generator { 'mode' => $mode, ]; - $this->legacyEventDispatcher->dispatch( - IPreview::EVENT, - new GenericEvent($file, $specification) - ); $this->eventDispatcher->dispatchTyped(new BeforePreviewFetchedEvent( - $file + $file, + $width, + $height, + $crop, + $mode, + $mimeType, )); + $this->logger->debug('Requesting preview for {path} with width={width}, height={height}, crop={crop}, mode={mode}, mimeType={mimeType}', [ + 'path' => $file->getPath(), + 'width' => $width, + 'height' => $height, + 'crop' => $crop, + 'mode' => $mode, + 'mimeType' => $mimeType, + ]); + + // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want - return $this->generatePreviews($file, [$specification], $mimeType); + return $this->generatePreviews($file, [$specification], $mimeType, $cacheResult); } /** * Generates previews of a file * - * @param File $file - * @param non-empty-array $specifications - * @param string $mimeType - * @return ISimpleFile the last preview that was generated * @throws NotFoundException * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) */ - public function generatePreviews(File $file, array $specifications, $mimeType = null) { + public function generatePreviews(File $file, array $specifications, ?string $mimeType = null, bool $cacheResult = true): ISimpleFile { //Make sure that we can read the file if (!$file->isReadable()) { + $this->logger->warning('Cannot read file: {path}, skipping preview generation.', ['path' => $file->getPath()]); throw new NotFoundException('Cannot read file'); } @@ -137,39 +105,29 @@ class Generator { } $previewFolder = $this->getPreviewFolder($file); + // List every existing preview first instead of trying to find them one by one + $previewFiles = $previewFolder->getDirectoryListing(); $previewVersion = ''; if ($file instanceof IVersionedPreviewFile) { $previewVersion = $file->getPreviewVersion() . '-'; } - // If imaginary is enabled, and we request a small thumbnail, - // let's not generate the max preview for performance reasons - if (count($specifications) === 1 - && ($specifications[0]['width'] <= 256 || $specifications[0]['height'] <= 256) - && preg_match(Imaginary::supportedMimeTypes(), $mimeType) - && $this->config->getSystemValueString('preview_imaginary_url', 'invalid') !== 'invalid') { - $crop = $specifications[0]['crop'] ?? false; - $preview = $this->getSmallImagePreview($previewFolder, $file, $mimeType, $previewVersion, $crop); - - if ($preview->getSize() === 0) { - $preview->delete(); - throw new NotFoundException('Cached preview size 0, invalid!'); - } - - return $preview; - } - // Get the max preview and infer the max preview sizes from that - $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType, $previewVersion); + $maxPreview = $this->getMaxPreview($previewFolder, $previewFiles, $file, $mimeType, $previewVersion); $maxPreviewImage = null; // only load the image when we need it if ($maxPreview->getSize() === 0) { $maxPreview->delete(); + $this->logger->error('Max preview generated for file {path} has size 0, deleting and throwing exception.', ['path' => $file->getPath()]); throw new NotFoundException('Max preview size 0, invalid!'); } [$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion); + if ($maxWidth <= 0 || $maxHeight <= 0) { + throw new NotFoundException('The maximum preview sizes are zero or less pixels'); + } + $preview = null; foreach ($specifications as $specification) { @@ -197,7 +155,7 @@ class Generator { // Try to get a cached preview. Else generate (and store) one try { try { - $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); + $preview = $this->getCachedPreview($previewFiles, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); } catch (NotFoundException $e) { if (!$this->previewManager->isMimeSupported($mimeType)) { throw new NotFoundException(); @@ -207,10 +165,13 @@ class Generator { $maxPreviewImage = $this->helper->getImage($maxPreview); } - $preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); + $this->logger->debug('Cached preview not found for file {path}, generating a new preview.', ['path' => $file->getPath()]); + $preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion, $cacheResult); + // New file, augment our array + $previewFiles[] = $preview; } } catch (\InvalidArgumentException $e) { - throw new NotFoundException("", 0, $e); + throw new NotFoundException('', 0, $e); } if ($preview->getSize() === 0) { @@ -230,88 +191,13 @@ class Generator { } /** - * Generate a small image straight away without generating a max preview first - * Preview generated is 256x256 - * - * @throws NotFoundException - */ - private function getSmallImagePreview(ISimpleFolder $previewFolder, File $file, string $mimeType, string $prefix, bool $crop): ISimpleFile { - $nodes = $previewFolder->getDirectoryListing(); - - foreach ($nodes as $node) { - $name = $node->getName(); - if (($prefix === '' || str_starts_with($name, $prefix))) { - // Prefix match - if (str_starts_with($name, $prefix . '256-256-crop') && $crop) { - // Cropped image - return $node; - } - - if (str_starts_with($name, $prefix . '256-256.') && !$crop) { - // Uncropped image - return $node; - } - } - } - - $previewProviders = $this->previewManager->getProviders(); - foreach ($previewProviders as $supportedMimeType => $providers) { - // Filter out providers that does not support this mime - if (!preg_match($supportedMimeType, $mimeType)) { - continue; - } - - foreach ($providers as $providerClosure) { - $provider = $this->helper->getProvider($providerClosure); - if (!($provider instanceof IProviderV2)) { - continue; - } - - if (!$provider->isAvailable($file)) { - continue; - } - - $preview = $this->helper->getThumbnail($provider, $file, 256, 256, $crop); - - if (!($preview instanceof IImage)) { - continue; - } - - // Try to get the extension. - try { - $ext = $this->getExtention($preview->dataMimeType()); - } catch (\InvalidArgumentException $e) { - // Just continue to the next iteration if this preview doesn't have a valid mimetype - continue; - } - - $path = $this->generatePath(256, 256, $crop, $preview->dataMimeType(), $prefix); - try { - $file = $previewFolder->newFile($path); - if ($preview instanceof IStreamImage) { - $file->putContent($preview->resource()); - } else { - $file->putContent($preview->data()); - } - } catch (NotPermittedException $e) { - throw new NotFoundException(); - } - - return $file; - } - } - - throw new NotFoundException('No provider successfully handled the preview generation'); - } - - /** * Acquire a semaphore of the specified id and concurrency, blocking if necessary. * Return an identifier of the semaphore on success, which can be used to release it via * {@see Generator::unguardWithSemaphore()}. * * @param int $semId * @param int $concurrency - * @return false|resource the semaphore on success or false on failure + * @return false|\SysvSemaphore the semaphore on success or false on failure */ public static function guardWithSemaphore(int $semId, int $concurrency) { if (!extension_loaded('sysvsem')) { @@ -330,11 +216,11 @@ class Generator { /** * Releases the semaphore acquired from {@see Generator::guardWithSemaphore()}. * - * @param resource|bool $semId the semaphore identifier returned by guardWithSemaphore + * @param false|\SysvSemaphore $semId the semaphore identifier returned by guardWithSemaphore * @return bool */ - public static function unguardWithSemaphore($semId): bool { - if (!is_resource($semId) || !extension_loaded('sysvsem')) { + public static function unguardWithSemaphore(false|\SysvSemaphore $semId): bool { + if ($semId === false || !($semId instanceof \SysvSemaphore)) { return false; } return sem_release($semId); @@ -347,9 +233,15 @@ class Generator { */ public static function getHardwareConcurrency(): int { static $width; + if (!isset($width)) { - if (is_file("/proc/cpuinfo")) { - $width = substr_count(file_get_contents("/proc/cpuinfo"), "processor"); + if (function_exists('ini_get')) { + $openBasedir = ini_get('open_basedir'); + if (empty($openBasedir) || strpos($openBasedir, '/proc/cpuinfo') !== false) { + $width = is_readable('/proc/cpuinfo') ? substr_count(file_get_contents('/proc/cpuinfo'), 'processor') : 0; + } else { + $width = 0; + } } else { $width = 0; } @@ -373,20 +265,20 @@ class Generator { * @return int number of concurrent preview generations, or -1 if $type is invalid */ public function getNumConcurrentPreviews(string $type): int { - static $cached = array(); + static $cached = []; if (array_key_exists($type, $cached)) { return $cached[$type]; } $hardwareConcurrency = self::getHardwareConcurrency(); switch ($type) { - case "preview_concurrency_all": + case 'preview_concurrency_all': $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency * 2 : 8; $concurrency_all = $this->config->getSystemValueInt($type, $fallback); - $concurrency_new = $this->getNumConcurrentPreviews("preview_concurrency_new"); + $concurrency_new = $this->getNumConcurrentPreviews('preview_concurrency_new'); $cached[$type] = max($concurrency_all, $concurrency_new); break; - case "preview_concurrency_new": + case 'preview_concurrency_new': $fallback = $hardwareConcurrency > 0 ? $hardwareConcurrency : 4; $cached[$type] = $this->config->getSystemValueInt($type, $fallback); break; @@ -398,22 +290,30 @@ class Generator { /** * @param ISimpleFolder $previewFolder + * @param ISimpleFile[] $previewFiles * @param File $file * @param string $mimeType * @param string $prefix * @return ISimpleFile * @throws NotFoundException */ - private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) { - $nodes = $previewFolder->getDirectoryListing(); - - foreach ($nodes as $node) { + private function getMaxPreview(ISimpleFolder $previewFolder, array $previewFiles, File $file, $mimeType, $prefix) { + // We don't know the max preview size, so we can't use getCachedPreview. + // It might have been generated with a higher resolution than the current value. + foreach ($previewFiles as $node) { $name = $node->getName(); - if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) { + if (($prefix === '' || str_starts_with($name, $prefix)) && strpos($name, 'max')) { return $node; } } + $maxWidth = $this->config->getSystemValueInt('preview_max_x', 4096); + $maxHeight = $this->config->getSystemValueInt('preview_max_y', 4096); + + return $this->generateProviderPreview($previewFolder, $file, $maxWidth, $maxHeight, false, true, $mimeType, $prefix); + } + + private function generateProviderPreview(ISimpleFolder $previewFolder, File $file, int $width, int $height, bool $crop, bool $max, string $mimeType, string $prefix) { $previewProviders = $this->previewManager->getProviders(); foreach ($previewProviders as $supportedMimeType => $providers) { // Filter out providers that does not support this mime @@ -431,13 +331,15 @@ class Generator { continue; } - $maxWidth = $this->config->getSystemValueInt('preview_max_x', 4096); - $maxHeight = $this->config->getSystemValueInt('preview_max_y', 4096); - $previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new'); $sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency); try { - $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); + $this->logger->debug('Calling preview provider for {mimeType} with width={width}, height={height}', [ + 'mimeType' => $mimeType, + 'width' => $width, + 'height' => $height, + ]); + $preview = $this->helper->getThumbnail($provider, $file, $width, $height); } finally { self::unguardWithSemaphore($sem); } @@ -446,21 +348,12 @@ class Generator { continue; } - // Try to get the extention. - try { - $ext = $this->getExtention($preview->dataMimeType()); - } catch (\InvalidArgumentException $e) { - // Just continue to the next iteration if this preview doesn't have a valid mimetype - continue; - } - - $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; + $path = $this->generatePath($preview->width(), $preview->height(), $crop, $max, $preview->dataMimeType(), $prefix); try { - $file = $previewFolder->newFile($path); if ($preview instanceof IStreamImage) { - $file->putContent($preview->resource()); + return $previewFolder->newFile($path, $preview->resource()); } else { - $file->putContent($preview->data()); + return $previewFolder->newFile($path, $preview->data()); } } catch (NotPermittedException $e) { throw new NotFoundException(); @@ -470,7 +363,7 @@ class Generator { } } - throw new NotFoundException(); + throw new NotFoundException('No provider successfully handled the preview generation'); } /** @@ -487,17 +380,21 @@ class Generator { * @param int $width * @param int $height * @param bool $crop + * @param bool $max * @param string $mimeType * @param string $prefix * @return string */ - private function generatePath($width, $height, $crop, $mimeType, $prefix) { + private function generatePath($width, $height, $crop, $max, $mimeType, $prefix) { $path = $prefix . (string)$width . '-' . (string)$height; if ($crop) { $path .= '-crop'; } + if ($max) { + $path .= '-max'; + } - $ext = $this->getExtention($mimeType); + $ext = $this->getExtension($mimeType); $path .= '.' . $ext; return $path; } @@ -591,19 +488,20 @@ class Generator { } /** - * @param ISimpleFolder $previewFolder - * @param ISimpleFile $maxPreview - * @param int $width - * @param int $height - * @param bool $crop - * @param int $maxWidth - * @param int $maxHeight - * @param string $prefix - * @return ISimpleFile * @throws NotFoundException * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) */ - private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) { + private function generatePreview( + ISimpleFolder $previewFolder, + IImage $maxPreview, + int $width, + int $height, + bool $crop, + int $maxWidth, + int $maxHeight, + string $prefix, + bool $cacheResult, + ): ISimpleFile { $preview = $maxPreview; if (!$preview->valid()) { throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); @@ -637,19 +535,22 @@ class Generator { self::unguardWithSemaphore($sem); } - $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix); + + $path = $this->generatePath($width, $height, $crop, false, $preview->dataMimeType(), $prefix); try { - $file = $previewFolder->newFile($path); - $file->putContent($preview->data()); + if ($cacheResult) { + return $previewFolder->newFile($path, $preview->data()); + } else { + return new InMemoryFile($path, $preview->data()); + } } catch (NotPermittedException $e) { throw new NotFoundException(); } - return $file; } /** - * @param ISimpleFolder $previewFolder + * @param ISimpleFile[] $files Array of FileInfo, as the result of getDirectoryListing() * @param int $width * @param int $height * @param bool $crop @@ -659,10 +560,15 @@ class Generator { * * @throws NotFoundException */ - private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) { - $path = $this->generatePath($width, $height, $crop, $mimeType, $prefix); - - return $previewFolder->getFile($path); + private function getCachedPreview($files, $width, $height, $crop, $mimeType, $prefix) { + $path = $this->generatePath($width, $height, $crop, false, $mimeType, $prefix); + foreach ($files as $file) { + if ($file->getName() === $path) { + $this->logger->debug('Found cached preview: {path}', ['path' => $path]); + return $file; + } + } + throw new NotFoundException(); } /** @@ -693,12 +599,14 @@ class Generator { * @return null|string * @throws \InvalidArgumentException */ - private function getExtention($mimeType) { + private function getExtension($mimeType) { switch ($mimeType) { case 'image/png': return 'png'; case 'image/jpeg': return 'jpg'; + case 'image/webp': + return 'webp'; case 'image/gif': return 'gif'; default: |