Add new Provider interface for preview providers

the main difference is passing the `File` object to the provider
instead of a `View` + path

Old providers will still continue to work as before

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2019-06-04 15:25:25 +02:00
parent f6ad353c7c
commit 6150614374
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
36 changed files with 353 additions and 138 deletions

View File

@ -351,6 +351,7 @@ return array(
'OCP\\OCS\\IDiscoveryService' => $baseDir . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\OCS\\IDiscoveryService' => $baseDir . '/lib/public/OCS/IDiscoveryService.php',
'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\\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',
@ -1016,6 +1017,8 @@ return array(
'OC\\Preview\\Photoshop' => $baseDir . '/lib/private/Preview/Photoshop.php', 'OC\\Preview\\Photoshop' => $baseDir . '/lib/private/Preview/Photoshop.php',
'OC\\Preview\\Postscript' => $baseDir . '/lib/private/Preview/Postscript.php', 'OC\\Preview\\Postscript' => $baseDir . '/lib/private/Preview/Postscript.php',
'OC\\Preview\\Provider' => $baseDir . '/lib/private/Preview/Provider.php', 'OC\\Preview\\Provider' => $baseDir . '/lib/private/Preview/Provider.php',
'OC\\Preview\\ProviderV1Adapter' => $baseDir . '/lib/private/Preview/ProviderV1Adapter.php',
'OC\\Preview\\ProviderV2' => $baseDir . '/lib/private/Preview/ProviderV2.php',
'OC\\Preview\\SVG' => $baseDir . '/lib/private/Preview/SVG.php', 'OC\\Preview\\SVG' => $baseDir . '/lib/private/Preview/SVG.php',
'OC\\Preview\\StarOffice' => $baseDir . '/lib/private/Preview/StarOffice.php', 'OC\\Preview\\StarOffice' => $baseDir . '/lib/private/Preview/StarOffice.php',
'OC\\Preview\\TIFF' => $baseDir . '/lib/private/Preview/TIFF.php', 'OC\\Preview\\TIFF' => $baseDir . '/lib/private/Preview/TIFF.php',

View File

@ -385,6 +385,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\OCS\\IDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\OCS\\IDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCS/IDiscoveryService.php',
'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\\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',
@ -1050,6 +1051,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Preview\\Photoshop' => __DIR__ . '/../../..' . '/lib/private/Preview/Photoshop.php', 'OC\\Preview\\Photoshop' => __DIR__ . '/../../..' . '/lib/private/Preview/Photoshop.php',
'OC\\Preview\\Postscript' => __DIR__ . '/../../..' . '/lib/private/Preview/Postscript.php', 'OC\\Preview\\Postscript' => __DIR__ . '/../../..' . '/lib/private/Preview/Postscript.php',
'OC\\Preview\\Provider' => __DIR__ . '/../../..' . '/lib/private/Preview/Provider.php', 'OC\\Preview\\Provider' => __DIR__ . '/../../..' . '/lib/private/Preview/Provider.php',
'OC\\Preview\\ProviderV1Adapter' => __DIR__ . '/../../..' . '/lib/private/Preview/ProviderV1Adapter.php',
'OC\\Preview\\ProviderV2' => __DIR__ . '/../../..' . '/lib/private/Preview/ProviderV2.php',
'OC\\Preview\\SVG' => __DIR__ . '/../../..' . '/lib/private/Preview/SVG.php', 'OC\\Preview\\SVG' => __DIR__ . '/../../..' . '/lib/private/Preview/SVG.php',
'OC\\Preview\\StarOffice' => __DIR__ . '/../../..' . '/lib/private/Preview/StarOffice.php', 'OC\\Preview\\StarOffice' => __DIR__ . '/../../..' . '/lib/private/Preview/StarOffice.php',
'OC\\Preview\\TIFF' => __DIR__ . '/../../..' . '/lib/private/Preview/TIFF.php', 'OC\\Preview\\TIFF' => __DIR__ . '/../../..' . '/lib/private/Preview/TIFF.php',

View File

@ -26,7 +26,7 @@ class BMP extends Image {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/image\/bmp/'; return '/image\/bmp/';
} }
} }

View File

