Merge pull request #7453 from owncloud/preview-fileinfo
Cache the fileinfo in OC\Preview
This commit is contained in:
commit
ad195a881e
|
@ -52,6 +52,11 @@ class Preview {
|
|||
static private $providers = array();
|
||||
static private $registeredProviders = array();
|
||||
|
||||
/**
|
||||
* @var \OCP\Files\FileInfo
|
||||
*/
|
||||
protected $info;
|
||||
|
||||
/**
|
||||
* @brief check if thumbnail or bigger version of thumbnail of file is cached
|
||||
* @param string $user userid - if no user is given, OC_User::getUser will be used
|
||||
|
@ -64,9 +69,9 @@ class Preview {
|
|||
* false if thumbnail does not exist
|
||||
* path to thumbnail if thumbnail exists
|
||||
*/
|
||||
public function __construct($user='', $root='/', $file='', $maxX=1, $maxY=1, $scalingUp=true) {
|
||||
public function __construct($user = '', $root = '/', $file = '', $maxX = 1, $maxY = 1, $scalingUp = true) {
|
||||
//init fileviews
|
||||
if($user === ''){
|
||||
if ($user === '') {
|
||||
$user = \OC_User::getUser();
|
||||
}
|
||||
$this->fileView = new \OC\Files\View('/' . $user . '/' . $root);
|
||||
|
@ -86,11 +91,11 @@ class Preview {
|
|||
$this->preview = null;
|
||||
|
||||
//check if there are preview backends
|
||||
if(empty(self::$providers)) {
|
||||
if (empty(self::$providers)) {
|
||||
self::initProviders();
|
||||
}
|
||||
|
||||
if(empty(self::$providers)) {
|
||||
if (empty(self::$providers)) {
|
||||
\OC_Log::write('core', 'No preview providers exist', \OC_Log::ERROR);
|
||||
throw new \Exception('No preview providers');
|
||||
}
|
||||
|
@ -160,6 +165,13 @@ class Preview {
|
|||
return $this->configMaxY;
|
||||
}
|
||||
|
||||
protected function getFileInfo() {
|
||||
if (!$this->info) {
|
||||
$this->info = $this->fileView->getFileInfo($this->file);
|
||||
}
|
||||
return $this->info;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief set the path of the file you want a thumbnail from
|
||||
* @param string $file
|
||||
|
@ -167,8 +179,9 @@ class Preview {
|
|||
*/
|
||||
public function setFile($file) {
|
||||
$this->file = $file;
|
||||
$this->info = null;
|
||||
if ($file !== '') {
|
||||
$this->mimetype = $this->fileView->getMimeType($this->file);
|
||||
$this->mimetype = $this->getFileInfo()->getMimetype();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
@ -186,13 +199,13 @@ class Preview {
|
|||
* @param int $maxX
|
||||
* @return $this
|
||||
*/
|
||||
public function setMaxX($maxX=1) {
|
||||
if($maxX <= 0) {
|
||||
public function setMaxX($maxX = 1) {
|
||||
if ($maxX <= 0) {
|
||||
throw new \Exception('Cannot set width of 0 or smaller!');
|
||||
}
|
||||
$configMaxX = $this->getConfigMaxX();
|
||||
if(!is_null($configMaxX)) {
|
||||
if($maxX > $configMaxX) {
|
||||
if (!is_null($configMaxX)) {
|
||||
if ($maxX > $configMaxX) {
|
||||
\OC_Log::write('core', 'maxX reduced from ' . $maxX . ' to ' . $configMaxX, \OC_Log::DEBUG);
|
||||
$maxX = $configMaxX;
|
||||
}
|
||||
|
@ -206,13 +219,13 @@ class Preview {
|
|||
* @param int $maxY
|
||||
* @return $this
|
||||
*/
|
||||
public function setMaxY($maxY=1) {
|
||||
if($maxY <= 0) {
|
||||
public function setMaxY($maxY = 1) {
|
||||
if ($maxY <= 0) {
|
||||
throw new \Exception('Cannot set height of 0 or smaller!');
|
||||
}
|
||||
$configMaxY = $this->getConfigMaxY();
|
||||
if(!is_null($configMaxY)) {
|
||||
if($maxY > $configMaxY) {
|
||||
if (!is_null($configMaxY)) {
|
||||
if ($maxY > $configMaxY) {
|
||||
\OC_Log::write('core', 'maxX reduced from ' . $maxY . ' to ' . $configMaxY, \OC_Log::DEBUG);
|
||||
$maxY = $configMaxY;
|
||||
}
|
||||
|
@ -227,7 +240,7 @@ class Preview {
|
|||
* @return $this
|
||||
*/
|
||||
public function setScalingup($scalingUp) {
|
||||
if($this->getMaxScaleFactor() === 1) {
|
||||
if ($this->getMaxScaleFactor() === 1) {
|
||||
$scalingUp = false;
|
||||
}
|
||||
$this->scalingup = $scalingUp;
|
||||
|
@ -240,12 +253,12 @@ class Preview {
|
|||
*/
|
||||
public function isFileValid() {
|
||||
$file = $this->getFile();
|
||||
if($file === '') {
|
||||
if ($file === '') {
|
||||
\OC_Log::write('core', 'No filename passed', \OC_Log::DEBUG);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$this->fileView->file_exists($file)) {
|
||||
if (!$this->fileView->file_exists($file)) {
|
||||
\OC_Log::write('core', 'File:"' . $file . '" not found', \OC_Log::DEBUG);
|
||||
return false;
|
||||
}
|
||||
|
@ -260,12 +273,11 @@ class Preview {
|
|||
public function deletePreview() {
|
||||
$file = $this->getFile();
|
||||
|
||||
$fileInfo = $this->fileView->getFileInfo($file);
|
||||
$fileId = $fileInfo['fileid'];
|
||||
$fileInfo = $this->getFileInfo($file);
|
||||
$fileId = $fileInfo->getId();
|
||||
|
||||
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/' . $this->getMaxX() . '-' . $this->getMaxY() . '.png';
|
||||
$this->userView->unlink($previewPath);
|
||||
return !$this->userView->file_exists($previewPath);
|
||||
return $this->userView->unlink($previewPath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -275,13 +287,12 @@ class Preview {
|
|||
public function deleteAllPreviews() {
|
||||
$file = $this->getFile();
|
||||
|
||||
$fileInfo = $this->fileView->getFileInfo($file);
|
||||
$fileId = $fileInfo['fileid'];
|
||||
$fileInfo = $this->getFileInfo($file);
|
||||
$fileId = $fileInfo->getId();
|
||||
|
||||
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/';
|
||||
$this->userView->deleteAll($previewPath);
|
||||
$this->userView->rmdir($previewPath);
|
||||
return !$this->userView->is_dir($previewPath);
|
||||
return $this->userView->rmdir($previewPath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -297,75 +308,75 @@ class Preview {
|
|||
$scalingUp = $this->getScalingUp();
|
||||
$maxScaleFactor = $this->getMaxScaleFactor();
|
||||
|
||||
$fileInfo = $this->fileView->getFileInfo($file);
|
||||
$fileId = $fileInfo['fileid'];
|
||||
$fileInfo = $this->getFileInfo($file);
|
||||
$fileId = $fileInfo->getId();
|
||||
|
||||
if(is_null($fileId)) {
|
||||
if (is_null($fileId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/';
|
||||
if(!$this->userView->is_dir($previewPath)) {
|
||||
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')) {
|
||||
if ($this->userView->file_exists($previewPath . $maxX . '-' . $maxY . '.png')) {
|
||||
return $previewPath . $maxX . '-' . $maxY . '.png';
|
||||
}
|
||||
|
||||
$wantedAspectRatio = (float) ($maxX / $maxY);
|
||||
$wantedAspectRatio = (float)($maxX / $maxY);
|
||||
|
||||
//array for usable cached thumbnails
|
||||
$possibleThumbnails = array();
|
||||
|
||||
$allThumbnails = $this->userView->getDirectoryContent($previewPath);
|
||||
foreach($allThumbnails as $thumbnail) {
|
||||
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);
|
||||
if($aspectRatio !== $wantedAspectRatio) {
|
||||
$aspectRatio = (float)($x / $y);
|
||||
if ($aspectRatio !== $wantedAspectRatio) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if($x < $maxX || $y < $maxY) {
|
||||
if($scalingUp) {
|
||||
if ($x < $maxX || $y < $maxY) {
|
||||
if ($scalingUp) {
|
||||
$scalefactor = $maxX / $x;
|
||||
if($scalefactor > $maxScaleFactor) {
|
||||
if ($scalefactor > $maxScaleFactor) {
|
||||
continue;
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$possibleThumbnails[$x] = $thumbnail['path'];
|
||||
}
|
||||
|
||||
if(count($possibleThumbnails) === 0) {
|
||||
if (count($possibleThumbnails) === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(count($possibleThumbnails) === 1) {
|
||||
if (count($possibleThumbnails) === 1) {
|
||||
return current($possibleThumbnails);
|
||||
}
|
||||
|
||||
ksort($possibleThumbnails);
|
||||
|
||||
if(key(reset($possibleThumbnails)) > $maxX) {
|
||||
if (key(reset($possibleThumbnails)) > $maxX) {
|
||||
return current(reset($possibleThumbnails));
|
||||
}
|
||||
|
||||
if(key(end($possibleThumbnails)) < $maxX) {
|
||||
if (key(end($possibleThumbnails)) < $maxX) {
|
||||
return current(end($possibleThumbnails));
|
||||
}
|
||||
|
||||
foreach($possibleThumbnails as $width => $path) {
|
||||
if($width < $maxX) {
|
||||
foreach ($possibleThumbnails as $width => $path) {
|
||||
if ($width < $maxX) {
|
||||
continue;
|
||||
}else{
|
||||
} else {
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
@ -376,7 +387,7 @@ class Preview {
|
|||
* @return \OC_Image
|
||||
*/
|
||||
public function getPreview() {
|
||||
if(!is_null($this->preview) && $this->preview->valid()){
|
||||
if (!is_null($this->preview) && $this->preview->valid()) {
|
||||
return $this->preview;
|
||||
}
|
||||
|
||||
|
@ -386,22 +397,22 @@ class Preview {
|
|||
$maxY = $this->getMaxY();
|
||||
$scalingUp = $this->getScalingUp();
|
||||
|
||||
$fileInfo = $this->fileView->getFileInfo($file);
|
||||
$fileId = $fileInfo['fileid'];
|
||||
$fileInfo = $this->getFileInfo($file);
|
||||
$fileId = $fileInfo->getId();
|
||||
|
||||
$cached = $this->isCached();
|
||||
|
||||
if($cached) {
|
||||
if ($cached) {
|
||||
$image = new \OC_Image($this->userView->file_get_contents($cached, 'r'));
|
||||
$this->preview = $image->valid() ? $image : null;
|
||||
$this->resizeAndCrop();
|
||||
}
|
||||
|
||||
if(is_null($this->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;
|
||||
}
|
||||
|
||||
|
@ -409,7 +420,7 @@ class Preview {
|
|||
|
||||
$preview = $provider->getThumbnail($file, $maxX, $maxY, $scalingUp, $this->fileView);
|
||||
|
||||
if(!($preview instanceof \OC_Image)) {
|
||||
if (!($preview instanceof \OC_Image)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -419,11 +430,11 @@ class Preview {
|
|||
$previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/';
|
||||
$cachePath = $previewPath . $maxX . '-' . $maxY . '.png';
|
||||
|
||||
if($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) {
|
||||
if ($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) {
|
||||
$this->userView->mkdir($this->getThumbnailsFolder() . '/');
|
||||
}
|
||||
|
||||
if($this->userView->is_dir($previewPath) === false) {
|
||||
if ($this->userView->is_dir($previewPath) === false) {
|
||||
$this->userView->mkdir($previewPath);
|
||||
}
|
||||
|
||||
|
@ -433,7 +444,7 @@ class Preview {
|
|||
}
|
||||
}
|
||||
|
||||
if(is_null($this->preview)) {
|
||||
if (is_null($this->preview)) {
|
||||
$this->preview = new \OC_Image();
|
||||
}
|
||||
|
||||
|
@ -446,7 +457,7 @@ class Preview {
|
|||
*/
|
||||
public function showPreview() {
|
||||
\OCP\Response::enableCaching(3600 * 24); // 24 hours
|
||||
if(is_null($this->preview)) {
|
||||
if (is_null($this->preview)) {
|
||||
$this->getPreview();
|
||||
}
|
||||
$this->preview->show();
|
||||
|
@ -473,17 +484,17 @@ class Preview {
|
|||
$scalingUp = $this->getScalingUp();
|
||||
$maxscalefactor = $this->getMaxScaleFactor();
|
||||
|
||||
if(!($image instanceof \OC_Image)) {
|
||||
if (!($image instanceof \OC_Image)) {
|
||||
\OC_Log::write('core', '$this->preview is not an instance of OC_Image', \OC_Log::DEBUG);
|
||||
return;
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
|
@ -491,36 +502,36 @@ class Preview {
|
|||
$factorX = $x / $realx;
|
||||
$factorY = $y / $realy;
|
||||
|
||||
if($factorX >= $factorY) {
|
||||
if ($factorX >= $factorY) {
|
||||
$factor = $factorX;
|
||||
}else{
|
||||
} else {
|
||||
$factor = $factorY;
|
||||
}
|
||||
|
||||
if($scalingUp === false) {
|
||||
if($factor > 1) {
|
||||
if ($scalingUp === false) {
|
||||
if ($factor > 1) {
|
||||
$factor = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(!is_null($maxscalefactor)) {
|
||||
if($factor > $maxscalefactor) {
|
||||
if (!is_null($maxscalefactor)) {
|
||||
if ($factor > $maxscalefactor) {
|
||||
\OC_Log::write('core', 'scalefactor 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);
|
||||
|
||||
if($newXsize === $x && $newYsize === $y) {
|
||||
if ($newXsize === $x && $newYsize === $y) {
|
||||
$this->preview = $image;
|
||||
return;
|
||||
}
|
||||
|
||||
if($newXsize >= $x && $newYsize >= $y) {
|
||||
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);
|
||||
|
@ -532,19 +543,19 @@ class Preview {
|
|||
return;
|
||||
}
|
||||
|
||||
if($newXsize < $x || $newYsize < $y) {
|
||||
if($newXsize > $x) {
|
||||
if ($newXsize < $x || $newYsize < $y) {
|
||||
if ($newXsize > $x) {
|
||||
$cropX = floor(($newXsize - $x) * 0.5);
|
||||
$image->crop($cropX, 0, $x, $newYsize);
|
||||
}
|
||||
|
||||
if($newYsize > $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);
|
||||
|
@ -573,8 +584,8 @@ class Preview {
|
|||
* @param array $options
|
||||
* @return void
|
||||
*/
|
||||
public static function registerProvider($class, $options=array()) {
|
||||
self::$registeredProviders[]=array('class'=>$class, 'options'=>$options);
|
||||
public static function registerProvider($class, $options = array()) {
|
||||
self::$registeredProviders[] = array('class' => $class, 'options' => $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -582,19 +593,19 @@ class Preview {
|
|||
* @return void
|
||||
*/
|
||||
private static function initProviders() {
|
||||
if(!\OC_Config::getValue('enable_previews', true)) {
|
||||
if (!\OC_Config::getValue('enable_previews', true)) {
|
||||
$provider = new Preview\Unknown(array());
|
||||
self::$providers = array($provider->getMimeType() => $provider);
|
||||
return;
|
||||
}
|
||||
|
||||
if(count(self::$providers)>0) {
|
||||
if (count(self::$providers) > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(self::$registeredProviders as $provider) {
|
||||
$class=$provider['class'];
|
||||
$options=$provider['options'];
|
||||
foreach (self::$registeredProviders as $provider) {
|
||||
$class = $provider['class'];
|
||||
$options = $provider['options'];
|
||||
|
||||
$object = new $class($options);
|
||||
|
||||
|
@ -611,7 +622,7 @@ class Preview {
|
|||
|
||||
public static function post_delete($args) {
|
||||
$path = $args['path'];
|
||||
if(substr($path, 0, 1) === '/') {
|
||||
if (substr($path, 0, 1) === '/') {
|
||||
$path = substr($path, 1);
|
||||
}
|
||||
$preview = new Preview(\OC_User::getUser(), 'files/', $path);
|
||||
|
@ -622,19 +633,19 @@ class Preview {
|
|||
* @param string $mimetype
|
||||
*/
|
||||
public static function isMimeSupported($mimetype) {
|
||||
if(!\OC_Config::getValue('enable_previews', true)) {
|
||||
if (!\OC_Config::getValue('enable_previews', true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//check if there are preview backends
|
||||
if(empty(self::$providers)) {
|
||||
if (empty(self::$providers)) {
|
||||
self::initProviders();
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue