Merge pull request #17703 from owncloud/preview-better-cache-use

Better cache usage for previews
This commit is contained in:
Robin Appelman 2015-10-01 16:24:29 +02:00
commit 8ae4f5bf1a
3 changed files with 80 additions and 23 deletions

View File

@ -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);

View File

@ -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();
@ -374,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;
@ -478,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, basename($preview))) {
return $preview;
}
@ -523,6 +524,24 @@ class Preview {
return [$maxPreviewX, $maxPreviewY];
}
/**
* Check if a specific thumbnail size is cached
*
* @param FileInfo[] $allThumbnails the list of all our cached thumbnails
* @param string $name
* @return bool
*/
private function thumbnailSizeExists(array $allThumbnails, $name) {
foreach ($allThumbnails as $thumbnail) {
if ($name === $thumbnail->getName()) {
return true;
}
}
return false;
}
/**
* Determines the size of the preview we should be looking for in the cache
*

View File

@ -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([]);
@ -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()));
}
}