mirror of
https://github.com/nextcloud/server.git
synced 2025-03-13 16:03:55 +00:00
generate user themed icons
Signed-off-by: Simon L <szaimen@e.mail.de> Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com> Co-Authored-By: Côme Chilliet <91878298+come-nc@users.noreply.github.com>
This commit is contained in:
parent
f021172eab
commit
04b236e320
6 changed files with 36 additions and 17 deletions
apps/theming
|
@ -86,16 +86,17 @@ class IconController extends Controller {
|
|||
* @throws \Exception
|
||||
*/
|
||||
public function getThemedIcon(string $app, string $image): Response {
|
||||
$color = $this->themingDefaults->getColorPrimary();
|
||||
try {
|
||||
$iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image));
|
||||
$iconFileName = $this->imageManager->getCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image));
|
||||
} catch (NotFoundException $exception) {
|
||||
$icon = $this->iconBuilder->colorSvg($app, $image);
|
||||
if ($icon === false || $icon === '') {
|
||||
return new NotFoundResponse();
|
||||
}
|
||||
$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
|
||||
$iconFileName = $this->imageManager->setCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image), $icon);
|
||||
}
|
||||
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
||||
$response = new FileDisplayResponse($iconFileName, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
||||
$response->cacheFor(86400, false, true);
|
||||
return $response;
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ class JSDataService implements \JsonSerializable {
|
|||
'imprintUrl' => $this->themingDefaults->getImprintUrl(),
|
||||
'privacyUrl' => $this->themingDefaults->getPrivacyUrl(),
|
||||
'inverted' => $this->util->invertTextColor($this->themingDefaults->getColorPrimary()),
|
||||
'cacheBuster' => $this->appConfig->getAppValue(Application::APP_ID, 'cachebuster', '0'),
|
||||
'cacheBuster' => $this->util->getCacheBuster(),
|
||||
'enabledThemes' => $this->themesService->getEnabledThemes(),
|
||||
];
|
||||
}
|
||||
|
|
|
@ -24,27 +24,30 @@ namespace OCA\Theming\Service;
|
|||
|
||||
use OCA\Theming\AppInfo\Application;
|
||||
use OCA\Theming\Themes\DefaultTheme;
|
||||
use OCA\Theming\Util;
|
||||
use OCP\IConfig;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUserSession;
|
||||
use OCP\Util;
|
||||
|
||||
class ThemeInjectionService {
|
||||
|
||||
private IURLGenerator $urlGenerator;
|
||||
private ThemesService $themesService;
|
||||
private DefaultTheme $defaultTheme;
|
||||
private Util $util;
|
||||
private IConfig $config;
|
||||
private ?string $userId;
|
||||
|
||||
public function __construct(IURLGenerator $urlGenerator,
|
||||
ThemesService $themesService,
|
||||
DefaultTheme $defaultTheme,
|
||||
Util $util,
|
||||
IConfig $config,
|
||||
IUserSession $userSession) {
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->themesService = $themesService;
|
||||
$this->defaultTheme = $defaultTheme;
|
||||
$this->util = $util;
|
||||
$this->config = $config;
|
||||
if ($userSession->getUser() !== null) {
|
||||
$this->userId = $userSession->getUser()->getUID();
|
||||
|
@ -87,20 +90,12 @@ class ThemeInjectionService {
|
|||
* @param string $media media query to use in the <link> element
|
||||
*/
|
||||
private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
|
||||
$cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
|
||||
if ($this->userId !== null) {
|
||||
// need to bust the cache for the CSS file when the user background changed as its
|
||||
// URL is served in those files
|
||||
$userCacheBuster = $this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
|
||||
$cacheBuster .= $this->userId . '_' . $userCacheBuster;
|
||||
}
|
||||
|
||||
$linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
|
||||
'themeId' => $themeId,
|
||||
'plain' => $plain,
|
||||
'v' => substr(sha1($cacheBuster), 0, 8),
|
||||
'v' => $this->util->getCacheBuster(),
|
||||
]);
|
||||
Util::addHeader('link', [
|
||||
\OCP\Util::addHeader('link', [
|
||||
'rel' => 'stylesheet',
|
||||
'media' => $media,
|
||||
'href' => $linkToCSS,
|
||||
|
|
|
@ -420,7 +420,7 @@ class ThemingDefaults extends \OC_Defaults {
|
|||
}
|
||||
|
||||
if ($route) {
|
||||
return $route . '?v=' . $cacheBusterValue;
|
||||
return $route . '?v=' . $this->util->getCacheBuster();
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
|
@ -34,6 +34,7 @@ use OCP\Files\IAppData;
|
|||
use OCP\Files\NotFoundException;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
use OCP\IConfig;
|
||||
use OCP\IUserSession;
|
||||
use Mexitek\PHPColors\Color;
|
||||
|
||||
class Util {
|
||||
|
@ -266,4 +267,20 @@ class Util {
|
|||
return $this->imageManager->hasImage('logo')
|
||||
|| $this->imageManager->hasImage('logoheader');
|
||||
}
|
||||
|
||||
public function getCacheBuster(): string {
|
||||
$userSession = \OC::$server->get(IUserSession::class);
|
||||
$userId = '';
|
||||
$user = $userSession->getUser();
|
||||
if (!is_null($user)) {
|
||||
$userId = $user->getUID();
|
||||
}
|
||||
$userCacheBuster = '';
|
||||
if ($userId) {
|
||||
$userCacheBusterValue = (int)$this->config->getUserValue($userId, 'theming', 'userCacheBuster', '0');
|
||||
$userCacheBuster = $userId . '_' . $userCacheBusterValue;
|
||||
}
|
||||
$systemCacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
|
||||
return substr(sha1($userCacheBuster . $systemCacheBuster), 0, 8);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -868,7 +868,7 @@ class ThemingDefaultsTest extends TestCase {
|
|||
}
|
||||
|
||||
/** @dataProvider dataReplaceImagePath */
|
||||
public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=0') {
|
||||
public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=1234abcd') {
|
||||
$this->cache->expects($this->any())
|
||||
->method('get')
|
||||
->with('shouldReplaceIcons')
|
||||
|
@ -882,6 +882,12 @@ class ThemingDefaultsTest extends TestCase {
|
|||
->expects($this->any())
|
||||
->method('linkToRoute')
|
||||
->willReturn('themingRoute');
|
||||
if ($result) {
|
||||
$this->util
|
||||
->expects($this->once())
|
||||
->method('getCacheBuster')
|
||||
->willReturn('1234abcd');
|
||||
}
|
||||
$this->assertEquals($result, $this->template->replaceImagePath($app, $image));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue