Completed
Push — master ( dc037d...3c4251 )
by Roeland
23:27 queued 11:42
created
apps/theming/lib/ImageManager.php 2 patches
Indentation   +207 added lines, -207 removed lines patch added patch discarded remove patch
@@ -36,211 +36,211 @@
 block discarded – undo
36 36
 
37 37
 class ImageManager {
38 38
 
39
-	/** @var IConfig */
40
-	private $config;
41
-	/** @var IAppData */
42
-	private $appData;
43
-	/** @var IURLGenerator */
44
-	private $urlGenerator;
45
-	/** @var array */
46
-	private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
47
-	/** @var ICacheFactory */
48
-	private $cacheFactory;
49
-	/** @var ILogger */
50
-	private $logger;
51
-
52
-	/**
53
-	 * ImageManager constructor.
54
-	 *
55
-	 * @param IConfig $config
56
-	 * @param IAppData $appData
57
-	 * @param IURLGenerator $urlGenerator
58
-	 * @param ICacheFactory $cacheFactory
59
-	 * @param ILogger $logger
60
-	 */
61
-	public function __construct(IConfig $config,
62
-								IAppData $appData,
63
-								IURLGenerator $urlGenerator,
64
-								ICacheFactory $cacheFactory,
65
-								ILogger $logger
66
-	) {
67
-		$this->config = $config;
68
-		$this->appData = $appData;
69
-		$this->urlGenerator = $urlGenerator;
70
-		$this->cacheFactory = $cacheFactory;
71
-		$this->logger = $logger;
72
-	}
73
-
74
-	public function getImageUrl(string $key, bool $useSvg = true): string {
75
-		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
76
-		try {
77
-			$image = $this->getImage($key, $useSvg);
78
-			return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter;
79
-		} catch (NotFoundException $e) {
80
-		}
81
-
82
-		switch ($key) {
83
-			case 'logo':
84
-			case 'logoheader':
85
-			case 'favicon':
86
-				return $this->urlGenerator->imagePath('core', 'logo.png') . '?v=' . $cacheBusterCounter;
87
-			case 'background':
88
-				return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter;
89
-		}
90
-	}
91
-
92
-	public function getImageUrlAbsolute(string $key, bool $useSvg = true): string {
93
-		return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key, $useSvg));
94
-	}
95
-
96
-	/**
97
-	 * @param string $key
98
-	 * @param bool $useSvg
99
-	 * @return ISimpleFile
100
-	 * @throws NotFoundException
101
-	 * @throws NotPermittedException
102
-	 */
103
-	public function getImage(string $key, bool $useSvg = true): ISimpleFile {
104
-		$pngFile = null;
105
-		$logo = $this->config->getAppValue('theming', $key . 'Mime', false);
106
-		$folder = $this->appData->getFolder('images');
107
-		if ($logo === false || !$folder->fileExists($key)) {
108
-			throw new NotFoundException();
109
-		}
110
-		if (!$useSvg && $this->shouldReplaceIcons()) {
111
-			if (!$folder->fileExists($key . '.png')) {
112
-				try {
113
-					$finalIconFile = new \Imagick();
114
-					$finalIconFile->setBackgroundColor('none');
115
-					$finalIconFile->readImageBlob($folder->getFile($key)->getContent());
116
-					$finalIconFile->setImageFormat('png32');
117
-					$pngFile = $folder->newFile($key . '.png');
118
-					$pngFile->putContent($finalIconFile->getImageBlob());
119
-				} catch (\ImagickException $e) {
120
-					$this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: ' . $e->getMessage());
121
-					$pngFile = null;
122
-				}
123
-			} else {
124
-				$pngFile = $folder->getFile($key . '.png');
125
-			}
126
-		}
127
-		if ($pngFile !== null) {
128
-			return $pngFile;
129
-		}
130
-		return $folder->getFile($key);
131
-	}
132
-
133
-	public function getCustomImages(): array {
134
-		$images = [];
135
-		foreach ($this->supportedImageKeys as $key) {
136
-			$images[$key] = [
137
-				'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
138
-				'url' => $this->getImageUrl($key),
139
-			];
140
-		}
141
-		return $images;
142
-	}
143
-
144
-	/**
145
-	 * Get folder for current theming files
146
-	 *
147
-	 * @return ISimpleFolder
148
-	 * @throws NotPermittedException
149
-	 */
150
-	public function getCacheFolder(): ISimpleFolder {
151
-		$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
152
-		try {
153
-			$folder = $this->appData->getFolder($cacheBusterValue);
154
-		} catch (NotFoundException $e) {
155
-			$folder = $this->appData->newFolder($cacheBusterValue);
156
-			$this->cleanup();
157
-		}
158
-		return $folder;
159
-	}
160
-
161
-	/**
162
-	 * Get a file from AppData
163
-	 *
164
-	 * @param string $filename
165
-	 * @throws NotFoundException
166
-	 * @return \OCP\Files\SimpleFS\ISimpleFile
167
-	 * @throws NotPermittedException
168
-	 */
169
-	public function getCachedImage(string $filename): ISimpleFile {
170
-		$currentFolder = $this->getCacheFolder();
171
-		return $currentFolder->getFile($filename);
172
-	}
173
-
174
-	/**
175
-	 * Store a file for theming in AppData
176
-	 *
177
-	 * @param string $filename
178
-	 * @param string $data
179
-	 * @return \OCP\Files\SimpleFS\ISimpleFile
180
-	 * @throws NotFoundException
181
-	 * @throws NotPermittedException
182
-	 */
183
-	public function setCachedImage(string $filename, string $data): ISimpleFile {
184
-		$currentFolder = $this->getCacheFolder();
185
-		if ($currentFolder->fileExists($filename)) {
186
-			$file = $currentFolder->getFile($filename);
187
-		} else {
188
-			$file = $currentFolder->newFile($filename);
189
-		}
190
-		$file->putContent($data);
191
-		return $file;
192
-	}
193
-
194
-	public function delete(string $key) {
195
-		/* ignore exceptions, since we don't want to fail hard if something goes wrong during cleanup */
196
-		try {
197
-			$file = $this->appData->getFolder('images')->getFile($key);
198
-			$file->delete();
199
-		} catch (NotFoundException $e) {
200
-		} catch (NotPermittedException $e) {
201
-		}
202
-		try {
203
-			$file = $this->appData->getFolder('images')->getFile($key . '.png');
204
-			$file->delete();
205
-		} catch (NotFoundException $e) {
206
-		} catch (NotPermittedException $e) {
207
-		}
208
-	}
209
-
210
-	/**
211
-	 * remove cached files that are not required any longer
212
-	 *
213
-	 * @throws NotPermittedException
214
-	 * @throws NotFoundException
215
-	 */
216
-	public function cleanup() {
217
-		$currentFolder = $this->getCacheFolder();
218
-		$folders = $this->appData->getDirectoryListing();
219
-		foreach ($folders as $folder) {
220
-			if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
221
-				$folder->delete();
222
-			}
223
-		}
224
-	}
225
-
226
-	/**
227
-	 * Check if Imagemagick is enabled and if SVG is supported
228
-	 * otherwise we can't render custom icons
229
-	 *
230
-	 * @return bool
231
-	 */
232
-	public function shouldReplaceIcons() {
233
-		$cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
234
-		if($value = $cache->get('shouldReplaceIcons')) {
235
-			return (bool)$value;
236
-		}
237
-		$value = false;
238
-		if(extension_loaded('imagick')) {
239
-			if (count(\Imagick::queryFormats('SVG')) >= 1) {
240
-				$value = true;
241
-			}
242
-		}
243
-		$cache->set('shouldReplaceIcons', $value);
244
-		return $value;
245
-	}
39
+    /** @var IConfig */
40
+    private $config;
41
+    /** @var IAppData */
42
+    private $appData;
43
+    /** @var IURLGenerator */
44
+    private $urlGenerator;
45
+    /** @var array */
46
+    private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
47
+    /** @var ICacheFactory */
48
+    private $cacheFactory;
49
+    /** @var ILogger */
50
+    private $logger;
51
+
52
+    /**
53
+     * ImageManager constructor.
54
+     *
55
+     * @param IConfig $config
56
+     * @param IAppData $appData
57
+     * @param IURLGenerator $urlGenerator
58
+     * @param ICacheFactory $cacheFactory
59
+     * @param ILogger $logger
60
+     */
61
+    public function __construct(IConfig $config,
62
+                                IAppData $appData,
63
+                                IURLGenerator $urlGenerator,
64
+                                ICacheFactory $cacheFactory,
65
+                                ILogger $logger
66
+    ) {
67
+        $this->config = $config;
68
+        $this->appData = $appData;
69
+        $this->urlGenerator = $urlGenerator;
70
+        $this->cacheFactory = $cacheFactory;
71
+        $this->logger = $logger;
72
+    }
73
+
74
+    public function getImageUrl(string $key, bool $useSvg = true): string {
75
+        $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
76
+        try {
77
+            $image = $this->getImage($key, $useSvg);
78
+            return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter;
79
+        } catch (NotFoundException $e) {
80
+        }
81
+
82
+        switch ($key) {
83
+            case 'logo':
84
+            case 'logoheader':
85
+            case 'favicon':
86
+                return $this->urlGenerator->imagePath('core', 'logo.png') . '?v=' . $cacheBusterCounter;
87
+            case 'background':
88
+                return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter;
89
+        }
90
+    }
91
+
92
+    public function getImageUrlAbsolute(string $key, bool $useSvg = true): string {
93
+        return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key, $useSvg));
94
+    }
95
+
96
+    /**
97
+     * @param string $key
98
+     * @param bool $useSvg
99
+     * @return ISimpleFile
100
+     * @throws NotFoundException
101
+     * @throws NotPermittedException
102
+     */
103
+    public function getImage(string $key, bool $useSvg = true): ISimpleFile {
104
+        $pngFile = null;
105
+        $logo = $this->config->getAppValue('theming', $key . 'Mime', false);
106
+        $folder = $this->appData->getFolder('images');
107
+        if ($logo === false || !$folder->fileExists($key)) {
108
+            throw new NotFoundException();
109
+        }
110
+        if (!$useSvg && $this->shouldReplaceIcons()) {
111
+            if (!$folder->fileExists($key . '.png')) {
112
+                try {
113
+                    $finalIconFile = new \Imagick();
114
+                    $finalIconFile->setBackgroundColor('none');
115
+                    $finalIconFile->readImageBlob($folder->getFile($key)->getContent());
116
+                    $finalIconFile->setImageFormat('png32');
117
+                    $pngFile = $folder->newFile($key . '.png');
118
+                    $pngFile->putContent($finalIconFile->getImageBlob());
119
+                } catch (\ImagickException $e) {
120
+                    $this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: ' . $e->getMessage());
121
+                    $pngFile = null;
122
+                }
123
+            } else {
124
+                $pngFile = $folder->getFile($key . '.png');
125
+            }
126
+        }
127
+        if ($pngFile !== null) {
128
+            return $pngFile;
129
+        }
130
+        return $folder->getFile($key);
131
+    }
132
+
133
+    public function getCustomImages(): array {
134
+        $images = [];
135
+        foreach ($this->supportedImageKeys as $key) {
136
+            $images[$key] = [
137
+                'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
138
+                'url' => $this->getImageUrl($key),
139
+            ];
140
+        }
141
+        return $images;
142
+    }
143
+
144
+    /**
145
+     * Get folder for current theming files
146
+     *
147
+     * @return ISimpleFolder
148
+     * @throws NotPermittedException
149
+     */
150
+    public function getCacheFolder(): ISimpleFolder {
151
+        $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
152
+        try {
153
+            $folder = $this->appData->getFolder($cacheBusterValue);
154
+        } catch (NotFoundException $e) {
155
+            $folder = $this->appData->newFolder($cacheBusterValue);
156
+            $this->cleanup();
157
+        }
158
+        return $folder;
159
+    }
160
+
161
+    /**
162
+     * Get a file from AppData
163
+     *
164
+     * @param string $filename
165
+     * @throws NotFoundException
166
+     * @return \OCP\Files\SimpleFS\ISimpleFile
167
+     * @throws NotPermittedException
168
+     */
169
+    public function getCachedImage(string $filename): ISimpleFile {
170
+        $currentFolder = $this->getCacheFolder();
171
+        return $currentFolder->getFile($filename);
172
+    }
173
+
174
+    /**
175
+     * Store a file for theming in AppData
176
+     *
177
+     * @param string $filename
178
+     * @param string $data
179
+     * @return \OCP\Files\SimpleFS\ISimpleFile
180
+     * @throws NotFoundException
181
+     * @throws NotPermittedException
182
+     */
183
+    public function setCachedImage(string $filename, string $data): ISimpleFile {
184
+        $currentFolder = $this->getCacheFolder();
185
+        if ($currentFolder->fileExists($filename)) {
186
+            $file = $currentFolder->getFile($filename);
187
+        } else {
188
+            $file = $currentFolder->newFile($filename);
189
+        }
190
+        $file->putContent($data);
191
+        return $file;
192
+    }
193
+
194
+    public function delete(string $key) {
195
+        /* ignore exceptions, since we don't want to fail hard if something goes wrong during cleanup */
196
+        try {
197
+            $file = $this->appData->getFolder('images')->getFile($key);
198
+            $file->delete();
199
+        } catch (NotFoundException $e) {
200
+        } catch (NotPermittedException $e) {
201
+        }
202
+        try {
203
+            $file = $this->appData->getFolder('images')->getFile($key . '.png');
204
+            $file->delete();
205
+        } catch (NotFoundException $e) {
206
+        } catch (NotPermittedException $e) {
207
+        }
208
+    }
209
+
210
+    /**
211
+     * remove cached files that are not required any longer
212
+     *
213
+     * @throws NotPermittedException
214
+     * @throws NotFoundException
215
+     */
216
+    public function cleanup() {
217
+        $currentFolder = $this->getCacheFolder();
218
+        $folders = $this->appData->getDirectoryListing();
219
+        foreach ($folders as $folder) {
220
+            if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
221
+                $folder->delete();
222
+            }
223
+        }
224
+    }
225
+
226
+    /**
227
+     * Check if Imagemagick is enabled and if SVG is supported
228
+     * otherwise we can't render custom icons
229
+     *
230
+     * @return bool
231
+     */
232
+    public function shouldReplaceIcons() {
233
+        $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
234
+        if($value = $cache->get('shouldReplaceIcons')) {
235
+            return (bool)$value;
236
+        }
237
+        $value = false;
238
+        if(extension_loaded('imagick')) {
239
+            if (count(\Imagick::queryFormats('SVG')) >= 1) {
240
+                $value = true;
241
+            }
242
+        }
243
+        $cache->set('shouldReplaceIcons', $value);
244
+        return $value;
245
+    }
246 246
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
 		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
