diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2019-07-09 11:06:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-09 11:06:44 +0200 |
commit | 027486e27d71023265b20aae0299a70f3daa2110 (patch) | |
tree | 0b58f89fa49159feecb914a7cb90a8b9592adde3 /lib/private | |
parent | 5cef8957b5f7e1c86af4c4f1ec80b1ccc698b8e1 (diff) | |
parent | 5e082f8946b92fdd552724e6c1a3cd74c89fccbf (diff) | |
download | nextcloud-server-027486e27d71023265b20aae0299a70f3daa2110.tar.gz nextcloud-server-027486e27d71023265b20aae0299a70f3daa2110.zip |
Merge pull request #15867 from nextcloud/preview-versioning
allow keeping multiple preview "versions" of the same file
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Preview/Generator.php | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index f9c020389f9..79c512c84aa 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -35,6 +35,8 @@ use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; use OCP\IImage; use OCP\IPreview; +use OCP\Preview\IProvider; +use OCP\Preview\IVersionedPreviewFile; use OCP\Preview\IProviderV2; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -98,7 +100,7 @@ class Generator { $this->eventDispatcher->dispatch( IPreview::EVENT, - new GenericEvent($file,[ + new GenericEvent($file, [ 'width' => $width, 'height' => $height, 'crop' => $crop, @@ -115,14 +117,19 @@ class Generator { $previewFolder = $this->getPreviewFolder($file); + $previewVersion = ''; + if ($file instanceof IVersionedPreviewFile) { + $previewVersion = $file->getPreviewVersion() . '-'; + } + // Get the max preview and infer the max preview sizes from that - $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType); + $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType, $previewVersion); if ($maxPreview->getSize() === 0) { $maxPreview->delete(); throw new NotFoundException('Max preview size 0, invalid!'); } - list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview); + list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview, $previewVersion); // If both width and heigth are -1 we just want the max preview if ($width === -1 && $height === -1) { @@ -141,9 +148,9 @@ class Generator { // Try to get a cached preview. Else generate (and store) one try { try { - $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType()); + $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); } catch (NotFoundException $e) { - $preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight); + $preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); } } catch (\InvalidArgumentException $e) { throw new NotFoundException(); @@ -161,14 +168,16 @@ class Generator { * @param ISimpleFolder $previewFolder * @param File $file * @param string $mimeType + * @param string $prefix * @return ISimpleFile * @throws NotFoundException */ - private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) { + private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) { $nodes = $previewFolder->getDirectoryListing(); foreach ($nodes as $node) { - if (strpos($node->getName(), 'max')) { + $name = $node->getName(); + if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) { return $node; } } @@ -206,7 +215,7 @@ class Generator { continue; } - $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; + $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; try { $file = $previewFolder->newFile($path); $file->putContent($preview->data()); @@ -223,10 +232,11 @@ class Generator { /** * @param ISimpleFile $file + * @param string $prefix * @return int[] */ - private function getPreviewSize(ISimpleFile $file) { - $size = explode('-', $file->getName()); + private function getPreviewSize(ISimpleFile $file, string $prefix = '') { + $size = explode('-', substr($file->getName(), strlen($prefix))); return [(int)$size[0], (int)$size[1]]; } @@ -235,10 +245,11 @@ class Generator { * @param int $height * @param bool $crop * @param string $mimeType + * @param string $prefix * @return string */ - private function generatePath($width, $height, $crop, $mimeType) { - $path = (string)$width . '-' . (string)$height; + private function generatePath($width, $height, $crop, $mimeType, $prefix) { + $path = $prefix . (string)$width . '-' . (string)$height; if ($crop) { $path .= '-crop'; } @@ -249,7 +260,6 @@ class Generator { } - /** * @param int $width * @param int $height @@ -346,11 +356,12 @@ class Generator { * @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, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) { + private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) { $preview = $this->helper->getImage($maxPreview); if (!$preview->valid()) { @@ -380,7 +391,7 @@ class Generator { } - $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType()); + $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix); try { $file = $previewFolder->newFile($path); $file->putContent($preview->data()); @@ -397,12 +408,13 @@ class Generator { * @param int $height * @param bool $crop * @param string $mimeType + * @param string $prefix * @return ISimpleFile * * @throws NotFoundException */ - private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) { - $path = $this->generatePath($width, $height, $crop, $mimeType); + private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) { + $path = $this->generatePath($width, $height, $crop, $mimeType, $prefix); return $previewFolder->getFile($path); } |