]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add new Provider interface for preview providers 15870/head
authorRobin Appelman <robin@icewind.nl>
Tue, 4 Jun 2019 13:25:25 +0000 (15:25 +0200)
committerRobin Appelman <robin@icewind.nl>
Mon, 17 Jun 2019 12:09:09 +0000 (14:09 +0200)
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>
36 files changed:
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/Preview/BMP.php
lib/private/Preview/Bitmap.php
lib/private/Preview/Font.php
lib/private/Preview/GIF.php
lib/private/Preview/Generator.php
lib/private/Preview/GeneratorHelper.php
lib/private/Preview/HEIC.php
lib/private/Preview/Illustrator.php
lib/private/Preview/Image.php
lib/private/Preview/JPEG.php
lib/private/Preview/MP3.php
lib/private/Preview/MSOffice2003.php
lib/private/Preview/MSOffice2007.php
lib/private/Preview/MSOfficeDoc.php
lib/private/Preview/MarkDown.php
lib/private/Preview/Movie.php
lib/private/Preview/Office.php
lib/private/Preview/OpenDocument.php
lib/private/Preview/PDF.php
lib/private/Preview/PNG.php
lib/private/Preview/Photoshop.php
lib/private/Preview/Postscript.php
lib/private/Preview/ProviderV1Adapter.php [new file with mode: 0644]
lib/private/Preview/ProviderV2.php [new file with mode: 0644]
lib/private/Preview/SVG.php
lib/private/Preview/StarOffice.php
lib/private/Preview/TIFF.php
lib/private/Preview/TXT.php
lib/private/Preview/XBitmap.php
lib/private/PreviewManager.php
lib/public/Preview/IProvider.php
lib/public/Preview/IProviderV2.php [new file with mode: 0644]
tests/lib/Preview/GeneratorTest.php
tests/lib/Preview/Provider.php