@ -26,44 +26,43 @@
namespace OC\Preview; namespace OC\Preview;
use Imagick; use Imagick;
use OCP\IImage;
use OCP\ILogger; use OCP\ILogger;
use OCP\Files\File;
/** /**
* Creates a PNG preview using ImageMagick via the PECL extension * Creates a PNG preview using ImageMagick via the PECL extension
* *
* @package OC\Preview * @package OC\Preview
*/ */
abstract class Bitmap extends Provider { abstract class Bitmap extends ProviderV2 {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$tmpPath = $fileview->toTmpFile($path); $tmpPath = $this->getLocalFile($file);
if (!$tmpPath) {
return false;
}
// Creates \Imagick object from bitmap or vector file // Creates \Imagick object from bitmap or vector file
try { try {
$bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
} catch (\Exception $e) { } catch (\Exception $e) {
\OC::$server->getLogger()->logException($e, [ \OC::$server->getLogger()->logException($e, [
'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:', 'message' => 'File: ' . $file->getPath() . ' Imagick says:',
'level' => ILogger::ERROR, 'level' => ILogger::ERROR,
'app' => 'core', 'app' => 'core',
]); ]);
return false; return null;
} }
unlink($tmpPath); $this->cleanTmpFiles();
//new bitmap image object //new bitmap image object
$image = new \OC_Image(); $image = new \OC_Image();
$image->loadFromData($bp); $image->loadFromData($bp);
//check if image object is valid //check if image object is valid
return $image->valid() ? $image : false; return $image->valid() ? $image : null;
} }
/** /**

View File

@ -27,7 +27,7 @@ class Font extends Bitmap {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/(?:font-sfnt|x-font$)/'; return '/application\/(?:font-sfnt|x-font$)/';
} }
} }

View File

@ -26,7 +26,7 @@ class GIF extends Image {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/image\/gif/'; return '/image\/gif/';
} }
} }

View File

@ -35,6 +35,8 @@ use OCP\IConfig;
use OCP\IImage; use OCP\IImage;
use OCP\IPreview; use OCP\IPreview;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
use OCP\Preview\IProviderV2;
use OCP\Preview\IVersionedPreviewFile;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
@ -178,9 +180,9 @@ class Generator {
continue; continue;
} }
foreach ($providers as $provider) { foreach ($providers as $providerClosure) {
$provider = $this->helper->getProvider($provider); $provider = $this->helper->getProvider($providerClosure);
if (!($provider instanceof IProvider)) { if (!($provider instanceof IProviderV2)) {
continue; continue;
} }

View File

@ -30,6 +30,7 @@ use OCP\IConfig;
use OCP\IImage; use OCP\IImage;
use OCP\Image as OCPImage; use OCP\Image as OCPImage;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
use OCP\Preview\IProviderV2;
/** /**
* Very small wrapper class to make the generator fully unit testable * Very small wrapper class to make the generator fully unit testable
@ -54,21 +55,8 @@ class GeneratorHelper {
* @param int $maxHeight * @param int $maxHeight
* @return bool|IImage * @return bool|IImage
*/ */
public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) { public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight) {
list($view, $path) = $this->getViewAndPath($file); return $provider->getThumbnail($file, $maxWidth, $maxHeight);
return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view);
}
/**
* @param File $file
* @return array
* This is required to create the old view and path
*/
private function getViewAndPath(File $file) {
$view = new View($file->getParent()->getPath());
$path = $file->getName();
return [$view, $path];
} }
/** /**
@ -82,10 +70,14 @@ class GeneratorHelper {
} }
/** /**
* @param $provider * @param callable $providerClosure
* @return IProvider * @return IProviderV2
*/ */
public function getProvider($provider) { public function getProvider($providerClosure) {
return $provider(); $provider = $providerClosure();
if ($provider instanceof IProvider) {
$provider = new ProviderV1Adapter($provider);
}
return $provider;
} }
} }

View File

