Test Failed
Push — master ( 02e784...85ec4e )
by Daniel
42:33 queued 17:51
created

AssetsService::getAssetsPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 5.667

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 2
cts 6
cp 0.3333
rs 10
cc 3
nc 3
nop 1
crap 5.667
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 OCP\Files\InvalidPathException;
38
use OCP\Files\NotFoundException;
39
use OCP\Files\NotPermittedException;
40
41
class AssetsService
42
{
43
	/**
44
	 * @param Website $website
45
	 *
46
	 * @return PicoAsset
47
	 * @throws WebsiteInvalidFilesystemException
48
	 * @throws WebsiteNotPermittedException
49
	 * @throws AssetInvalidPathException
50
	 * @throws AssetNotFoundException
51
	 * @throws AssetNotPermittedException
52
	 */
53
	public function getAsset(Website $website): PicoAsset
54
	{
55
		try {
56
			$asset = $website->getPage();
57
58
			$assetsDir = PicoService::DIR_ASSETS . '/';
59
			$assetsDirLength = strlen($assetsDir);
60
			if (substr($asset, 0, $assetsDirLength) !== $assetsDir) {
61
				throw new InvalidPathException();
62
			}
63
64
			$website->assertViewerAccess($asset);
65
66
			$asset = substr($asset, $assetsDirLength);
67
			if ($asset === '') {
68
				throw new InvalidPathException();
69
			}
70
71
			/** @var StorageFile $assetFile */
72
			$assetFile = $this->getAssetsFolder($website)->getFile($asset);
73
			$picoAsset = new PicoAsset($assetFile, $website->getType() === WebsiteCore::TYPE_PUBLIC);
74
		} catch (InvalidPathException $e) {
75
			throw new AssetInvalidPathException($e);
76
		} catch (NotFoundException $e) {
77
			throw new AssetNotFoundException($e);
78
		} catch (NotPermittedException $e) {
79
			throw new AssetNotPermittedException($e);
80
		}
81
82
		return $picoAsset;
83
	}
84
85
	/**
86
	 * @param Website $website
87
	 *
88
	 * @return StorageFolder
89
	 * @throws WebsiteInvalidFilesystemException
90
	 */
91 2
	public function getAssetsFolder(Website $website): StorageFolder
92
	{
93
		try {
94 2
			return $website->getWebsiteFolder()->getFolder(PicoService::DIR_ASSETS)->fakeRoot();
95
		} catch (InvalidPathException $e) {
96
			throw new WebsiteInvalidFilesystemException($e);
97
		} catch (NotFoundException $e) {
98
			throw new WebsiteInvalidFilesystemException($e);
99
		}
100
	}
101
102
	/**
103
	 * @param Website $website
104
	 *
105
	 * @return string
106
	 * @throws WebsiteInvalidFilesystemException
107
	 */
108 2
	public function getAssetsPath(Website $website): string
109
	{
110
		try {
111 2
			return $this->getAssetsFolder($website)->getLocalPath() . '/';
112
		} catch (InvalidPathException $e) {
113
			throw new WebsiteInvalidFilesystemException($e);
114
		} catch (NotFoundException $e) {
115
			throw new WebsiteInvalidFilesystemException($e);
116
		}
117
	}
118
119
	/**
120
	 * @param Website $website
121
	 *
122
	 * @return string
123
	 * @throws WebsiteInvalidFilesystemException
124
	 */
125 2
	public function getAssetsUrl(Website $website): string
126
	{
127 2
		$assetsETag = $this->getAssetsFolder($website)->getOCNode()->getEtag();
128 2
		return $website->getWebsiteUrl() . PicoService::DIR_ASSETS . '-' . $assetsETag . '/';
129
	}
130
}
131