diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-03-18 12:32:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-18 12:32:56 +0100 |
commit | 34988cff1915d2cbe4be0824ba4e2c8c2658c755 (patch) | |
tree | 2c95ff4a3f3d6880e373919d7afc380a04d37e2e /lib/private/Preview | |
parent | d70e9d65167de800dfe7e8a9d3a2f6a83d2eb8a0 (diff) | |
parent | 9b6a1cc8ae41807e98f9c4155b6241d3f1f7e470 (diff) | |
download | nextcloud-server-34988cff1915d2cbe4be0824ba4e2c8c2658c755.tar.gz nextcloud-server-34988cff1915d2cbe4be0824ba4e2c8c2658c755.zip |
Merge pull request #24166 from nextcloud/imaginary-prototype
Send images to Imaginary docker to generate previews
Diffstat (limited to 'lib/private/Preview')
-rw-r--r-- | lib/private/Preview/Generator.php | 82 | ||||
-rw-r--r-- | lib/private/Preview/GeneratorHelper.php | 7 | ||||
-rw-r--r-- | lib/private/Preview/Imaginary.php | 136 |
3 files changed, 222 insertions, 3 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(); } diff --git a/lib/private/Preview/GeneratorHelper.php b/lib/private/Preview/GeneratorHelper.php index a5a9dacf63b..6a94b948241 100644 --- a/lib/private/Preview/GeneratorHelper.php +++ b/lib/private/Preview/GeneratorHelper.php @@ -58,8 +58,11 @@ class GeneratorHelper { * * @return bool|IImage */ - public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight) { - return $provider->getThumbnail($file, $maxWidth, $maxHeight); + public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight, bool $crop = false) { + if ($provider instanceof Imaginary) { + return $provider->getCroppedThumbnail($file, $maxWidth, $maxHeight, $crop) ?? false; + } + return $provider->getThumbnail($file, $maxWidth, $maxHeight) ?? false; } /** diff --git a/lib/private/Preview/Imaginary.php b/lib/private/Preview/Imaginary.php new file mode 100644 index 00000000000..7e6ce86d4eb --- /dev/null +++ b/lib/private/Preview/Imaginary.php @@ -0,0 +1,136 @@ +<?php +/** + * @copyright Copyright (c) 2020, Nextcloud, GmbH. + * + * @author Vincent Petry <vincent@nextcloud.com> + * @author Carl Schwan <carl@carlschwan.eu> + * + * @license AGPL-3.0-or-later + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Preview; + +use OCP\Files\File; +use OCP\Http\Client\IClientService; +use OCP\IConfig; +use OCP\IImage; + +use OC\StreamImage; +use Psr\Log\LoggerInterface; + +class Imaginary extends ProviderV2 { + /** @var IConfig */ + private $config; + + /** @var IClientService */ + private $service; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(array $config) { + parent::__construct($config); + $this->config = \OC::$server->get(IConfig::class); + $this->service = \OC::$server->get(IClientService::class); + $this->logger = \OC::$server->get(LoggerInterface::class); + } + + /** + * {@inheritDoc} + */ + public function getMimeType(): string { + return self::supportedMimeTypes(); + } + + public static function supportedMimeTypes(): string { + return '/image\/(bmp|x-bitmap|png|jpeg|gif|heic|svg|webp)/'; + } + + public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop): ?IImage { + $maxSizeForImages = $this->config->getSystemValue('preview_max_filesize_image', 50); + + $size = $file->getSize(); + + if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) { + return null; + } + + $imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); + if ($imaginaryUrl === 'invalid') { + $this->logger->error('Imaginary preview provider is enabled, but no url is configured. Please provide the url of your imaginary server to the \'preview_imaginary_url\' config variable.'); + return null; + } + $imaginaryUrl = rtrim($imaginaryUrl, '/'); + + // Object store + $stream = $file->fopen('r'); + + $httpClient = $this->service->newClient(); + + switch ($file->getMimeType()) { + case 'image/gif': + case 'image/png': + $mimeType = 'png'; + break; + default: + $mimeType = 'jpeg'; + } + + $parameters = [ + 'width' => $maxX, + 'height' => $maxY, + 'stripmeta' => 'true', + 'type' => $mimeType, + ]; + + + try { + $response = $httpClient->post( + $imaginaryUrl . ($crop ? '/smartcrop' : '/fit'), [ + 'query' => $parameters, + 'stream' => true, + 'content-type' => $file->getMimeType(), + 'body' => $stream, + 'nextcloud' => ['allow_local_address' => true], + ]); + } catch (\Exception $e) { + $this->logger->error('Imaginary preview generation failed: ' . $e->getMessage(), [ + 'exception' => $e, + ]); + return null; + } + + if ($response->getStatusCode() !== 200) { + $this->logger->error('Imaginary preview generation failed: ' . json_decode($response->getBody())['message']); + return null; + } + + if ($response->getHeader('X-Image-Width') && $response->getHeader('X-Image-Height')) { + $maxX = (int)$response->getHeader('X-Image-Width'); + $maxY = (int)$response->getHeader('X-Image-Height'); + } + + $image = new StreamImage($response->getBody(), $response->getHeader('Content-Type'), $maxX, $maxY); + return $image->valid() ? $image : null; + } + + /** + * {@inheritDoc} + */ + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + return $this->getCroppedThumbnail($file, $maxX, $maxY, false); + } +} |