Use appdata logo when generating icons

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2017-05-14 23:52:14 +02:00
parent 443cbdc739
commit d3865b6096
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
5 changed files with 66 additions and 34 deletions

View File

@ -26,6 +26,7 @@ namespace OCA\Theming;
use Imagick; use Imagick;
use ImagickPixel; use ImagickPixel;
use OCP\App\AppPathNotFoundException; use OCP\App\AppPathNotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
class IconBuilder { class IconBuilder {
/** @var ThemingDefaults */ /** @var ThemingDefaults */
@ -86,19 +87,23 @@ class IconBuilder {
* @return Imagick|false * @return Imagick|false
*/ */
public function renderAppIcon($app, $size) { public function renderAppIcon($app, $size) {
try {
$appIcon = $this->util->getAppIcon($app); $appIcon = $this->util->getAppIcon($app);
$appIconContent = file_get_contents($appIcon); if($appIcon === false) {
} catch (AppPathNotFoundException $e) {
return false; return false;
} }
if ($appIcon instanceof ISimpleFile) {
$appIconContent = $appIcon->getContent();
$mime = $appIcon->getMimeType();
} else {
$appIconContent = file_get_contents($appIcon);
$mime = mime_content_type($appIcon);
}
if($appIconContent === false) { if($appIconContent === false || $appIconContent === "") {
return false; return false;
} }
$color = $this->themingDefaults->getColorPrimary(); $color = $this->themingDefaults->getColorPrimary();
$mime = mime_content_type($appIcon);
// generate background image with rounded corners // generate background image with rounded corners
$background = '<?xml version="1.0" encoding="UTF-8"?>' . $background = '<?xml version="1.0" encoding="UTF-8"?>' .
@ -136,10 +141,9 @@ class IconBuilder {
} else { } else {
$appIconFile = new Imagick(); $appIconFile = new Imagick();
$appIconFile->setBackgroundColor(new ImagickPixel('transparent')); $appIconFile->setBackgroundColor(new ImagickPixel('transparent'));
$appIconFile->readImageBlob(file_get_contents($appIcon)); $appIconFile->readImageBlob($appIconContent);
$appIconFile->scaleImage(512, 512, true); $appIconFile->scaleImage(512, 512, true);
} }
// offset for icon positioning // offset for icon positioning
$border_w = (int)($appIconFile->getImageWidth() * 0.05); $border_w = (int)($appIconFile->getImageWidth() * 0.05);
$border_h = (int)($appIconFile->getImageHeight() * 0.05); $border_h = (int)($appIconFile->getImageHeight() * 0.05);

View File

@ -25,6 +25,9 @@ namespace OCA\Theming;
use OCP\App\AppPathNotFoundException; use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig; use OCP\IConfig;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
@ -33,23 +36,23 @@ class Util {
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var IRootFolder */
private $rootFolder;
/** @var IAppManager */ /** @var IAppManager */
private $appManager; private $appManager;
/** @var IAppData */
private $appData;
/** /**
* Util constructor. * Util constructor.
* *
* @param IConfig $config * @param IConfig $config
* @param IRootFolder $rootFolder
* @param IAppManager $appManager * @param IAppManager $appManager
* @param IAppData $appData
*/ */
public function __construct(IConfig $config, IRootFolder $rootFolder, IAppManager $appManager) { public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData) {
$this->config = $config; $this->config = $config;
$this->rootFolder = $rootFolder;
$this->appManager = $appManager; $this->appManager = $appManager;
$this->appData = $appData;
} }
/** /**
@ -111,7 +114,7 @@ class Util {
/** /**
* @param $app string app name * @param $app string app name
* @return string path to app icon / logo * @return string|ISimpleFile path to app icon / file of logo
*/ */
public function getAppIcon($app) { public function getAppIcon($app) {
$app = str_replace(array('\0', '/', '\\', '..'), '', $app); $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
@ -127,8 +130,14 @@ class Util {
} }
} catch (AppPathNotFoundException $e) {} } catch (AppPathNotFoundException $e) {}
if($this->config->getAppValue('theming', 'logoMime', '') !== '' && $this->rootFolder->nodeExists('/themedinstancelogo')) { if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/themedinstancelogo'; $logoFile = null;
try {
$folder = $this->appData->getFolder('images');
if ($folder !== null) {
return $folder->getFile('logo');
}
} catch (NotFoundException $e) {}
} }
return \OC::$SERVERROOT . '/core/img/logo.svg'; return \OC::$SERVERROOT . '/core/img/logo.svg';
} }

View File

