diff options
Diffstat (limited to 'lib/private/Preview/Generator.php')
-rw-r--r-- | lib/private/Preview/Generator.php | 82 |
1 files changed, 81 insertions, 1 deletions
diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index 6e1e0997a68..a770fe3b459 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -38,6 +38,7 @@ use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; use OCP\IImage; use OCP\IPreview; +use OCP\IStreamImage; use OCP\Preview\IProviderV2; use OCP\Preview\IVersionedPreviewFile; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -136,6 +137,22 @@ class Generator { $previewVersion = $file->getPreviewVersion() . '-'; } + if (count($specifications) === 1 + && (($specifications[0]['width'] === 250 && $specifications[0]['height'] === 250) + || ($specifications[0]['width'] === 150 && $specifications[0]['height'] === 150)) + && 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); $maxPreviewImage = null; // only load the image when we need it @@ -204,6 +221,65 @@ class Generator { return $preview; } + private function getSmallImagePreview(ISimpleFolder $previewFolder, File $file, string $mimeType, string $prefix, bool $crop) { + $nodes = $previewFolder->getDirectoryListing(); + + foreach ($nodes as $node) { + $name = $node->getName(); + if (($prefix === '' || strpos($name, $prefix) === 0) + && (str_starts_with($name, '256-256-crop') && $crop || str_starts_with($name, '256-256') && !$crop)) { + 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, true); + + 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; + } + } + } + /** * @param ISimpleFolder $previewFolder * @param File $file @@ -259,7 +335,11 @@ class Generator { $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; try { $file = $previewFolder->newFile($path); - $file->putContent($preview->data()); + if ($preview instanceof IStreamImage) { + $file->putContent($preview->resource()); + } else { + $file->putContent($preview->data()); + } } catch (NotPermittedException $e) { throw new NotFoundException(); } |