extract more methods

This commit is contained in:
Jörn Friedrich Dreyer 2014-04-02 18:32:32 +02:00
parent 436a78db44
commit b9a8bd7e1f
1 changed files with 72 additions and 55 deletions

View File

@ -314,20 +314,17 @@ class Preview {
/** /**
* @brief check if thumbnail or bigger version of thumbnail of file is cached * @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 * @return string|false path to thumbnail if it exists or false
*/ */
private function isCached() { private function isCached($fileId) {
$file = $this->getFile();
$maxX = $this->getMaxX();
$maxY = $this->getMaxY();
$fileInfo = $this->getFileInfo($file);
$fileId = $fileInfo->getId();
if (is_null($fileId)) { if (is_null($fileId)) {
return false; return false;
} }
$maxX = $this->getMaxX();
$maxY = $this->getMaxY();
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/';
//does a preview with the wanted height and width already exist? //does a preview with the wanted height and width already exist?
@ -335,65 +332,24 @@ class Preview {
return $previewPath . $maxX . '-' . $maxY . '.png'; return $previewPath . $maxX . '-' . $maxY . '.png';
} }
return $this->isCachedBigger(); return $this->isCachedBigger($fileId);
} }
/** /**
* @brief check if a bigger version of thumbnail of file is cached * @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 * @return string|false path to bigger thumbnail if it exists or false
*/ */
private function isCachedBigger() { private function isCachedBigger($fileId) {
$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)) { if (is_null($fileId)) {
return false; return false;
} }
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; $maxX = $this->getMaxX();
if (!$this->userView->is_dir($previewPath)) {
return false;
}
$wantedAspectRatio = (float) ($maxX / $maxY);
//array for usable cached thumbnails //array for usable cached thumbnails
$possibleThumbnails = array(); $possibleThumbnails = $this->getPossibleThumbnails($fileId);
$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);
foreach ($possibleThumbnails as $width => $path) { foreach ($possibleThumbnails as $width => $path) {
if ($width < $maxX) { if ($width < $maxX) {
@ -405,6 +361,67 @@ class Preview {
return false; 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 * @brief return a preview of a file
* @return \OC_Image * @return \OC_Image
@ -426,7 +443,7 @@ class Preview {
} }
$fileId = $fileInfo->getId(); $fileId = $fileInfo->getId();
$cached = $this->isCached(); $cached = $this->isCached($fileId);
if ($cached) { if ($cached) {
$stream = $this->userView->fopen($cached, 'r'); $stream = $this->userView->fopen($cached, 'r');