@@ -35,226 +35,226 @@ |
||
35 | 35 | |
36 | 36 | class IconsCacher { |
37 | 37 | |
38 | - /** @var ILogger */ |
|
39 | - protected $logger; |
|
40 | - |
|
41 | - /** @var IAppData */ |
|
42 | - protected $appData; |
|
43 | - |
|
44 | - /** @var ISimpleFolder */ |
|
45 | - private $folder; |
|
46 | - |
|
47 | - /** @var IURLGenerator */ |
|
48 | - protected $urlGenerator; |
|
49 | - |
|
50 | - /** @var ITimeFactory */ |
|
51 | - protected $timeFactory; |
|
52 | - |
|
53 | - /** @var string */ |
|
54 | - private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+):\s?url\(["\']?([a-zA-Z0-9-_\~\/\.\?\&\=\:\;\+\,]+)[^;]+;/m'; |
|
55 | - |
|
56 | - /** @var string */ |
|
57 | - private $fileName = 'icons-vars.css'; |
|
58 | - |
|
59 | - private $iconList = 'icons-list.template'; |
|
60 | - |
|
61 | - private $cachedCss; |
|
62 | - private $cachedList; |
|
63 | - |
|
64 | - /** |
|
65 | - * @param ILogger $logger |
|
66 | - * @param Factory $appDataFactory |
|
67 | - * @param IURLGenerator $urlGenerator |
|
68 | - * @param ITimeFactory $timeFactory |
|
69 | - * @throws \OCP\Files\NotPermittedException |
|
70 | - */ |
|
71 | - public function __construct(ILogger $logger, |
|
72 | - Factory $appDataFactory, |
|
73 | - IURLGenerator $urlGenerator, |
|
74 | - ITimeFactory $timeFactory) { |
|
75 | - $this->logger = $logger; |
|
76 | - $this->appData = $appDataFactory->get('css'); |
|
77 | - $this->urlGenerator = $urlGenerator; |
|
78 | - $this->timeFactory = $timeFactory; |
|
79 | - |
|
80 | - try { |
|
81 | - $this->folder = $this->appData->getFolder('icons'); |
|
82 | - } catch (NotFoundException $e) { |
|
83 | - $this->folder = $this->appData->newFolder('icons'); |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - private function getIconsFromCss(string $css): array { |
|
88 | - preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER); |
|
89 | - $icons = []; |
|
90 | - foreach ($matches as $icon) { |
|
91 | - $icons[$icon[1]] = $icon[2]; |
|
92 | - } |
|
93 | - |
|
94 | - return $icons; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @param string $css |
|
99 | - * @return string |
|
100 | - * @throws NotFoundException |
|
101 | - * @throws \OCP\Files\NotPermittedException |
|
102 | - */ |
|
103 | - public function setIconsCss(string $css): string { |
|
104 | - |
|
105 | - $cachedFile = $this->getCachedList(); |
|
106 | - if (!$cachedFile) { |
|
107 | - $currentData = ''; |
|
108 | - $cachedFile = $this->folder->newFile($this->iconList); |
|
109 | - } else { |
|
110 | - $currentData = $cachedFile->getContent(); |
|
111 | - } |
|
112 | - |
|
113 | - $cachedVarsCssFile = $this->getCachedCSS(); |
|
114 | - if (!$cachedVarsCssFile) { |
|
115 | - $cachedVarsCssFile = $this->folder->newFile($this->fileName); |
|
116 | - } |
|
117 | - |
|
118 | - $icons = $this->getIconsFromCss($currentData . $css); |
|
119 | - |
|
120 | - $data = ''; |
|
121 | - $list = ''; |
|
122 | - foreach ($icons as $icon => $url) { |
|
123 | - $list .= "--$icon: url('$url');"; |
|
124 | - list($location,$color) = $this->parseUrl($url); |
|
125 | - $svg = false; |
|
126 | - if ($location !== '' && \file_exists($location)) { |
|
127 | - $svg = \file_get_contents($location); |
|
128 | - } |
|
129 | - if ($svg === false) { |
|
130 | - $this->logger->debug('Failed to get icon file ' . $location); |
|
131 | - $data .= "--$icon: url('$url');"; |
|
132 | - continue; |
|
133 | - } |
|
134 | - $encode = base64_encode($this->colorizeSvg($svg, $color)); |
|
135 | - $data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');'; |
|
136 | - } |
|
137 | - |
|
138 | - if (\strlen($data) > 0 && \strlen($list) > 0) { |
|
139 | - $data = ":root {\n$data\n}"; |
|
140 | - $cachedVarsCssFile->putContent($data); |
|
141 | - $list = ":root {\n$list\n}"; |
|
142 | - $cachedFile->putContent($list); |
|
143 | - $this->cachedList = null; |
|
144 | - $this->cachedCss = null; |
|
145 | - } |
|
146 | - |
|
147 | - return preg_replace($this->iconVarRE, '', $css); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param $url |
|
152 | - * @return array |
|
153 | - */ |
|
154 | - private function parseUrl($url): array { |
|
155 | - $location = ''; |
|
156 | - $color = ''; |
|
157 | - $base = $this->getRoutePrefix() . '/svg/'; |
|
158 | - $cleanUrl = \substr($url, \strlen($base)); |
|
159 | - if (\strpos($url, $base . 'core') === 0) { |
|
160 | - $cleanUrl = \substr($cleanUrl, \strlen('core')); |
|
161 | - if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) { |
|
162 | - list(,$cleanUrl,$color) = $matches; |
|
163 | - $location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg'; |
|
164 | - } |
|
165 | - } elseif (\strpos($url, $base) === 0) { |
|
166 | - if(\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) { |
|
167 | - list(,$app,$cleanUrl, $color) = $matches; |
|
168 | - $location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg'; |
|
169 | - if ($app === 'settings') { |
|
170 | - $location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg'; |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - } |
|
175 | - return [ |
|
176 | - $location, |
|
177 | - $color |
|
178 | - ]; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @param $svg |
|
183 | - * @param $color |
|
184 | - * @return string |
|
185 | - */ |
|
186 | - public function colorizeSvg($svg, $color): string { |
|
187 | - if (!preg_match('/^[0-9a-f]{3,6}$/i', $color)) { |
|
188 | - // Prevent not-sane colors from being written into the SVG |
|
189 | - $color = '000'; |
|
190 | - } |
|
191 | - |
|
192 | - // add fill (fill is not present on black elements) |
|
193 | - $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/mi'; |
|
194 | - $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg); |
|
195 | - |
|
196 | - // replace any fill or stroke colors |
|
197 | - $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg); |
|
198 | - $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg); |
|
199 | - return $svg; |
|
200 | - } |
|
201 | - |
|
202 | - private function getRoutePrefix() { |
|
203 | - $frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
204 | - $prefix = \OC::$WEBROOT . '/index.php'; |
|
205 | - if ($frontControllerActive) { |
|
206 | - $prefix = \OC::$WEBROOT; |
|
207 | - } |
|
208 | - return $prefix; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Get icons css file |
|
213 | - * @return ISimpleFile|boolean |
|
214 | - */ |
|
215 | - public function getCachedCSS() { |
|
216 | - try { |
|
217 | - if (!$this->cachedCss) { |
|
218 | - $this->cachedCss = $this->folder->getFile($this->fileName); |
|
219 | - } |
|
220 | - return $this->cachedCss; |
|
221 | - } catch (NotFoundException $e) { |
|
222 | - return false; |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Get icon-vars list template |
|
228 | - * @return ISimpleFile|boolean |
|
229 | - */ |
|
230 | - public function getCachedList() { |
|
231 | - try { |
|
232 | - if (!$this->cachedList) { |
|
233 | - $this->cachedList = $this->folder->getFile($this->iconList); |
|
234 | - } |
|
235 | - return $this->cachedList; |
|
236 | - } catch (NotFoundException $e) { |
|
237 | - return false; |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - public function injectCss() { |
|
242 | - $mtime = $this->timeFactory->getTime(); |
|
243 | - $file = $this->getCachedList(); |
|
244 | - if ($file) { |
|
245 | - $mtime = $file->getMTime(); |
|
246 | - } |
|
247 | - // Only inject once |
|
248 | - foreach (\OC_Util::$headers as $header) { |
|
249 | - if ( |
|
250 | - array_key_exists('attributes', $header) && |
|
251 | - array_key_exists('href', $header['attributes']) && |
|
252 | - strpos($header['attributes']['href'], $this->fileName) !== false) { |
|
253 | - return; |
|
254 | - } |
|
255 | - } |
|
256 | - $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName, 'v' => $mtime]); |
|
257 | - \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true); |
|
258 | - } |
|
38 | + /** @var ILogger */ |
|
39 | + protected $logger; |
|
40 | + |
|
41 | + /** @var IAppData */ |
|
42 | + protected $appData; |
|
43 | + |
|
44 | + /** @var ISimpleFolder */ |
|
45 | + private $folder; |
|
46 | + |
|
47 | + /** @var IURLGenerator */ |
|
48 | + protected $urlGenerator; |
|
49 | + |
|
50 | + /** @var ITimeFactory */ |
|
51 | + protected $timeFactory; |
|
52 | + |
|
53 | + /** @var string */ |
|
54 | + private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+):\s?url\(["\']?([a-zA-Z0-9-_\~\/\.\?\&\=\:\;\+\,]+)[^;]+;/m'; |
|
55 | + |
|
56 | + /** @var string */ |
|
57 | + private $fileName = 'icons-vars.css'; |
|
58 | + |
|
59 | + private $iconList = 'icons-list.template'; |
|
60 | + |
|
61 | + private $cachedCss; |
|
62 | + private $cachedList; |
|
63 | + |
|
64 | + /** |
|
65 | + * @param ILogger $logger |
|
66 | + * @param Factory $appDataFactory |
|
67 | + * @param IURLGenerator $urlGenerator |
|
68 | + * @param ITimeFactory $timeFactory |
|
69 | + * @throws \OCP\Files\NotPermittedException |
|
70 | + */ |
|
71 | + public function __construct(ILogger $logger, |
|
72 | + Factory $appDataFactory, |
|
73 | + IURLGenerator $urlGenerator, |
|
74 | + ITimeFactory $timeFactory) { |
|
75 | + $this->logger = $logger; |
|
76 | + $this->appData = $appDataFactory->get('css'); |
|
77 | + $this->urlGenerator = $urlGenerator; |
|
78 | + $this->timeFactory = $timeFactory; |
|
79 | + |
|
80 | + try { |
|
81 | + $this->folder = $this->appData->getFolder('icons'); |
|
82 | + } catch (NotFoundException $e) { |
|
83 | + $this->folder = $this->appData->newFolder('icons'); |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + private function getIconsFromCss(string $css): array { |
|
88 | + preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER); |
|
89 | + $icons = []; |
|
90 | + foreach ($matches as $icon) { |
|
91 | + $icons[$icon[1]] = $icon[2]; |
|
92 | + } |
|
93 | + |
|
94 | + return $icons; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @param string $css |
|
99 | + * @return string |
|
100 | + * @throws NotFoundException |
|
101 | + * @throws \OCP\Files\NotPermittedException |
|
102 | + */ |
|
103 | + public function setIconsCss(string $css): string { |
|
104 | + |
|
105 | + $cachedFile = $this->getCachedList(); |
|
106 | + if (!$cachedFile) { |
|
107 | + $currentData = ''; |
|
108 | + $cachedFile = $this->folder->newFile($this->iconList); |
|
109 | + } else { |
|
110 | + $currentData = $cachedFile->getContent(); |
|
111 | + } |
|
112 | + |
|
113 | + $cachedVarsCssFile = $this->getCachedCSS(); |
|
114 | + if (!$cachedVarsCssFile) { |
|
115 | + $cachedVarsCssFile = $this->folder->newFile($this->fileName); |
|
116 | + } |
|
117 | + |
|
118 | + $icons = $this->getIconsFromCss($currentData . $css); |
|
119 | + |
|
120 | + $data = ''; |
|
121 | + $list = ''; |
|
122 | + foreach ($icons as $icon => $url) { |
|
123 | + $list .= "--$icon: url('$url');"; |
|
124 | + list($location,$color) = $this->parseUrl($url); |
|
125 | + $svg = false; |
|
126 | + if ($location !== '' && \file_exists($location)) { |
|
127 | + $svg = \file_get_contents($location); |
|
128 | + } |
|
129 | + if ($svg === false) { |
|
130 | + $this->logger->debug('Failed to get icon file ' . $location); |
|
131 | + $data .= "--$icon: url('$url');"; |
|
132 | + continue; |
|
133 | + } |
|
134 | + $encode = base64_encode($this->colorizeSvg($svg, $color)); |
|
135 | + $data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');'; |
|
136 | + } |
|
137 | + |
|
138 | + if (\strlen($data) > 0 && \strlen($list) > 0) { |
|
139 | + $data = ":root {\n$data\n}"; |
|
140 | + $cachedVarsCssFile->putContent($data); |
|
141 | + $list = ":root {\n$list\n}"; |
|
142 | + $cachedFile->putContent($list); |
|
143 | + $this->cachedList = null; |
|
144 | + $this->cachedCss = null; |
|
145 | + } |
|
146 | + |
|
147 | + return preg_replace($this->iconVarRE, '', $css); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param $url |
|
152 | + * @return array |
|
153 | + */ |
|
154 | + private function parseUrl($url): array { |
|
155 | + $location = ''; |
|
156 | + $color = ''; |
|
157 | + $base = $this->getRoutePrefix() . '/svg/'; |
|
158 | + $cleanUrl = \substr($url, \strlen($base)); |
|
159 | + if (\strpos($url, $base . 'core') === 0) { |
|
160 | + $cleanUrl = \substr($cleanUrl, \strlen('core')); |
|
161 | + if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) { |
|
162 | + list(,$cleanUrl,$color) = $matches; |
|
163 | + $location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg'; |
|
164 | + } |
|
165 | + } elseif (\strpos($url, $base) === 0) { |
|
166 | + if(\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) { |
|
167 | + list(,$app,$cleanUrl, $color) = $matches; |
|
168 | + $location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg'; |
|
169 | + if ($app === 'settings') { |
|
170 | + $location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg'; |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + } |
|
175 | + return [ |
|
176 | + $location, |
|
177 | + $color |
|
178 | + ]; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @param $svg |
|
183 | + * @param $color |
|
184 | + * @return string |
|
185 | + */ |
|
186 | + public function colorizeSvg($svg, $color): string { |
|
187 | + if (!preg_match('/^[0-9a-f]{3,6}$/i', $color)) { |
|
188 | + // Prevent not-sane colors from being written into the SVG |
|
189 | + $color = '000'; |
|
190 | + } |
|
191 | + |
|
192 | + // add fill (fill is not present on black elements) |
|
193 | + $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/mi'; |
|
194 | + $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg); |
|
195 | + |
|
196 | + // replace any fill or stroke colors |
|
197 | + $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg); |
|
198 | + $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg); |
|
199 | + return $svg; |
|
200 | + } |
|
201 | + |
|
202 | + private function getRoutePrefix() { |
|
203 | + $frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
204 | + $prefix = \OC::$WEBROOT . '/index.php'; |
|
205 | + if ($frontControllerActive) { |
|
206 | + $prefix = \OC::$WEBROOT; |
|
207 | + } |
|
208 | + return $prefix; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Get icons css file |
|
213 | + * @return ISimpleFile|boolean |
|
214 | + */ |
|
215 | + public function getCachedCSS() { |
|
216 | + try { |
|
217 | + if (!$this->cachedCss) { |
|
218 | + $this->cachedCss = $this->folder->getFile($this->fileName); |
|
219 | + } |
|
220 | + return $this->cachedCss; |
|
221 | + } catch (NotFoundException $e) { |
|
222 | + return false; |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Get icon-vars list template |
|
228 | + * @return ISimpleFile|boolean |
|
229 | + */ |
|
230 | + public function getCachedList() { |
|
231 | + try { |
|
232 | + if (!$this->cachedList) { |
|
233 | + $this->cachedList = $this->folder->getFile($this->iconList); |
|
234 | + } |
|
235 | + return $this->cachedList; |
|
236 | + } catch (NotFoundException $e) { |
|
237 | + return false; |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + public function injectCss() { |
|
242 | + $mtime = $this->timeFactory->getTime(); |
|
243 | + $file = $this->getCachedList(); |
|
244 | + if ($file) { |
|
245 | + $mtime = $file->getMTime(); |
|
246 | + } |
|
247 | + // Only inject once |
|
248 | + foreach (\OC_Util::$headers as $header) { |
|
249 | + if ( |
|
250 | + array_key_exists('attributes', $header) && |
|
251 | + array_key_exists('href', $header['attributes']) && |
|
252 | + strpos($header['attributes']['href'], $this->fileName) !== false) { |
|
253 | + return; |
|
254 | + } |
|
255 | + } |
|
256 | + $linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName, 'v' => $mtime]); |
|
257 | + \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true); |
|
258 | + } |
|
259 | 259 | |
260 | 260 | } |
@@ -35,110 +35,110 @@ |
||
35 | 35 | |
36 | 36 | class SvgController extends Controller { |
37 | 37 | |
38 | - /** @var string */ |
|
39 | - protected $serverRoot; |
|
40 | - |
|
41 | - /** @var ITimeFactory */ |
|
42 | - protected $timeFactory; |
|
43 | - |
|
44 | - /** @var IAppManager */ |
|
45 | - protected $appManager; |
|
46 | - |
|
47 | - /** @var IconsCacher */ |
|
48 | - private $iconsCacher; |
|
49 | - |
|
50 | - public function __construct(string $appName, |
|
51 | - IRequest $request, |
|
52 | - ITimeFactory $timeFactory, |
|
53 | - IAppManager $appManager, |
|
54 | - IconsCacher $iconsCacher) { |
|
55 | - parent::__construct($appName, $request); |
|
56 | - |
|
57 | - $this->serverRoot = \OC::$SERVERROOT; |
|
58 | - $this->timeFactory = $timeFactory; |
|
59 | - $this->appManager = $appManager; |
|
60 | - $this->iconsCacher = $iconsCacher; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * @PublicPage |
|
65 | - * @NoCSRFRequired |
|
66 | - * @NoSameSiteCookieRequired |
|
67 | - * |
|
68 | - * Generate svg from filename with the requested color |
|
69 | - * |
|
70 | - * @param string $folder |
|
71 | - * @param string $fileName |
|
72 | - * @param string $color |
|
73 | - * @return DataDisplayResponse|NotFoundResponse |
|
74 | - */ |
|
75 | - public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') { |
|
76 | - $path = $this->serverRoot . "/core/img/$folder/$fileName.svg"; |
|
77 | - return $this->getSvg($path, $color, $fileName); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @PublicPage |
|
82 | - * @NoCSRFRequired |
|
83 | - * @NoSameSiteCookieRequired |
|
84 | - * |
|
85 | - * Generate svg from filename with the requested color |
|
86 | - * |
|
87 | - * @param string $app |
|
88 | - * @param string $fileName |
|
89 | - * @param string $color |
|
90 | - * @return DataDisplayResponse|NotFoundResponse |
|
91 | - */ |
|
92 | - public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') { |
|
93 | - |
|
94 | - if ($app === 'settings') { |
|
95 | - $path = $this->serverRoot . "/settings/img/$fileName.svg"; |
|
96 | - return $this->getSvg($path, $color, $fileName); |
|
97 | - } |
|
98 | - |
|
99 | - $appRootPath = $this->appManager->getAppPath($app); |
|
100 | - $appPath = substr($appRootPath, strlen($this->serverRoot)); |
|
101 | - |
|
102 | - if (!$appPath) { |
|
103 | - return new NotFoundResponse(); |
|
104 | - } |
|
105 | - $path = $this->serverRoot . $appPath ."/img/$fileName.svg"; |
|
106 | - return $this->getSvg($path, $color, $fileName); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Generate svg from filename with the requested color |
|
111 | - * |
|
112 | - * @param string $path |
|
113 | - * @param string $color |
|
114 | - * @param string $fileName |
|
115 | - * @return DataDisplayResponse|NotFoundResponse |
|
116 | - */ |
|
117 | - private function getSvg(string $path, string $color, string $fileName) { |
|
118 | - if (!file_exists($path)) { |
|
119 | - return new NotFoundResponse(); |
|
120 | - } |
|
121 | - |
|
122 | - $svg = file_get_contents($path); |
|
123 | - |
|
124 | - if ($svg === null) { |
|
125 | - return new NotFoundResponse(); |
|
126 | - } |
|
127 | - |
|
128 | - $svg = $this->iconsCacher->colorizeSvg($svg, $color); |
|
129 | - |
|
130 | - $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); |
|
131 | - |
|
132 | - // Set cache control |
|
133 | - $ttl = 31536000; |
|
134 | - $response->cacheFor($ttl); |
|
135 | - $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"'); |
|
136 | - $expires = new \DateTime(); |
|
137 | - $expires->setTimestamp($this->timeFactory->getTime()); |
|
138 | - $expires->add(new \DateInterval('PT' . $ttl . 'S')); |
|
139 | - $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
140 | - $response->addHeader('Pragma', 'cache'); |
|
141 | - |
|
142 | - return $response; |
|
143 | - } |
|
38 | + /** @var string */ |
|
39 | + protected $serverRoot; |
|
40 | + |
|
41 | + /** @var ITimeFactory */ |
|
42 | + protected $timeFactory; |
|
43 | + |
|
44 | + /** @var IAppManager */ |
|
45 | + protected $appManager; |
|
46 | + |
|
47 | + /** @var IconsCacher */ |
|
48 | + private $iconsCacher; |
|
49 | + |
|
50 | + public function __construct(string $appName, |
|
51 | + IRequest $request, |
|
52 | + ITimeFactory $timeFactory, |
|
53 | + IAppManager $appManager, |
|
54 | + IconsCacher $iconsCacher) { |
|
55 | + parent::__construct($appName, $request); |
|
56 | + |
|
57 | + $this->serverRoot = \OC::$SERVERROOT; |
|
58 | + $this->timeFactory = $timeFactory; |
|
59 | + $this->appManager = $appManager; |
|
60 | + $this->iconsCacher = $iconsCacher; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * @PublicPage |
|
65 | + * @NoCSRFRequired |
|
66 | + * @NoSameSiteCookieRequired |
|
67 | + * |
|
68 | + * Generate svg from filename with the requested color |
|
69 | + * |
|
70 | + * @param string $folder |
|
71 | + * @param string $fileName |
|
72 | + * @param string $color |
|
73 | + * @return DataDisplayResponse|NotFoundResponse |
|
74 | + */ |
|
75 | + public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') { |
|
76 | + $path = $this->serverRoot . "/core/img/$folder/$fileName.svg"; |
|
77 | + return $this->getSvg($path, $color, $fileName); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @PublicPage |
|
82 | + * @NoCSRFRequired |
|
83 | + * @NoSameSiteCookieRequired |
|
84 | + * |
|
85 | + * Generate svg from filename with the requested color |
|
86 | + * |
|
87 | + * @param string $app |
|
88 | + * @param string $fileName |
|
89 | + * @param string $color |
|
90 | + * @return DataDisplayResponse|NotFoundResponse |
|
91 | + */ |
|
92 | + public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') { |
|
93 | + |
|
94 | + if ($app === 'settings') { |
|
95 | + $path = $this->serverRoot . "/settings/img/$fileName.svg"; |
|
96 | + return $this->getSvg($path, $color, $fileName); |
|
97 | + } |
|
98 | + |
|
99 | + $appRootPath = $this->appManager->getAppPath($app); |
|
100 | + $appPath = substr($appRootPath, strlen($this->serverRoot)); |
|
101 | + |
|
102 | + if (!$appPath) { |
|
103 | + return new NotFoundResponse(); |
|
104 | + } |
|
105 | + $path = $this->serverRoot . $appPath ."/img/$fileName.svg"; |
|
106 | + return $this->getSvg($path, $color, $fileName); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Generate svg from filename with the requested color |
|
111 | + * |
|
112 | + * @param string $path |
|
113 | + * @param string $color |
|
114 | + * @param string $fileName |
|
115 | + * @return DataDisplayResponse|NotFoundResponse |
|
116 | + */ |
|
117 | + private function getSvg(string $path, string $color, string $fileName) { |
|
118 | + if (!file_exists($path)) { |
|
119 | + return new NotFoundResponse(); |
|
120 | + } |
|
121 | + |
|
122 | + $svg = file_get_contents($path); |
|
123 | + |
|
124 | + if ($svg === null) { |
|
125 | + return new NotFoundResponse(); |
|
126 | + } |
|
127 | + |
|
128 | + $svg = $this->iconsCacher->colorizeSvg($svg, $color); |
|
129 | + |
|
130 | + $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); |
|
131 | + |
|
132 | + // Set cache control |
|
133 | + $ttl = 31536000; |
|
134 | + $response->cacheFor($ttl); |
|
135 | + $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"'); |
|
136 | + $expires = new \DateTime(); |
|
137 | + $expires->setTimestamp($this->timeFactory->getTime()); |
|
138 | + $expires->add(new \DateInterval('PT' . $ttl . 'S')); |
|
139 | + $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
140 | + $response->addHeader('Pragma', 'cache'); |
|
141 | + |
|
142 | + return $response; |
|
143 | + } |
|
144 | 144 | } |