@@ -46,267 +46,267 @@ |
||
46 | 46 | |
47 | 47 | class ImageManager { |
48 | 48 | |
49 | - /** @var IConfig */ |
|
50 | - private $config; |
|
51 | - /** @var IAppData */ |
|
52 | - private $appData; |
|
53 | - /** @var IURLGenerator */ |
|
54 | - private $urlGenerator; |
|
55 | - /** @var array */ |
|
56 | - private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon']; |
|
57 | - /** @var ICacheFactory */ |
|
58 | - private $cacheFactory; |
|
59 | - /** @var ILogger */ |
|
60 | - private $logger; |
|
61 | - /** @var ITempManager */ |
|
62 | - private $tempManager; |
|
49 | + /** @var IConfig */ |
|
50 | + private $config; |
|
51 | + /** @var IAppData */ |
|
52 | + private $appData; |
|
53 | + /** @var IURLGenerator */ |
|
54 | + private $urlGenerator; |
|
55 | + /** @var array */ |
|
56 | + private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon']; |
|
57 | + /** @var ICacheFactory */ |
|
58 | + private $cacheFactory; |
|
59 | + /** @var ILogger */ |
|
60 | + private $logger; |
|
61 | + /** @var ITempManager */ |
|
62 | + private $tempManager; |
|
63 | 63 | |
64 | - public function __construct(IConfig $config, |
|
65 | - IAppData $appData, |
|
66 | - IURLGenerator $urlGenerator, |
|
67 | - ICacheFactory $cacheFactory, |
|
68 | - ILogger $logger, |
|
69 | - ITempManager $tempManager |
|
70 | - ) { |
|
71 | - $this->config = $config; |
|
72 | - $this->appData = $appData; |
|
73 | - $this->urlGenerator = $urlGenerator; |
|
74 | - $this->cacheFactory = $cacheFactory; |
|
75 | - $this->logger = $logger; |
|
76 | - $this->tempManager = $tempManager; |
|
77 | - } |
|
64 | + public function __construct(IConfig $config, |
|
65 | + IAppData $appData, |
|
66 | + IURLGenerator $urlGenerator, |
|
67 | + ICacheFactory $cacheFactory, |
|
68 | + ILogger $logger, |
|
69 | + ITempManager $tempManager |
|
70 | + ) { |
|
71 | + $this->config = $config; |
|
72 | + $this->appData = $appData; |
|
73 | + $this->urlGenerator = $urlGenerator; |
|
74 | + $this->cacheFactory = $cacheFactory; |
|
75 | + $this->logger = $logger; |
|
76 | + $this->tempManager = $tempManager; |
|
77 | + } |
|
78 | 78 | |
79 | - public function getImageUrl(string $key, bool $useSvg = true): string { |
|
80 | - $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); |
|
81 | - try { |
|
82 | - $image = $this->getImage($key, $useSvg); |
|
83 | - return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter; |
|
84 | - } catch (NotFoundException $e) { |
|
85 | - } |
|
79 | + public function getImageUrl(string $key, bool $useSvg = true): string { |
|
80 | + $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); |
|
81 | + try { |
|
82 | + $image = $this->getImage($key, $useSvg); |
|
83 | + return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter; |
|
84 | + } catch (NotFoundException $e) { |
|
85 | + } |
|
86 | 86 | |
87 | - switch ($key) { |
|
88 | - case 'logo': |
|
89 | - case 'logoheader': |
|
90 | - case 'favicon': |
|
91 | - return $this->urlGenerator->imagePath('core', 'logo/logo.png') . '?v=' . $cacheBusterCounter; |
|
92 | - case 'background': |
|
93 | - return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter; |
|
94 | - } |
|
95 | - } |
|
87 | + switch ($key) { |
|
88 | + case 'logo': |
|
89 | + case 'logoheader': |
|
90 | + case 'favicon': |
|
91 | + return $this->urlGenerator->imagePath('core', 'logo/logo.png') . '?v=' . $cacheBusterCounter; |
|
92 | + case 'background': |
|
93 | + return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter; |
|
94 | + } |
|
95 | + } |
|
96 | 96 | |
97 | - public function getImageUrlAbsolute(string $key, bool $useSvg = true): string { |
|
98 | - return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key, $useSvg)); |
|
99 | - } |
|
97 | + public function getImageUrlAbsolute(string $key, bool $useSvg = true): string { |
|
98 | + return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key, $useSvg)); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @param string $key |
|
103 | - * @param bool $useSvg |
|
104 | - * @return ISimpleFile |
|
105 | - * @throws NotFoundException |
|
106 | - * @throws NotPermittedException |
|
107 | - */ |
|
108 | - public function getImage(string $key, bool $useSvg = true): ISimpleFile { |
|
109 | - $logo = $this->config->getAppValue('theming', $key . 'Mime', ''); |
|
110 | - $folder = $this->appData->getFolder('images'); |
|
111 | - if ($logo === '' || !$folder->fileExists($key)) { |
|
112 | - throw new NotFoundException(); |
|
113 | - } |
|
114 | - if (!$useSvg && $this->shouldReplaceIcons()) { |
|
115 | - if (!$folder->fileExists($key . '.png')) { |
|
116 | - try { |
|
117 | - $finalIconFile = new \Imagick(); |
|
118 | - $finalIconFile->setBackgroundColor('none'); |
|
119 | - $finalIconFile->readImageBlob($folder->getFile($key)->getContent()); |
|
120 | - $finalIconFile->setImageFormat('png32'); |
|
121 | - $pngFile = $folder->newFile($key . '.png'); |
|
122 | - $pngFile->putContent($finalIconFile->getImageBlob()); |
|
123 | - return $pngFile; |
|
124 | - } catch (\ImagickException $e) { |
|
125 | - $this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: ' . $e->getMessage()); |
|
126 | - } |
|
127 | - } else { |
|
128 | - return $folder->getFile($key . '.png'); |
|
129 | - } |
|
130 | - } |
|
131 | - return $folder->getFile($key); |
|
132 | - } |
|
101 | + /** |
|
102 | + * @param string $key |
|
103 | + * @param bool $useSvg |
|
104 | + * @return ISimpleFile |
|
105 | + * @throws NotFoundException |
|
106 | + * @throws NotPermittedException |
|
107 | + */ |
|
108 | + public function getImage(string $key, bool $useSvg = true): ISimpleFile { |
|
109 | + $logo = $this->config->getAppValue('theming', $key . 'Mime', ''); |
|
110 | + $folder = $this->appData->getFolder('images'); |
|
111 | + if ($logo === '' || !$folder->fileExists($key)) { |
|
112 | + throw new NotFoundException(); |
|
113 | + } |
|
114 | + if (!$useSvg && $this->shouldReplaceIcons()) { |
|
115 | + if (!$folder->fileExists($key . '.png')) { |
|
116 | + try { |
|
117 | + $finalIconFile = new \Imagick(); |
|
118 | + $finalIconFile->setBackgroundColor('none'); |
|
119 | + $finalIconFile->readImageBlob($folder->getFile($key)->getContent()); |
|
120 | + $finalIconFile->setImageFormat('png32'); |
|
121 | + $pngFile = $folder->newFile($key . '.png'); |
|
122 | + $pngFile->putContent($finalIconFile->getImageBlob()); |
|
123 | + return $pngFile; |
|
124 | + } catch (\ImagickException $e) { |
|
125 | + $this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: ' . $e->getMessage()); |
|
126 | + } |
|
127 | + } else { |
|
128 | + return $folder->getFile($key . '.png'); |
|
129 | + } |
|
130 | + } |
|
131 | + return $folder->getFile($key); |
|
132 | + } |
|
133 | 133 | |
134 | - public function getCustomImages(): array { |
|
135 | - $images = []; |
|
136 | - foreach ($this->supportedImageKeys as $key) { |
|
137 | - $images[$key] = [ |
|
138 | - 'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''), |
|
139 | - 'url' => $this->getImageUrl($key), |
|
140 | - ]; |
|
141 | - } |
|
142 | - return $images; |
|
143 | - } |
|
134 | + public function getCustomImages(): array { |
|
135 | + $images = []; |
|
136 | + foreach ($this->supportedImageKeys as $key) { |
|
137 | + $images[$key] = [ |
|
138 | + 'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''), |
|
139 | + 'url' => $this->getImageUrl($key), |
|
140 | + ]; |
|
141 | + } |
|
142 | + return $images; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * Get folder for current theming files |
|
147 | - * |
|
148 | - * @return ISimpleFolder |
|
149 | - * @throws NotPermittedException |
|
150 | - */ |
|
151 | - public function getCacheFolder(): ISimpleFolder { |
|
152 | - $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); |
|
153 | - try { |
|
154 | - $folder = $this->appData->getFolder($cacheBusterValue); |
|
155 | - } catch (NotFoundException $e) { |
|
156 | - $folder = $this->appData->newFolder($cacheBusterValue); |
|
157 | - $this->cleanup(); |
|
158 | - } |
|
159 | - return $folder; |
|
160 | - } |
|
145 | + /** |
|
146 | + * Get folder for current theming files |
|
147 | + * |
|
148 | + * @return ISimpleFolder |
|
149 | + * @throws NotPermittedException |
|
150 | + */ |
|
151 | + public function getCacheFolder(): ISimpleFolder { |
|
152 | + $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); |
|
153 | + try { |
|
154 | + $folder = $this->appData->getFolder($cacheBusterValue); |
|
155 | + } catch (NotFoundException $e) { |
|
156 | + $folder = $this->appData->newFolder($cacheBusterValue); |
|
157 | + $this->cleanup(); |
|
158 | + } |
|
159 | + return $folder; |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * Get a file from AppData |
|
164 | - * |
|
165 | - * @param string $filename |
|
166 | - * @throws NotFoundException |
|
167 | - * @return \OCP\Files\SimpleFS\ISimpleFile |
|
168 | - * @throws NotPermittedException |
|
169 | - */ |
|
170 | - public function getCachedImage(string $filename): ISimpleFile { |
|
171 | - $currentFolder = $this->getCacheFolder(); |
|
172 | - return $currentFolder->getFile($filename); |
|
173 | - } |
|
162 | + /** |
|
163 | + * Get a file from AppData |
|
164 | + * |
|
165 | + * @param string $filename |
|
166 | + * @throws NotFoundException |
|
167 | + * @return \OCP\Files\SimpleFS\ISimpleFile |
|
168 | + * @throws NotPermittedException |
|
169 | + */ |
|
170 | + public function getCachedImage(string $filename): ISimpleFile { |
|
171 | + $currentFolder = $this->getCacheFolder(); |
|
172 | + return $currentFolder->getFile($filename); |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Store a file for theming in AppData |
|
177 | - * |
|
178 | - * @param string $filename |
|
179 | - * @param string $data |
|
180 | - * @return \OCP\Files\SimpleFS\ISimpleFile |
|
181 | - * @throws NotFoundException |
|
182 | - * @throws NotPermittedException |
|
183 | - */ |
|
184 | - public function setCachedImage(string $filename, string $data): ISimpleFile { |
|
185 | - $currentFolder = $this->getCacheFolder(); |
|
186 | - if ($currentFolder->fileExists($filename)) { |
|
187 | - $file = $currentFolder->getFile($filename); |
|
188 | - } else { |
|
189 | - $file = $currentFolder->newFile($filename); |
|
190 | - } |
|
191 | - $file->putContent($data); |
|
192 | - return $file; |
|
193 | - } |
|
175 | + /** |
|
176 | + * Store a file for theming in AppData |
|
177 | + * |
|
178 | + * @param string $filename |
|
179 | + * @param string $data |
|
180 | + * @return \OCP\Files\SimpleFS\ISimpleFile |
|
181 | + * @throws NotFoundException |
|
182 | + * @throws NotPermittedException |
|
183 | + */ |
|
184 | + public function setCachedImage(string $filename, string $data): ISimpleFile { |
|
185 | + $currentFolder = $this->getCacheFolder(); |
|
186 | + if ($currentFolder->fileExists($filename)) { |
|
187 | + $file = $currentFolder->getFile($filename); |
|
188 | + } else { |
|
189 | + $file = $currentFolder->newFile($filename); |
|
190 | + } |
|
191 | + $file->putContent($data); |
|
192 | + return $file; |
|
193 | + } |
|
194 | 194 | |
195 | - public function delete(string $key) { |
|
196 | - /* ignore exceptions, since we don't want to fail hard if something goes wrong during cleanup */ |
|
197 | - try { |
|
198 | - $file = $this->appData->getFolder('images')->getFile($key); |
|
199 | - $file->delete(); |
|
200 | - } catch (NotFoundException $e) { |
|
201 | - } catch (NotPermittedException $e) { |
|
202 | - } |
|
203 | - try { |
|
204 | - $file = $this->appData->getFolder('images')->getFile($key . '.png'); |
|
205 | - $file->delete(); |
|
206 | - } catch (NotFoundException $e) { |
|
207 | - } catch (NotPermittedException $e) { |
|
208 | - } |
|
209 | - } |
|
195 | + public function delete(string $key) { |
|
196 | + /* ignore exceptions, since we don't want to fail hard if something goes wrong during cleanup */ |
|
197 | + try { |
|
198 | + $file = $this->appData->getFolder('images')->getFile($key); |
|
199 | + $file->delete(); |
|
200 | + } catch (NotFoundException $e) { |
|
201 | + } catch (NotPermittedException $e) { |
|
202 | + } |
|
203 | + try { |
|
204 | + $file = $this->appData->getFolder('images')->getFile($key . '.png'); |
|
205 | + $file->delete(); |
|
206 | + } catch (NotFoundException $e) { |
|
207 | + } catch (NotPermittedException $e) { |
|
208 | + } |
|
209 | + } |
|
210 | 210 | |
211 | - public function updateImage(string $key, string $tmpFile) { |
|
212 | - $this->delete($key); |
|
211 | + public function updateImage(string $key, string $tmpFile) { |
|
212 | + $this->delete($key); |
|
213 | 213 | |
214 | - try { |
|
215 | - $folder = $this->appData->getFolder('images'); |
|
216 | - } catch (NotFoundException $e) { |
|
217 | - $folder = $this->appData->newFolder('images'); |
|
218 | - } |
|
214 | + try { |
|
215 | + $folder = $this->appData->getFolder('images'); |
|
216 | + } catch (NotFoundException $e) { |
|
217 | + $folder = $this->appData->newFolder('images'); |
|
218 | + } |
|
219 | 219 | |
220 | - $target = $folder->newFile($key); |
|
221 | - $supportedFormats = $this->getSupportedUploadImageFormats($key); |
|
222 | - $detectedMimeType = mime_content_type($tmpFile); |
|
223 | - if (!in_array($detectedMimeType, $supportedFormats, true)) { |
|
224 | - throw new \Exception('Unsupported image type'); |
|
225 | - } |
|
220 | + $target = $folder->newFile($key); |
|
221 | + $supportedFormats = $this->getSupportedUploadImageFormats($key); |
|
222 | + $detectedMimeType = mime_content_type($tmpFile); |
|
223 | + if (!in_array($detectedMimeType, $supportedFormats, true)) { |
|
224 | + throw new \Exception('Unsupported image type'); |
|
225 | + } |
|
226 | 226 | |
227 | - if ($key === 'background' && strpos($detectedMimeType, 'image/svg') === false && strpos($detectedMimeType, 'image/gif') === false) { |
|
228 | - // Optimize the image since some people may upload images that will be |
|
229 | - // either to big or are not progressive rendering. |
|
230 | - $newImage = @imagecreatefromstring(file_get_contents($tmpFile)); |
|
227 | + if ($key === 'background' && strpos($detectedMimeType, 'image/svg') === false && strpos($detectedMimeType, 'image/gif') === false) { |
|
228 | + // Optimize the image since some people may upload images that will be |
|
229 | + // either to big or are not progressive rendering. |
|
230 | + $newImage = @imagecreatefromstring(file_get_contents($tmpFile)); |
|
231 | 231 | |
232 | - // Preserve transparency |
|
233 | - imagesavealpha($newImage, true); |
|
234 | - imagealphablending($newImage, true); |
|
232 | + // Preserve transparency |
|
233 | + imagesavealpha($newImage, true); |
|
234 | + imagealphablending($newImage, true); |
|
235 | 235 | |
236 | - $tmpFile = $this->tempManager->getTemporaryFile(); |
|
237 | - $newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096); |
|
238 | - $newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth)); |
|
239 | - $outputImage = imagescale($newImage, $newWidth, $newHeight); |
|
236 | + $tmpFile = $this->tempManager->getTemporaryFile(); |
|
237 | + $newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096); |
|
238 | + $newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth)); |
|
239 | + $outputImage = imagescale($newImage, $newWidth, $newHeight); |
|
240 | 240 | |
241 | - imageinterlace($outputImage, 1); |
|
242 | - imagepng($outputImage, $tmpFile, 8); |
|
243 | - imagedestroy($outputImage); |
|
241 | + imageinterlace($outputImage, 1); |
|
242 | + imagepng($outputImage, $tmpFile, 8); |
|
243 | + imagedestroy($outputImage); |
|
244 | 244 | |
245 | - $target->putContent(file_get_contents($tmpFile)); |
|
246 | - } else { |
|
247 | - $target->putContent(file_get_contents($tmpFile)); |
|
248 | - } |
|
245 | + $target->putContent(file_get_contents($tmpFile)); |
|
246 | + } else { |
|
247 | + $target->putContent(file_get_contents($tmpFile)); |
|
248 | + } |
|
249 | 249 | |
250 | - return $detectedMimeType; |
|
251 | - } |
|
250 | + return $detectedMimeType; |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * Returns a list of supported mime types for image uploads. |
|
255 | - * "favicon" images are only allowed to be SVG when imagemagick with SVG support is available. |
|
256 | - * |
|
257 | - * @param string $key The image key, e.g. "favicon" |
|
258 | - * @return array |
|
259 | - */ |
|
260 | - private function getSupportedUploadImageFormats(string $key): array { |
|
261 | - $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; |
|
253 | + /** |
|
254 | + * Returns a list of supported mime types for image uploads. |
|
255 | + * "favicon" images are only allowed to be SVG when imagemagick with SVG support is available. |
|
256 | + * |
|
257 | + * @param string $key The image key, e.g. "favicon" |
|
258 | + * @return array |
|
259 | + */ |
|
260 | + private function getSupportedUploadImageFormats(string $key): array { |
|
261 | + $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; |
|
262 | 262 | |
263 | - if ($key !== 'favicon' || $this->shouldReplaceIcons() === true) { |
|
264 | - $supportedFormats[] = 'image/svg+xml'; |
|
265 | - $supportedFormats[] = 'image/svg'; |
|
266 | - } |
|
263 | + if ($key !== 'favicon' || $this->shouldReplaceIcons() === true) { |
|
264 | + $supportedFormats[] = 'image/svg+xml'; |
|
265 | + $supportedFormats[] = 'image/svg'; |
|
266 | + } |
|
267 | 267 | |
268 | - if ($key === 'favicon') { |
|
269 | - $supportedFormats[] = 'image/x-icon'; |
|
270 | - $supportedFormats[] = 'image/vnd.microsoft.icon'; |
|
271 | - } |
|
268 | + if ($key === 'favicon') { |
|
269 | + $supportedFormats[] = 'image/x-icon'; |
|
270 | + $supportedFormats[] = 'image/vnd.microsoft.icon'; |
|
271 | + } |
|
272 | 272 | |
273 | - return $supportedFormats; |
|
274 | - } |
|
273 | + return $supportedFormats; |
|
274 | + } |
|
275 | 275 | |
276 | - /** |
|
277 | - * remove cached files that are not required any longer |
|
278 | - * |
|
279 | - * @throws NotPermittedException |
|
280 | - * @throws NotFoundException |
|
281 | - */ |
|
282 | - public function cleanup() { |
|
283 | - $currentFolder = $this->getCacheFolder(); |
|
284 | - $folders = $this->appData->getDirectoryListing(); |
|
285 | - foreach ($folders as $folder) { |
|
286 | - if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) { |
|
287 | - $folder->delete(); |
|
288 | - } |
|
289 | - } |
|
290 | - } |
|
276 | + /** |
|
277 | + * remove cached files that are not required any longer |
|
278 | + * |
|
279 | + * @throws NotPermittedException |
|
280 | + * @throws NotFoundException |
|
281 | + */ |
|
282 | + public function cleanup() { |
|
283 | + $currentFolder = $this->getCacheFolder(); |
|
284 | + $folders = $this->appData->getDirectoryListing(); |
|
285 | + foreach ($folders as $folder) { |
|
286 | + if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) { |
|
287 | + $folder->delete(); |
|
288 | + } |
|
289 | + } |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * Check if Imagemagick is enabled and if SVG is supported |
|
294 | - * otherwise we can't render custom icons |
|
295 | - * |
|
296 | - * @return bool |
|
297 | - */ |
|
298 | - public function shouldReplaceIcons() { |
|
299 | - $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl()); |
|
300 | - if ($value = $cache->get('shouldReplaceIcons')) { |
|
301 | - return (bool)$value; |
|
302 | - } |
|
303 | - $value = false; |
|
304 | - if (extension_loaded('imagick')) { |
|
305 | - if (count(\Imagick::queryFormats('SVG')) >= 1) { |
|
306 | - $value = true; |
|
307 | - } |
|
308 | - } |
|
309 | - $cache->set('shouldReplaceIcons', $value); |
|
310 | - return $value; |
|
311 | - } |
|
292 | + /** |
|
293 | + * Check if Imagemagick is enabled and if SVG is supported |
|
294 | + * otherwise we can't render custom icons |
|
295 | + * |
|
296 | + * @return bool |
|
297 | + */ |
|
298 | + public function shouldReplaceIcons() { |
|
299 | + $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl()); |
|
300 | + if ($value = $cache->get('shouldReplaceIcons')) { |
|
301 | + return (bool)$value; |
|
302 | + } |
|
303 | + $value = false; |
|
304 | + if (extension_loaded('imagick')) { |
|
305 | + if (count(\Imagick::queryFormats('SVG')) >= 1) { |
|
306 | + $value = true; |
|
307 | + } |
|
308 | + } |
|
309 | + $cache->set('shouldReplaceIcons', $value); |
|
310 | + return $value; |
|
311 | + } |
|
312 | 312 | } |