diff options
author | Robin Appelman <robin@icewind.nl> | 2019-06-04 13:44:15 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2019-07-08 22:04:16 +0200 |
commit | 5e082f8946b92fdd552724e6c1a3cd74c89fccbf (patch) | |
tree | 1bcb436d2a366b0fa17975d703aac0fd0f249bbd /lib/private/Preview | |
parent | eb092bbdc74fd10253e7a75850d5725df27daa25 (diff) | |
download | nextcloud-server-5e082f8946b92fdd552724e6c1a3cd74c89fccbf.tar.gz nextcloud-server-5e082f8946b92fdd552724e6c1a3cd74c89fccbf.zip |
allow keeping multiple preview "versions" of the same file
The main use case here is storage provided versioning where we dont have
separate file ids for all the versions, by allowing a prefix for the
version we can store separate previews for all the versions.
Additionally, by keeping all the version previews in the same folder as the
"normal" previews they will be cleaned up properly when the file is deleted
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private/Preview')
-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); } |