Skip to content

Commit

Permalink
WIP: try to generate user themed icons
Browse files Browse the repository at this point in the history
Signed-off-by: Simon L <szaimen@e.mail.de>
  • Loading branch information
szaimen committed Nov 15, 2022
1 parent 102617f commit 4680a29
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
5 changes: 3 additions & 2 deletions apps/theming/lib/Controller/IconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ 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));
$iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image) . $color);
} 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);
$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image) . $color, $icon);
}
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
$response->cacheFor(86400, false, true);
Expand Down
5 changes: 5 additions & 0 deletions apps/theming/lib/Service/JSDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@
use OCA\Theming\ThemingDefaults;
use OCA\Theming\Util;
use OCP\IConfig;
use OCP\IUserSession;

class JSDataService implements \JsonSerializable {
private ThemingDefaults $themingDefaults;
private Util $util;
private IConfig $appConfig;
private IUserSession $userSession;
private ThemesService $themesService;

public function __construct(
ThemingDefaults $themingDefaults,
Util $util,
IConfig $appConfig,
IUserSession $userSession,
ThemesService $themesService
) {
$this->themingDefaults = $themingDefaults;
$this->util = $util;
$this->appConfig = $appConfig;
$this->userSession = $userSession;
$this->themesService = $themesService;
}

Expand All @@ -60,6 +64,7 @@ public function jsonSerialize(): array {
'privacyUrl' => $this->themingDefaults->getPrivacyUrl(),
'inverted' => $this->util->invertTextColor($this->themingDefaults->getColorPrimary()),
'cacheBuster' => $this->appConfig->getAppValue(Application::APP_ID, 'cachebuster', '0'),
'userCacheBuster' => $this->util->getUserCacheBuster(Application::APP_ID, $this->appConfig, $this->userSession),
'enabledThemes' => $this->themesService->getEnabledThemes(),
];
}
Expand Down
5 changes: 5 additions & 0 deletions apps/theming/lib/ThemingDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ public function replaceImagePath($app, $image) {
}

if ($route) {
$userCacheBusterValue = $this->util->getUserCacheBuster(Application::APP_ID, $this->config, $this->userSession);
$cacheBusterValue = '';
if ($userCacheBusterValue) {
$cacheBusterValue = $userCacheBusterValue;
}
return $route . '?v=' . $cacheBusterValue;
}

Expand Down
11 changes: 11 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,14 @@ public function isBackgroundThemed() {
$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
return $backgroundLogo !== '' && $backgroundLogo !== 'backgroundColor';
}

public function getUserCacheBuster(string $appId, IConfig $config, ?IUserSession $user): string {
$userId = $user->getUser() ? $user->getUser()->getUID() : '';
$cacheBusterValue = '';
if ($userId) {
$userCacheBusterValue = (int)$config->getUserValue($userId, $appId, 'userCacheBuster', '0');
$cacheBusterValue = substr(sha1($userId . '_' . $userCacheBusterValue), 0, 8);
}
return $cacheBusterValue;
}
}
6 changes: 5 additions & 1 deletion core/js/mimetype.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ OC.MimeType = {
path += '.svg';

if(OCA.Theming) {
path += "?v=" + OCA.Theming.cacheBuster;
if (OCA.Theming.userCacheBuster) {
path += "?v=" + OCA.Theming.userCacheBuster;
} else {
path += "?v=" + OCA.Theming.cacheBuster;
}
}

// Cache the result
Expand Down

0 comments on commit 4680a29

Please sign in to comment.