From b8248f4a0acf0de228774aec0f7150a538ed09e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Wed, 2 Apr 2014 17:32:27 +0200 Subject: [PATCH 1/7] compare floats with epsilon --- lib/private/preview.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 0c1af3c958..963cda6699 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -355,7 +355,8 @@ class Preview { $y = (int)$size[1]; $aspectRatio = (float)($x / $y); - if ($aspectRatio !== $wantedAspectRatio) { + $epsilon = 0.000001; + if (($aspectRatio - $wantedAspectRatio) >= $epsilon) { continue; } From bca6cc6f741ae957a345ca21d689a5f23d964d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Wed, 2 Apr 2014 17:34:48 +0200 Subject: [PATCH 2/7] remove unnecessary code --- lib/private/preview.php | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 963cda6699..493ed90f77 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -373,24 +373,8 @@ class Preview { $possibleThumbnails[$x] = $thumbnail['path']; } - if (count($possibleThumbnails) === 0) { - return false; - } - - if (count($possibleThumbnails) === 1) { - return current($possibleThumbnails); - } - ksort($possibleThumbnails); - if (key(reset($possibleThumbnails)) > $maxX) { - return current(reset($possibleThumbnails)); - } - - if (key(end($possibleThumbnails)) < $maxX) { - return current(end($possibleThumbnails)); - } - foreach ($possibleThumbnails as $width => $path) { if ($width < $maxX) { continue; @@ -398,6 +382,8 @@ class Preview { return $path; } } + + return false; } /** From 436a78db448d4263f90de0490202b8d7a936d3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Wed, 2 Apr 2014 17:59:39 +0200 Subject: [PATCH 3/7] extract method isCachedBigger --- lib/private/preview.php | 47 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 493ed90f77..7be392a50d 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -314,16 +314,12 @@ class Preview { /** * @brief check if thumbnail or bigger version of thumbnail of file is cached - * @return mixed (bool / string) - * false if thumbnail does not exist - * path to thumbnail if thumbnail exists + * @return string|false path to thumbnail if it exists or false */ private function isCached() { $file = $this->getFile(); $maxX = $this->getMaxX(); $maxY = $this->getMaxY(); - $scalingUp = $this->getScalingUp(); - $maxScaleFactor = $this->getMaxScaleFactor(); $fileInfo = $this->getFileInfo($file); $fileId = $fileInfo->getId(); @@ -333,16 +329,40 @@ class Preview { } $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; - if (!$this->userView->is_dir($previewPath)) { - return false; - } //does a preview with the wanted height and width already exist? if ($this->userView->file_exists($previewPath . $maxX . '-' . $maxY . '.png')) { return $previewPath . $maxX . '-' . $maxY . '.png'; } - $wantedAspectRatio = (float)($maxX / $maxY); + return $this->isCachedBigger(); + } + + /** + * @brief check if a bigger version of thumbnail of file is cached + * @return string|false path to bigger thumbnail if it exists or false + */ + private function isCachedBigger() { + + $file = $this->getFile(); + $maxX = $this->getMaxX(); + $maxY = $this->getMaxY(); + $scalingUp = $this->getScalingUp(); + $maxScaleFactor = $this->getMaxScaleFactor(); + + $fileInfo = $this->fileView->getFileInfo($file); + $fileId = $fileInfo['fileid']; + + if (is_null($fileId)) { + return false; + } + + $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; + if (!$this->userView->is_dir($previewPath)) { + return false; + } + + $wantedAspectRatio = (float) ($maxX / $maxY); //array for usable cached thumbnails $possibleThumbnails = array(); @@ -351,10 +371,10 @@ class Preview { foreach ($allThumbnails as $thumbnail) { $name = rtrim($thumbnail['name'], '.png'); $size = explode('-', $name); - $x = (int)$size[0]; - $y = (int)$size[1]; + $x = (int) $size[0]; + $y = (int) $size[1]; - $aspectRatio = (float)($x / $y); + $aspectRatio = (float) ($x / $y); $epsilon = 0.000001; if (($aspectRatio - $wantedAspectRatio) >= $epsilon) { continue; @@ -382,10 +402,9 @@ class Preview { return $path; } } - + return false; } - /** * @brief return a preview of a file * @return \OC_Image From b9a8bd7e1f6f99fe5d26e1693d13ebc6ddf81777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Wed, 2 Apr 2014 18:32:32 +0200 Subject: [PATCH 4/7] extract more methods --- lib/private/preview.php | 127 +++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 55 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 7be392a50d..36cc5d9a2e 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -314,19 +314,16 @@ class Preview { /** * @brief check if thumbnail or bigger version of thumbnail of file is cached + * @param int $fileId fileId of the original image * @return string|false path to thumbnail if it exists or false */ - private function isCached() { - $file = $this->getFile(); - $maxX = $this->getMaxX(); - $maxY = $this->getMaxY(); - - $fileInfo = $this->getFileInfo($file); - $fileId = $fileInfo->getId(); - + private function isCached($fileId) { if (is_null($fileId)) { return false; } + + $maxX = $this->getMaxX(); + $maxY = $this->getMaxY(); $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; @@ -335,65 +332,24 @@ class Preview { return $previewPath . $maxX . '-' . $maxY . '.png'; } - return $this->isCachedBigger(); + return $this->isCachedBigger($fileId); } /** * @brief check if a bigger version of thumbnail of file is cached + * @param int $fileId fileId of the original image * @return string|false path to bigger thumbnail if it exists or false */ - private function isCachedBigger() { - - $file = $this->getFile(); - $maxX = $this->getMaxX(); - $maxY = $this->getMaxY(); - $scalingUp = $this->getScalingUp(); - $maxScaleFactor = $this->getMaxScaleFactor(); - - $fileInfo = $this->fileView->getFileInfo($file); - $fileId = $fileInfo['fileid']; + private function isCachedBigger($fileId) { if (is_null($fileId)) { return false; } - $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; - if (!$this->userView->is_dir($previewPath)) { - return false; - } - - $wantedAspectRatio = (float) ($maxX / $maxY); + $maxX = $this->getMaxX(); //array for usable cached thumbnails - $possibleThumbnails = array(); - - $allThumbnails = $this->userView->getDirectoryContent($previewPath); - foreach ($allThumbnails as $thumbnail) { - $name = rtrim($thumbnail['name'], '.png'); - $size = explode('-', $name); - $x = (int) $size[0]; - $y = (int) $size[1]; - - $aspectRatio = (float) ($x / $y); - $epsilon = 0.000001; - if (($aspectRatio - $wantedAspectRatio) >= $epsilon) { - continue; - } - - if ($x < $maxX || $y < $maxY) { - if ($scalingUp) { - $scalefactor = $maxX / $x; - if ($scalefactor > $maxScaleFactor) { - continue; - } - } else { - continue; - } - } - $possibleThumbnails[$x] = $thumbnail['path']; - } - - ksort($possibleThumbnails); + $possibleThumbnails = $this->getPossibleThumbnails($fileId); foreach ($possibleThumbnails as $width => $path) { if ($width < $maxX) { @@ -405,6 +361,67 @@ class Preview { return false; } + /** + * @brief get possible bigger thumbnails of the given image + * @param int $fileId fileId of the original image + * @return array of paths to bigger thumbnails + */ + private function getPossibleThumbnails($fileId) { + + if (is_null($fileId)) { + return array(); + } + + $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; + + $wantedAspectRatio = (float) ($this->getMaxX() / $this->getMaxY()); + + //array for usable cached thumbnails + $possibleThumbnails = array(); + + $allThumbnails = $this->userView->getDirectoryContent($previewPath); + foreach ($allThumbnails as $thumbnail) { + $name = rtrim($thumbnail['name'], '.png'); + list($x, $y, $aspectRatio) = $this->getDimensionsFromFilename($name); + + if (($aspectRatio - $wantedAspectRatio) >= 0.000001 + || $this->unscalable($x, $y) + ) { + continue; + } + $possibleThumbnails[$x] = $thumbnail['path']; + } + + ksort($possibleThumbnails); + + return $possibleThumbnails; + } + private function getDimensionsFromFilename($name) { + $size = explode('-', $name); + $x = (int) $size[0]; + $y = (int) $size[1]; + $aspectRatio = (float) ($x / $y); + return array('x' => $x,'y' => $y,'aspectRatio' => $aspectRatio); + } + private function unscalable($x, $y) { + + $maxX = $this->getMaxX(); + $maxY = $this->getMaxY(); + $scalingUp = $this->getScalingUp(); + $maxScaleFactor = $this->getMaxScaleFactor(); + + if ($x < $maxX || $y < $maxY) { + if ($scalingUp) { + $scalefactor = $maxX / $x; + if ($scalefactor > $maxScaleFactor) { + return true; + } + } else { + return true; + } + } + return false; + } /** * @brief return a preview of a file * @return \OC_Image @@ -426,7 +443,7 @@ class Preview { } $fileId = $fileInfo->getId(); - $cached = $this->isCached(); + $cached = $this->isCached($fileId); if ($cached) { $stream = $this->userView->fopen($cached, 'r'); From c7324f7e44d1d1bd5c39a9b967dff76b274b9048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 3 Apr 2014 12:00:53 +0200 Subject: [PATCH 5/7] fix float comparison for negative results --- lib/private/preview.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 36cc5d9a2e..71c16687fb 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -109,7 +109,7 @@ class Preview { * @brief returns the path of the file you want a thumbnail from * @return string */ - public function getFile() { + public function getFile() { return $this->file; } @@ -384,7 +384,7 @@ class Preview { $name = rtrim($thumbnail['name'], '.png'); list($x, $y, $aspectRatio) = $this->getDimensionsFromFilename($name); - if (($aspectRatio - $wantedAspectRatio) >= 0.000001 + if (abs($aspectRatio - $wantedAspectRatio) >= 0.000001 || $this->unscalable($x, $y) ) { continue; From f029312e5b20a540aa94f2ead4581a8b653b1c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Fri, 4 Apr 2014 11:37:47 +0200 Subject: [PATCH 6/7] fixing typos/spellings --- lib/private/preview.php | 15 +++++++++++---- tests/lib/preview.php | 32 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 71c16687fb..0fecb25341 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -13,6 +13,8 @@ */ namespace OC; +use OC\Preview\Provider; + require_once 'preview/image.php'; require_once 'preview/movies.php'; require_once 'preview/mp3.php'; @@ -46,8 +48,9 @@ class Preview { // index is path, value is fileinfo static public $deleteFileMapper = array(); - //preview images object /** + * preview images object + * * @var \OC_Image */ private $preview; @@ -198,7 +201,7 @@ class Preview { } /** - * @brief set mimetype explicitely + * @brief set mimetype explicitly * @param string $mimetype */ public function setMimetype($mimetype) { @@ -361,6 +364,7 @@ class Preview { return false; } + /** * @brief get possible bigger thumbnails of the given image * @param int $fileId fileId of the original image @@ -396,6 +400,7 @@ class Preview { return $possibleThumbnails; } + private function getDimensionsFromFilename($name) { $size = explode('-', $name); $x = (int) $size[0]; @@ -403,6 +408,7 @@ class Preview { $aspectRatio = (float) ($x / $y); return array('x' => $x,'y' => $y,'aspectRatio' => $aspectRatio); } + private function unscalable($x, $y) { $maxX = $this->getMaxX(); @@ -422,6 +428,7 @@ class Preview { } return false; } + /** * @brief return a preview of a file * @return \OC_Image @@ -464,6 +471,7 @@ class Preview { \OC_Log::write('core', 'Generating preview for "' . $file . '" with "' . get_class($provider) . '"', \OC_Log::DEBUG); + /** @var $provider Provider */ $preview = $provider->getThumbnail($file, $maxX, $maxY, $scalingUp, $this->fileView); if (!($preview instanceof \OC_Image)) { @@ -507,7 +515,6 @@ class Preview { $this->getPreview(); } $this->preview->show('image/png'); - return; } /** @@ -516,7 +523,6 @@ class Preview { */ public function show() { $this->showPreview(); - return; } /** @@ -653,6 +659,7 @@ class Preview { $class = $provider['class']; $options = $provider['options']; + /** @var $object Provider */ $object = new $class($options); self::$providers[$object->getMimeType()] = $object; diff --git a/tests/lib/preview.php b/tests/lib/preview.php index 353b66fd6d..fb1c959b45 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -17,9 +17,9 @@ class Preview extends \PHPUnit_Framework_TestCase { $rootView->mkdir('/'.$user); $rootView->mkdir('/'.$user.'/files'); - $samplefile = '/'.$user.'/files/test.txt'; + $sampleFile = '/'.$user.'/files/test.txt'; - $rootView->file_put_contents($samplefile, 'dummy file data'); + $rootView->file_put_contents($sampleFile, 'dummy file data'); $x = 50; $y = 50; @@ -27,16 +27,16 @@ class Preview extends \PHPUnit_Framework_TestCase { $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y); $preview->getPreview(); - $fileinfo = $rootView->getFileInfo($samplefile); - $fileid = $fileinfo['fileid']; + $fileInfo = $rootView->getFileInfo($sampleFile); + $fileId = $fileInfo['fileid']; - $thumbcachefile = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $x . '-' . $y . '.png'; + $thumbCacheFile = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png'; - $this->assertEquals($rootView->file_exists($thumbcachefile), true); + $this->assertEquals($rootView->file_exists($thumbCacheFile), true); $preview->deletePreview(); - $this->assertEquals($rootView->file_exists($thumbcachefile), false); + $this->assertEquals($rootView->file_exists($thumbCacheFile), false); } public function testAreAllPreviewsDeleted() { @@ -46,9 +46,9 @@ class Preview extends \PHPUnit_Framework_TestCase { $rootView->mkdir('/'.$user); $rootView->mkdir('/'.$user.'/files'); - $samplefile = '/'.$user.'/files/test.txt'; + $sampleFile = '/'.$user.'/files/test.txt'; - $rootView->file_put_contents($samplefile, 'dummy file data'); + $rootView->file_put_contents($sampleFile, 'dummy file data'); $x = 50; $y = 50; @@ -56,16 +56,16 @@ class Preview extends \PHPUnit_Framework_TestCase { $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y); $preview->getPreview(); - $fileinfo = $rootView->getFileInfo($samplefile); - $fileid = $fileinfo['fileid']; + $fileInfo = $rootView->getFileInfo($sampleFile); + $fileId = $fileInfo['fileid']; - $thumbcachefolder = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/'; + $thumbCacheFolder = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/'; - $this->assertEquals($rootView->is_dir($thumbcachefolder), true); + $this->assertEquals($rootView->is_dir($thumbCacheFolder), true); $preview->deleteAllPreviews(); - $this->assertEquals($rootView->is_dir($thumbcachefolder), false); + $this->assertEquals($rootView->is_dir($thumbCacheFolder), false); } public function testIsMaxSizeWorking() { @@ -81,9 +81,9 @@ class Preview extends \PHPUnit_Framework_TestCase { $rootView->mkdir('/'.$user); $rootView->mkdir('/'.$user.'/files'); - $samplefile = '/'.$user.'/files/test.txt'; + $sampleFile = '/'.$user.'/files/test.txt'; - $rootView->file_put_contents($samplefile, 'dummy file data'); + $rootView->file_put_contents($sampleFile, 'dummy file data'); $preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000); $image = $preview->getPreview(); From ccf1287fbf8baa083c7c043b0ed0df054ecaf9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Fri, 4 Apr 2014 16:21:50 +0200 Subject: [PATCH 7/7] adding unit test for cache mechanism --- lib/private/preview.php | 98 ++++++++++++++--------------- tests/lib/preview.php | 136 ++++++++++++++++++++++++++++------------ 2 files changed, 145 insertions(+), 89 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 0fecb25341..995cfc3f6d 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -41,8 +41,8 @@ class Preview { private $file; private $maxX; private $maxY; - private $scalingup; - private $mimetype; + private $scalingUp; + private $mimeType; //filemapper used for deleting previews // index is path, value is fileinfo @@ -137,7 +137,7 @@ class Preview { * @return bool */ public function getScalingUp() { - return $this->scalingup; + return $this->scalingUp; } /** @@ -194,18 +194,18 @@ class Preview { if ($file !== '') { $this->getFileInfo(); if($this->info !== null && $this->info !== false) { - $this->mimetype = $this->info->getMimetype(); + $this->mimeType = $this->info->getMimetype(); } } return $this; } /** - * @brief set mimetype explicitly - * @param string $mimetype + * @brief set mime type explicitly + * @param string $mimeType */ - public function setMimetype($mimetype) { - $this->mimetype = $mimetype; + public function setMimetype($mimeType) { + $this->mimeType = $mimeType; } /** @@ -257,7 +257,7 @@ class Preview { if ($this->getMaxScaleFactor() === 1) { $scalingUp = false; } - $this->scalingup = $scalingUp; + $this->scalingUp = $scalingUp; return $this; } @@ -320,7 +320,7 @@ class Preview { * @param int $fileId fileId of the original image * @return string|false path to thumbnail if it exists or false */ - private function isCached($fileId) { + public function isCached($fileId) { if (is_null($fileId)) { return false; } @@ -406,7 +406,7 @@ class Preview { $x = (int) $size[0]; $y = (int) $size[1]; $aspectRatio = (float) ($x / $y); - return array('x' => $x,'y' => $y,'aspectRatio' => $aspectRatio); + return array($x, $y, $aspectRatio); } private function unscalable($x, $y) { @@ -464,8 +464,8 @@ class Preview { if (is_null($this->preview)) { $preview = null; - foreach (self::$providers as $supportedMimetype => $provider) { - if (!preg_match($supportedMimetype, $this->mimetype)) { + foreach (self::$providers as $supportedMimeType => $provider) { + if (!preg_match($supportedMimeType, $this->mimeType)) { continue; } @@ -534,7 +534,7 @@ class Preview { $x = $this->getMaxX(); $y = $this->getMaxY(); $scalingUp = $this->getScalingUp(); - $maxscalefactor = $this->getMaxScaleFactor(); + $maxScaleFactor = $this->getMaxScaleFactor(); if (!($image instanceof \OC_Image)) { \OC_Log::write('core', '$this->preview is not an instance of OC_Image', \OC_Log::DEBUG); @@ -543,16 +543,16 @@ class Preview { $image->fixOrientation(); - $realx = (int)$image->width(); - $realy = (int)$image->height(); + $realX = (int)$image->width(); + $realY = (int)$image->height(); - if ($x === $realx && $y === $realy) { + if ($x === $realX && $y === $realY) { $this->preview = $image; return; } - $factorX = $x / $realx; - $factorY = $y / $realy; + $factorX = $x / $realX; + $factorY = $y / $realY; if ($factorX >= $factorY) { $factor = $factorX; @@ -566,25 +566,25 @@ class Preview { } } - if (!is_null($maxscalefactor)) { - if ($factor > $maxscalefactor) { - \OC_Log::write('core', 'scalefactor reduced from ' . $factor . ' to ' . $maxscalefactor, \OC_Log::DEBUG); - $factor = $maxscalefactor; + if (!is_null($maxScaleFactor)) { + if ($factor > $maxScaleFactor) { + \OC_Log::write('core', 'scale factor reduced from ' . $factor . ' to ' . $maxScaleFactor, \OC_Log::DEBUG); + $factor = $maxScaleFactor; } } - $newXsize = (int)($realx * $factor); - $newYsize = (int)($realy * $factor); + $newXSize = (int)($realX * $factor); + $newYSize = (int)($realY * $factor); - $image->preciseResize($newXsize, $newYsize); + $image->preciseResize($newXSize, $newYSize); - if ($newXsize === $x && $newYsize === $y) { + if ($newXSize === $x && $newYSize === $y) { $this->preview = $image; return; } - if ($newXsize >= $x && $newYsize >= $y) { - $cropX = floor(abs($x - $newXsize) * 0.5); + if ($newXSize >= $x && $newYSize >= $y) { + $cropX = floor(abs($x - $newXSize) * 0.5); //don't crop previews on the Y axis, this sucks if it's a document. //$cropY = floor(abs($y - $newYsize) * 0.5); $cropY = 0; @@ -595,36 +595,36 @@ class Preview { return; } - if ($newXsize < $x || $newYsize < $y) { - if ($newXsize > $x) { - $cropX = floor(($newXsize - $x) * 0.5); - $image->crop($cropX, 0, $x, $newYsize); + if ($newXSize < $x || $newYSize < $y) { + if ($newXSize > $x) { + $cropX = floor(($newXSize - $x) * 0.5); + $image->crop($cropX, 0, $x, $newYSize); } - if ($newYsize > $y) { - $cropY = floor(($newYsize - $y) * 0.5); - $image->crop(0, $cropY, $newXsize, $y); + if ($newYSize > $y) { + $cropY = floor(($newYSize - $y) * 0.5); + $image->crop(0, $cropY, $newXSize, $y); } - $newXsize = (int)$image->width(); - $newYsize = (int)$image->height(); + $newXSize = (int)$image->width(); + $newYSize = (int)$image->height(); //create transparent background layer - $backgroundlayer = imagecreatetruecolor($x, $y); - $white = imagecolorallocate($backgroundlayer, 255, 255, 255); - imagefill($backgroundlayer, 0, 0, $white); + $backgroundLayer = imagecreatetruecolor($x, $y); + $white = imagecolorallocate($backgroundLayer, 255, 255, 255); + imagefill($backgroundLayer, 0, 0, $white); $image = $image->resource(); - $mergeX = floor(abs($x - $newXsize) * 0.5); - $mergeY = floor(abs($y - $newYsize) * 0.5); + $mergeX = floor(abs($x - $newXSize) * 0.5); + $mergeY = floor(abs($y - $newYSize) * 0.5); - imagecopy($backgroundlayer, $image, $mergeX, $mergeY, 0, 0, $newXsize, $newYsize); + imagecopy($backgroundLayer, $image, $mergeX, $mergeY, 0, 0, $newXSize, $newYSize); //$black = imagecolorallocate(0,0,0); //imagecolortransparent($transparentlayer, $black); - $image = new \OC_Image($backgroundlayer); + $image = new \OC_Image($backgroundLayer); $this->preview = $image; return; @@ -706,9 +706,9 @@ class Preview { } /** - * @param string $mimetype + * @param string $mimeType */ - public static function isMimeSupported($mimetype) { + public static function isMimeSupported($mimeType) { if (!\OC_Config::getValue('enable_previews', true)) { return false; } @@ -720,8 +720,8 @@ class Preview { //remove last element because it has the mimetype * $providers = array_slice(self::$providers, 0, -1); - foreach ($providers as $supportedMimetype => $provider) { - if (preg_match($supportedMimetype, $mimetype)) { + foreach ($providers as $supportedMimeType => $provider) { + if (preg_match($supportedMimeType, $mimeType)) { return true; } } diff --git a/tests/lib/preview.php b/tests/lib/preview.php index fb1c959b45..4ef61fb825 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -10,66 +10,73 @@ namespace Test; class Preview extends \PHPUnit_Framework_TestCase { + /** + * @var string + */ + private $user; + + /** + * @var \OC\Files\View + */ + private $rootView; + + public function setUp() { + $this->user = $this->initFS(); + + $this->rootView = new \OC\Files\View(''); + $this->rootView->mkdir('/'.$this->user); + $this->rootView->mkdir('/'.$this->user.'/files'); + } + public function testIsPreviewDeleted() { - $user = $this->initFS(); - $rootView = new \OC\Files\View(''); - $rootView->mkdir('/'.$user); - $rootView->mkdir('/'.$user.'/files'); + $sampleFile = '/'.$this->user.'/files/test.txt'; - $sampleFile = '/'.$user.'/files/test.txt'; - - $rootView->file_put_contents($sampleFile, 'dummy file data'); + $this->rootView->file_put_contents($sampleFile, 'dummy file data'); $x = 50; $y = 50; - $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y); + $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y); $preview->getPreview(); - $fileInfo = $rootView->getFileInfo($sampleFile); + $fileInfo = $this->rootView->getFileInfo($sampleFile); $fileId = $fileInfo['fileid']; - $thumbCacheFile = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png'; + $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png'; - $this->assertEquals($rootView->file_exists($thumbCacheFile), true); + $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true); $preview->deletePreview(); - $this->assertEquals($rootView->file_exists($thumbCacheFile), false); + $this->assertEquals($this->rootView->file_exists($thumbCacheFile), false); } public function testAreAllPreviewsDeleted() { - $user = $this->initFS(); - $rootView = new \OC\Files\View(''); - $rootView->mkdir('/'.$user); - $rootView->mkdir('/'.$user.'/files'); + $sampleFile = '/'.$this->user.'/files/test.txt'; - $sampleFile = '/'.$user.'/files/test.txt'; - - $rootView->file_put_contents($sampleFile, 'dummy file data'); + $this->rootView->file_put_contents($sampleFile, 'dummy file data'); $x = 50; $y = 50; - $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y); + $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y); $preview->getPreview(); - $fileInfo = $rootView->getFileInfo($sampleFile); + $fileInfo = $this->rootView->getFileInfo($sampleFile); $fileId = $fileInfo['fileid']; - $thumbCacheFolder = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/'; + $thumbCacheFolder = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/'; - $this->assertEquals($rootView->is_dir($thumbCacheFolder), true); + $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), true); $preview->deleteAllPreviews(); - $this->assertEquals($rootView->is_dir($thumbCacheFolder), false); + $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), false); } public function testIsMaxSizeWorking() { - $user = $this->initFS(); $maxX = 250; $maxY = 250; @@ -77,15 +84,11 @@ class Preview extends \PHPUnit_Framework_TestCase { \OC_Config::setValue('preview_max_x', $maxX); \OC_Config::setValue('preview_max_y', $maxY); - $rootView = new \OC\Files\View(''); - $rootView->mkdir('/'.$user); - $rootView->mkdir('/'.$user.'/files'); + $sampleFile = '/'.$this->user.'/files/test.txt'; - $sampleFile = '/'.$user.'/files/test.txt'; + $this->rootView->file_put_contents($sampleFile, 'dummy file data'); - $rootView->file_put_contents($sampleFile, 'dummy file data'); - - $preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000); + $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 1000, 1000); $image = $preview->getPreview(); $this->assertEquals($image->width(), $maxX); @@ -108,18 +111,13 @@ class Preview extends \PHPUnit_Framework_TestCase { * @dataProvider txtBlacklist */ public function testIsTransparent($extension, $data, $expectedResult) { - $user = $this->initFS(); - - $rootView = new \OC\Files\View(''); - $rootView->mkdir('/'.$user); - $rootView->mkdir('/'.$user.'/files'); $x = 32; $y = 32; - $sample = '/'.$user.'/files/test.'.$extension; - $rootView->file_put_contents($sample, $data); - $preview = new \OC\Preview($user, 'files/', 'test.'.$extension, $x, $y); + $sample = '/'.$this->user.'/files/test.'.$extension; + $this->rootView->file_put_contents($sample, $data); + $preview = new \OC\Preview($this->user, 'files/', 'test.'.$extension, $x, $y); $image = $preview->getPreview(); $resource = $image->resource(); @@ -133,6 +131,64 @@ class Preview extends \PHPUnit_Framework_TestCase { ); } + public function testCreationFromCached() { + + $sampleFile = '/'.$this->user.'/files/test.txt'; + + $this->rootView->file_put_contents($sampleFile, 'dummy file data'); + + // create base preview + $x = 150; + $y = 150; + + $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y); + $preview->getPreview(); + + $fileInfo = $this->rootView->getFileInfo($sampleFile); + $fileId = $fileInfo['fileid']; + + $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png'; + + $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true); + + + // create smaller previews + $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 50, 50); + $isCached = $preview->isCached($fileId); + + $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached); + } + + /* + public function testScalingUp() { + + $sampleFile = '/'.$this->user.'/files/test.txt'; + + $this->rootView->file_put_contents($sampleFile, 'dummy file data'); + + // create base preview + $x = 150; + $y = 150; + + $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y); + $preview->getPreview(); + + $fileInfo = $this->rootView->getFileInfo($sampleFile); + $fileId = $fileInfo['fileid']; + + $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png'; + + $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true); + + + // create bigger previews - with scale up + $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 250, 250); + $isCached = $preview->isCached($fileId); + + $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached); + } + */ + private function initFS() { // create a new user with his own filesystem view // this gets called by each test in this test class