diff options
author | Jörn Friedrich Dreyer <jfd@butonic.de> | 2014-04-02 18:32:32 +0200 |
---|---|---|
committer | Jörn Friedrich Dreyer <jfd@butonic.de> | 2014-04-02 18:32:32 +0200 |
commit | b9a8bd7e1f6f99fe5d26e1693d13ebc6ddf81777 (patch) | |
tree | a431f89676fc600dc4cc6b452e53fb63e2c52820 /lib/private/preview.php | |
parent | 436a78db448d4263f90de0490202b8d7a936d3a2 (diff) | |
download | nextcloud-server-b9a8bd7e1f6f99fe5d26e1693d13ebc6ddf81777.tar.gz nextcloud-server-b9a8bd7e1f6f99fe5d26e1693d13ebc6ddf81777.zip |
extract more methods
Diffstat (limited to 'lib/private/preview.php')
-rwxr-xr-x | lib/private/preview.php | 109 |
1 files changed, 63 insertions, 46 deletions
diff --git a/lib/private/preview.php b/lib/private/preview.php index 7be392a50d9..36cc5d9a2e0 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,34 +332,49 @@ 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(); + private function isCachedBigger($fileId) { + + if (is_null($fileId)) { + return false; + } + $maxX = $this->getMaxX(); - $maxY = $this->getMaxY(); - $scalingUp = $this->getScalingUp(); - $maxScaleFactor = $this->getMaxScaleFactor(); - $fileInfo = $this->fileView->getFileInfo($file); - $fileId = $fileInfo['fileid']; + //array for usable cached thumbnails + $possibleThumbnails = $this->getPossibleThumbnails($fileId); + + foreach ($possibleThumbnails as $width => $path) { + if ($width < $maxX) { + continue; + } else { + return $path; + } + } + + 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 false; + return array(); } $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; - if (!$this->userView->is_dir($previewPath)) { - return false; - } - - $wantedAspectRatio = (float) ($maxX / $maxY); + + $wantedAspectRatio = (float) ($this->getMaxX() / $this->getMaxY()); //array for usable cached thumbnails $possibleThumbnails = array(); @@ -370,39 +382,44 @@ class Preview { $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]; + list($x, $y, $aspectRatio) = $this->getDimensionsFromFilename($name); - $aspectRatio = (float) ($x / $y); - $epsilon = 0.000001; - if (($aspectRatio - $wantedAspectRatio) >= $epsilon) { + if (($aspectRatio - $wantedAspectRatio) >= 0.000001 + || $this->unscalable($x, $y) + ) { continue; } - - if ($x < $maxX || $y < $maxY) { - if ($scalingUp) { - $scalefactor = $maxX / $x; - if ($scalefactor > $maxScaleFactor) { - continue; - } - } else { - continue; - } - } $possibleThumbnails[$x] = $thumbnail['path']; } ksort($possibleThumbnails); - foreach ($possibleThumbnails as $width => $path) { - if ($width < $maxX) { - continue; + 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 $path; + return true; } } - return false; } /** @@ -426,7 +443,7 @@ class Preview { } $fileId = $fileInfo->getId(); - $cached = $this->isCached(); + $cached = $this->isCached($fileId); if ($cached) { $stream = $this->userView->fopen($cached, 'r'); |