@@ -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 | - public function __construct(string $appName, |
|
48 | - IRequest $request, |
|
49 | - ITimeFactory $timeFactory, |
|
50 | - IAppManager $appManager) { |
|
51 | - parent::__construct($appName, $request); |
|
52 | - |
|
53 | - $this->serverRoot = \OC::$SERVERROOT; |
|
54 | - $this->timeFactory = $timeFactory; |
|
55 | - $this->appManager = $appManager; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @PublicPage |
|
60 | - * @NoCSRFRequired |
|
61 | - * |
|
62 | - * Generate svg from filename with the requested color |
|
63 | - * |
|
64 | - * @param string $folder |
|
65 | - * @param string $fileName |
|
66 | - * @param string $color |
|
67 | - * @return DataDisplayResponse|NotFoundResponse |
|
68 | - */ |
|
69 | - public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') { |
|
70 | - $path = $this->serverRoot . "/core/img/$folder/$fileName.svg"; |
|
71 | - return $this->getSvg($path, $color, $fileName); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @PublicPage |
|
76 | - * @NoCSRFRequired |
|
77 | - * |
|
78 | - * Generate svg from filename with the requested color |
|
79 | - * |
|
80 | - * @param string $app |
|
81 | - * @param string $fileName |
|
82 | - * @param string $color |
|
83 | - * @return DataDisplayResponse|NotFoundResponse |
|
84 | - */ |
|
85 | - public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') { |
|
86 | - |
|
87 | - if ($app === 'settings') { |
|
88 | - $path = $this->serverRoot . "/settings/img/$fileName.svg"; |
|
89 | - return $this->getSvg($path, $color, $fileName); |
|
90 | - } |
|
91 | - |
|
92 | - $appRootPath = $this->appManager->getAppPath($app); |
|
93 | - $appPath = substr($appRootPath, strlen($this->serverRoot)); |
|
38 | + /** @var string */ |
|
39 | + protected $serverRoot; |
|
40 | + |
|
41 | + /** @var ITimeFactory */ |
|
42 | + protected $timeFactory; |
|
43 | + |
|
44 | + /** @var IAppManager */ |
|
45 | + protected $appManager; |
|
46 | + |
|
47 | + public function __construct(string $appName, |
|
48 | + IRequest $request, |
|
49 | + ITimeFactory $timeFactory, |
|
50 | + IAppManager $appManager) { |
|
51 | + parent::__construct($appName, $request); |
|
52 | + |
|
53 | + $this->serverRoot = \OC::$SERVERROOT; |
|
54 | + $this->timeFactory = $timeFactory; |
|
55 | + $this->appManager = $appManager; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @PublicPage |
|
60 | + * @NoCSRFRequired |
|
61 | + * |
|
62 | + * Generate svg from filename with the requested color |
|
63 | + * |
|
64 | + * @param string $folder |
|
65 | + * @param string $fileName |
|
66 | + * @param string $color |
|
67 | + * @return DataDisplayResponse|NotFoundResponse |
|
68 | + */ |
|
69 | + public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') { |
|
70 | + $path = $this->serverRoot . "/core/img/$folder/$fileName.svg"; |
|
71 | + return $this->getSvg($path, $color, $fileName); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @PublicPage |
|
76 | + * @NoCSRFRequired |
|
77 | + * |
|
78 | + * Generate svg from filename with the requested color |
|
79 | + * |
|
80 | + * @param string $app |
|
81 | + * @param string $fileName |
|
82 | + * @param string $color |
|
83 | + * @return DataDisplayResponse|NotFoundResponse |
|
84 | + */ |
|
85 | + public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') { |
|
86 | + |
|
87 | + if ($app === 'settings') { |
|
88 | + $path = $this->serverRoot . "/settings/img/$fileName.svg"; |
|
89 | + return $this->getSvg($path, $color, $fileName); |
|
90 | + } |
|
91 | + |
|
92 | + $appRootPath = $this->appManager->getAppPath($app); |
|
93 | + $appPath = substr($appRootPath, strlen($this->serverRoot)); |
|
94 | 94 | |
95 | - if (!$appPath) { |
|
96 | - return new NotFoundResponse(); |
|
97 | - } |
|
98 | - $path = $this->serverRoot . $appPath ."/img/$fileName.svg"; |
|
99 | - return $this->getSvg($path, $color, $fileName); |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Generate svg from filename with the requested color |
|
105 | - * |
|
106 | - * @param string $path |
|
107 | - * @param string $color |
|
108 | - * @return DataDisplayResponse|NotFoundResponse |
|
109 | - */ |
|
110 | - private function getSvg(string $path, string $color, string $fileName) { |
|
111 | - if (!file_exists($path)) { |
|
112 | - return new NotFoundResponse(); |
|
113 | - } |
|
114 | - |
|
115 | - $svg = file_get_contents($path); |
|
116 | - |
|
117 | - if (is_null($svg)) { |
|
118 | - return new NotFoundResponse(); |
|
119 | - } |
|
120 | - |
|
121 | - // add fill (fill is not present on black elements) |
|
122 | - $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi'; |
|
123 | - |
|
124 | - $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg); |
|
125 | - |
|
126 | - // replace any fill or stroke colors |
|
127 | - $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg); |
|
128 | - $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg); |
|
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 | - } |
|
95 | + if (!$appPath) { |
|
96 | + return new NotFoundResponse(); |
|
97 | + } |
|
98 | + $path = $this->serverRoot . $appPath ."/img/$fileName.svg"; |
|
99 | + return $this->getSvg($path, $color, $fileName); |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Generate svg from filename with the requested color |
|
105 | + * |
|
106 | + * @param string $path |
|
107 | + * @param string $color |
|
108 | + * @return DataDisplayResponse|NotFoundResponse |
|
109 | + */ |
|
110 | + private function getSvg(string $path, string $color, string $fileName) { |
|
111 | + if (!file_exists($path)) { |
|
112 | + return new NotFoundResponse(); |
|
113 | + } |
|
114 | + |
|
115 | + $svg = file_get_contents($path); |
|
116 | + |
|
117 | + if (is_null($svg)) { |
|
118 | + return new NotFoundResponse(); |
|
119 | + } |
|
120 | + |
|
121 | + // add fill (fill is not present on black elements) |
|
122 | + $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi'; |
|
123 | + |
|
124 | + $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg); |
|
125 | + |
|
126 | + // replace any fill or stroke colors |
|
127 | + $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg); |
|
128 | + $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg); |
|
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 | } |