@ -23,14 +23,16 @@ declare(strict_types=1);
namespace OC\Preview; namespace OC\Preview;
use OCP\IImage;
use OCP\ILogger; use OCP\ILogger;
use OCP\Files\File;
/** /**
* Creates a JPG preview using ImageMagick via the PECL extension * Creates a JPG preview using ImageMagick via the PECL extension
* *
* @package OC\Preview * @package OC\Preview
*/ */
class HEIC extends Provider { class HEIC extends ProviderV2 {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -48,11 +50,8 @@ class HEIC extends Provider {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$tmpPath = $fileview->toTmpFile($path); $tmpPath = $this->getLocalFile($file);
if (!$tmpPath) {
return false;
}
// Creates \Imagick object from the heic file // Creates \Imagick object from the heic file
try { try {
@ -60,20 +59,20 @@ class HEIC extends Provider {
$bp->setFormat('jpg'); $bp->setFormat('jpg');
} catch (\Exception $e) { } catch (\Exception $e) {
\OC::$server->getLogger()->logException($e, [ \OC::$server->getLogger()->logException($e, [
'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:', 'message' => 'File: ' . $file->getPath() . ' Imagick says:',
'level' => ILogger::ERROR, 'level' => ILogger::ERROR,
'app' => 'core', 'app' => 'core',
]); ]);
return false; return null;
} }
unlink($tmpPath); $this->cleanTmpFiles();
//new bitmap image object //new bitmap image object
$image = new \OC_Image(); $image = new \OC_Image();
$image->loadFromData($bp); $image->loadFromData($bp);
//check if image object is valid //check if image object is valid
return $image->valid() ? $image : false; return $image->valid() ? $image : null;
} }
/** /**

View File

@ -28,7 +28,7 @@ class Illustrator extends Bitmap {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/illustrator/'; return '/application\/illustrator/';
} }
} }

View File

@ -27,44 +27,37 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
abstract class Image extends Provider { use OCP\Files\File;
use OCP\IImage;
abstract class Image extends ProviderV2 {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
//get fileinfo
$fileInfo = $fileview->getFileInfo($path);
if (!$fileInfo) {
return false;
}
$maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50); $maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50);
$size = $fileInfo->getSize(); $size = $file->getSize();
if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) { if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) {
return false; return null;
} }
$image = new \OC_Image(); $image = new \OC_Image();
$useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal(); $fileName = $this->getLocalFile($file);
if ($useTempFile) {
$fileName = $fileview->toTmpFile($path);
} else {
$fileName = $fileview->getLocalFile($path);
}
$image->loadFromFile($fileName); $image->loadFromFile($fileName);
$image->fixOrientation(); $image->fixOrientation();
if ($useTempFile) {
unlink($fileName); $this->cleanTmpFiles();
}
if ($image->valid()) { if ($image->valid()) {
$image->scaleDownToFit($maxX, $maxY); $image->scaleDownToFit($maxX, $maxY);
return $image; return $image;
} }
return false; return null;
} }
} }

View File

@ -26,7 +26,7 @@ class JPEG extends Image {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/image\/jpeg/'; return '/image\/jpeg/';
} }
} }

View File

@ -28,23 +28,26 @@ namespace OC\Preview;
use ID3Parser\ID3Parser; use ID3Parser\ID3Parser;
class MP3 extends Provider { use OCP\Files\File;
use OCP\IImage;
class MP3 extends ProviderV2 {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/audio\/mpeg/'; return '/audio\/mpeg/';
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$getID3 = new ID3Parser(); $getID3 = new ID3Parser();
$tmpPath = $fileview->toTmpFile($path); $tmpPath = $this->getLocalFile($file);
$tags = $getID3->analyze($tmpPath); $tags = $getID3->analyze($tmpPath);
unlink($tmpPath); $this->cleanTmpFiles();
$picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null; $picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null;
if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) { if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) {
$picture = $tags['id3v2']['PIC'][0]['data']; $picture = $tags['id3v2']['PIC'][0]['data'];
@ -61,6 +64,6 @@ class MP3 extends Provider {
} }
} }
return false; return null;
} }
} }

View File

@ -26,7 +26,7 @@ class MSOffice2003 extends Office {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/vnd.ms-.*/'; return '/application\/vnd.ms-.*/';
} }
} }

View File

@ -26,7 +26,7 @@ class MSOffice2007 extends Office {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/vnd.openxmlformats-officedocument.*/'; return '/application\/vnd.openxmlformats-officedocument.*/';
} }
} }

View File

@ -26,7 +26,7 @@ class MSOfficeDoc extends Office {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/msword/'; return '/application\/msword/';
} }
} }

View File

@ -25,7 +25,7 @@ class MarkDown extends TXT {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/text\/(x-)?markdown/'; return '/text\/(x-)?markdown/';
} }

View File

