diff options
author | Robin Appelman <robin@icewind.nl> | 2020-02-16 01:45:47 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2020-04-09 12:50:55 +0200 |
commit | 5cd12cd7c39c2ea156ab35266c9359cdd0f4c070 (patch) | |
tree | 47c65715b84db981b59ef3247dbc456ad32e54dc /lib/private/Preview | |
parent | 981278a666370ce40d144ea8d7783e712605c1c0 (diff) | |
download | nextcloud-server-5cd12cd7c39c2ea156ab35266c9359cdd0f4c070.tar.gz nextcloud-server-5cd12cd7c39c2ea156ab35266c9359cdd0f4c070.zip |
allow generating multiple preview sizes for a single file at once
this saves having to do some of the overhead multiple times
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private/Preview')
-rw-r--r-- | lib/private/Preview/Generator.php | 94 |
1 files changed, 60 insertions, 34 deletions
diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index bb597460f9d..073001d6788 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -91,22 +91,37 @@ class Generator { * @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) { + $specification = [ + 'width' => $width, + 'height' => $height, + 'crop' => $crop, + 'mode' => $mode, + ]; + $this->eventDispatcher->dispatch( + IPreview::EVENT, + new GenericEvent($file, $specification) + ); + + // 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); + } + + /** + * Generates previews of a file + * + * @param File $file + * @param 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) { //Make sure that we can read the file if (!$file->isReadable()) { throw new NotFoundException('Cannot read file'); } - - $this->eventDispatcher->dispatch( - IPreview::EVENT, - new GenericEvent($file, [ - 'width' => $width, - 'height' => $height, - 'crop' => $crop, - 'mode' => $mode - ]) - ); - if ($mimeType === null) { $mimeType = $file->getMimeType(); } @@ -128,36 +143,47 @@ class Generator { throw new NotFoundException('Max preview size 0, invalid!'); } - list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview, $previewVersion); + [$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion); - // If both width and heigth are -1 we just want the max preview - if ($width === -1 && $height === -1) { - $width = $maxWidth; - $height = $maxHeight; - } + $preview = null; - // Calculate the preview size - list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); + foreach ($specifications as $specification) { + $width = $specification['width'] ?? -1; + $height = $specification['height'] ?? -1; + $crop = $specification['crop'] ?? false; + $mode = $specification['mode'] ?? IPreview::MODE_FILL; - // No need to generate a preview that is just the max preview - if ($width === $maxWidth && $height === $maxHeight) { - return $maxPreview; - } + // If both width and heigth are -1 we just want the max preview + if ($width === -1 && $height === -1) { + $width = $maxWidth; + $height = $maxHeight; + } - // Try to get a cached preview. Else generate (and store) one - try { + // Calculate the preview size + [$width, $height] = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); + + // No need to generate a preview that is just the max preview + if ($width === $maxWidth && $height === $maxHeight) { + // ensure correct return value if this was the last one + $preview = $maxPreview; + continue; + } + + // Try to get a cached preview. Else generate (and store) one try { - $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); - } catch (NotFoundException $e) { - $preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); + try { + $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); + } catch (NotFoundException $e) { + $preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); + } + } catch (\InvalidArgumentException $e) { + throw new NotFoundException(); } - } catch (\InvalidArgumentException $e) { - throw new NotFoundException(); - } - if ($preview->getSize() === 0) { - $preview->delete(); - throw new NotFoundException('Cached preview size 0, invalid!'); + if ($preview->getSize() === 0) { + $preview->delete(); + throw new NotFoundException('Cached preview size 0, invalid!'); + } } return $preview; |