Passed
Pull Request — master (#77)
by Daniel
47:29
created

AssetsService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 20.59%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 34
c 2
b 0
f 0
dl 0
loc 88
ccs 7
cts 34
cp 0.2059
rs 10

4 Methods

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