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
|
|
|
public function getAsset(WebsiteRequest $websiteRequest): PicoAsset |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
|
|
$website = $websiteRequest->getWebsite(); |
58
|
|
|
$asset = $websiteRequest->getPage(); |
59
|
|
|
|
60
|
|
|
$assetsDir = PicoService::DIR_ASSETS . '/'; |
61
|
|
|
$assetsDirLength = strlen($assetsDir); |
62
|
|
|
if (substr_compare($asset, $assetsDir, 0, $assetsDirLength) !== 0) { |
63
|
|
|
throw new InvalidPathException(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$websiteRequest->assertViewerAccess($asset); |
67
|
|
|
|
68
|
|
|
$asset = substr($asset, $assetsDirLength); |
69
|
|
|
if ($asset === '') { |
70
|
|
|
throw new InvalidPathException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @var StorageFile $assetFile */ |
74
|
|
|
$assetFile = $this->getAssetsFolder($website)->getFile($asset); |
75
|
|
|
$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
|
|
|
return $picoAsset; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Website $website |
89
|
|
|
* |
90
|
|
|
* @return StorageFolder |
91
|
|
|
* @throws WebsiteInvalidFilesystemException |
92
|
|
|
*/ |
93
|
|
|
public function getAssetsFolder(Website $website): StorageFolder |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
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
|
|
|
public function getAssetsPath(Website $website): string |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
|
|
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
|
|
|
public function getAssetsUrl(WebsiteRequest $websiteRequest): string |
124
|
|
|
{ |
125
|
|
|
$website = $websiteRequest->getWebsite(); |
126
|
|
|
$websiteUrl = $website->getWebsiteUrl($websiteRequest->isProxyRequest()); |
127
|
|
|
$assetsETag = $this->getAssetsFolder($websiteRequest->getWebsite())->getOCNode()->getEtag(); |
128
|
|
|
return $websiteUrl . PicoService::DIR_ASSETS . '-' . $assetsETag . '/'; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|