@ -27,7 +27,9 @@ use OCA\Theming\ThemingDefaults;
use OCA\Theming\Util; use OCA\Theming\Util;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\Http\NotFoundResponse;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IConfig; use OCP\IConfig;
use Test\TestCase; use Test\TestCase;
@ -35,8 +37,8 @@ class IconBuilderTest extends TestCase {
/** @var IConfig */ /** @var IConfig */
protected $config; protected $config;
/** @var IRootFolder */ /** @var IAppData */
protected $rootFolder; protected $appData;
/** @var ThemingDefaults */ /** @var ThemingDefaults */
protected $themingDefaults; protected $themingDefaults;
/** @var Util */ /** @var Util */
@ -50,11 +52,11 @@ class IconBuilderTest extends TestCase {
parent::setUp(); parent::setUp();
$this->config = $this->getMockBuilder('\OCP\IConfig')->getMock(); $this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock(); $this->appData = $this->createMock(IAppData::class);
$this->themingDefaults = $this->getMockBuilder('OCA\Theming\ThemingDefaults') $this->themingDefaults = $this->getMockBuilder('OCA\Theming\ThemingDefaults')
->disableOriginalConstructor()->getMock(); ->disableOriginalConstructor()->getMock();
$this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock(); $this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
$this->util = new Util($this->config, $this->rootFolder, $this->appManager); $this->util = new Util($this->config, $this->appManager, $this->appData);
$this->iconBuilder = new IconBuilder($this->themingDefaults, $this->util); $this->iconBuilder = new IconBuilder($this->themingDefaults, $this->util);
} }
@ -89,6 +91,10 @@ class IconBuilderTest extends TestCase {
$this->themingDefaults->expects($this->once()) $this->themingDefaults->expects($this->once())
->method('getColorPrimary') ->method('getColorPrimary')
->willReturn($color); ->willReturn($color);
$this->appData->expects($this->once())
->method('getFolder')
->with('images')
->willThrowException(new NotFoundException());
$expectedIcon = new \Imagick(realpath(dirname(__FILE__)). "/data/" . $file); $expectedIcon = new \Imagick(realpath(dirname(__FILE__)). "/data/" . $file);
$icon = $this->iconBuilder->renderAppIcon($app, 512); $icon = $this->iconBuilder->renderAppIcon($app, 512);

View File

@ -24,6 +24,10 @@ namespace OCA\Theming\Tests;
use OCA\Theming\Util; use OCA\Theming\Util;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use Test\TestCase; use Test\TestCase;
@ -34,17 +38,17 @@ class UtilTest extends TestCase {
protected $util; protected $util;
/** @var IConfig */ /** @var IConfig */
protected $config; protected $config;
/** @var IRootFolder */ /** @var IAppData */
protected $rootFolder; protected $appData;
/** @var IAppManager */ /** @var IAppManager */
protected $appManager; protected $appManager;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->config = $this->getMockBuilder('\OCP\IConfig')->getMock(); $this->config = $this->createMock(IConfig::class);
$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock(); $this->appData = $this->createMock(IAppData::class);
$this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock(); $this->appManager = $this->createMock(IAppManager::class);
$this->util = new Util($this->config, $this->rootFolder, $this->appManager); $this->util = new Util($this->config, $this->appManager, $this->appData);
} }
public function testInvertTextColorLight() { public function testInvertTextColorLight() {
@ -112,6 +116,10 @@ class UtilTest extends TestCase {
* @dataProvider dataGetAppIcon * @dataProvider dataGetAppIcon
*/ */
public function testGetAppIcon($app, $expected) { public function testGetAppIcon($app, $expected) {
$this->appData->expects($this->any())
->method('getFolder')
->with('images')
->willThrowException(new NotFoundException());
$this->appManager->expects($this->once()) $this->appManager->expects($this->once())
->method('getAppPath') ->method('getAppPath')
->with($app) ->with($app)
@ -129,13 +137,18 @@ class UtilTest extends TestCase {
} }
public function testGetAppIconThemed() { public function testGetAppIconThemed() {
$this->rootFolder->expects($this->once()) $file = $this->createMock(ISimpleFile::class);
->method('nodeExists') $folder = $this->createMock(ISimpleFolder::class);
->with('/themedinstancelogo') $folder->expects($this->once())
->willReturn(true); ->method('getFile')
$expected = '/themedinstancelogo'; ->with('logo')
->willReturn($file);
$this->appData->expects($this->once())
->method('getFolder')
->with('images')
->willReturn($folder);
$icon = $this->util->getAppIcon('noapplikethis'); $icon = $this->util->getAppIcon('noapplikethis');
$this->assertEquals($expected, $icon); $this->assertEquals($file, $icon);
} }
/** /**

View File

@ -853,7 +853,7 @@ class Server extends ServerContainer implements IServerContainer {
$c->getURLGenerator(), $c->getURLGenerator(),
$c->getAppDataDir('theming'), $c->getAppDataDir('theming'),
$c->getMemCacheFactory(), $c->getMemCacheFactory(),
new Util($c->getConfig(), $this->getRootFolder(), $this->getAppManager()) new Util($c->getConfig(), $this->getAppManager(), $this->getAppDataDir('theming'))
); );
} }
return new \OC_Defaults(); return new \OC_Defaults();