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] 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');