index f49865ae6edc3dad4ed4f71af3d49bb8948c4e10..73c64b16b9a22d4fce34db6a3848fd8d17e76aad 100644 (file)
@@ -351,6 +351,7 @@ return array(
     'OCP\\OCS\\IDiscoveryService' => $baseDir . '/lib/public/OCS/IDiscoveryService.php',
     '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\\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',
@@ -1016,6 +1017,8 @@ return array(
     'OC\\Preview\\Photoshop' => $baseDir . '/lib/private/Preview/Photoshop.php',
     'OC\\Preview\\Postscript' => $baseDir . '/lib/private/Preview/Postscript.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\\StarOffice' => $baseDir . '/lib/private/Preview/StarOffice.php',
     'OC\\Preview\\TIFF' => $baseDir . '/lib/private/Preview/TIFF.php',
index 5f204e4e23eb162137e32a63fce254267350865a..4977c73b795fa6920f32f10d49da5b1e4c075338 100644 (file)
@@ -385,6 +385,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OCP\\OCS\\IDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCS/IDiscoveryService.php',
         '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\\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',
@@ -1050,6 +1051,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OC\\Preview\\Photoshop' => __DIR__ . '/../../..' . '/lib/private/Preview/Photoshop.php',
         'OC\\Preview\\Postscript' => __DIR__ . '/../../..' . '/lib/private/Preview/Postscript.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\\StarOffice' => __DIR__ . '/../../..' . '/lib/private/Preview/StarOffice.php',
         'OC\\Preview\\TIFF' => __DIR__ . '/../../..' . '/lib/private/Preview/TIFF.php',
index de08cfe0c8f163d8243719e15a3064287cd5e9f1..fed0c458d0efb8070e4efe21351521ac122c909f 100644 (file)
@@ -26,7 +26,7 @@ class BMP extends Image {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/image\/bmp/';
        }
 }
index 1fd42a0ea97276327793164d9eb5ae923e8198f9..000b1f8277f21a9a272a67dc4382298986ff1c3c 100644 (file)
 namespace OC\Preview;
 
 use Imagick;
+use OCP\IImage;
 use OCP\ILogger;
+use OCP\Files\File;
 
 /**
  * Creates a PNG preview using ImageMagick via the PECL extension
  *
  * @package OC\Preview
  */
-abstract class Bitmap extends Provider {
+abstract class Bitmap extends ProviderV2 {
 
        /**
         * {@inheritDoc}
         */
-       public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
+       public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
 
-               $tmpPath = $fileview->toTmpFile($path);
-               if (!$tmpPath) {
-                       return false;
-               }
+               $tmpPath = $this->getLocalFile($file);
 
                // Creates \Imagick object from bitmap or vector file
                try {
                        $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
                } catch (\Exception $e) {
                        \OC::$server->getLogger()->logException($e, [
-                               'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:',
+                               'message' => 'File: ' . $file->getPath() . ' Imagick says:',
                                'level' => ILogger::ERROR,
                                'app' => 'core',
                        ]);
-                       return false;
+                       return null;
                }
 
-               unlink($tmpPath);
+               $this->cleanTmpFiles();
 
                //new bitmap image object
                $image = new \OC_Image();
                $image->loadFromData($bp);
                //check if image object is valid
-               return $image->valid() ? $image : false;
+               return $image->valid() ? $image : null;
        }
 
        /**
index 4d5732eb88017f8899fee41b0cb5059f76f47a04..3f24bf3e738c8caf20d9ccb4fd73121f0f53af26 100644 (file)
@@ -27,7 +27,7 @@ class Font extends Bitmap {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/(?:font-sfnt|x-font$)/';
        }
-}
\ No newline at end of file
+}
index 2189273b3b5077f78a9d73f5e6a2b0e1e723096e..2652efc4dddd3e96e26c91856459c3be3c6a9df6 100644 (file)
@@ -26,7 +26,7 @@ class GIF extends Image {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/image\/gif/';
        }
 }
index 1f7decf2b79669ca6a4813f56cfa44f90236c5b5..2f9401af385cb41ca7186825b0f26e3453be441a 100644 (file)
@@ -35,6 +35,8 @@ use OCP\IConfig;
 use OCP\IImage;
 use OCP\IPreview;
 use OCP\Preview\IProvider;
+use OCP\Preview\IProviderV2;
+use OCP\Preview\IVersionedPreviewFile;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\EventDispatcher\GenericEvent;
 
@@ -178,9 +180,9 @@ class Generator {
                                continue;
                        }
 
-                       foreach ($providers as $provider) {
-                               $provider = $this->helper->getProvider($provider);
-                               if (!($provider instanceof IProvider)) {
+                       foreach ($providers as $providerClosure) {
+                               $provider = $this->helper->getProvider($providerClosure);
+                               if (!($provider instanceof IProviderV2)) {
                                        continue;
                                }
 
index 2f1a1f8aeff02bf8e6e487213321522c03023417..7e35b5360d44512395688d02adeb9e4a1d1e330e 100644 (file)
@@ -30,6 +30,7 @@ use OCP\IConfig;
 use OCP\IImage;
 use OCP\Image as OCPImage;
 use OCP\Preview\IProvider;
+use OCP\Preview\IProviderV2;
 
 /**
  * Very small wrapper class to make the generator fully unit testable
@@ -54,21 +55,8 @@ class GeneratorHelper {
         * @param int $maxHeight
         * @return bool|IImage
         */
-       public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) {
-               list($view, $path) = $this->getViewAndPath($file);
-               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];
+       public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight) {
+               return $provider->getThumbnail($file, $maxWidth, $maxHeight);
        }
 
        /**
@@ -82,10 +70,14 @@ class GeneratorHelper {
        }
 
        /**
-        * @param $provider
-        * @return IProvider
+        * @param callable $providerClosure
+        * @return IProviderV2
         */
-       public function getProvider($provider) {
-               return $provider();
+       public function getProvider($providerClosure) {
+               $provider = $providerClosure();
+               if ($provider instanceof IProvider) {
+                       $provider = new ProviderV1Adapter($provider);
+               }
+               return $provider;
        }
 }
index 54bc212b6b50245901ef7eed99e92011d05c3b0f..d23e6c6bd1f27e7e62889ef6a914c7dcfd175058 100644 (file)
@@ -23,14 +23,16 @@ declare(strict_types=1);
 
 namespace OC\Preview;
 
