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>
This commit is contained in:
Robin Appelman 2019-06-04 13:44:15 +02:00
parent eb092bbdc7
commit 5e082f8946
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
4 changed files with 70 additions and 17 deletions

View File

@ -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',

View File

@ -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',

View File

@ -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);
}

View File

@ -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;
}