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 | |
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>
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Preview/Generator.php | 46 | ||||
-rw-r--r-- | lib/public/Preview/IVersionedPreviewFile.php | 39 |
4 files changed, 70 insertions, 17 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 2a5d410acec..286062163f2 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -355,6 +355,7 @@ return array( 'OCP\\PreConditionNotMetException' => $baseDir . '/lib/public/PreConditionNotMetException.php', 'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php', 'OCP\\Preview\\IProviderV2' => $baseDir . '/lib/public/Preview/IProviderV2.php', + 'OCP\\Preview\\IVersionedPreviewFile' => $baseDir . '/lib/public/Preview/IVersionedPreviewFile.php', 'OCP\\Remote\\Api\\IApiCollection' => $baseDir . '/lib/public/Remote/Api/IApiCollection.php', 'OCP\\Remote\\Api\\IApiFactory' => $baseDir . '/lib/public/Remote/Api/IApiFactory.php', 'OCP\\Remote\\Api\\ICapabilitiesApi' => $baseDir . '/lib/public/Remote/Api/ICapabilitiesApi.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 807b498043b..b4a8aec749b 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -389,6 +389,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\PreConditionNotMetException' => __DIR__ . '/../../..' . '/lib/public/PreConditionNotMetException.php', 'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php', 'OCP\\Preview\\IProviderV2' => __DIR__ . '/../../..' . '/lib/public/Preview/IProviderV2.php', + 'OCP\\Preview\\IVersionedPreviewFile' => __DIR__ . '/../../..' . '/lib/public/Preview/IVersionedPreviewFile.php', 'OCP\\Remote\\Api\\IApiCollection' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiCollection.php', 'OCP\\Remote\\Api\\IApiFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiFactory.php', 'OCP\\Remote\\Api\\ICapabilitiesApi' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/ICapabilitiesApi.php', 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); } diff --git a/lib/public/Preview/IVersionedPreviewFile.php b/lib/public/Preview/IVersionedPreviewFile.php new file mode 100644 index 00000000000..e4da914da02 --- /dev/null +++ b/lib/public/Preview/IVersionedPreviewFile.php @@ -0,0 +1,39 @@ +<?php declare(strict_types=1); +/** + * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Preview; + +/** + * Marks files that should keep multiple preview "versions" for the same file id + * + * Examples of this are files where the storage backend provides versioning, for those + * files, we dont have fileids for the different versions but still need to be able to generate + * previews for all versions + * + * @since 17.0.0 + */ +interface IVersionedPreviewFile { + /** + * @return string + * @since 17.0.0 + */ + public function getPreviewVersion(): string; +} |