From 95e0e0ce0a5b0c97514245c945051cc41534640a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Fri, 20 Oct 2017 17:54:47 +0200 Subject: [PATCH] Introduce \OCP\Preview\IProvider2 with the new interface --- lib/private/Preview.php | 5 ++- lib/private/Preview/Bitmap.php | 32 ++++++++++------- lib/private/Preview/Font.php | 2 +- lib/private/Preview/Image.php | 34 ++++++++---------- lib/private/Preview/MP3.php | 34 ++++++++++++++---- lib/private/Preview/Movie.php | 27 ++++++++++---- lib/private/Preview/Office.php | 25 +++++++++++-- lib/private/Preview/Provider.php | 57 ----------------------------- lib/private/Preview/SVG.php | 16 +++++++-- lib/private/Preview/TXT.php | 25 ++++++++----- lib/public/Preview/IProvider.php | 8 ++--- lib/public/Preview/IProvider2.php | 59 +++++++++++++++++++++++++++++++ 12 files changed, 199 insertions(+), 125 deletions(-) delete mode 100644 lib/private/Preview/Provider.php create mode 100644 lib/public/Preview/IProvider2.php diff --git a/lib/private/Preview.php b/lib/private/Preview.php index 1b79f28511c1..8187412943cc 100644 --- a/lib/private/Preview.php +++ b/lib/private/Preview.php @@ -1107,7 +1107,7 @@ private function generatePreview() { foreach ($providers as $closure) { $provider = $closure(); - if (!($provider instanceof \OCP\Preview\IProvider)) { + if (!($provider instanceof \OCP\Preview\IProvider2)) { continue; } @@ -1117,8 +1117,7 @@ private function generatePreview() { ); /** @var $provider Provider */ - $preview = $provider->getThumbnail( - $file, $this->configMaxWidth, $this->configMaxHeight, $scalingUp = false); + $preview = $provider->getThumbnail($file, $this->configMaxWidth, $this->configMaxHeight, $scalingUp = false); if (!($preview instanceof \OCP\IImage)) { continue; diff --git a/lib/private/Preview/Bitmap.php b/lib/private/Preview/Bitmap.php index f2122a5f9c11..b63c5fe86783 100644 --- a/lib/private/Preview/Bitmap.php +++ b/lib/private/Preview/Bitmap.php @@ -25,33 +25,33 @@ namespace OC\Preview; use Imagick; +use OCP\Files\File; +use OCP\Preview\IProvider2; +use OCP\Util; /** * Creates a PNG preview using ImageMagick via the PECL extension * * @package OC\Preview */ -abstract class Bitmap extends Provider { +abstract class Bitmap implements IProvider2 { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) { - $tmpPath = $fileview->toTmpFile($path); - if (!$tmpPath) { - return false; - } + $stream = $file->fopen('r'); // Creates \Imagick object from bitmap or vector file try { - $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); + $bp = $this->getResizedPreview($stream, $maxX, $maxY); } catch (\Exception $e) { - \OCP\Util::writeLog('core', 'ImageMagick says: ' . $e->getmessage(), \OCP\Util::ERROR); + Util::writeLog('core', 'ImageMagick says: ' . $e->getmessage(), Util::ERROR); return false; } - unlink($tmpPath); + fclose($stream); //new bitmap image object $image = new \OC_Image(); @@ -60,6 +60,13 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { return $image->valid() ? $image : false; } + /** + * @inheritdoc + */ + public function isAvailable(File $file) { + return true; + } + /** * Returns a preview of maxX times maxY dimensions in PNG format * @@ -68,17 +75,16 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { * ICC profiles are here: http://www.color.org/srgbprofiles.xalter * * It's possible to Gamma-correct an image via gammaImage() * - * @param string $tmpPath the location of the file to convert + * @param resource $stream the handle of the file to convert * @param int $maxX * @param int $maxY * * @return \Imagick */ - private function getResizedPreview($tmpPath, $maxX, $maxY) { + private function getResizedPreview($stream, $maxX, $maxY) { $bp = new Imagick(); - // Layer 0 contains either the bitmap or a flat representation of all vector layers - $bp->readImage($tmpPath . '[0]'); + $bp->readImageFile($stream); $bp = $this->resize($bp, $maxX, $maxY); diff --git a/lib/private/Preview/Font.php b/lib/private/Preview/Font.php index 54d385a31d50..775147d83eaa 100644 --- a/lib/private/Preview/Font.php +++ b/lib/private/Preview/Font.php @@ -29,4 +29,4 @@ class Font extends Bitmap { public function getMimeType() { return '/application\/(?:font-sfnt|x-font$)/'; } -} \ No newline at end of file +} diff --git a/lib/private/Preview/Image.php b/lib/private/Preview/Image.php index d658eeef63e7..ceeeb6679c98 100644 --- a/lib/private/Preview/Image.php +++ b/lib/private/Preview/Image.php @@ -26,38 +26,28 @@ */ namespace OC\Preview; -abstract class Image extends Provider { +use OCP\Files\File; +use OCP\Preview\IProvider2; + +abstract class Image implements IProvider2 { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { - //get fileinfo - $fileInfo = $fileview->getFileInfo($path); - if (!$fileInfo) { - return false; - } - + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) { $maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50); - $size = $fileInfo->getSize(); + $size = $file->getSize(); if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) { return false; } $image = new \OC_Image(); + $stream = $file->fopen('r'); - $useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal(); - if ($useTempFile) { - $fileName = $fileview->toTmpFile($path); - } else { - $fileName = $fileview->getLocalFile($path); - } - $image->loadFromFile($fileName); + $image->loadFromFileHandle($stream); $image->fixOrientation(); - if ($useTempFile) { - unlink($fileName); - } + fclose($stream); if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); @@ -66,4 +56,10 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { return false; } + /** + * @inheritdoc + */ + public function isAvailable(File $file) { + return true; + } } diff --git a/lib/private/Preview/MP3.php b/lib/private/Preview/MP3.php index 178baaf567fd..46e499ddd563 100644 --- a/lib/private/Preview/MP3.php +++ b/lib/private/Preview/MP3.php @@ -26,8 +26,10 @@ namespace OC\Preview; use ID3Parser\ID3Parser; +use OCP\Files\File; +use OCP\Preview\IProvider2; -class MP3 extends Provider { +class MP3 implements IProvider2 { /** * {@inheritDoc} */ @@ -38,12 +40,22 @@ public function getMimeType() { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { - $getID3 = new ID3Parser(); + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) { + + $useFileDirectly = (!$file->isEncrypted() && !$file->isMounted()); + if ($useFileDirectly) { + $absPath = $file->getStorage()->getLocalFile($file->getInternalPath()); + } else { + $absPath = \OC::$server->getTempManager()->getTemporaryFile(); - $tmpPath = $fileview->toTmpFile($path); - $tags = $getID3->analyze($tmpPath); - unlink($tmpPath); + $handle = $file->fopen('rb'); + file_put_contents($absPath, $handle); + fclose($handle); + } + + $getID3 = new ID3Parser(); + $tags = $getID3->analyze($absPath); + unlink($absPath); $picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null; if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) { $picture = $tags['id3v2']['PIC'][0]['data']; @@ -80,4 +92,14 @@ private function getNoCoverThumbnail() { return $image->valid() ? $image : false; } + /** + * Check if a preview can be generated for $path + * + * @param File $file + * @return bool + * @since 10.1.0 + */ + public function isAvailable(File $file) { + return true; + } } diff --git a/lib/private/Preview/Movie.php b/lib/private/Preview/Movie.php index 82c84620dc9a..2dd9d7423a2c 100644 --- a/lib/private/Preview/Movie.php +++ b/lib/private/Preview/Movie.php @@ -25,7 +25,10 @@ */ namespace OC\Preview; -class Movie extends Provider { +use OCP\Files\File; +use OCP\Preview\IProvider2; + +class Movie implements IProvider2 { public static $avconvBinary; public static $ffmpegBinary; public static $atomicParsleyBinary; @@ -46,23 +49,22 @@ public function getMimeType() { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) { // TODO: use proc_open() and stream the source file ? - $fileInfo = $fileview->getFileInfo($path); - $useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted()); - + $useFileDirectly = (!$file->isEncrypted() && !$file->isMounted()); if ($useFileDirectly) { - $absPath = $fileview->getLocalFile($path); + $absPath = $file->getStorage()->getLocalFile($file->getInternalPath()); } else { $absPath = \OC::$server->getTempManager()->getTemporaryFile(); - $handle = $fileview->fopen($path, 'rb'); + $handle = $file->fopen('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); + fclose($handle); } $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); @@ -174,4 +176,15 @@ private function generateThumbNail($maxX, $maxY, $absPath, $second) { } return false; } + + /** + * Check if a preview can be generated for $path + * + * @param File $file + * @return bool + * @since 10.1.0 + */ + public function isAvailable(File $file) { + return true; + } } diff --git a/lib/private/Preview/Office.php b/lib/private/Preview/Office.php index e7df7155bf21..4cdc31d814c5 100644 --- a/lib/private/Preview/Office.php +++ b/lib/private/Preview/Office.php @@ -24,19 +24,31 @@ */ namespace OC\Preview; -abstract class Office extends Provider { +use OCP\Files\File; +use OCP\Preview\IProvider2; + +abstract class Office implements IProvider2 { private $cmd; /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) { $this->initCmd(); if (is_null($this->cmd)) { return false; } - $absPath = $fileview->toTmpFile($path); + $useFileDirectly = (!$file->isEncrypted() && !$file->isMounted()); + if ($useFileDirectly) { + $absPath = $file->getStorage()->getLocalFile($file->getInternalPath()); + } else { + $absPath = \OC::$server->getTempManager()->getTemporaryFile(); + + $handle = $file->fopen('rb'); + file_put_contents($absPath, $handle); + fclose($handle); + } $tmpDir = \OC::$server->getTempManager()->getTempBaseDir(); @@ -77,6 +89,13 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { } + /** + * @inheritdoc + */ + public function isAvailable(File $file) { + return true; + } + private function initCmd() { $cmd = ''; diff --git a/lib/private/Preview/Provider.php b/lib/private/Preview/Provider.php deleted file mode 100644 index 448071c39477..000000000000 --- a/lib/private/Preview/Provider.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @author Joas Schilling - * @author Jörn Friedrich Dreyer - * @author Olivier Paroz - * @author Robin Appelman - * - * @copyright Copyright (c) 2018, ownCloud GmbH - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ -namespace OC\Preview; - -use OCP\Preview\IProvider; - -abstract class Provider implements IProvider { - private $options; - - /** - * 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(); - - /** - * @inheritdoc - */ - public function isAvailable(\OCP\Files\FileInfo $file) { - return true; - } - - /** - * @inheritdoc - */ - abstract public function getThumbnail($path, $maxX, $maxY, $scalingup); -} diff --git a/lib/private/Preview/SVG.php b/lib/private/Preview/SVG.php index c34f3acf7f77..ae1e85264182 100644 --- a/lib/private/Preview/SVG.php +++ b/lib/private/Preview/SVG.php @@ -23,7 +23,10 @@ */ namespace OC\Preview; -class SVG extends Provider { +use OCP\Files\File; +use OCP\Preview\IProvider2; + +class SVG implements IProvider2 { /** * {@inheritDoc} */ @@ -34,12 +37,12 @@ public function getMimeType() { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) { try { $svg = new \Imagick(); $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) !== '' . $content; } @@ -67,4 +70,11 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { } return false; } + + /** + * @inheritdoc + */ + public function isAvailable(File $file) { + return true; + } } diff --git a/lib/private/Preview/TXT.php b/lib/private/Preview/TXT.php index 4adf302ec2a3..a996d1ffe279 100644 --- a/lib/private/Preview/TXT.php +++ b/lib/private/Preview/TXT.php @@ -25,7 +25,10 @@ */ namespace OC\Preview; -class TXT extends Provider { +use OCP\Files\File; +use OCP\Preview\IProvider2; + +class TXT implements IProvider2 { /** * {@inheritDoc} */ @@ -36,14 +39,7 @@ public function getMimeType() { /** * {@inheritDoc} */ - public function isAvailable(\OCP\Files\FileInfo $file) { - return $file->getSize() > 0; - } - - /** - * {@inheritDoc} - */ - public function getThumbnail($file, $maxX, $maxY, $scalingup) { + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) { $content = $file->fopen('r'); $content = stream_get_contents($content,3000); @@ -89,4 +85,15 @@ public function getThumbnail($file, $maxX, $maxY, $scalingup) { return $image->valid() ? $image : false; } + + /** + * Check if a preview can be generated for $path + * + * @param File $file + * @return bool + * @since 10.1.0 + */ + public function isAvailable(File $file) { + return $file->getSize() > 0; + } } diff --git a/lib/public/Preview/IProvider.php b/lib/public/Preview/IProvider.php index 50bf5c325cd1..c9644ce0b7f1 100644 --- a/lib/public/Preview/IProvider.php +++ b/lib/public/Preview/IProvider.php @@ -21,13 +21,12 @@ */ namespace OCP\Preview; -use OCP\Files\File; - /** * Interface IProvider * * @package OCP\Preview * @since 8.1.0 + * @deprecated 10.1.0 */ interface IProvider { /** @@ -48,12 +47,13 @@ public function isAvailable(\OCP\Files\FileInfo $file); /** * get thumbnail for file at path $path * - * @param File $path Path of file + * @param string $path Path of 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 * @param bool $scalingup Disable/Enable upscaling of previews + * @param \OC\Files\View $fileview fileview object of user folder * @return bool|\OCP\IImage false if no preview was generated * @since 8.1.0 */ - public function getThumbnail($path, $maxX, $maxY, $scalingup); + public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview); } diff --git a/lib/public/Preview/IProvider2.php b/lib/public/Preview/IProvider2.php new file mode 100644 index 000000000000..a8802af32393 --- /dev/null +++ b/lib/public/Preview/IProvider2.php @@ -0,0 +1,59 @@ + + * @author Morris Jobke + * + * @copyright Copyright (c) 2017, ownCloud GmbH + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see + * + */ +namespace OCP\Preview; + +use OCP\Files\File; + +/** + * Interface IProvider2 + * + * @package OCP\Preview + * @since 10.1.0 + */ +interface IProvider2 { + /** + * @return string Regex with the mimetypes that are supported by this provider + * @since 10.1.0 + */ + public function getMimeType(); + + /** + * Check if a preview can be generated for $path + * + * @param File $file + * @return bool + * @since 10.1.0 + */ + public function isAvailable(File $file); + + /** + * get thumbnail for file at path $path + * + * @param File $file Path of 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 + * @param bool $scalingUp Disable/Enable upscaling of previews + * @return bool|\OCP\IImage false if no preview was generated + * @since 10.1.0 + */ + public function getThumbnail(File $file, $maxX, $maxY, $scalingUp); +}