Skip to content

Commit 9880e58

Browse files
committed
Improve unit tests for image generation
1 parent 5d77830 commit 9880e58

File tree

11 files changed

+202
-150
lines changed

11 files changed

+202
-150
lines changed

apps/theming/lib/Controller/IconController.php

Lines changed: 4 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class IconController extends Controller {
5353
/** @var IRootFolder */
5454
private $rootFolder;
5555

56+
5657
/**
5758
* IconController constructor.
5859
*
@@ -94,10 +95,10 @@ public function __construct(
9495
* @return StreamResponse|DataResponse
9596
*/
9697
public function getThemedIcon($app, $image) {
97-
$image = $this->getAppImage($app, $image);
98+
$image = $this->util->getAppImage($app, $image);
9899
$svg = file_get_contents($image);
99100
$color = $this->util->elementColor($this->themingDefaults->getMailHeaderColor());
100-
$svg = $this->colorizeSvg($svg, $color);
101+
$svg = $this->util->colorizeSvg($svg, $color);
101102

102103
$response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
103104
$response->cacheFor(86400);
@@ -152,20 +153,15 @@ public function getTouchIcon($app="core") {
152153
* @return Imagick
153154
*/
154155
private function renderAppIcon($app) {
155-
$appIcon = $this->getAppIcon($app);
156+
$appIcon = $this->util->getAppIcon($app);
156157
$color = $this->themingDefaults->getMailHeaderColor();
157158
$mime = mime_content_type($appIcon);
158-
// FIXME: test if we need this
159-
if ($color === "") {
160-
$color = '#0082c9';
161-
}
162159
// generate background image with rounded corners
163160
$background = '<?xml version="1.0" encoding="UTF-8"?>' .
164161
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:cc="http://creativecommons.org/ns#" width="512" height="512" xmlns:xlink="http://www.w3.org/1999/xlink">' .
165162
'<rect x="0" y="0" rx="75" ry="75" width="512" height="512" style="fill:' . $color . ';" />' .
166163
'</svg>';
167164

168-
169165
// resize svg magic as this seems broken in Imagemagick
170166
if($mime === "image/svg+xml") {
171167
$svg = file_get_contents($appIcon);
@@ -214,76 +210,6 @@ private function renderAppIcon($app) {
214210
return $finalIconFile;
215211
}
216212

217-
/**
218-
* @param $app app name
219-
* @return string path to app icon / logo
220-
*/
221-
private function getAppIcon($app) {
222-
$appPath = \OC_App::getAppPath($app);
223213

224-
$icon = $appPath . '/img/' . $app . '.svg';
225-
if(file_exists($icon)) {
226-
return $icon;
227-
}
228-
$icon = $appPath . '/img/app.svg';
229-
if(file_exists($icon)) {
230-
return $icon;
231-
}
232-
233-
if($this->rootFolder->nodeExists('/themedinstancelogo')) {
234-
return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/') . '/themedinstancelogo';
235-
}
236-
return \OC::$SERVERROOT . '/core/img/logo.svg';
237-
}
238-
239-
/**
240-
* @param $app app name
241-
* @param $image relative path to image in app folder
242-
* @return string absolute path to image
243-
*/
244-
private function getAppImage($app, $image) {
245-
$appPath = \OC_App::getAppPath($app);
246-
247-
if($app==="core") {
248-
$icon = \OC::$SERVERROOT . '/core/img/' . $image;
249-
if(file_exists($icon)) {
250-
return $icon;
251-
}
252-
}
253-
254-
$icon = $appPath . '/img/' . $image;
255-
if(file_exists($icon)) {
256-
return $icon;
257-
}
258-
$icon = $appPath . '/img/' . $image . '.svg';
259-
if(file_exists($icon)) {
260-
return $icon;
261-
}
262-
$icon = $appPath . '/img/' . $image . '.png';
263-
if(file_exists($icon)) {
264-
return $icon;
265-
}
266-
$icon = $appPath . '/img/' . $image . '.gif';
267-
if(file_exists($icon)) {
268-
return $icon;
269-
}
270-
$icon = $appPath . '/img/' . $image . '.jpg';
271-
if(file_exists($icon)) {
272-
return $icon;
273-
}
274-
return false;
275-
}
276-
277-
/**
278-
* replace default color with a custom one
279-
*
280-
* @param $svg content of a svg file
281-
* @param $color color to match
282-
* @return string
283-
*/
284-
private function colorizeSvg($svg, $color) {
285-
$svg = preg_replace('/#0082c9/i', $color, $svg);
286-
return $svg;
287-
}
288214

289215
}

apps/theming/lib/Util.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@
2323

2424
namespace OCA\Theming;
2525

26+
use OCP\IConfig;
27+
use OCP\Files\IRootFolder;
28+
2629
class Util {
2730

31+
private $config;
32+
private $rootFolder;
33+
34+
public function __construct(IConfig $config, IRootFolder $rootFolder) {
35+
$this->config = $config;
36+
$this->rootFolder = $rootFolder;
37+
}
38+
2839
/**
2940
* @param string $color rgb color value
3041
* @return bool
@@ -81,4 +92,77 @@ public function generateRadioButton($color) {
8192
return base64_encode($radioButtonIcon);
8293
}
8394

95+
96+
/**
97+
* @param $app app name
98+
* @return string path to app icon / logo
99+
*/
100+
public function getAppIcon($app) {
101+
$appPath = \OC_App::getAppPath($app);
102+
103+
$icon = $appPath . '/img/' . $app . '.svg';
104+
if(file_exists($icon)) {
105+
return $icon;
106+
}
107+
$icon = $appPath . '/img/app.svg';
108+
if(file_exists($icon)) {
109+
return $icon;
110+
}
111+
112+
if($this->config->getAppValue('theming', 'logoMime', '') !== '' && $this->rootFolder->nodeExists('/themedinstancelogo')) {
113+
return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/') . '/themedinstancelogo';
114+
}
115+
return \OC::$SERVERROOT . '/core/img/logo.svg';
116+
}
117+
118+
/**
119+
* @param $app app name
120+
* @param $image relative path to image in app folder
121+
* @return string absolute path to image
122+
*/
123+
public function getAppImage($app, $image) {
124+
$appPath = \OC_App::getAppPath($app);
125+
126+
if($app==="core") {
127+
$icon = \OC::$SERVERROOT . '/core/img/' . $image;
128+
if(file_exists($icon)) {
129+
return $icon;
130+
}
131+
}
132+
133+
$icon = $appPath . '/img/' . $image;
134+
if(file_exists($icon)) {
135+
return $icon;
136+
}
137+
$icon = $appPath . '/img/' . $image . '.svg';
138+
if(file_exists($icon)) {
139+
return $icon;
140+
}
141+
$icon = $appPath . '/img/' . $image . '.png';
142+
if(file_exists($icon)) {
143+
return $icon;
144+
}
145+
$icon = $appPath . '/img/' . $image . '.gif';
146+
if(file_exists($icon)) {
147+
return $icon;
148+
}
149+
$icon = $appPath . '/img/' . $image . '.jpg';
150+
if(file_exists($icon)) {
151+
return $icon;
152+
}
153+
return false;
154+
}
155+
156+
/**
157+
* replace default color with a custom one
158+
*
159+
* @param $svg content of a svg file
160+
* @param $color color to match
161+
* @return string
162+
*/
163+
public function colorizeSvg($svg, $color) {
164+
$svg = preg_replace('/#0082c9/i', $color, $svg);
165+
return $svg;
166+
}
167+
84168
}

0 commit comments

Comments
 (0)