Skip to content

Commit 04b236e

Browse files
szaimencome-nc
andcommitted
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>
1 parent f021172 commit 04b236e

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

apps/theming/lib/Controller/IconController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ public function __construct(
8686
* @throws \Exception
8787
*/
8888
public function getThemedIcon(string $app, string $image): Response {
89+
$color = $this->themingDefaults->getColorPrimary();
8990
try {
90-
$iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image));
91+
$iconFileName = $this->imageManager->getCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image));
9192
} catch (NotFoundException $exception) {
9293
$icon = $this->iconBuilder->colorSvg($app, $image);
9394
if ($icon === false || $icon === '') {
9495
return new NotFoundResponse();
9596
}
96-
$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
97+
$iconFileName = $this->imageManager->setCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image), $icon);
9798
}
98-
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
99+
$response = new FileDisplayResponse($iconFileName, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
99100
$response->cacheFor(86400, false, true);
100101
return $response;
101102
}

apps/theming/lib/Service/JSDataService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function jsonSerialize(): array {
5959
'imprintUrl' => $this->themingDefaults->getImprintUrl(),
6060
'privacyUrl' => $this->themingDefaults->getPrivacyUrl(),
6161
'inverted' => $this->util->invertTextColor($this->themingDefaults->getColorPrimary()),
62-
'cacheBuster' => $this->appConfig->getAppValue(Application::APP_ID, 'cachebuster', '0'),
62+
'cacheBuster' => $this->util->getCacheBuster(),
6363
'enabledThemes' => $this->themesService->getEnabledThemes(),
6464
];
6565
}

apps/theming/lib/Service/ThemeInjectionService.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,30 @@
2424

2525
use OCA\Theming\AppInfo\Application;
2626
use OCA\Theming\Themes\DefaultTheme;
27+
use OCA\Theming\Util;
2728
use OCP\IConfig;
2829
use OCP\IURLGenerator;
2930
use OCP\IUserSession;
30-
use OCP\Util;
3131

3232
class ThemeInjectionService {
3333

3434
private IURLGenerator $urlGenerator;
3535
private ThemesService $themesService;
3636
private DefaultTheme $defaultTheme;
37+
private Util $util;
3738
private IConfig $config;
3839
private ?string $userId;
3940

4041
public function __construct(IURLGenerator $urlGenerator,
4142
ThemesService $themesService,
4243
DefaultTheme $defaultTheme,
44+
Util $util,
4345
IConfig $config,
4446
IUserSession $userSession) {
4547
$this->urlGenerator = $urlGenerator;
4648
$this->themesService = $themesService;
4749
$this->defaultTheme = $defaultTheme;
50+
$this->util = $util;
4851
$this->config = $config;
4952
if ($userSession->getUser() !== null) {
5053
$this->userId = $userSession->getUser()->getUID();
@@ -87,20 +90,12 @@ public function injectHeaders() {
8790
* @param string $media media query to use in the <link> element
8891
*/
8992
private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
90-
$cacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
91-
if ($this->userId !== null) {
92-
// need to bust the cache for the CSS file when the user background changed as its
93-
// URL is served in those files
94-
$userCacheBuster = $this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0');
95-
$cacheBuster .= $this->userId . '_' . $userCacheBuster;
96-
}
97-
9893
$linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
9994
'themeId' => $themeId,
10095
'plain' => $plain,
101-
'v' => substr(sha1($cacheBuster), 0, 8),
96+
'v' => $this->util->getCacheBuster(),
10297
]);
103-
Util::addHeader('link', [
98+
\OCP\Util::addHeader('link', [
10499
'rel' => 'stylesheet',
105100
'media' => $media,
106101
'href' => $linkToCSS,

apps/theming/lib/ThemingDefaults.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public function replaceImagePath($app, $image) {
420420
}
421421

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

426426
return false;

apps/theming/lib/Util.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCP\Files\NotFoundException;
3535
use OCP\Files\SimpleFS\ISimpleFile;
3636
use OCP\IConfig;
37+
use OCP\IUserSession;
3738
use Mexitek\PHPColors\Color;
3839

3940
class Util {
@@ -266,4 +267,20 @@ public function isLogoThemed() {
266267
return $this->imageManager->hasImage('logo')
267268
|| $this->imageManager->hasImage('logoheader');
268269
}
270+
271+
public function getCacheBuster(): string {
272+
$userSession = \OC::$server->get(IUserSession::class);
273+
$userId = '';
274+
$user = $userSession->getUser();
275+
if (!is_null($user)) {
276+
$userId = $user->getUID();
277+
}
278+
$userCacheBuster = '';
279+
if ($userId) {
280+
$userCacheBusterValue = (int)$this->config->getUserValue($userId, 'theming', 'userCacheBuster', '0');
281+
$userCacheBuster = $userId . '_' . $userCacheBusterValue;
282+
}
283+
$systemCacheBuster = $this->config->getAppValue('theming', 'cachebuster', '0');
284+
return substr(sha1($userCacheBuster . $systemCacheBuster), 0, 8);
285+
}
269286
}

apps/theming/tests/ThemingDefaultsTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ public function dataReplaceImagePath() {
868868
}
869869

870870
/** @dataProvider dataReplaceImagePath */
871-
public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=0') {
871+
public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=1234abcd') {
872872
$this->cache->expects($this->any())
873873
->method('get')
874874
->with('shouldReplaceIcons')
@@ -882,6 +882,12 @@ public function testReplaceImagePath($app, $image, $result = 'themingRoute?v=0')
882882
->expects($this->any())
883883
->method('linkToRoute')
884884
->willReturn('themingRoute');
885+
if ($result) {
886+
$this->util
887+
->expects($this->once())
888+
->method('getCacheBuster')
889+
->willReturn('1234abcd');
890+
}
885891
$this->assertEquals($result, $this->template->replaceImagePath($app, $image));
886892
}
887893
}

0 commit comments

Comments
 (0)