From f4d180ee5c6be5b3d061a035299079fc675b945f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 17 Jul 2015 13:18:01 +0200 Subject: [PATCH 1/6] allow passing the fileinfo to the preview manager --- core/ajax/preview.php | 2 +- lib/private/preview.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/ajax/preview.php b/core/ajax/preview.php index c8bc05453e..b6362fb3a7 100644 --- a/core/ajax/preview.php +++ b/core/ajax/preview.php @@ -53,7 +53,7 @@ if (!$info instanceof OCP\Files\FileInfo || !$always && !\OC::$server->getPrevie \OC_Response::setStatus(404); } else { $preview = new \OC\Preview(\OC_User::getUser(), 'files'); - $preview->setFile($file); + $preview->setFile($file, $info); $preview->setMaxX($maxX); $preview->setMaxY($maxY); $preview->setScalingUp($scalingUp); diff --git a/lib/private/preview.php b/lib/private/preview.php index 1127048b7f..996bf81703 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -252,12 +252,13 @@ class Preview { * Sets the path of the file you want a preview of * * @param string $file + * @param \OCP\Files\FileInfo|null $info * * @return \OC\Preview */ - public function setFile($file) { + public function setFile($file, $info = null) { $this->file = $file; - $this->info = null; + $this->info = $info; if ($file !== '') { $this->getFileInfo(); From 52d4495793377c8eb2c2cdabf8ec8b8cce2dd9d1 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 17 Jul 2015 13:18:19 +0200 Subject: [PATCH 2/6] use the fileinfo to check if we have a valid file --- lib/private/preview.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 996bf81703..e445f5356a 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -375,7 +375,7 @@ class Preview { return false; } - if (!$this->fileView->file_exists($file)) { + if (!$this->getFileInfo() instanceof FileInfo) { \OCP\Util::writeLog('core', 'File:"' . $file . '" not found', \OCP\Util::DEBUG); return false; From 9925ff33f10bbece12816c0638756eb0e6ddbe92 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 17 Jul 2015 13:25:09 +0200 Subject: [PATCH 3/6] use cache data we already have to check if a thumbnail of specific size is cached --- lib/private/preview.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index e445f5356a..dabb274323 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -479,7 +479,7 @@ class Preview { $preview = $this->buildCachePath($fileId, $previewWidth, $previewHeight); // This checks if we have a preview of those exact dimensions in the cache - if ($this->userView->file_exists($preview)) { + if ($this->thumbnailSizeExists($allThumbnails, $previewWidth, $previewHeight)) { return $preview; } @@ -524,6 +524,29 @@ class Preview { return [$maxPreviewX, $maxPreviewY]; } + /** + * Check if a specific thumbnail size is cached + * + * @param FileInfo[] $allThumbnails the list of all our cached thumbnails + * @param int $width + * @param int $height + * @return bool + */ + private function thumbnailSizeExists($allThumbnails, $width, $height) { + + foreach ($allThumbnails as $thumbnail) { + $name = $thumbnail['name']; + if (strpos($name, 'max')) { + list($cachedWidth, $cachedHeight) = $this->getDimensionsFromFilename($name); + if ($cachedWidth === $width && $cachedHeight === $height) { + return true; + } + } + } + + return false; + } + /** * Determines the size of the preview we should be looking for in the cache * From 884e2fcd6ecd9a402619fced47b8b5c9a63a85dd Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 17 Jul 2015 14:41:16 +0200 Subject: [PATCH 4/6] to much copy paste --- lib/private/preview.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index dabb274323..41fc29e045 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -536,11 +536,9 @@ class Preview { foreach ($allThumbnails as $thumbnail) { $name = $thumbnail['name']; - if (strpos($name, 'max')) { - list($cachedWidth, $cachedHeight) = $this->getDimensionsFromFilename($name); - if ($cachedWidth === $width && $cachedHeight === $height) { - return true; - } + list($cachedWidth, $cachedHeight) = $this->getDimensionsFromFilename($name); + if ($cachedWidth === $width && $cachedHeight === $height) { + return true; } } From dc49e767472ef67bcfd6318133537bef03ce7a7c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 17 Jul 2015 14:44:24 +0200 Subject: [PATCH 5/6] stricter check if thumbnail is cached --- lib/private/preview.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 41fc29e045..24a9082b17 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -479,7 +479,7 @@ class Preview { $preview = $this->buildCachePath($fileId, $previewWidth, $previewHeight); // This checks if we have a preview of those exact dimensions in the cache - if ($this->thumbnailSizeExists($allThumbnails, $previewWidth, $previewHeight)) { + if ($this->thumbnailSizeExists($allThumbnails, basename($preview))) { return $preview; } @@ -528,16 +528,13 @@ class Preview { * Check if a specific thumbnail size is cached * * @param FileInfo[] $allThumbnails the list of all our cached thumbnails - * @param int $width - * @param int $height + * @param string $name * @return bool */ - private function thumbnailSizeExists($allThumbnails, $width, $height) { + private function thumbnailSizeExists($allThumbnails, $name) { foreach ($allThumbnails as $thumbnail) { - $name = $thumbnail['name']; - list($cachedWidth, $cachedHeight) = $this->getDimensionsFromFilename($name); - if ($cachedWidth === $width && $cachedHeight === $height) { + if ($name === $thumbnail->getName()) { return true; } } From 1c3e28a73b602423dfc4ba447b26dcd3b8b45fac Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 29 Sep 2015 14:40:14 +0200 Subject: [PATCH 6/6] Add tests --- lib/private/preview.php | 2 +- tests/lib/preview.php | 74 +++++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/lib/private/preview.php b/lib/private/preview.php index 24a9082b17..b2accdfd00 100644 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -531,7 +531,7 @@ class Preview { * @param string $name * @return bool */ - private function thumbnailSizeExists($allThumbnails, $name) { + private function thumbnailSizeExists(array $allThumbnails, $name) { foreach ($allThumbnails as $thumbnail) { if ($name === $thumbnail->getName()) { diff --git a/tests/lib/preview.php b/tests/lib/preview.php index 9374cf2393..e4c599c66c 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -22,7 +22,15 @@ namespace Test; +use OC\Files\FileInfo; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use Test\Traits\MountProviderTrait; +use Test\Traits\UserTrait; + class Preview extends TestCase { + use UserTrait; + use MountProviderTrait; const TEST_PREVIEW_USER1 = "test-preview-user1"; @@ -59,11 +67,7 @@ class Preview extends TestCase { protected function setUp() { parent::setUp(); - $userManager = \OC::$server->getUserManager(); - $userManager->clearBackends(); - $backend = new \Test\Util\User\Dummy(); - $userManager->registerBackend($backend); - $backend->createUser(self::TEST_PREVIEW_USER1, self::TEST_PREVIEW_USER1); + $this->createUser(self::TEST_PREVIEW_USER1, self::TEST_PREVIEW_USER1); $this->loginAsUser(self::TEST_PREVIEW_USER1); $storage = new \OC\Files\Storage\Temporary([]); @@ -75,13 +79,13 @@ class Preview extends TestCase { // We simulate the max dimension set in the config \OC::$server->getConfig() - ->setSystemValue('preview_max_x', $this->configMaxWidth); + ->setSystemValue('preview_max_x', $this->configMaxWidth); \OC::$server->getConfig() - ->setSystemValue('preview_max_y', $this->configMaxHeight); + ->setSystemValue('preview_max_y', $this->configMaxHeight); // Used to test upscaling $this->maxScaleFactor = 2; \OC::$server->getConfig() - ->setSystemValue('preview_max_scale_factor', $this->maxScaleFactor); + ->setSystemValue('preview_max_scale_factor', $this->maxScaleFactor); // We need to enable the providers we're going to use in the tests $providers = [ @@ -92,7 +96,7 @@ class Preview extends TestCase { 'OC\\Preview\\Postscript' ]; \OC::$server->getConfig() - ->setSystemValue('enabledPreviewProviders', $providers); + ->setSystemValue('enabledPreviewProviders', $providers); // Sample is 1680x1050 JPEG $this->prepareSample('testimage.jpg', 1680, 1050); @@ -161,7 +165,7 @@ class Preview extends TestCase { $fileId = $fileInfo['fileid']; $thumbCacheFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . - '/' . $fileId . '/'; + '/' . $fileId . '/'; $this->assertSame(true, $this->rootView->is_dir($thumbCacheFolder), "$thumbCacheFolder \n"); @@ -318,7 +322,7 @@ class Preview extends TestCase { // There should be no cached thumbnails $thumbnailFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . - '/' . $sampleFileId; + '/' . $sampleFileId; $this->assertSame(false, $this->rootView->is_dir($thumbnailFolder)); $image = $preview->getPreview(); @@ -611,7 +615,7 @@ class Preview extends TestCase { // Need to take care of special postfix added to the dimensions $postfix = ''; $isMaxPreview = ($width === $this->maxPreviewWidth - && $height === $this->maxPreviewHeight) ? true : false; + && $height === $this->maxPreviewHeight) ? true : false; if ($isMaxPreview) { $postfix = '-max'; } @@ -731,7 +735,7 @@ class Preview extends TestCase { } return $userPath . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId - . '/' . $width . '-' . $height . $postfix . '.png'; + . '/' . $width . '-' . $height . $postfix . '.png'; } /** @@ -752,11 +756,11 @@ class Preview extends TestCase { $this->samples[] = [ - 'sampleFileId' => $fileInfo['fileid'], - 'sampleFileName' => $fileName, - 'sampleWidth' => $sampleWidth, - 'sampleHeight' => $sampleHeight, - 'maxPreviewWidth' => $maxPreviewWidth, + 'sampleFileId' => $fileInfo['fileid'], + 'sampleFileName' => $fileName, + 'sampleWidth' => $sampleWidth, + 'sampleHeight' => $sampleHeight, + 'maxPreviewWidth' => $maxPreviewWidth, 'maxPreviewHeight' => $maxPreviewHeight ]; } @@ -915,4 +919,38 @@ class Preview extends TestCase { $this->assertGreaterThanOrEqual(150, $image->width()); $this->assertGreaterThanOrEqual(150, $image->height()); } + + public function testSetFileWithInfo() { + $info = new FileInfo('/foo', null, '/foo', ['mimetype' => 'foo/bar'], null); + $preview = new \OC\Preview(); + $preview->setFile('/foo', $info); + $this->assertEquals($info, $this->invokePrivate($preview, 'getFileInfo')); + } + + public function testIsCached() { + $sourceFile = __DIR__ . '/../data/testimage.png'; + $userId = $this->getUniqueID(); + $this->createUser($userId, 'pass'); + + $storage = new Temporary(); + $storage->mkdir('files'); + $this->registerMount($userId, $storage, '/' . $userId); + + \OC_Util::tearDownFS(); + \OC_Util::setupFS($userId); + $preview = new \OC\Preview($userId, 'files'); + $view = new View('/' . $userId . '/files'); + $view->file_put_contents('test.png', file_get_contents($sourceFile)); + $info = $view->getFileInfo('test.png'); + $preview->setFile('test.png', $info); + + $preview->setMaxX(64); + $preview->setMaxY(64); + + $this->assertFalse($preview->isCached($info->getId())); + + $preview->getPreview(); + + $this->assertEquals('thumbnails/' . $info->getId() . '/64-64.png', $preview->isCached($info->getId())); + } }