+use OCP\IImage;
 use OCP\ILogger;
+use OCP\Files\File;
 
 /**
  * Creates a JPG preview using ImageMagick via the PECL extension
  *
  * @package OC\Preview
  */
-class HEIC extends Provider {
+class HEIC extends ProviderV2 {
        /**
         * {@inheritDoc}
         */
@@ -48,11 +50,8 @@ class HEIC extends Provider {
        /**
         * {@inheritDoc}
         */
-       public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
-               $tmpPath = $fileview->toTmpFile($path);
-               if (!$tmpPath) {
-                       return false;
-               }
+       public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
+               $tmpPath = $this->getLocalFile($file);
 
                // Creates \Imagick object from the heic file
                try {
@@ -60,20 +59,20 @@ class HEIC extends Provider {
                        $bp->setFormat('jpg');
                } catch (\Exception $e) {
                        \OC::$server->getLogger()->logException($e, [
-                               'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:',
+                               'message' => 'File: ' . $file->getPath() . ' Imagick says:',
                                'level' => ILogger::ERROR,
                                'app' => 'core',
                        ]);
-                       return false;
+                       return null;
                }
 
-               unlink($tmpPath);
+               $this->cleanTmpFiles();
 
                //new bitmap image object
                $image = new \OC_Image();
                $image->loadFromData($bp);
                //check if image object is valid
-               return $image->valid() ? $image : false;
+               return $image->valid() ? $image : null;
        }
 
        /**
index 733b679f3f53b172c3bb1c42c75a0f594e113b4f..b8f29d4754f449e9d0f00f324f5696dc08dfde14 100644 (file)
@@ -28,7 +28,7 @@ class Illustrator extends Bitmap {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/illustrator/';
        }
 }
index 86ce3bcd07126fabfbc956066eb14ce2e3ba3c02..6f82904a6a778e61b3edcfd17d543ab0e2e0b254 100644 (file)
  */
 namespace OC\Preview;
 
-abstract class Image extends Provider {
+use OCP\Files\File;
+use OCP\IImage;
+
+abstract class Image extends ProviderV2 {
 
        /**
         * {@inheritDoc}
         */
-       public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
-               //get fileinfo
-               $fileInfo = $fileview->getFileInfo($path);
-               if (!$fileInfo) {
-                       return false;
-               }
-
+       public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
                $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;
+                       return null;
                }
 
                $image = new \OC_Image();
 
-               $useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal();
-               if ($useTempFile) {
-                       $fileName = $fileview->toTmpFile($path);
-               } else {
-                       $fileName = $fileview->getLocalFile($path);
-               }
+               $fileName = $this->getLocalFile($file);
+
                $image->loadFromFile($fileName);
                $image->fixOrientation();
-               if ($useTempFile) {
-                       unlink($fileName);
-               }
+
+               $this->cleanTmpFiles();
+
                if ($image->valid()) {
                        $image->scaleDownToFit($maxX, $maxY);
 
                        return $image;
                }
-               return false;
+               return null;
        }
 
 }
index 1937d4a4a8a863c41ef973292fe27228ab54fa77..9948d2ecfd58559d08eab7d0fd4505b25461995f 100644 (file)
@@ -26,7 +26,7 @@ class JPEG extends Image {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/image\/jpeg/';
        }
 }
index 11a512286ec21f41ec6ec87127f2b5ef5d41efa2..f560f10010996a2b03bf90fdf07e765dc0704682 100644 (file)
@@ -28,23 +28,26 @@ namespace OC\Preview;
 
 use ID3Parser\ID3Parser;
 
-class MP3 extends Provider {
+use OCP\Files\File;
+use OCP\IImage;
+
+class MP3 extends ProviderV2 {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/audio\/mpeg/';
        }
 
        /**
         * {@inheritDoc}
         */
-       public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
+       public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
                $getID3 = new ID3Parser();
 
-               $tmpPath = $fileview->toTmpFile($path);
+               $tmpPath = $this->getLocalFile($file);
                $tags = $getID3->analyze($tmpPath);
-               unlink($tmpPath);
+               $this->cleanTmpFiles();
                $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'];
@@ -61,6 +64,6 @@ class MP3 extends Provider {
                        }
                }
 
-               return false;
+               return null;
        }
 }
