Completed
Push — master ( 3e14f2...b3a0de )
by Morris
15:06
created

ImageManager::getImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Haertl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
25
namespace OCA\Theming;
26
27
use OCP\Files\SimpleFS\ISimpleFile;
28
use OCP\Files\SimpleFS\ISimpleFolder;
29
use OCP\IConfig;
30
use OCP\Files\IAppData;
31
use OCP\Files\NotFoundException;
32
use OCP\Files\NotPermittedException;
33
use OCP\IURLGenerator;
34
35
/**
36
 * @property IURLGenerator urlGenerator
37
 */
38
class ImageManager {
39
40
	/** @var IConfig */
41
	private $config;
42
	/** @var IAppData */
43
	private $appData;
44
45
	/** @var array */
46
	private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
47
48
	/**
49
	 * ImageManager constructor.
50
	 *
51
	 * @param IConfig $config
52
	 * @param IAppData $appData
53
	 * @param IURLGenerator $urlGenerator
54
	 */
55
	public function __construct(IConfig $config,
56
								IAppData $appData,
57
								IURLGenerator $urlGenerator
58
	) {
59
		$this->config = $config;
60
		$this->appData = $appData;
61
		$this->urlGenerator = $urlGenerator;
62
	}
63
64
	public function getImageUrl(string $key): string {
65
		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
66
		try {
67
			$this->getImage($key);
68
			return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter;
69
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
		}
71
72
		switch ($key) {
73
			case 'logo':
74
			case 'logoheader':
75
			case 'favicon':
76
				return $this->urlGenerator->imagePath('core', 'logo.png') . '?v=' . $cacheBusterCounter;
77
			case 'background':
78
				return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter;
79
		}
80
	}
81
82
	public function getImageUrlAbsolute(string $key): string {
83
		return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key));
84
	}
85
86
	/**
87
	 * @param $key
88
	 * @return ISimpleFile
89
	 * @throws NotFoundException
90
	 */
91
	public function getImage(string $key): ISimpleFile {
92
		$logo = $this->config->getAppValue('theming', $key . 'Mime', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
		if ($logo === false) {
94
			throw new NotFoundException();
95
		}
96
		$folder = $this->appData->getFolder('images');
97
		return $folder->getFile($key);
98
	}
99
100
	public function getCustomImages(): array {
101
		$images = [];
102
		foreach ($this->supportedImageKeys as $key) {
103
			$images[$key] = [
104
				'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
105
				'url' => $this->getImageUrl($key),
106
			];
107
		}
108
		return $images;
109
	}
110
111
	/**
112
	 * Get folder for current theming files
113
	 *
114
	 * @return ISimpleFolder
115
	 * @throws NotPermittedException
116
	 */
117
	public function getCacheFolder(): ISimpleFolder {
118
		$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
119
		try {
120
			$folder = $this->appData->getFolder($cacheBusterValue);
121
		} catch (NotFoundException $e) {
122
			$folder = $this->appData->newFolder($cacheBusterValue);
123
			$this->cleanup();
124
		}
125
		return $folder;
126
	}
127
128
	/**
129
	 * Get a file from AppData
130
	 *
131
	 * @param string $filename
132
	 * @throws NotFoundException
133
	 * @return \OCP\Files\SimpleFS\ISimpleFile
134
	 * @throws NotPermittedException
135
	 */
136
	public function getCachedImage(string $filename): ISimpleFile {
137
		$currentFolder = $this->getCacheFolder();
138
		return $currentFolder->getFile($filename);
139
	}
140
141
	/**
142
	 * Store a file for theming in AppData
143
	 *
144
	 * @param string $filename
145
	 * @param string $data
146
	 * @return \OCP\Files\SimpleFS\ISimpleFile
147
	 * @throws NotFoundException
148
	 * @throws NotPermittedException
149
	 */
150
	public function setCachedImage(string $filename, string $data): ISimpleFile {
151
		$currentFolder = $this->getCacheFolder();
152
		if ($currentFolder->fileExists($filename)) {
153
			$file = $currentFolder->getFile($filename);
154
		} else {
155
			$file = $currentFolder->newFile($filename);
156
		}
157
		$file->putContent($data);
158
		return $file;
159
	}
160
161
	public function delete(string $key) {
162
		try {
163
			$file = $this->appData->getFolder('images')->getFile($key);
164
			$file->delete();
165
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
166
		} catch (NotPermittedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
167
		}
168
	}
169
170
	/**
171
	 * remove cached files that are not required any longer
172
	 *
173
	 * @throws NotPermittedException
174
	 * @throws NotFoundException
175
	 */
176
	public function cleanup() {
177
		$currentFolder = $this->getCacheFolder();
178
		$folders = $this->appData->getDirectoryListing();
179
		foreach ($folders as $folder) {
180
			if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
181
				$folder->delete();
182
			}
183
		}
184
	}
185
}
186