Merge pull request #6451 from nextcloud/theming-manifest-json

Theming app support for chrome manifest.json file
This commit is contained in:
Morris Jobke 2017-09-12 15:10:08 +02:00 committed by GitHub
commit 83508d7be3
8 changed files with 176 additions and 13 deletions

View File

@ -60,6 +60,12 @@ return ['routes' => [
'url' => '/js/theming', 'url' => '/js/theming',
'verb' => 'GET', 'verb' => 'GET',
], ],
[
'name' => 'Theming#getManifest',
'url' => '/manifest/{app}',
'verb' => 'GET',
'defaults' => array('app' => 'core')
],
[ [
'name' => 'Icon#getFavicon', 'name' => 'Icon#getFavicon',
'url' => '/favicon/{app}', 'url' => '/favicon/{app}',

View File

@ -423,4 +423,39 @@ class ThemingController extends Controller {
$response->cacheFor(3600); $response->cacheFor(3600);
return $response; return $response;
} }
/**
* @NoCSRFRequired
* @PublicPage
*
* @return Http\JSONResponse
*/
public function getManifest($app) {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
$responseJS = [
'name' => $this->themingDefaults->getName(),
'start_url' => $this->urlGenerator->getBaseUrl(),
'icons' =>
[
[
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon',
['app' => $app]) . '?v=' . $cacheBusterValue,
'type'=> 'image/png',
'sizes'=> '128x128'
],
[
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getFavicon',
['app' => $app]) . '?v=' . $cacheBusterValue,
'type' => 'image/svg+xml',
'sizes' => '16x16'
]
],
'display' => 'standalone'
];
$response = new Http\JSONResponse($responseJS);
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Pragma', 'cache');
$response->cacheFor(3600);
return $response;
}
} }

View File