76 76
 		try {
77 77
 			$image = $this->getImage($key, $useSvg);
78
-			return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter;
78
+			return $this->urlGenerator->linkToRoute('theming.Theming.getImage', ['key' => $key]).'?v='.$cacheBusterCounter;
79 79
 		} catch (NotFoundException $e) {
80 80
 		}
81 81
 
@@ -83,9 +83,9 @@  discard block
 block discarded – undo
83 83
 			case 'logo':
84 84
 			case 'logoheader':
85 85
 			case 'favicon':
86
-				return $this->urlGenerator->imagePath('core', 'logo.png') . '?v=' . $cacheBusterCounter;
86
+				return $this->urlGenerator->imagePath('core', 'logo.png').'?v='.$cacheBusterCounter;
87 87
 			case 'background':
88
-				return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter;
88
+				return $this->urlGenerator->imagePath('core', 'background.png').'?v='.$cacheBusterCounter;
89 89
 		}
90 90
 	}
91 91
 
@@ -102,26 +102,26 @@  discard block
 block discarded – undo
102 102
 	 */
103 103
 	public function getImage(string $key, bool $useSvg = true): ISimpleFile {
104 104
 		$pngFile = null;
105
-		$logo = $this->config->getAppValue('theming', $key . 'Mime', false);
105
+		$logo = $this->config->getAppValue('theming', $key.'Mime', false);
106 106
 		$folder = $this->appData->getFolder('images');
107 107
 		if ($logo === false || !$folder->fileExists($key)) {
108 108
 			throw new NotFoundException();
109 109
 		}
110 110
 		if (!$useSvg && $this->shouldReplaceIcons()) {
111
-			if (!$folder->fileExists($key . '.png')) {
111
+			if (!$folder->fileExists($key.'.png')) {
112 112
 				try {
113 113
 					$finalIconFile = new \Imagick();
114 114
 					$finalIconFile->setBackgroundColor('none');
115 115
 					$finalIconFile->readImageBlob($folder->getFile($key)->getContent());
116 116
 					$finalIconFile->setImageFormat('png32');
117
-					$pngFile = $folder->newFile($key . '.png');
117
+					$pngFile = $folder->newFile($key.'.png');
118 118
 					$pngFile->putContent($finalIconFile->getImageBlob());
119 119
 				} catch (\ImagickException $e) {
120
-					$this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: ' . $e->getMessage());
120
+					$this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: '.$e->getMessage());
121 121
 					$pngFile = null;
122 122
 				}
123 123
 			} else {
124
-				$pngFile = $folder->getFile($key . '.png');
124
+				$pngFile = $folder->getFile($key.'.png');
125 125
 			}
126 126
 		}
127 127
 		if ($pngFile !== null) {
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 		$images = [];
135 135
 		foreach ($this->supportedImageKeys as $key) {
136 136
 			$images[$key] = [
137
-				'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
137
+				'mime' => $this->config->getAppValue('theming', $key.'Mime', ''),
138 138
 				'url' => $this->getImageUrl($key),
139 139
 			];
140 140
 		}
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
 		} catch (NotPermittedException $e) {
201 201
 		}
202 202
 		try {
203
-			$file = $this->appData->getFolder('images')->getFile($key . '.png');
203
+			$file = $this->appData->getFolder('images')->getFile($key.'.png');
204 204
 			$file->delete();
205 205
 		} catch (NotFoundException $e) {
206 206
 		} catch (NotPermittedException $e) {
@@ -230,12 +230,12 @@  discard block
 block discarded – undo
230 230
 	 * @return bool
231 231
 	 */
232 232
 	public function shouldReplaceIcons() {
233
-		$cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
234
-		if($value = $cache->get('shouldReplaceIcons')) {
235
-			return (bool)$value;
233
+		$cache = $this->cacheFactory->createDistributed('theming-'.$this->urlGenerator->getBaseUrl());
234
+		if ($value = $cache->get('shouldReplaceIcons')) {
235
+			return (bool) $value;
236 236
 		}
237 237
 		$value = false;
238
-		if(extension_loaded('imagick')) {
238
+		if (extension_loaded('imagick')) {
239 239
 			if (count(\Imagick::queryFormats('SVG')) >= 1) {
240 240
 				$value = true;
241 241
 			}
Please login to merge, or discard this patch.