@ -25,38 +25,27 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
class Movie extends Provider { use OCP\Files\File;
use OCP\IImage;
class Movie extends ProviderV2 {
public static $avconvBinary; public static $avconvBinary;
public static $ffmpegBinary; public static $ffmpegBinary;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/video\/.*/'; return '/video\/.*/';
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
// TODO: use proc_open() and stream the source file ? // TODO: use proc_open() and stream the source file ?
$fileInfo = $fileview->getFileInfo($path); $absPath = $this->getLocalFile($file, 5242880); // only use the first 5MB
$useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted());
if ($useFileDirectly) {
$absPath = $fileview->getLocalFile($path);
} else {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();
$handle = $fileview->fopen($path, 'rb');
// we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB.
// in some cases 1MB was no enough to generate thumbnail
$firstmb = stream_get_contents($handle, 5242880);
file_put_contents($absPath, $firstmb);
}
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
if ($result === false) { if ($result === false) {
@ -66,9 +55,7 @@ class Movie extends Provider {
} }
} }
if (!$useFileDirectly) { $this->cleanTmpFiles();
unlink($absPath);
}
return $result; return $result;
} }
@ -78,9 +65,9 @@ class Movie extends Provider {
* @param int $maxY * @param int $maxY
* @param string $absPath * @param string $absPath
* @param int $second * @param int $second
* @return bool|\OCP\IImage * @return null|\OCP\IImage
*/ */
private function generateThumbNail($maxX, $maxY, $absPath, $second) { private function generateThumbNail($maxX, $maxY, $absPath, $second): ?IImage {
$tmpPath = \OC::$server->getTempManager()->getTemporaryFile(); $tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
if (self::$avconvBinary) { if (self::$avconvBinary) {
@ -109,6 +96,6 @@ class Movie extends Provider {
} }
} }
unlink($tmpPath); unlink($tmpPath);
return false; return null;
} }
} }

View File

@ -25,21 +25,23 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
use OCP\IImage;
use OCP\ILogger; use OCP\ILogger;
use OCP\Files\File;
abstract class Office extends Provider { abstract class Office extends ProviderV2 {
private $cmd; private $cmd;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$this->initCmd(); $this->initCmd();
if (is_null($this->cmd)) { if (is_null($this->cmd)) {
return false; return null;
} }
$absPath = $fileview->toTmpFile($path); $absPath = $this->getLocalFile($file);
$tmpDir = \OC::$server->getTempManager()->getTempBaseDir(); $tmpDir = \OC::$server->getTempManager()->getTempBaseDir();
@ -59,19 +61,19 @@ abstract class Office extends Provider {
$png = new \imagick($pngPreview . '[0]'); $png = new \imagick($pngPreview . '[0]');
$png->setImageFormat('jpg'); $png->setImageFormat('jpg');
} catch (\Exception $e) { } catch (\Exception $e) {
unlink($absPath); $this->cleanTmpFiles();
unlink($pngPreview); unlink($pngPreview);
\OC::$server->getLogger()->logException($e, [ \OC::$server->getLogger()->logException($e, [
'level' => ILogger::ERROR, 'level' => ILogger::ERROR,
'app' => 'core', 'app' => 'core',
]); ]);
return false; return null;
} }
$image = new \OC_Image(); $image = new \OC_Image();
$image->loadFromData($png); $image->loadFromData($png);
unlink($absPath); $this->cleanTmpFiles();
unlink($pngPreview); unlink($pngPreview);
if ($image->valid()) { if ($image->valid()) {
@ -79,7 +81,7 @@ abstract class Office extends Provider {
return $image; return $image;
} }
return false; return null;
} }

View File

@ -26,7 +26,7 @@ class OpenDocument extends Office {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/vnd.oasis.opendocument.*/'; return '/application\/vnd.oasis.opendocument.*/';
} }
} }

View File

@ -28,7 +28,7 @@ class PDF extends Bitmap {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/pdf/'; return '/application\/pdf/';
} }
} }

View File

@ -26,7 +26,7 @@ class PNG extends Image {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/image\/png/'; return '/image\/png/';
} }
} }

View File

@ -28,7 +28,7 @@ class Photoshop extends Bitmap {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/x-photoshop/'; return '/application\/x-photoshop/';
} }
} }

View File

@ -28,7 +28,7 @@ class Postscript extends Bitmap {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/postscript/'; return '/application\/postscript/';
} }
} }

View File