@ -23,6 +23,8 @@
namespace OCA\Theming; namespace OCA\Theming;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
@ -41,6 +43,10 @@ class ThemingDefaults extends \OC_Defaults {
private $appData; private $appData;
/** @var ICacheFactory */ /** @var ICacheFactory */
private $cacheFactory; private $cacheFactory;
/** @var Util */
private $util;
/** @var IAppManager */
private $appManager;
/** @var string */ /** @var string */
private $name; private $name;
/** @var string */ /** @var string */
@ -49,8 +55,7 @@ class ThemingDefaults extends \OC_Defaults {
private $slogan; private $slogan;
/** @var string */ /** @var string */
private $color; private $color;
/** @var Util */
private $util;
/** @var string */ /** @var string */
private $iTunesAppId; private $iTunesAppId;
/** @var string */ /** @var string */
@ -68,13 +73,15 @@ class ThemingDefaults extends \OC_Defaults {
* @param IAppData $appData * @param IAppData $appData
* @param ICacheFactory $cacheFactory * @param ICacheFactory $cacheFactory
* @param Util $util * @param Util $util
* @param IAppManager $appManager
*/ */
public function __construct(IConfig $config, public function __construct(IConfig $config,
IL10N $l, IL10N $l,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IAppData $appData, IAppData $appData,
ICacheFactory $cacheFactory, ICacheFactory $cacheFactory,
Util $util Util $util,
IAppManager $appManager
) { ) {
parent::__construct(); parent::__construct();
$this->config = $config; $this->config = $config;
@ -83,6 +90,7 @@ class ThemingDefaults extends \OC_Defaults {
$this->appData = $appData; $this->appData = $appData;
$this->cacheFactory = $cacheFactory; $this->cacheFactory = $cacheFactory;
$this->util = $util; $this->util = $util;
$this->appManager = $appManager;
$this->name = parent::getName(); $this->name = parent::getName();
$this->url = parent::getBaseUrl(); $this->url = parent::getBaseUrl();
@ -248,6 +256,38 @@ class ThemingDefaults extends \OC_Defaults {
return $variables; return $variables;
} }
/**
* Check if the image should be replaced by the theming app
* and return the new image location then
*
* @param string $app name of the app
* @param string $image filename of the image
* @return bool|string false if image should not replaced, otherwise the location of the image
*/
public function replaceImagePath($app, $image) {
if($app==='') {
$app = 'core';
}
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
if ($image === 'favicon.ico' && $this->shouldReplaceIcons()) {
return $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]) . '?v=' . $cacheBusterValue;
}
if ($image === 'favicon-touch.png' && $this->shouldReplaceIcons()) {
return $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]) . '?v=' . $cacheBusterValue;
}
if ($image === 'manifest.json') {
try {
$appPath = $this->appManager->getAppPath($app);
if (file_exists($appPath . '/img/manifest.json')) {
return false;
}
} catch (AppPathNotFoundException $e) {}
return $this->urlGenerator->linkToRoute('theming.Theming.getManifest') . '?v=' . $cacheBusterValue;
}
return false;
}
/** /**
* Check if Imagemagick is enabled and if SVG is supported * Check if Imagemagick is enabled and if SVG is supported
* otherwise we can't render custom icons * otherwise we can't render custom icons

View File

@ -729,4 +729,53 @@ class ThemingControllerTest extends TestCase {
$expected->cacheFor(3600); $expected->cacheFor(3600);
@$this->assertEquals($expected, $this->themingController->getJavascript()); @$this->assertEquals($expected, $this->themingController->getJavascript());
} }
public function testGetManifest() {
$this->config
->expects($this->once())
->method('getAppValue')
->with('theming', 'cachebuster', '0')
->willReturn('0');
$this->themingDefaults
->expects($this->any())
->method('getName')
->willReturn('Nextcloud');
$this->urlGenerator
->expects($this->at(0))
->method('getBaseUrl')
->willReturn('localhost');
$this->urlGenerator
->expects($this->at(1))
->method('linkToRoute')
->with('theming.Icon.getTouchIcon', ['app' => 'core'])
->willReturn('touchicon');
$this->urlGenerator
->expects($this->at(2))
->method('linkToRoute')
->with('theming.Icon.getFavicon', ['app' => 'core'])
->willReturn('favicon');
$response = new Http\JSONResponse([
'name' => 'Nextcloud',
'start_url' => 'localhost',
'icons' =>
[
[
'src' => 'touchicon?v=0',
'type'=> 'image/png',
'sizes'=> '128x128'
],
[
'src' => 'favicon?v=0',
'type' => 'image/svg+xml',
'sizes' => '16x16'
]
],
'display' => 'standalone'
]);
$response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime()));
$response->addHeader('Pragma', 'cache');
$response->cacheFor(3600);
$this->assertEquals($response, $this->themingController->getManifest('core'));
}
} }

View File

@ -24,6 +24,7 @@
namespace OCA\Theming\Tests; namespace OCA\Theming\Tests;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\App\IAppManager;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCA\Theming\Util; use OCA\Theming\Util;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
@ -55,6 +56,8 @@ class ThemingDefaultsTest extends TestCase {
private $util; private $util;
/** @var ICache|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */
private $cache; private $cache;
/** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
private $appManager;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
@ -65,6 +68,7 @@ class ThemingDefaultsTest extends TestCase {
$this->cacheFactory = $this->createMock(ICacheFactory::class); $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->cache = $this->createMock(ICache::class); $this->cache = $this->createMock(ICache::class);
$this->util = $this->createMock(Util::class); $this->util = $this->createMock(Util::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->defaults = new \OC_Defaults(); $this->defaults = new \OC_Defaults();
$this->cacheFactory $this->cacheFactory
->expects($this->any()) ->expects($this->any())
@ -77,7 +81,8 @@ class ThemingDefaultsTest extends TestCase {
$this->urlGenerator, $this->urlGenerator,
$this->appData, $this->appData,
$this->cacheFactory, $this->cacheFactory,
$this->util $this->util,
$this->appManager
); );
} }
@ -607,4 +612,31 @@ class ThemingDefaultsTest extends TestCase {
$this->assertEquals('1234567890', $this->template->getiTunesAppId()); $this->assertEquals('1234567890', $this->template->getiTunesAppId());
} }
public function dataReplaceImagePath() {
return [
['core', 'test.png', false],
['core', 'manifest.json'],
['core', 'favicon.ico'],
['core', 'favicon-touch.png']
];
}
/** @dataProvider dataReplaceImagePath */
public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=0') {
$this->cache->expects($this->any())
->method('get')
->with('shouldReplaceIcons')
->willReturn(true);
$this->config
->expects($this->any())
->method('getAppValue')
->with('theming', 'cachebuster', '0')
->willReturn('0');
$this->urlGenerator
->expects($this->any())
->method('linkToRoute')
->willReturn('themingRoute');
$this->assertEquals($result, $this->template->replaceImagePath($app, $image));
}
} }

