Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable25] generate user themed icons #35261

Merged
merged 1 commit into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/theming/lib/Controller/IconController.php
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
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
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
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
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
Expand Up @@ -846,7 +846,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 @@ -860,6 +860,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));
}
}