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 | NotFoundException $e) { |
95
|
|
|
throw new WebsiteInvalidFilesystemException($e); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Website $website |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
* @throws WebsiteInvalidFilesystemException |
104
|
|
|
*/ |
105
|
2 |
|
public function getAssetsPath(Website $website): string |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
2 |
|
return $this->getAssetsFolder($website)->getLocalPath() . '/'; |
109
|
|
|
} catch (InvalidPathException | NotFoundException $e) { |
110
|
|
|
throw new WebsiteInvalidFilesystemException($e); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Website $website |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
* @throws WebsiteInvalidFilesystemException |
119
|
|
|
*/ |
120
|
2 |
|
public function getAssetsUrl(Website $website): string |
121
|
|
|
{ |
122
|
2 |
|
$assetsETag = $this->getAssetsFolder($website)->getOCNode()->getEtag(); |
123
|
2 |
|
return $website->getWebsiteUrl() . PicoService::DIR_ASSETS . '-' . $assetsETag . '/'; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|