Merge pull request #15867 from nextcloud/preview-versioning

allow keeping multiple preview "versions" of the same file
This commit is contained in:
Roeland Jago Douma 2019-07-09 11:06:44 +02:00 committed by GitHub
commit 027486e27d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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\\PreConditionNotMetException' => $baseDir . '/lib/public/PreConditionNotMetException.php',
'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php', 'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php',
'OCP\\Preview\\IProviderV2' => $baseDir . '/lib/public/Preview/IProviderV2.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\\IApiCollection' => $baseDir . '/lib/public/Remote/Api/IApiCollection.php',
'OCP\\Remote\\Api\\IApiFactory' => $baseDir . '/lib/public/Remote/Api/IApiFactory.php', 'OCP\\Remote\\Api\\IApiFactory' => $baseDir . '/lib/public/Remote/Api/IApiFactory.php',
'OCP\\Remote\\Api\\ICapabilitiesApi' => $baseDir . '/lib/public/Remote/Api/ICapabilitiesApi.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\\PreConditionNotMetException' => __DIR__ . '/../../..' . '/lib/public/PreConditionNotMetException.php',
'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php', 'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php',
'OCP\\Preview\\IProviderV2' => __DIR__ . '/../../..' . '/lib/public/Preview/IProviderV2.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\\IApiCollection' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiCollection.php',
'OCP\\Remote\\Api\\IApiFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiFactory.php', 'OCP\\Remote\\Api\\IApiFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiFactory.php',
'OCP\\Remote\\Api\\ICapabilitiesApi' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/ICapabilitiesApi.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\IConfig;
use OCP\IImage; use OCP\IImage;
use OCP\IPreview; use OCP\IPreview;
use OCP\Preview\IProvider;
use OCP\Preview\IVersionedPreviewFile;
use OCP\Preview\IProviderV2; use OCP\Preview\IProviderV2;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
@ -115,14 +117,19 @@ class Generator {
$previewFolder = $this->getPreviewFolder($file); $previewFolder = $this->getPreviewFolder($file);
$previewVersion = '';
if ($file instanceof IVersionedPreviewFile) {
$previewVersion = $file->getPreviewVersion() . '-';
}
// Get the max preview and infer the max preview sizes from that // 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) { if ($maxPreview->getSize() === 0) {
$maxPreview->delete(); $maxPreview->delete();
throw new NotFoundException('Max preview size 0, invalid!'); 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 both width and heigth are -1 we just want the max preview
if ($width === -1 && $height === -1) { if ($width === -1 && $height === -1) {
@ -141,9 +148,9 @@ class Generator {
// Try to get a cached preview. Else generate (and store) one // Try to get a cached preview. Else generate (and store) one
try { try {
try { try {
$preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType()); $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion);
} catch (NotFoundException $e) { } 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) { } catch (\InvalidArgumentException $e) {
throw new NotFoundException(); throw new NotFoundException();
@ -161,14 +168,16 @@ class Generator {
* @param ISimpleFolder $previewFolder * @param ISimpleFolder $previewFolder
* @param File $file * @param File $file
* @param string $mimeType * @param string $mimeType
* @param string $prefix
* @return ISimpleFile * @return ISimpleFile
* @throws NotFoundException * @throws NotFoundException
*/ */
private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) { private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) {
$nodes = $previewFolder->getDirectoryListing(); $nodes = $previewFolder->getDirectoryListing();
foreach ($nodes as $node) { foreach ($nodes as $node) {
if (strpos($node->getName(), 'max')) { $name = $node->getName();
if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) {
return $node; return $node;
} }
} }
@ -206,7 +215,7 @@ class Generator {
continue; continue;
} }
$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
try { try {
$file = $previewFolder->newFile($path); $file = $previewFolder->newFile($path);
$file->putContent($preview->data()); $file->putContent($preview->data());
@ -223,10 +232,11 @@ class Generator {
/** /**
* @param ISimpleFile $file * @param ISimpleFile $file
* @param string $prefix
* @return int[] * @return int[]
*/ */
private function getPreviewSize(ISimpleFile $file) { private function getPreviewSize(ISimpleFile $file, string $prefix = '') {
$size = explode('-', $file->getName()); $size = explode('-', substr($file->getName(), strlen($prefix)));
return [(int)$size[0], (int)$size[1]]; return [(int)$size[0], (int)$size[1]];
} }
@ -235,10 +245,11 @@ class Generator {
* @param int $height * @param int $height
* @param bool $crop * @param bool $crop
* @param string $mimeType * @param string $mimeType
* @param string $prefix
* @return string * @return string
*/ */
private function generatePath($width, $height, $crop, $mimeType) { private function generatePath($width, $height, $crop, $mimeType, $prefix) {
$path = (string)$width . '-' . (string)$height; $path = $prefix . (string)$width . '-' . (string)$height;
if ($crop) { if ($crop) {
$path .= '-crop'; $path .= '-crop';
} }
@ -249,7 +260,6 @@ class Generator {
} }
/** /**
* @param int $width * @param int $width
* @param int $height * @param int $height
@ -346,11 +356,12 @@ class Generator {
* @param bool $crop * @param bool $crop
* @param int $maxWidth * @param int $maxWidth
* @param int $maxHeight * @param int $maxHeight
* @param string $prefix
* @return ISimpleFile * @return ISimpleFile
* @throws NotFoundException * @throws NotFoundException
* @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) * @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); $preview = $this->helper->getImage($maxPreview);
if (!$preview->valid()) { 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 { try {
$file = $previewFolder->newFile($path); $file = $previewFolder->newFile($path);
$file->putContent($preview->data()); $file->putContent($preview->data());
@ -397,12 +408,13 @@ class Generator {
* @param int $height * @param int $height
* @param bool $crop * @param bool $crop
* @param string $mimeType * @param string $mimeType
* @param string $prefix
* @return ISimpleFile * @return ISimpleFile
* *
* @throws NotFoundException * @throws NotFoundException
*/ */
private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) { private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) {
$path = $this->generatePath($width, $height, $crop, $mimeType); $path = $this->generatePath($width, $height, $crop, $mimeType, $prefix);
return $previewFolder->getFile($path); 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;
}