Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed icons detection and caching
Signed-off-by: John Molakvoæ (skjnldsv) <[email protected]>
  • Loading branch information
skjnldsv authored and juliusknorr committed Jul 19, 2018
commit 9e5885963c5dc09c20183732f0f94ca01598530e
26 changes: 23 additions & 3 deletions apps/accessibility/lib/Controller/AccessibilityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use OC\Template\SCSSCacher;
use OC\Template\IconsCacher;

class AccessibilityController extends Controller {

Expand Down Expand Up @@ -67,6 +67,9 @@ class AccessibilityController extends Controller {
/** @var IAppManager */
private $appManager;

/** @var IconsCacher */
protected $iconsCacher;

/**
* Account constructor.
*
Expand All @@ -88,7 +91,8 @@ public function __construct(string $appName,
IURLGenerator $urlGenerator,
ITimeFactory $timeFactory,
IUserSession $userSession,
IAppManager $appManager) {
IAppManager $appManager,
IconsCacher $iconsCacher) {
parent::__construct($appName, $request);
$this->appName = $appName;
$this->config = $config;
Expand All @@ -98,6 +102,7 @@ public function __construct(string $appName,
$this->timeFactory = $timeFactory;
$this->userSession = $userSession;
$this->appManager = $appManager;
$this->iconsCacher = $iconsCacher;

$this->serverRoot = \OC::$SERVERROOT;
$this->appRoot = $this->appManager->getAppPath($this->appName);
Expand Down Expand Up @@ -148,7 +153,12 @@ public function getCss(): DataDisplayResponse {

// Rebase all urls
$appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT));
$css = $this->rebaseUrls($css, $appWebRoot . '/css');
$css = $this->rebaseUrls($css, $appWebRoot . '/css');

if ($this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) {
$iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedCSS()->getContent());
$css = $css . $iconsCss;
}

$response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']);

Expand Down Expand Up @@ -200,4 +210,14 @@ private function rebaseUrls(string $css, string $webDir): string {

return preg_replace($re, $subst, $css);
}

/**
* Remove all matches from the $rule regex
*
* @param string $css string to parse
* @return string
*/
private function invertSvgIconsColor(string $css) {
return str_replace(['/000', '/fff'], ['/fff', '/000'], $css);
}
}
14 changes: 4 additions & 10 deletions core/Controller/SvgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\NotFoundException;
use OCP\IRequest;
use OC\Template\IconsCacher;

class SvgController extends Controller {

Expand All @@ -41,18 +40,13 @@ class SvgController extends Controller {
/** @var ITimeFactory */
protected $timeFactory;

/** @var IconsCacher */
protected $iconsCacher;

public function __construct(string $appName,
IRequest $request,
ITimeFactory $timeFactory,
IconsCacher $iconsCacher) {
ITimeFactory $timeFactory) {
parent::__construct($appName, $request);

$this->serverRoot = \OC::$SERVERROOT;
$this->timeFactory = $timeFactory;
$this->iconsCacher = $iconsCacher;
}

/**
Expand All @@ -68,7 +62,7 @@ public function __construct(string $appName,
*/
public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also declare return types ;)

$path = $this->serverRoot . "/core/img/$folder/$fileName.svg";
return $this->getSvg($path, $color);
return $this->getSvg($path, $color, $fileName);
}

/**
Expand All @@ -94,7 +88,7 @@ public function getSvgFromApp(string $app, string $fileName, string $color = 'ff
return new NotFoundResponse();
}
$path = $this->serverRoot . $appPath ."/img/$fileName.svg";
return $this->getSvg($path, $color);
return $this->getSvg($path, $color, $fileName);
}


Expand All @@ -105,7 +99,7 @@ public function getSvgFromApp(string $app, string $fileName, string $color = 'ff
* @param string $color
* @return DataDisplayResponse|NotFoundException
*/
private function getSvg(string $path, string $color) {
private function getSvg(string $path, string $color, string $fileName) {
if (!file_exists($path)) {
return new NotFoundResponse();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getGID() {

public function getDisplayName() {
if (is_null($this->displayName)) {
return $this->gid.'-name';
return $this->gid;
}
return $this->displayName;
}
Expand Down
18 changes: 13 additions & 5 deletions lib/private/Template/IconsCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IconsCacher {
protected $urlGenerator;

/** @var string */
private $iconVarRE = '/--([a-z-]+): url\("([a-z0-9-\/]+)[^;]+;/m';
private $iconVarRE = '/--([a-z0-9-]+): url\(["\']([a-z0-9-\/]+)[^;]+;/m';

/** @var string */
private $fileName = 'icons-vars.css';
Expand Down Expand Up @@ -84,12 +84,17 @@ private function getIconsFromCss(string $css): array{
* @param string $css
*/
public function setIconsCss(string $css) {

try {
$data = $this->folder->getFile($this->fileName)->getContent();
$currentData = $this->folder->getFile($this->fileName)->getContent();
} catch (NotFoundException $e) {
$data = '';
$currentData = '';
}
$icons = $this->getIconsFromCss($data . $css);

// remove :root
$currentData = str_replace([':root {', '}'], '', $currentData);

$icons = $this->getIconsFromCss($currentData . $css);

$data = '';
foreach ($icons as $icon => $url) {
Expand Down Expand Up @@ -127,7 +132,10 @@ public function getCachedCSS() {
public function injectCss() {
// Only inject once
foreach (\OC_Util::$headers as $header) {
if (strpos($header['attributes']['href'], $this->fileName) !== false) {
if (
array_key_exists('attributes', $header) &&
array_key_exists('href', $header['attributes']) &&
strpos($header['attributes']['href'], $this->fileName) !== false) {
return;
}
}
Expand Down