Test Setup Failed
Push — master ( 4a93bf...5a5f5f )
by Daniel
23:25
created

AssetsService::getAsset()   B

Complexity

Conditions 6
Paths 31

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.304

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 31
ccs 12
cts 20
cp 0.6
rs 8.9777
cc 6
nc 31
nop 1
crap 8.304
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[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
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Service;
26
27
use OCA\CMSPico\Exceptions\AssetInvalidPathException;
28
use OCA\CMSPico\Exceptions\AssetNotFoundException;
29
use OCA\CMSPico\Exceptions\AssetNotPermittedException;
30
use OCA\CMSPico\Exceptions\WebsiteInvalidFilesystemException;
31
use OCA\CMSPico\Exceptions\WebsiteNotPermittedException;
32
use OCA\CMSPico\Files\StorageFile;
33
use OCA\CMSPico\Files\StorageFolder;
34
use OCA\CMSPico\Model\PicoAsset;
35
use OCA\CMSPico\Model\Website;
36
use OCA\CMSPico\Model\WebsiteCore;
37
use OCA\CMSPico\Model\WebsiteRequest;
38
use OCP\Files\InvalidPathException;
39
use OCP\Files\NotFoundException;
40
use OCP\Files\NotPermittedException;
41
42
class AssetsService
43
{
44
	/**
45
	 * @param WebsiteRequest $websiteRequest
46
	 *
47
	 * @return PicoAsset
48
	 * @throws WebsiteInvalidFilesystemException
49
	 * @throws WebsiteNotPermittedException
50
	 * @throws AssetInvalidPathException
51
	 * @throws AssetNotFoundException
52
	 * @throws AssetNotPermittedException
53
	 */
54 2
	public function getAsset(WebsiteRequest $websiteRequest): PicoAsset
55
	{
56
		try {
57 2
			$website = $websiteRequest->getWebsite();
58 2
			$asset = $websiteRequest->getPage();
59
60 2
			$assetsDir = PicoService::DIR_ASSETS . '/';
61 2
			$assetsDirLength = strlen($assetsDir);
62 2
			if (substr_compare($asset, $assetsDir, 0, $assetsDirLength) !== 0) {
63
				throw new InvalidPathException();
64
			}
65
66 2
			$websiteRequest->assertViewerAccess($asset);
67
68 2
			$asset = substr($asset, $assetsDirLength);
69 2
			if ($asset === '') {
70
				throw new InvalidPathException();
71
			}
72
73
			/** @var StorageFile $assetFile */
74 2
			$assetFile = $this->getAssetsFolder($website)->getFile($asset);
75 2
			$picoAsset = new PicoAsset($assetFile, $website->getType() === WebsiteCore::TYPE_PUBLIC);
76
		} catch (InvalidPathException $e) {
77
			throw new AssetInvalidPathException($e);
78
		} catch (NotFoundException $e) {
79
			throw new AssetNotFoundException($e);
80
		} catch (NotPermittedException $e) {
81
			throw new AssetNotPermittedException($e);
82
		}
83
84 2
		return $picoAsset;
85
	}
86
87
	/**
88
	 * @param Website $website
89
	 *
90
	 * @return StorageFolder
91
	 * @throws WebsiteInvalidFilesystemException
92
	 */
93 6
	public function getAssetsFolder(Website $website): StorageFolder
94
	{
95
		try {
96 6
			return $website->getWebsiteFolder()->getFolder(PicoService::DIR_ASSETS)->fakeRoot();
97
		} catch (InvalidPathException | NotFoundException $e) {
98
			throw new WebsiteInvalidFilesystemException($e);
99
		}
100
	}
101
102
	/**
103
	 * @param Website $website
104
	 *
105
	 * @return string
106
	 * @throws WebsiteInvalidFilesystemException
107
	 */
108 4
	public function getAssetsPath(Website $website): string
109
	{
110
		try {
111 4
			return $this->getAssetsFolder($website)->getLocalPath() . '/';
112
		} catch (InvalidPathException | NotFoundException $e) {
113
			throw new WebsiteInvalidFilesystemException($e);
114
		}
115
	}
116
117
	/**
118
	 * @param WebsiteRequest $websiteRequest
119
	 *
120
	 * @return string
121
	 * @throws WebsiteInvalidFilesystemException
122
	 */
123 4
	public function getAssetsUrl(WebsiteRequest $websiteRequest): string
124
	{
125 4
		$website = $websiteRequest->getWebsite();
126 4
		$websiteUrl = $website->getWebsiteUrl($websiteRequest->isProxyRequest());
127 4
		$assetsETag = $this->getAssetsFolder($websiteRequest->getWebsite())->getOCNode()->getEtag();
128 4
		return $websiteUrl . PicoService::DIR_ASSETS . '-' . $assetsETag . '/';
129
	}
130
}
131