Passed
Push — master ( 3231b3...73e3d0 )
by John
12:14 queued 12s
created

IconController::getTouchIcon()   B

Complexity

Conditions 7
Paths 21

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 21
nop 1
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Haertl <[email protected]>
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Julius Haertl <[email protected]>
8
 * @author Julius Härtl <[email protected]>
9
 * @author Michael Weimann <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
namespace OCA\Theming\Controller;
29
30
use OC\IntegrityCheck\Helpers\FileAccessHelper;
31
use OCA\Theming\IconBuilder;
32
use OCA\Theming\ImageManager;
33
use OCA\Theming\ThemingDefaults;
34
use OCP\AppFramework\Controller;
35
use OCP\AppFramework\Http;
36
use OCP\AppFramework\Http\DataDisplayResponse;
37
use OCP\AppFramework\Http\FileDisplayResponse;
38
use OCP\AppFramework\Http\NotFoundResponse;
39
use OCP\AppFramework\Http\Response;
40
use OCP\Files\NotFoundException;
41
use OCP\IRequest;
42
43
class IconController extends Controller {
44
	/** @var ThemingDefaults */
45
	private $themingDefaults;
46
	/** @var IconBuilder */
47
	private $iconBuilder;
48
	/** @var ImageManager */
49
	private $imageManager;
50
	/** @var FileAccessHelper */
51
	private $fileAccessHelper;
52
53
	/**
54
	 * IconController constructor.
55
	 *
56
	 * @param string $appName
57
	 * @param IRequest $request
58
	 * @param ThemingDefaults $themingDefaults
59
	 * @param IconBuilder $iconBuilder
60
	 * @param ImageManager $imageManager
61
	 * @param FileAccessHelper $fileAccessHelper
62
	 */
63
	public function __construct(
64
		$appName,
65
		IRequest $request,
66
		ThemingDefaults $themingDefaults,
67
		IconBuilder $iconBuilder,
68
		ImageManager $imageManager,
69
		FileAccessHelper $fileAccessHelper
70
	) {
71
		parent::__construct($appName, $request);
72
73
		$this->themingDefaults = $themingDefaults;
74
		$this->iconBuilder = $iconBuilder;
75
		$this->imageManager = $imageManager;
76
		$this->fileAccessHelper = $fileAccessHelper;
77
	}
78
79
	/**
80
	 * @PublicPage
81
	 * @NoCSRFRequired
82
	 *
83
	 * @param $app string app name
84
	 * @param $image string image file name (svg required)
85
	 * @return FileDisplayResponse|NotFoundResponse
86
	 * @throws \Exception
87
	 */
88
	public function getThemedIcon(string $app, string $image): Response {
89
		try {
90
			$iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image));
91
		} catch (NotFoundException $exception) {
92
			$icon = $this->iconBuilder->colorSvg($app, $image);
93
			if ($icon === false || $icon === '') {
94
				return new NotFoundResponse();
95
			}
96
			$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
97
		}
98
		$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
99
		$response->cacheFor(86400);
100
		return $response;
101
	}
102
103
	/**
104
	 * Return a 32x32 favicon as png
105
	 *
106
	 * @PublicPage
107
	 * @NoCSRFRequired
108
	 *
109
	 * @param $app string app name
110
	 * @return FileDisplayResponse|DataDisplayResponse|NotFoundResponse
111
	 * @throws \Exception
112
	 */
113
	public function getFavicon(string $app = 'core'): Response {
114
		$response = null;
115
		$iconFile = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $iconFile is dead and can be removed.
Loading history...
116
		try {
117
			$iconFile = $this->imageManager->getImage('favicon', false);
118
			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
119
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
120
		}
121
		if ($iconFile === null && $this->imageManager->shouldReplaceIcons()) {
122
			try {
123
				$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
124
			} catch (NotFoundException $exception) {
125
				$icon = $this->iconBuilder->getFavicon($app);
126
				if ($icon === false || $icon === '') {
127
					return new NotFoundResponse();
128
				}
129
				$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
130
			}
131
			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
132
		}
133
		if ($response === null) {
134
			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
135
			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
136
		}
137
		$response->cacheFor(86400);
138
		return $response;
139
	}
140
141
	/**
142
	 * Return a 512x512 icon for touch devices
143
	 *
144
	 * @PublicPage
145
	 * @NoCSRFRequired
146
	 *
147
	 * @param $app string app name
148
	 * @return DataDisplayResponse|FileDisplayResponse|NotFoundResponse
149
	 * @throws \Exception
150
	 */
151
	public function getTouchIcon(string $app = 'core'): Response {
152
		$response = null;
153
		try {
154
			$iconFile = $this->imageManager->getImage('favicon');
155
			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
156
		} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
157
		}
158
		if ($this->imageManager->shouldReplaceIcons()) {
159
			try {
160
				$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
161
			} catch (NotFoundException $exception) {
162
				$icon = $this->iconBuilder->getTouchIcon($app);
163
				if ($icon === false || $icon === '') {
164
					return new NotFoundResponse();
165
				}
166
				$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
167
			}
168
			$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
169
		}
170
		if ($response === null) {
171
			$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
172
			$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
173
		}
174
		$response->cacheFor(86400);
175
		return $response;
176
	}
177
}
178