Skip to content

Commit

Permalink
Merge pull request #35182 from nextcloud/enh/34654/try-go-generate-us…
Browse files Browse the repository at this point in the history
…er-themed-icons

generate user themed icons
  • Loading branch information
szaimen authored Nov 18, 2022
2 parents 09e55c7 + 04b236e commit 8e80f04
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
7 changes: 4 additions & 3 deletions apps/theming/lib/Controller/IconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,17 @@ public function __construct(
* @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;
}
Expand Down
2 changes: 1 addition & 1 deletion apps/theming/lib/Service/JSDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function jsonSerialize(): array {
'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(),
];
}
Expand Down
17 changes: 6 additions & 11 deletions apps/theming/lib/Service/ThemeInjectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,30 @@

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();
Expand Down Expand Up @@ -87,20 +90,12 @@ public function injectHeaders() {
* @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,
Expand Down
2 changes: 1 addition & 1 deletion apps/theming/lib/ThemingDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public function replaceImagePath($app, $image) {
}

if ($route) {
return $route . '?v=' . $cacheBusterValue;
return $route . '?v=' . $this->util->getCacheBuster();
}

return false;
Expand Down
17 changes: 17 additions & 0 deletions apps/theming/lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IUserSession;
use Mexitek\PHPColors\Color;

class Util {
Expand Down Expand Up @@ -266,4 +267,20 @@ public function isLogoThemed() {
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);
}
}
8 changes: 7 additions & 1 deletion apps/theming/tests/ThemingDefaultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ public function dataReplaceImagePath() {
}

/** @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')
Expand All @@ -882,6 +882,12 @@ public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=0')
->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));
}
}

0 comments on commit 8e80f04

Please sign in to comment.