parent
9e28a3ba12
commit
492d0b9f9b
@ -0,0 +1,118 @@ |
||||
<?php |
||||
/** |
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> |
||||
* |
||||
* @author Julius Härtl <jus@bitgrid.net> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
|
||||
|
||||
namespace OCA\Theming; |
||||
|
||||
use OCP\IConfig; |
||||
use OCP\Files\IAppData; |
||||
use OCP\Files\NotFoundException; |
||||
use OCP\Files\NotPermittedException; |
||||
|
||||
class ImageManager { |
||||
|
||||
/** @var IConfig */ |
||||
private $config; |
||||
/** @var IAppData */ |
||||
private $appData; |
||||
|
||||
/** |
||||
* ImageManager constructor. |
||||
* |
||||
* @param IConfig $config |
||||
* @param IAppData $appData |
||||
*/ |
||||
public function __construct(IConfig $config, |
||||
IAppData $appData |
||||
) { |
||||
$this->config = $config; |
||||
$this->appData = $appData; |
||||
} |
||||
|
||||
/** |
||||
* Get folder for current theming files |
||||
* |
||||
* @return \OCP\Files\SimpleFS\ISimpleFolder |
||||
* @throws NotPermittedException |
||||
* @throws \RuntimeException |
||||
*/ |
||||
public function getCacheFolder() { |
||||
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||||
try { |
||||
$folder = $this->appData->getFolder($cacheBusterValue); |
||||
} catch (NotFoundException $e) { |
||||
$folder = $this->appData->newFolder($cacheBusterValue); |
||||
$this->cleanup(); |
||||
} |
||||
return $folder; |
||||
} |
||||
|
||||
/** |
||||
* Get a file from AppData |
||||
* |
||||
* @param string $filename |
||||
* @return null|\OCP\Files\SimpleFS\ISimpleFile |
||||
*/ |
||||
public function getCachedImage($filename) { |
||||
$currentFolder = $this->getCacheFolder(); |
||||
if($currentFolder->fileExists($filename)) { |
||||
return $currentFolder->getFile($filename); |
||||
} else { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Store a file for theming in AppData |
||||
* |
||||
* @param string $filename |
||||
* @param string $data |
||||
* @return \OCP\Files\SimpleFS\ISimpleFile |
||||
*/ |
||||
public function setCachedImage($filename, $data) { |
||||
$currentFolder = $this->getCacheFolder(); |
||||
if ($currentFolder->fileExists($filename)) { |
||||
$file = $currentFolder->getFile($filename); |
||||
} else { |
||||
$file = $currentFolder->newFile($filename); |
||||
} |
||||
$file->putContent($data); |
||||
return $file; |
||||
} |
||||
|
||||
/** |
||||
* remove cached files that are not required any longer |
||||
*/ |
||||
public function cleanup() { |
||||
$currentFolder = $this->getCacheFolder(); |
||||
$folders = $this->appData->getDirectoryListing(); |
||||
foreach ($folders as $folder) { |
||||
if ($folder->getName() !== $currentFolder->getName()) { |
||||
$folder->delete(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,185 @@ |
||||
<?php |
||||
/** |
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> |
||||
* |
||||
* @author Julius Härtl <jus@bitgrid.net> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU Affero General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Affero General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
* |
||||
*/ |
||||
namespace OCA\Theming\Tests; |
||||
|
||||
use OCP\Files\SimpleFS\ISimpleFile; |
||||
use OCP\IConfig; |
||||
use Test\TestCase; |
||||
use OCP\Files\SimpleFS\ISimpleFolder; |
||||
use OCP\Files\IAppData; |
||||
use OCP\Files\NotFoundException; |
||||
|
||||
class ImageManager extends TestCase { |
||||
|
||||
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ |
||||
protected $config; |
||||
/** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ |
||||
protected $appData; |
||||
/** @var ImageManager */ |
||||
protected $imageManager; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
$this->config = $this->getMockBuilder('\OCP\IConfig')->getMock(); |
||||
$this->appData = $this->getMockBuilder('OCP\Files\IAppData')->getMock(); |
||||
$this->imageManager = new \OCA\Theming\ImageManager( |
||||
$this->config, |
||||
$this->appData |
||||
); |
||||
} |
||||
|
||||
public function testGetCacheFolder() { |
||||
$folder = $this->createMock(ISimpleFolder::class); |
||||
$this->config->expects($this->once()) |
||||
->method('getAppValue') |
||||
->with('theming', 'cachebuster', '0') |
||||
->willReturn('0'); |
||||
$this->appData->expects($this->at(0)) |
||||
->method('getFolder') |
||||
->with('0') |
||||
->willReturn($folder); |
||||
$this->assertEquals($folder, $this->imageManager->getCacheFolder()); |
||||
} |
||||
public function testGetCacheFolderCreate() { |
||||
$folder = $this->createMock(ISimpleFolder::class); |
||||
$this->config->expects($this->exactly(2)) |
||||
->method('getAppValue') |
||||
->with('theming', 'cachebuster', '0') |
||||
->willReturn('0'); |
||||
$this->appData->expects($this->at(0)) |
||||
->method('getFolder') |
||||
->willThrowException(new NotFoundException()); |
||||
$this->appData->expects($this->at(1)) |
||||
->method('newFolder') |
||||
->with('0') |
||||
->willReturn($folder); |
||||
$this->appData->expects($this->at(2)) |
||||
->method('getFolder') |
||||
->with('0') |
||||
->willReturn($folder); |
||||
$this->appData->expects($this->once()) |
||||
->method('getDirectoryListing') |
||||
->willReturn([]); |
||||
$this->assertEquals($folder, $this->imageManager->getCacheFolder()); |
||||
} |
||||
|
||||
public function testGetCachedImage() { |
||||
$folder = $this->setupCacheFolder(); |
||||
$folder->expects($this->once()) |
||||
->method('fileExists') |
||||
->with('filename') |
||||
->willReturn(true); |
||||
$folder->expects($this->once()) |
||||
->method('getFile') |
||||
->with('filename') |
||||
->willReturn('filecontent'); |
||||
$expected = 'filecontent'; |
||||
$this->assertEquals($expected, $this->imageManager->getCachedImage('filename')); |
||||
} |
||||
|
||||
public function testGetCachedImageNotFound() { |
||||
$folder = $this->setupCacheFolder(); |
||||
$folder->expects($this->once()) |
||||
->method('fileExists') |
||||
->with('filename') |
||||
->willReturn(false); |
||||
$expected = null; |
||||
$this->assertEquals($expected, $this->imageManager->getCachedImage('filename')); |
||||
} |
||||
|
||||
public function testSetCachedImage() { |
||||
$folder = $this->setupCacheFolder(); |
||||
$file = $this->createMock(ISimpleFile::class); |
||||
$folder->expects($this->once()) |
||||
->method('fileExists') |
||||
->with('filename') |
||||
->willReturn(true); |
||||
$folder->expects($this->once()) |
||||
->method('getFile') |
||||
->with('filename') |
||||
->willReturn($file); |
||||
$file->expects($this->once()) |
||||
->method('putContent') |
||||
->with('filecontent'); |
||||
$this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent')); |
||||
} |
||||
|
||||
public function testSetCachedImageCreate() { |
||||
$folder = $this->setupCacheFolder(); |
||||
$file = $this->createMock(ISimpleFile::class); |
||||
$folder->expects($this->once()) |
||||
->method('fileExists') |
||||
->with('filename') |
||||
->willReturn(false); |
||||
$folder->expects($this->once()) |
||||
->method('newFile') |
||||
->with('filename') |
||||
->willReturn($file); |
||||
$file->expects($this->once()) |
||||
->method('putContent') |
||||
->with('filecontent'); |
||||
$this->assertEquals($file, $this->imageManager->setCachedImage('filename', 'filecontent')); |
||||
} |
||||
|
||||
private function setupCacheFolder() { |
||||
$folder = $this->createMock(ISimpleFolder::class); |
||||
$this->config->expects($this->once()) |
||||
->method('getAppValue') |
||||
->with('theming', 'cachebuster', '0') |
||||
->willReturn('0'); |
||||
$this->appData->expects($this->at(0)) |
||||
->method('getFolder') |
||||
->with('0') |
||||
->willReturn($folder); |
||||
return $folder; |
||||
} |
||||
|
||||
public function testCleanup() { |
||||
$folders = [ |
||||
$this->createMock(ISimpleFolder::class), |
||||
$this->createMock(ISimpleFolder::class), |
||||
$this->createMock(ISimpleFolder::class) |
||||
]; |
||||
foreach ($folders as $index=>$folder) { |
||||
$folder->expects($this->any()) |
||||
->method('getName') |
||||
->willReturn($index); |
||||
} |
||||
$folders[0]->expects($this->once())->method('delete'); |
||||
$folders[1]->expects($this->once())->method('delete'); |
||||
$folders[2]->expects($this->never())->method('delete'); |
||||
$this->config->expects($this->once()) |
||||
->method('getAppValue') |
||||
->with('theming','cachebuster','0') |
||||
->willReturn('2'); |
||||
$this->appData->expects($this->once()) |
||||
->method('getDirectoryListing') |
||||
->willReturn($folders); |
||||
$this->appData->expects($this->once()) |
||||
->method('getFolder') |
||||
->with('2') |
||||
->willReturn($folders[2]); |
||||
$this->imageManager->cleanup(); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue