commit
8df19c4379
|
@ -13,6 +13,8 @@
|
||||||
*/
|
*/
|
||||||
namespace OC;
|
namespace OC;
|
||||||
|
|
||||||
|
use OC\Preview\Provider;
|
||||||
|
|
||||||
require_once 'preview/image.php';
|
require_once 'preview/image.php';
|
||||||
require_once 'preview/movies.php';
|
require_once 'preview/movies.php';
|
||||||
require_once 'preview/mp3.php';
|
require_once 'preview/mp3.php';
|
||||||
|
@ -39,15 +41,16 @@ class Preview {
|
||||||
private $file;
|
private $file;
|
||||||
private $maxX;
|
private $maxX;
|
||||||
private $maxY;
|
private $maxY;
|
||||||
private $scalingup;
|
private $scalingUp;
|
||||||
private $mimetype;
|
private $mimeType;
|
||||||
|
|
||||||
//filemapper used for deleting previews
|
//filemapper used for deleting previews
|
||||||
// index is path, value is fileinfo
|
// index is path, value is fileinfo
|
||||||
static public $deleteFileMapper = array();
|
static public $deleteFileMapper = array();
|
||||||
|
|
||||||
//preview images object
|
|
||||||
/**
|
/**
|
||||||
|
* preview images object
|
||||||
|
*
|
||||||
* @var \OC_Image
|
* @var \OC_Image
|
||||||
*/
|
*/
|
||||||
private $preview;
|
private $preview;
|
||||||
|
@ -134,7 +137,7 @@ class Preview {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function getScalingUp() {
|
public function getScalingUp() {
|
||||||
return $this->scalingup;
|
return $this->scalingUp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -191,18 +194,18 @@ class Preview {
|
||||||
if ($file !== '') {
|
if ($file !== '') {
|
||||||
$this->getFileInfo();
|
$this->getFileInfo();
|
||||||
if($this->info !== null && $this->info !== false) {
|
if($this->info !== null && $this->info !== false) {
|
||||||
$this->mimetype = $this->info->getMimetype();
|
$this->mimeType = $this->info->getMimetype();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief set mimetype explicitely
|
* @brief set mime type explicitly
|
||||||
* @param string $mimetype
|
* @param string $mimeType
|
||||||
*/
|
*/
|
||||||
public function setMimetype($mimetype) {
|
public function setMimetype($mimeType) {
|
||||||
$this->mimetype = $mimetype;
|
$this->mimeType = $mimeType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -254,7 +257,7 @@ class Preview {
|
||||||
if ($this->getMaxScaleFactor() === 1) {
|
if ($this->getMaxScaleFactor() === 1) {
|
||||||
$scalingUp = false;
|
$scalingUp = false;
|
||||||
}
|
}
|
||||||
$this->scalingup = $scalingUp;
|
$this->scalingUp = $scalingUp;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -314,81 +317,42 @@ 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
|
||||||
* @return mixed (bool / string)
|
* @param int $fileId fileId of the original image
|
||||||
* false if thumbnail does not exist
|
* @return string|false path to thumbnail if it exists or false
|
||||||
* path to thumbnail if thumbnail exists
|
|
||||||
*/
|
*/
|
||||||
private function isCached() {
|
public function isCached($fileId) {
|
||||||
$file = $this->getFile();
|
|
||||||
$maxX = $this->getMaxX();
|
|
||||||
$maxY = $this->getMaxY();
|
|
||||||
$scalingUp = $this->getScalingUp();
|
|
||||||
$maxScaleFactor = $this->getMaxScaleFactor();
|
|
||||||
|
|
||||||
$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 . '/';
|
||||||
if (!$this->userView->is_dir($previewPath)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//does a preview with the wanted height and width already exist?
|
//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';
|
return $previewPath . $maxX . '-' . $maxY . '.png';
|
||||||
}
|
}
|
||||||
|
|
||||||
$wantedAspectRatio = (float)($maxX / $maxY);
|
return $this->isCachedBigger($fileId);
|
||||||
|
|
||||||
//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);
|
|
||||||
if ($aspectRatio !== $wantedAspectRatio) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($x < $maxX || $y < $maxY) {
|
/**
|
||||||
if ($scalingUp) {
|
* @brief check if a bigger version of thumbnail of file is cached
|
||||||
$scalefactor = $maxX / $x;
|
* @param int $fileId fileId of the original image
|
||||||
if ($scalefactor > $maxScaleFactor) {
|
* @return string|false path to bigger thumbnail if it exists or false
|
||||||
continue;
|
*/
|
||||||
}
|
private function isCachedBigger($fileId) {
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$possibleThumbnails[$x] = $thumbnail['path'];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($possibleThumbnails) === 0) {
|
if (is_null($fileId)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($possibleThumbnails) === 1) {
|
$maxX = $this->getMaxX();
|
||||||
return current($possibleThumbnails);
|
|
||||||
}
|
|
||||||
|
|
||||||
ksort($possibleThumbnails);
|
//array for usable cached thumbnails
|
||||||
|
$possibleThumbnails = $this->getPossibleThumbnails($fileId);
|
||||||
if (key(reset($possibleThumbnails)) > $maxX) {
|
|
||||||
return current(reset($possibleThumbnails));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key(end($possibleThumbnails)) < $maxX) {
|
|
||||||
return current(end($possibleThumbnails));
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($possibleThumbnails as $width => $path) {
|
foreach ($possibleThumbnails as $width => $path) {
|
||||||
if ($width < $maxX) {
|
if ($width < $maxX) {
|
||||||
|
@ -397,6 +361,72 @@ class Preview {
|
||||||
return $path;
|
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 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 (abs($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, $y, $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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -420,7 +450,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');
|
||||||
|
@ -434,13 +464,14 @@ class Preview {
|
||||||
if (is_null($this->preview)) {
|
if (is_null($this->preview)) {
|
||||||
$preview = null;
|
$preview = null;
|
||||||
|
|
||||||
foreach (self::$providers as $supportedMimetype => $provider) {
|
foreach (self::$providers as $supportedMimeType => $provider) {
|
||||||
if (!preg_match($supportedMimetype, $this->mimetype)) {
|
if (!preg_match($supportedMimeType, $this->mimeType)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
\OC_Log::write('core', 'Generating preview for "' . $file . '" with "' . get_class($provider) . '"', \OC_Log::DEBUG);
|
\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);
|
$preview = $provider->getThumbnail($file, $maxX, $maxY, $scalingUp, $this->fileView);
|
||||||
|
|
||||||
if (!($preview instanceof \OC_Image)) {
|
if (!($preview instanceof \OC_Image)) {
|
||||||
|
@ -484,7 +515,6 @@ class Preview {
|
||||||
$this->getPreview();
|
$this->getPreview();
|
||||||
}
|
}
|
||||||
$this->preview->show('image/png');
|
$this->preview->show('image/png');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -493,7 +523,6 @@ class Preview {
|
||||||
*/
|
*/
|
||||||
public function show() {
|
public function show() {
|
||||||
$this->showPreview();
|
$this->showPreview();
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -505,7 +534,7 @@ class Preview {
|
||||||
$x = $this->getMaxX();
|
$x = $this->getMaxX();
|
||||||
$y = $this->getMaxY();
|
$y = $this->getMaxY();
|
||||||
$scalingUp = $this->getScalingUp();
|
$scalingUp = $this->getScalingUp();
|
||||||
$maxscalefactor = $this->getMaxScaleFactor();
|
$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);
|
\OC_Log::write('core', '$this->preview is not an instance of OC_Image', \OC_Log::DEBUG);
|
||||||
|
@ -514,16 +543,16 @@ class Preview {
|
||||||
|
|
||||||
$image->fixOrientation();
|
$image->fixOrientation();
|
||||||
|
|
||||||
$realx = (int)$image->width();
|
$realX = (int)$image->width();
|
||||||
$realy = (int)$image->height();
|
$realY = (int)$image->height();
|
||||||
|
|
||||||
if ($x === $realx && $y === $realy) {
|
if ($x === $realX && $y === $realY) {
|
||||||
$this->preview = $image;
|
$this->preview = $image;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$factorX = $x / $realx;
|
$factorX = $x / $realX;
|
||||||
$factorY = $y / $realy;
|
$factorY = $y / $realY;
|
||||||
|
|
||||||
if ($factorX >= $factorY) {
|
if ($factorX >= $factorY) {
|
||||||
$factor = $factorX;
|
$factor = $factorX;
|
||||||
|
@ -537,25 +566,25 @@ class Preview {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_null($maxscalefactor)) {
|
if (!is_null($maxScaleFactor)) {
|
||||||
if ($factor > $maxscalefactor) {
|
if ($factor > $maxScaleFactor) {
|
||||||
\OC_Log::write('core', 'scalefactor reduced from ' . $factor . ' to ' . $maxscalefactor, \OC_Log::DEBUG);
|
\OC_Log::write('core', 'scale factor reduced from ' . $factor . ' to ' . $maxScaleFactor, \OC_Log::DEBUG);
|
||||||
$factor = $maxscalefactor;
|
$factor = $maxScaleFactor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$newXsize = (int)($realx * $factor);
|
$newXSize = (int)($realX * $factor);
|
||||||
$newYsize = (int)($realy * $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;
|
$this->preview = $image;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($newXsize >= $x && $newYsize >= $y) {
|
if ($newXSize >= $x && $newYSize >= $y) {
|
||||||
$cropX = floor(abs($x - $newXsize) * 0.5);
|
$cropX = floor(abs($x - $newXSize) * 0.5);
|
||||||
//don't crop previews on the Y axis, this sucks if it's a document.
|
//don't crop previews on the Y axis, this sucks if it's a document.
|
||||||
//$cropY = floor(abs($y - $newYsize) * 0.5);
|
//$cropY = floor(abs($y - $newYsize) * 0.5);
|
||||||
$cropY = 0;
|
$cropY = 0;
|
||||||
|
@ -566,36 +595,36 @@ class Preview {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($newXsize < $x || $newYsize < $y) {
|
if ($newXSize < $x || $newYSize < $y) {
|
||||||
if ($newXsize > $x) {
|
if ($newXSize > $x) {
|
||||||
$cropX = floor(($newXsize - $x) * 0.5);
|
$cropX = floor(($newXSize - $x) * 0.5);
|
||||||
$image->crop($cropX, 0, $x, $newYsize);
|
$image->crop($cropX, 0, $x, $newYSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($newYsize > $y) {
|
if ($newYSize > $y) {
|
||||||
$cropY = floor(($newYsize - $y) * 0.5);
|
$cropY = floor(($newYSize - $y) * 0.5);
|
||||||
$image->crop(0, $cropY, $newXsize, $y);
|
$image->crop(0, $cropY, $newXSize, $y);
|
||||||
}
|
}
|
||||||
|
|
||||||
$newXsize = (int)$image->width();
|
$newXSize = (int)$image->width();
|
||||||
$newYsize = (int)$image->height();
|
$newYSize = (int)$image->height();
|
||||||
|
|
||||||
//create transparent background layer
|
//create transparent background layer
|
||||||
$backgroundlayer = imagecreatetruecolor($x, $y);
|
$backgroundLayer = imagecreatetruecolor($x, $y);
|
||||||
$white = imagecolorallocate($backgroundlayer, 255, 255, 255);
|
$white = imagecolorallocate($backgroundLayer, 255, 255, 255);
|
||||||
imagefill($backgroundlayer, 0, 0, $white);
|
imagefill($backgroundLayer, 0, 0, $white);
|
||||||
|
|
||||||
$image = $image->resource();
|
$image = $image->resource();
|
||||||
|
|
||||||
$mergeX = floor(abs($x - $newXsize) * 0.5);
|
$mergeX = floor(abs($x - $newXSize) * 0.5);
|
||||||
$mergeY = floor(abs($y - $newYsize) * 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);
|
//$black = imagecolorallocate(0,0,0);
|
||||||
//imagecolortransparent($transparentlayer, $black);
|
//imagecolortransparent($transparentlayer, $black);
|
||||||
|
|
||||||
$image = new \OC_Image($backgroundlayer);
|
$image = new \OC_Image($backgroundLayer);
|
||||||
|
|
||||||
$this->preview = $image;
|
$this->preview = $image;
|
||||||
return;
|
return;
|
||||||
|
@ -630,6 +659,7 @@ class Preview {
|
||||||
$class = $provider['class'];
|
$class = $provider['class'];
|
||||||
$options = $provider['options'];
|
$options = $provider['options'];
|
||||||
|
|
||||||
|
/** @var $object Provider */
|
||||||
$object = new $class($options);
|
$object = new $class($options);
|
||||||
|
|
||||||
self::$providers[$object->getMimeType()] = $object;
|
self::$providers[$object->getMimeType()] = $object;
|
||||||
|
@ -676,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)) {
|
if (!\OC_Config::getValue('enable_previews', true)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -690,8 +720,8 @@ class Preview {
|
||||||
|
|
||||||
//remove last element because it has the mimetype *
|
//remove last element because it has the mimetype *
|
||||||
$providers = array_slice(self::$providers, 0, -1);
|
$providers = array_slice(self::$providers, 0, -1);
|
||||||
foreach ($providers as $supportedMimetype => $provider) {
|
foreach ($providers as $supportedMimeType => $provider) {
|
||||||
if (preg_match($supportedMimetype, $mimetype)) {
|
if (preg_match($supportedMimeType, $mimeType)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,66 +10,73 @@ namespace Test;
|
||||||
|
|
||||||
class Preview extends \PHPUnit_Framework_TestCase {
|
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() {
|
public function testIsPreviewDeleted() {
|
||||||
$user = $this->initFS();
|
|
||||||
|
|
||||||
$rootView = new \OC\Files\View('');
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
||||||
$rootView->mkdir('/'.$user);
|
|
||||||
$rootView->mkdir('/'.$user.'/files');
|
|
||||||
|
|
||||||
$samplefile = '/'.$user.'/files/test.txt';
|
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
|
||||||
|
|
||||||
$rootView->file_put_contents($samplefile, 'dummy file data');
|
|
||||||
|
|
||||||
$x = 50;
|
$x = 50;
|
||||||
$y = 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();
|
$preview->getPreview();
|
||||||
|
|
||||||
$fileinfo = $rootView->getFileInfo($samplefile);
|
$fileInfo = $this->rootView->getFileInfo($sampleFile);
|
||||||
$fileid = $fileinfo['fileid'];
|
$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();
|
$preview->deletePreview();
|
||||||
|
|
||||||
$this->assertEquals($rootView->file_exists($thumbcachefile), false);
|
$this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAreAllPreviewsDeleted() {
|
public function testAreAllPreviewsDeleted() {
|
||||||
$user = $this->initFS();
|
|
||||||
|
|
||||||
$rootView = new \OC\Files\View('');
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
||||||
$rootView->mkdir('/'.$user);
|
|
||||||
$rootView->mkdir('/'.$user.'/files');
|
|
||||||
|
|
||||||
$samplefile = '/'.$user.'/files/test.txt';
|
$this->rootView->file_put_contents($sampleFile, 'dummy file data');
|
||||||
|
|
||||||
$rootView->file_put_contents($samplefile, 'dummy file data');
|
|
||||||
|
|
||||||
$x = 50;
|
$x = 50;
|
||||||
$y = 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();
|
$preview->getPreview();
|
||||||
|
|
||||||
$fileinfo = $rootView->getFileInfo($samplefile);
|
$fileInfo = $this->rootView->getFileInfo($sampleFile);
|
||||||
$fileid = $fileinfo['fileid'];
|
$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();
|
$preview->deleteAllPreviews();
|
||||||
|
|
||||||
$this->assertEquals($rootView->is_dir($thumbcachefolder), false);
|
$this->assertEquals($this->rootView->is_dir($thumbCacheFolder), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsMaxSizeWorking() {
|
public function testIsMaxSizeWorking() {
|
||||||
$user = $this->initFS();
|
|
||||||
|
|
||||||
$maxX = 250;
|
$maxX = 250;
|
||||||
$maxY = 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_x', $maxX);
|
||||||
\OC_Config::setValue('preview_max_y', $maxY);
|
\OC_Config::setValue('preview_max_y', $maxY);
|
||||||
|
|
||||||
$rootView = new \OC\Files\View('');
|
$sampleFile = '/'.$this->user.'/files/test.txt';
|
||||||
$rootView->mkdir('/'.$user);
|
|
||||||
$rootView->mkdir('/'.$user.'/files');
|
|
||||||
|
|
||||||
$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($this->user, 'files/', 'test.txt', 1000, 1000);
|
||||||
|
|
||||||
$preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000);
|
|
||||||
$image = $preview->getPreview();
|
$image = $preview->getPreview();
|
||||||
|
|
||||||
$this->assertEquals($image->width(), $maxX);
|
$this->assertEquals($image->width(), $maxX);
|
||||||
|
@ -108,18 +111,13 @@ class Preview extends \PHPUnit_Framework_TestCase {
|
||||||
* @dataProvider txtBlacklist
|
* @dataProvider txtBlacklist
|
||||||
*/
|
*/
|
||||||
public function testIsTransparent($extension, $data, $expectedResult) {
|
public function testIsTransparent($extension, $data, $expectedResult) {
|
||||||
$user = $this->initFS();
|
|
||||||
|
|
||||||
$rootView = new \OC\Files\View('');
|
|
||||||
$rootView->mkdir('/'.$user);
|
|
||||||
$rootView->mkdir('/'.$user.'/files');
|
|
||||||
|
|
||||||
$x = 32;
|
$x = 32;
|
||||||
$y = 32;
|
$y = 32;
|
||||||
|
|
||||||
$sample = '/'.$user.'/files/test.'.$extension;
|
$sample = '/'.$this->user.'/files/test.'.$extension;
|
||||||
$rootView->file_put_contents($sample, $data);
|
$this->rootView->file_put_contents($sample, $data);
|
||||||
$preview = new \OC\Preview($user, 'files/', 'test.'.$extension, $x, $y);
|
$preview = new \OC\Preview($this->user, 'files/', 'test.'.$extension, $x, $y);
|
||||||
$image = $preview->getPreview();
|
$image = $preview->getPreview();
|
||||||
$resource = $image->resource();
|
$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() {
|
private function initFS() {
|
||||||
// create a new user with his own filesystem view
|
// create a new user with his own filesystem view
|
||||||
// this gets called by each test in this test class
|
// this gets called by each test in this test class
|
||||||
|
|
Loading…
Reference in New Issue