index 39ba4bc3f5c6bd6bb13bfa6ea5c8e861ff495156..5af66f727203740592dae1a4fcb182629b8be4f0 100644 (file)
@@ -26,7 +26,7 @@ class MSOffice2003 extends Office {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/vnd.ms-.*/';
        }
 }
index 2e033683c20e057607ccefc78d2791f0f3e82e76..0d1177d9de99ec41b838693d64d6f669a4193f2c 100644 (file)
@@ -26,7 +26,7 @@ class MSOffice2007 extends Office {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/vnd.openxmlformats-officedocument.*/';
        }
 }
index 7e9d22fcf591fbd3d7d2ffd920b754d7b2da7c0e..24d1a9d5b6a48b5f705cbbb4e929b8a9e2beaae2 100644 (file)
@@ -26,7 +26,7 @@ class MSOfficeDoc extends Office {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/msword/';
        }
 }
index f49efbaaa07188fe89b92dbb1c152c0888d10e0e..ae24c4f4419f447673329e18c0c00207fbcc2cbc 100644 (file)
@@ -25,7 +25,7 @@ class MarkDown extends TXT {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/text\/(x-)?markdown/';
        }
 
index 3576a889e564eb5e2c66627e896f78532fa3c460..2f64811b45e457500c1b1eef0806d7ddf49d31c0 100644 (file)
  */
 namespace OC\Preview;
 
-class Movie extends Provider {
+use OCP\Files\File;
+use OCP\IImage;
+
+class Movie extends ProviderV2 {
        public static $avconvBinary;
        public static $ffmpegBinary;
 
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/video\/.*/';
        }
 
        /**
         * {@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 ?
 
-               $fileInfo = $fileview->getFileInfo($path);
-               $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);
-               }
+               $absPath = $this->getLocalFile($file, 5242880); // only use the first 5MB
 
                $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
                if ($result === false) {
@@ -66,9 +55,7 @@ class Movie extends Provider {
                        }
                }
 
-               if (!$useFileDirectly) {
-                       unlink($absPath);
-               }
+               $this->cleanTmpFiles();
 
                return $result;
        }
@@ -78,9 +65,9 @@ class Movie extends Provider {
         * @param int $maxY
         * @param string $absPath
         * @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();
 
                if (self::$avconvBinary) {
@@ -109,6 +96,6 @@ class Movie extends Provider {
                        }
                }
                unlink($tmpPath);
-               return false;
+               return null;
        }
 }
index 4a2aa7f495387ca3afa9972e4aa9d4bd591b126d..f51023c5a833e5cb85842cd8f50f79e232b1caee 100644 (file)
  */
 namespace OC\Preview;
 
+use OCP\IImage;
 use OCP\ILogger;
+use OCP\Files\File;
 
-abstract class Office extends Provider {
+abstract class Office extends ProviderV2 {
        private $cmd;
 
        /**
         * {@inheritDoc}
         */
-       public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
+       public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
                $this->initCmd();
                if (is_null($this->cmd)) {
-                       return false;
+                       return null;
                }
 
-               $absPath = $fileview->toTmpFile($path);
+               $absPath = $this->getLocalFile($file);
 
                $tmpDir = \OC::$server->getTempManager()->getTempBaseDir();
 
@@ -59,19 +61,19 @@ abstract class Office extends Provider {
                        $png = new \imagick($pngPreview . '[0]');
                        $png->setImageFormat('jpg');
                } catch (\Exception $e) {
-                       unlink($absPath);
+                       $this->cleanTmpFiles();
                        unlink($pngPreview);
                        \OC::$server->getLogger()->logException($e, [
                                'level' => ILogger::ERROR,
                                'app' => 'core',
                        ]);
-                       return false;
+                       return null;
                }
 
                $image = new \OC_Image();
                $image->loadFromData($png);
 
-               unlink($absPath);
+               $this->cleanTmpFiles();
                unlink($pngPreview);
 
                if ($image->valid()) {
@@ -79,7 +81,7 @@ abstract class Office extends Provider {
 
                        return $image;
                }
-               return false;
+               return null;
 
        }
 
index 40971a0951be7e25ee37b18cdba95bac3aeffab6..929fecffb214a1c75a4776c0cd4300b42c96d105 100644 (file)
@@ -26,7 +26,7 @@ class OpenDocument extends Office {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/vnd.oasis.opendocument.*/';
        }
 }
index 04d7c66e59546bfd995ff9d85035ca5b3a922bc6..f23f58be365643a6548d023e83349e72e07643e8 100644 (file)
@@ -28,7 +28,7 @@ class PDF extends Bitmap {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/pdf/';
        }
 }
index cafe081818d87e3d83e2530d4f21d07c2425068b..1a1cf52c968908c1ad38bebd2f84b3da338f9a63 100644 (file)
@@ -26,7 +26,7 @@ class PNG extends Image {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/image\/png/';
        }
 }
index 243fd7052e50df1a51aa9404b592485e37ada72d..975acb8a03d5cc0ad4cb38dc9da513a7af10b14c 100644 (file)
@@ -28,7 +28,7 @@ class Photoshop extends Bitmap {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/x-photoshop/';
        }
 }
index c20347d74ffd061e01255c3911b2ae60b2c31826..4dd194849912f37c7b48017746dedf93fd4e3d67 100644 (file)
@@ -28,7 +28,7 @@ class Postscript extends Bitmap {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/postscript/';
        }
 }
diff --git a/lib/private/Preview/ProviderV1Adapter.php b/lib/private/Preview/ProviderV1Adapter.php
new file mode 100644 (file)
index 0000000..fc7245a
--- /dev/null
@@ -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];
+       }
+
+}
diff --git a/lib/private/Preview/ProviderV2.php b/lib/private/Preview/ProviderV2.php
new file mode 100644 (file)
index 0000000..8a64ac6
--- /dev/null
@@ -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 = [];
+       }
+}
index e283175c2e21c14dcde9dca70765203c69026a94..3f313f4bb9b4ca7d26cb4f6c79ff8276283a0499 100644 (file)
  */
 namespace OC\Preview;
 
+use OCP\IImage;
 use OCP\ILogger;
+use OCP\Files\File;
 
-class SVG extends Provider {
+class SVG extends ProviderV2 {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/image\/svg\+xml/';
        }
 
        /**
         * {@inheritDoc}
         */
-       public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
+       public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
                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) !== '<?xml') {
                                $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
                        }
 
                        // Do not parse SVG files with references
                        if (stripos($content, 'xlink:href') !== false) {
-                               return false;
+                               return null;
                        }
 
                        $svg->readImageBlob($content);
@@ -59,7 +61,7 @@ class SVG extends Provider {
                                'level' => ILogger::ERROR,
                                'app' => 'core',
                        ]);
-                       return false;
+                       return null;
                }
 
                //new image object
@@ -71,6 +73,6 @@ class SVG extends Provider {
 
                        return $image;
                }
-               return false;
+               return null;
        }
 }
index 3537d4baddb965f4b11bd71510ee5e2652e4c1cc..2c9542b22e677a476153ac238a0c6176198b3f35 100644 (file)
@@ -26,7 +26,7 @@ class StarOffice extends Office {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/application\/vnd.sun.xml.*/';
        }
 }
index 9f03e3c79f6be6c5feee4bda92c772cd7529f113..0c667117339b93fb40ebadad938736739590880b 100644 (file)
@@ -28,7 +28,7 @@ class TIFF extends Bitmap {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/image\/tiff/';
        }
 }
index 2bb121775d6b635b570ebece00494e1379bfc8aa..1ac98d62338c702426f186986ee0e7bd2f83d8a2 100644 (file)
  */
 namespace OC\Preview;
 
