Passed
Pull Request — master (#99)
by Daniel
26:12 queued 03:19
created

PicoController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 15.38%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 21
eloc 69
c 4
b 0
f 0
dl 0
loc 143
ccs 10
cts 65
cp 0.1538
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B getAsset() 0 39 10
B getPage() 0 43 10
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
6
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
declare(strict_types=1);
25
26
namespace OCA\CMSPico\Controller;
27
28
use OCA\CMSPico\AppInfo\Application;
29
use OCA\CMSPico\Exceptions\AssetInvalidPathException;
30
use OCA\CMSPico\Exceptions\AssetNotFoundException;
31
use OCA\CMSPico\Exceptions\AssetNotPermittedException;
32
use OCA\CMSPico\Exceptions\FilesystemNotLocalException;
33
use OCA\CMSPico\Exceptions\PageInvalidPathException;
34
use OCA\CMSPico\Exceptions\PageNotFoundException;
35
use OCA\CMSPico\Exceptions\PageNotPermittedException;
36
use OCA\CMSPico\Exceptions\PicoRuntimeException;
37
use OCA\CMSPico\Exceptions\ThemeNotCompatibleException;
38
use OCA\CMSPico\Exceptions\ThemeNotFoundException;
39
use OCA\CMSPico\Exceptions\WebsiteInvalidFilesystemException;
40
use OCA\CMSPico\Exceptions\WebsiteInvalidOwnerException;
41
use OCA\CMSPico\Exceptions\WebsiteNotFoundException;
42
use OCA\CMSPico\Exceptions\WebsiteNotPermittedException;
43
use OCA\CMSPico\Http\InternalServerErrorResponse;
44
use OCA\CMSPico\Http\NotFoundResponse;
45
use OCA\CMSPico\Http\NotModifiedResponse;
46
use OCA\CMSPico\Http\NotPermittedResponse;
47
use OCA\CMSPico\Http\PicoAssetResponse;
48
use OCA\CMSPico\Http\PicoErrorResponse;
49
use OCA\CMSPico\Http\PicoPageResponse;
50
use OCA\CMSPico\Service\FileService;
51
use OCA\CMSPico\Service\WebsitesService;
52
use OCP\AppFramework\Controller;
53
use OCP\AppFramework\Http\Response;
54
use OCP\IL10N;
55
use OCP\IRequest;
56
57
class PicoController extends Controller
58
{
59
	/** @var string|null */
60
	private $userId;
61
62
	/** @var IL10N */
63
	private $l10n;
64
65
	/** @var WebsitesService */
66
	private $websitesService;
67
68
	/** @var FileService */
69
	private $fileService;
70
71
	/**
72
	 * PicoController constructor.
73
	 *
74
	 * @param IRequest        $request
75
	 * @param string|null     $userId
76
	 * @param IL10N           $l10n
77
	 * @param WebsitesService $websitesService
78
	 * @param FileService     $fileService
79
	 */
80 1
	public function __construct(
81
		IRequest $request,
82
		?string $userId,
83
		IL10N $l10n,
84
		WebsitesService $websitesService,
85
		FileService $fileService
86
	) {
87 1
		parent::__construct(Application::APP_NAME, $request);
88
89 1
		$this->userId = $userId;
90 1
		$this->l10n = $l10n;
91 1
		$this->websitesService = $websitesService;
92 1
		$this->fileService = $fileService;
93 1
	}
94
95
	/**
96
	 * @PublicPage
97
	 * @NoCSRFRequired
98
	 *
99
	 * @param string $site
100
	 * @param string $page
101
	 * @param bool   $proxyRequest
102
	 *
103
	 * @return Response
104
	 */
105 1
	public function getPage(string $site, string $page, bool $proxyRequest = false): Response
106
	{
107
		try {
108 1
			$picoPage = $this->websitesService->getPage($site, $page, $this->userId, $proxyRequest);
109 1
			return new PicoPageResponse($picoPage);
110
		} catch (WebsiteNotFoundException | WebsiteInvalidOwnerException $e) {
111
			return new NotFoundResponse($this->l10n->t(
112
				'The requested website could not be found on the server. Maybe the website was deleted?'
113
			));
114
		} catch (WebsiteInvalidFilesystemException $e) {
115
			return new InternalServerErrorResponse($this->l10n->t(
116
				'The file and directory structure of this website appears to be broken and thus could not be accessed.'
117
			));
118
		} catch (WebsiteNotPermittedException $e) {
119
			return new NotPermittedResponse($this->l10n->t(
120
				'You don\'t have access to this private website. Maybe the share was deleted or has expired?'
121
			));
122
		} catch (FilesystemNotLocalException $e) {
123
			return new InternalServerErrorResponse($this->l10n->t(
124
				'This website is hosted on a non-local storage and thus could not be accessed.'
125
			));
126
		} catch (ThemeNotFoundException $e) {
127
			return new InternalServerErrorResponse($this->l10n->t(
128
				'This website uses a theme that could not be found on the server and thus could not be built.'
129
			));
130
		} catch (ThemeNotCompatibleException $e) {
131
			return new InternalServerErrorResponse($this->l10n->t(
132
				'This website uses a incompatible theme and thus could not be built.'
133
			));
134
		} catch (PageInvalidPathException | PageNotFoundException $e) {
135
			return new NotFoundResponse($this->l10n->t(
136
				'The requested website page could not be found on the server. Maybe the page was deleted?'
137
			));
138
		} catch (PageNotPermittedException $e) {
139
			return new NotPermittedResponse($this->l10n->t(
140
				'You don\'t have access to this website page. Maybe the share was deleted or has expired?'
141
			));
142
		} catch (PicoRuntimeException $e) {
143
			$errorMessage = $this->l10n->t(
144
				'The requested website page could not be built, '
145
					. 'so that the server was unable to complete your request.'
146
			);
147
			return new PicoErrorResponse($errorMessage, $e);
148
		}
149
	}
150
151
	/**
152
	 * @PublicPage
153
	 * @NoCSRFRequired
154
	 *
155
	 * @param string $site
156
	 * @param string $asset
157
	 * @param string $assetsETag
158
	 *
159
	 * @return Response
160
	 */
161
	public function getAsset(string $site, string $asset, string $assetsETag = ''): Response
162
	{
163
		try {
164
			$picoAsset = $this->websitesService->getAsset($site, $asset, $this->userId);
165
166
			$response = new PicoAssetResponse($picoAsset, (bool) $assetsETag);
167
168
			$assetETag = $picoAsset->getETag();
169
			$clientETag = $this->request->getHeader('If-None-Match');
170
			if ($assetETag && $clientETag) {
171
				if (preg_match('/^"?' . preg_quote($assetETag, '/') . '(?>"?$|-)/', $clientETag)) {
172
					return new NotModifiedResponse($response);
173
				}
174
			}
175
176
			return $response;
177
		} catch (WebsiteNotFoundException | WebsiteInvalidOwnerException $e) {
178
			return new NotFoundResponse($this->l10n->t(
179
				'The requested website could not be found on the server. Maybe the website was deleted?'
180
			));
181
		} catch (WebsiteInvalidFilesystemException $e) {
182
			return new InternalServerErrorResponse($this->l10n->t(
183
				'The file and directory structure of this website appears to be broken und thus could not be accessed.'
184
			));
185
		} catch (WebsiteNotPermittedException $e) {
186
			return new NotPermittedResponse($this->l10n->t(
187
				'You don\'t have access to this private website. Maybe the share was deleted or has expired?'
188
			));
189
		} catch (FilesystemNotLocalException $e) {
190
			return new InternalServerErrorResponse($this->l10n->t(
191
				'This website is hosted on a non-local storage and thus could not be accessed.'
192
			));
193
		} catch (AssetInvalidPathException | AssetNotFoundException $e) {
194
			return new NotFoundResponse($this->l10n->t(
195
				'The requested website asset could not be found on the server. Maybe the asset was deleted?'
196
			));
197
		} catch (AssetNotPermittedException $e) {
198
			return new NotPermittedResponse($this->l10n->t(
199
				'You don\'t have access to this website asset. Maybe the share was deleted or has expired?'
200
			));
201
		}
202
	}
203
}
204