@ -0,0 +1,59 @@
<?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 OC\Preview;
use OC\Files\View;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
use OCP\Preview\IProvider;
use OCP\Preview\IProviderV2;
class ProviderV1Adapter implements IProviderV2 {
private $providerV1;
public function __construct(IProvider $providerV1) {
$this->providerV1 = $providerV1;
}
public function getMimeType(): string {
return $this->providerV1->getMimeType();
}
public function isAvailable(FileInfo $file): bool {
return $this->isAvailable($file);
}
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
list($view, $path) = $this->getViewAndPath($file);
$thumbnail = $this->providerV1->getThumbnail($path, $maxX, $maxY, false, $view);
return $thumbnail === false ? null: $thumbnail;
}
private function getViewAndPath(File $file) {
$view = new View($file->getParent()->getPath());
$path = $file->getName();
return [$view, $path];
}
}

View File

@ -0,0 +1,105 @@
<?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 OC\Preview;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
use OCP\Preview\IProviderV2;
abstract class ProviderV2 implements IProviderV2 {
private $options;
private $tmpFiles = [];
/**
* Constructor
*
* @param array $options
*/
public function __construct(array $options = []) {
$this->options = $options;
}
/**
* @return string Regex with the mimetypes that are supported by this provider
*/
abstract public function getMimeType(): string ;
/**
* Check if a preview can be generated for $path
*
* @param FileInfo $file
* @return bool
*/
public function isAvailable(FileInfo $file): bool {
return true;
}
/**
* get thumbnail for file at path $path
*
* @param File $file
* @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
* @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
* @return null|\OCP\IImage false if no preview was generated
* @since 17.0.0
*/
abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
/**
* Get a path to either the local file or temporary file
*
* @param File $file
* @param int $maxSize maximum size for temporary files
* @return string
*/
protected function getLocalFile(File $file, int $maxSize = null): string {
$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
if ($useTempFile) {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();
$content = $file->fopen('r');
if ($maxSize) {
$content = stream_get_contents($content, $maxSize);
}
file_put_contents($absPath, $content);
$this->tmpFiles[] = $absPath;
return $absPath;
} else {
return $file->getStorage()->getLocalFile($file->getInternalPath());
}
}
/**
* Clean any generated temporary files
*/
protected function cleanTmpFiles() {
foreach ($this->tmpFiles as $tmpFile) {
unlink($tmpFile);
}
$this->tmpFiles = [];
}
}

View File

@ -24,32 +24,34 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
use OCP\IImage;
use OCP\ILogger; use OCP\ILogger;
use OCP\Files\File;
class SVG extends Provider { class SVG extends ProviderV2 {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/image\/svg\+xml/'; return '/image\/svg\+xml/';
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
try { try {
$svg = new \Imagick(); $svg = new \Imagick();
$svg->setBackgroundColor(new \ImagickPixel('transparent')); $svg->setBackgroundColor(new \ImagickPixel('transparent'));
$content = stream_get_contents($fileview->fopen($path, 'r')); $content = stream_get_contents($file->fopen('r'));
if (substr($content, 0, 5) !== '<?xml') { if (substr($content, 0, 5) !== '<?xml') {
$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content; $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
} }
// Do not parse SVG files with references // Do not parse SVG files with references
if (stripos($content, 'xlink:href') !== false) { if (stripos($content, 'xlink:href') !== false) {
return false; return null;
} }
$svg->readImageBlob($content); $svg->readImageBlob($content);
@ -59,7 +61,7 @@ class SVG extends Provider {
'level' => ILogger::ERROR, 'level' => ILogger::ERROR,
'app' => 'core', 'app' => 'core',
]); ]);
return false; return null;
} }
//new image object //new image object
@ -71,6 +73,6 @@ class SVG extends Provider {
return $image; return $image;
} }
return false; return null;
} }
} }

View File

@ -26,7 +26,7 @@ class StarOffice extends Office {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/application\/vnd.sun.xml.*/'; return '/application\/vnd.sun.xml.*/';
} }
} }

View File

@ -28,7 +28,7 @@ class TIFF extends Bitmap {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/image\/tiff/'; return '/image\/tiff/';
} }
} }

View File