-class TXT extends Provider {
+use OCP\Files\File;
+use OCP\Files\FileInfo;
+use OCP\IImage;
+
+class TXT extends ProviderV2 {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/text\/plain/';
        }
 
        /**
         * {@inheritDoc}
         */
-       public function isAvailable(\OCP\Files\FileInfo $file) {
+       public function isAvailable(FileInfo $file): bool {
                return $file->getSize() > 0;
        }
 
        /**
         * {@inheritDoc}
         */
-       public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
-               $content = $fileview->fopen($path, 'r');
+       public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
+               $content = $file->fopen('r');
 
                if ($content === false) {
-                       return false;
+                       return null;
                }
 
                $content = stream_get_contents($content,3000);
 
                //don't create previews of empty text files
                if(trim($content) === '') {
-                       return false;
+                       return null;
                }
 
                $lines = preg_split("/\r\n|\n|\r/", $content);
@@ -95,6 +99,6 @@ class TXT extends Provider {
                $imageObject = new \OC_Image();
                $imageObject->setResource($image);
 
-               return $imageObject->valid() ? $imageObject : false;
+               return $imageObject->valid() ? $imageObject : null;
        }
 }
index 7a282e5d1e1a9b246217eb7fbe81a22ca6739a01..ffec165a7b61f2752e1f04c9ff8c7b9cac46e1e2 100644 (file)
@@ -26,7 +26,7 @@ class XBitmap extends Image {
        /**
         * {@inheritDoc}
         */
-       public function getMimeType() {
+       public function getMimeType(): string {
                return '/image\/x-xbitmap/';
        }
 }
index 931b0035c4f8710f21466381cc7e79c55a390771..6635d919fb7f5fff81f5cfe9e6492613bcb1a7ef 100644 (file)
@@ -97,7 +97,7 @@ class PreviewManager implements IPreview {
         * In order to improve lazy loading a closure can be registered which will be
         * 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 \Closure $callable
index ca545d8eb764bc1003d6313680d6b4913009d6b1..5d62b3ce6d0265fdb443daa2f38963d2f9ff3716 100644 (file)
@@ -27,6 +27,7 @@ namespace OCP\Preview;
  *
  * @package OCP\Preview
  * @since 8.1.0
+ * @deprecated 17.0.0 use IProviderV2 instead
  */
 interface IProvider {
        /**
diff --git a/lib/public/Preview/IProviderV2.php b/lib/public/Preview/IProviderV2.php
new file mode 100644 (file)
index 0000000..97fca21
--- /dev/null
@@ -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;
+}
index 565b526b659e7cb0da66afcb962af357acc4c05a..9bca43a1a7efbf707426fd89e4a23b575154aad0 100644 (file)
@@ -33,6 +33,7 @@ use OCP\IConfig;
 use OCP\IImage;
 use OCP\IPreview;
 use OCP\Preview\IProvider;
+use OCP\Preview\IProviderV2;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Symfony\Component\EventDispatcher\GenericEvent;
 
@@ -149,13 +150,13 @@ class GeneratorTest extends \Test\TestCase {
                                return $defult;
                        }));
 
-               $invalidProvider = $this->createMock(IProvider::class);
+               $invalidProvider = $this->createMock(IProviderV2::class);
                $invalidProvider->method('isAvailable')
                        ->willReturn(true);
-               $unavailableProvider = $this->createMock(IProvider::class);
+               $unavailableProvider = $this->createMock(IProviderV2::class);
                $unavailableProvider->method('isAvailable')
                        ->willReturn(false);
-               $validProvider = $this->createMock(IProvider::class);
+               $validProvider = $this->createMock(IProviderV2::class);
                $validProvider->method('isAvailable')
                        ->with($file)
                        ->willReturn(true);
index 5377f30df8207761e9e25f3d21019e2465b0346e..2050b3f56b489ffbd586ffe8c727149e9ecf67d8 100644 (file)
@@ -21,6 +21,8 @@
 
 namespace Test\Preview;
 
+use OC\Files\Node\File;
+
 abstract class Provider extends \Test\TestCase {
 
        /** @var string */
@@ -136,7 +138,8 @@ abstract class Provider extends \Test\TestCase {
         * @return bool|\OCP\IImage
         */
        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->assertEquals(true, $preview->valid());