View File

@ -13,6 +13,7 @@
<link rel="icon" href="<?php print_unescaped(image_path('', 'favicon.ico')); /* IE11+ supports png */ ?>"> <link rel="icon" href="<?php print_unescaped(image_path('', 'favicon.ico')); /* IE11+ supports png */ ?>">
<link rel="apple-touch-icon-precomposed" href="<?php print_unescaped(image_path('', 'favicon-touch.png')); ?>"> <link rel="apple-touch-icon-precomposed" href="<?php print_unescaped(image_path('', 'favicon-touch.png')); ?>">
<link rel="mask-icon" sizes="any" href="<?php print_unescaped(image_path('', 'favicon-mask.svg')); ?>" color="<?php p($theme->getColorPrimary()); ?>"> <link rel="mask-icon" sizes="any" href="<?php print_unescaped(image_path('', 'favicon-mask.svg')); ?>" color="<?php p($theme->getColorPrimary()); ?>">
<link rel="manifest" href="<?php print_unescaped(image_path('', 'manifest.json')); ?>">
<?php emit_css_loading_tags($_); ?> <?php emit_css_loading_tags($_); ?>
<?php emit_script_loading_tags($_); ?> <?php emit_script_loading_tags($_); ?>
<?php print_unescaped($_['headers']); ?> <?php print_unescaped($_['headers']); ?>

View File

@ -892,7 +892,8 @@ 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->getAppManager(), $this->getAppDataDir('theming')) new Util($c->getConfig(), $this->getAppManager(), $this->getAppDataDir('theming')),
$this->getAppManager()
); );
} }
return new \OC_Defaults(); return new \OC_Defaults();

View File

@ -166,6 +166,11 @@ class URLGenerator implements IURLGenerator {
// Check if the app is in the app folder // Check if the app is in the app folder
$path = ''; $path = '';
$themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming'); $themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming');
$themingImagePath = false;
if($themingEnabled) {
$themingImagePath = \OC::$server->getThemingDefaults()->replaceImagePath($app, $image);
}
if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) { if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
$path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image"; $path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg") } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg")
@ -181,14 +186,8 @@ class URLGenerator implements IURLGenerator {
} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg") } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
&& file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) { && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
$path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
} elseif($themingEnabled && $image === "favicon.ico" && \OC::$server->getThemingDefaults()->shouldReplaceIcons()) { } elseif($themingEnabled && $themingImagePath) {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); $path = $themingImagePath;
if($app==="") { $app = "core"; }
$path = $this->linkToRoute('theming.Icon.getFavicon', [ 'app' => $app ]) . '?v='. $cacheBusterValue;
} elseif($themingEnabled && $image === "favicon-touch.png" && \OC::$server->getThemingDefaults()->shouldReplaceIcons()) {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
if($app==="") { $app = "core"; }
$path = $this->linkToRoute('theming.Icon.getTouchIcon', [ 'app' => $app ]) . '?v='. $cacheBusterValue;
} elseif ($appPath && file_exists($appPath . "/img/$image")) { } elseif ($appPath && file_exists($appPath . "/img/$image")) {
$path = \OC_App::getAppWebPath($app) . "/img/$image"; $path = \OC_App::getAppWebPath($app) . "/img/$image";
} elseif ($appPath && !file_exists($appPath . "/img/$basename.svg") } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")