@ -26,36 +26,40 @@
*/ */
namespace OC\Preview; namespace OC\Preview;
class TXT extends Provider { use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
class TXT extends ProviderV2 {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/text\/plain/'; return '/text\/plain/';
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function isAvailable(\OCP\Files\FileInfo $file) { public function isAvailable(FileInfo $file): bool {
return $file->getSize() > 0; return $file->getSize() > 0;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$content = $fileview->fopen($path, 'r'); $content = $file->fopen('r');
if ($content === false) { if ($content === false) {
return false; return null;
} }
$content = stream_get_contents($content,3000); $content = stream_get_contents($content,3000);
//don't create previews of empty text files //don't create previews of empty text files
if(trim($content) === '') { if(trim($content) === '') {
return false; return null;
} }
$lines = preg_split("/\r\n|\n|\r/", $content); $lines = preg_split("/\r\n|\n|\r/", $content);
@ -95,6 +99,6 @@ class TXT extends Provider {
$imageObject = new \OC_Image(); $imageObject = new \OC_Image();
$imageObject->setResource($image); $imageObject->setResource($image);
return $imageObject->valid() ? $imageObject : false; return $imageObject->valid() ? $imageObject : null;
} }
} }

View File

@ -26,7 +26,7 @@ class XBitmap extends Image {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getMimeType() { public function getMimeType(): string {
return '/image\/x-xbitmap/'; return '/image\/x-xbitmap/';
} }
} }

View File

@ -97,7 +97,7 @@ class PreviewManager implements IPreview {
* In order to improve lazy loading a closure can be registered which will be * In order to improve lazy loading a closure can be registered which will be
* called in case preview providers are actually requested * called in case preview providers are actually requested
* *
* $callable has to return an instance of \OCP\Preview\IProvider * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2
* *
* @param string $mimeTypeRegex Regex with the mime types that are supported by this provider * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider
* @param \Closure $callable * @param \Closure $callable

View File

@ -27,6 +27,7 @@ namespace OCP\Preview;
* *
* @package OCP\Preview * @package OCP\Preview
* @since 8.1.0 * @since 8.1.0
* @deprecated 17.0.0 use IProviderV2 instead
*/ */
interface IProvider { interface IProvider {
/** /**

View File

@ -0,0 +1,57 @@
<?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;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
/**
* @since 17.0.0
*/
interface IProviderV2 {
/**
* @return string Regex with the mimetypes that are supported by this provider
* @since 17.0.0
*/
public function getMimeType(): string;
/**
* Check if a preview can be generated for $path
*
* @param FileInfo $file
* @return bool
* @since 17.0.0
*/
public function isAvailable(FileInfo $file): bool;
/**
* get thumbnail for file at path $path
*
* @param File $file
* @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
* @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
* @return null|\OCP\IImage null if no preview was generated
* @since 17.0.0
*/
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
}

View File

@ -33,6 +33,7 @@ use OCP\IConfig;
use OCP\IImage; use OCP\IImage;
use OCP\IPreview; use OCP\IPreview;
use OCP\Preview\IProvider; use OCP\Preview\IProvider;
use OCP\Preview\IProviderV2;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
@ -149,13 +150,13 @@ class GeneratorTest extends \Test\TestCase {
return $defult; return $defult;
})); }));
$invalidProvider = $this->createMock(IProvider::class); $invalidProvider = $this->createMock(IProviderV2::class);
$invalidProvider->method('isAvailable') $invalidProvider->method('isAvailable')
->willReturn(true); ->willReturn(true);
$unavailableProvider = $this->createMock(IProvider::class); $unavailableProvider = $this->createMock(IProviderV2::class);
$unavailableProvider->method('isAvailable') $unavailableProvider->method('isAvailable')
->willReturn(false); ->willReturn(false);
$validProvider = $this->createMock(IProvider::class); $validProvider = $this->createMock(IProviderV2::class);
$validProvider->method('isAvailable') $validProvider->method('isAvailable')
->with($file) ->with($file)
->willReturn(true); ->willReturn(true);

View File

@ -21,6 +21,8 @@
namespace Test\Preview; namespace Test\Preview;
use OC\Files\Node\File;
abstract class Provider extends \Test\TestCase { abstract class Provider extends \Test\TestCase {
/** @var string */ /** @var string */
@ -136,7 +138,8 @@ abstract class Provider extends \Test\TestCase {
* @return bool|\OCP\IImage * @return bool|\OCP\IImage
*/ */
private function getPreview($provider) { private function getPreview($provider) {
$preview = $provider->getThumbnail($this->imgPath, $this->maxWidth, $this->maxHeight, $this->scalingUp, $this->rootView); $file = new File(\OC::$server->getRootFolder(), $this->rootView, $this->imgPath);
$preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);
$this->assertNotEquals(false, $preview); $this->assertNotEquals(false, $preview);
$this->assertEquals(true, $preview->valid()); $this->assertEquals(true, $preview->valid());