1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMS Pico - Create websites using Pico CMS for Nextcloud. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>) |
6
|
|
|
* @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>) |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
declare(strict_types=1); |
25
|
|
|
|
26
|
|
|
namespace OCA\CMSPico\Service; |
27
|
|
|
|
28
|
|
|
use OCA\CMSPico\AppInfo\Application; |
29
|
|
|
use OCA\CMSPico\Files\FolderInterface; |
30
|
|
|
use OCA\CMSPico\Files\LocalFolder; |
31
|
|
|
use OCA\CMSPico\Files\StorageFolder; |
32
|
|
|
use OCP\Files\InvalidPathException; |
33
|
|
|
use OCP\Files\IRootFolder; |
34
|
|
|
use OCP\Files\NotFoundException; |
35
|
|
|
use OCP\Files\NotPermittedException; |
36
|
|
|
|
37
|
|
|
class FileService |
38
|
|
|
{ |
39
|
|
|
/** @var string */ |
40
|
|
|
const APPDATA_PUBLIC = 'appdata_public'; |
41
|
|
|
|
42
|
|
|
/** @var string */ |
43
|
|
|
const APPDATA_SYSTEM = 'appdata'; |
44
|
|
|
|
45
|
|
|
/** @var IRootFolder */ |
46
|
|
|
private $rootFolder; |
47
|
|
|
|
48
|
|
|
/** @var ConfigService */ |
49
|
|
|
private $configService; |
50
|
|
|
|
51
|
|
|
/** @var FolderInterface */ |
52
|
|
|
private $publicFolder; |
53
|
|
|
|
54
|
|
|
/** @var FolderInterface */ |
55
|
|
|
private $systemFolder; |
56
|
|
|
|
57
|
|
|
/** @var FolderInterface */ |
58
|
|
|
private $appDataFolder; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* FileService constructor. |
62
|
|
|
* |
63
|
|
|
* @param IRootFolder $rootFolder |
64
|
|
|
* @param ConfigService $configService |
65
|
|
|
*/ |
66
|
1 |
|
public function __construct(IRootFolder $rootFolder, ConfigService $configService) |
67
|
|
|
{ |
68
|
1 |
|
$this->rootFolder = $rootFolder; |
69
|
1 |
|
$this->configService = $configService; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string|null $folderName |
74
|
|
|
* |
75
|
|
|
* @return FolderInterface |
76
|
|
|
* @throws InvalidPathException |
77
|
|
|
* @throws NotPermittedException |
78
|
|
|
*/ |
79
|
3 |
|
public function getPublicFolder(string $folderName = null): FolderInterface |
80
|
|
|
{ |
81
|
3 |
|
if ($this->publicFolder === null) { |
82
|
1 |
|
$publicFolderBasePath = Application::getAppPath() . '/' . self::APPDATA_PUBLIC; |
83
|
1 |
|
$this->publicFolder = new LocalFolder('/', $publicFolderBasePath); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
if ($folderName) { |
87
|
|
|
try { |
88
|
3 |
|
return $this->publicFolder->getFolder($folderName); |
89
|
|
|
} catch (NotFoundException $e) { |
90
|
|
|
return $this->publicFolder->newFolder($folderName); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->publicFolder; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string|null $folderName |
99
|
|
|
* |
100
|
|
|
* @return FolderInterface |
101
|
|
|
* @throws InvalidPathException |
102
|
|
|
* @throws NotPermittedException |
103
|
|
|
*/ |
104
|
3 |
|
public function getSystemFolder(string $folderName = null): FolderInterface |
105
|
|
|
{ |
106
|
3 |
|
if ($this->systemFolder === null) { |
107
|
1 |
|
$systemFolderBasePath = Application::getAppPath() . '/' . self::APPDATA_SYSTEM; |
108
|
1 |
|
$this->systemFolder = new LocalFolder('/', $systemFolderBasePath); |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
if ($folderName) { |
112
|
|
|
try { |
113
|
3 |
|
return $this->systemFolder->getFolder($folderName); |
114
|
|
|
} catch (NotFoundException $e) { |
115
|
|
|
return $this->systemFolder->newFolder($folderName); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->systemFolder; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string|null $folderName |
124
|
|
|
* |
125
|
|
|
* @return FolderInterface |
126
|
|
|
* @throws InvalidPathException |
127
|
|
|
* @throws NotPermittedException |
128
|
|
|
*/ |
129
|
7 |
|
public function getAppDataFolder(string $folderName = null): FolderInterface |
130
|
|
|
{ |
131
|
7 |
|
if ($this->appDataFolder === null) { |
132
|
1 |
|
$baseFolderName = 'appdata_' . $this->configService->getSystemValue('instanceid'); |
133
|
1 |
|
$appDataFolderName = Application::APP_NAME; |
134
|
|
|
|
135
|
1 |
|
$rootFolder = new StorageFolder($this->rootFolder); |
136
|
|
|
|
137
|
|
|
try { |
138
|
1 |
|
$baseFolder = $rootFolder->getFolder($baseFolderName); |
139
|
|
|
} catch (NotFoundException $e) { |
140
|
|
|
$baseFolder = $rootFolder->newFolder($baseFolderName); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
try { |
144
|
1 |
|
$appDataFolder = $baseFolder->getFolder($appDataFolderName); |
145
|
|
|
} catch (NotFoundException $e) { |
146
|
|
|
$appDataFolder = $baseFolder->newFolder($appDataFolderName); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
$this->appDataFolder = $appDataFolder->fakeRoot(); |
150
|
|
|
} |
151
|
|
|
|
152
|
7 |
|
if ($folderName) { |
153
|
|
|
try { |
154
|
7 |
|
return $this->appDataFolder->getFolder($folderName); |
155
|
|
|
} catch (NotFoundException $e) { |
156
|
|
|
return $this->appDataFolder->newFolder($folderName); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->appDataFolder; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string|null $folderName |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
2 |
|
public function getAppDataFolderPath(string $folderName = null): string |
169
|
|
|
{ |
170
|
2 |
|
$dataFolderPath = $this->configService->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data'); |
171
|
2 |
|
$baseAppDataFolderName = 'appdata_' . $this->configService->getSystemValue('instanceid'); |
172
|
2 |
|
$appDataFolderPath = Application::APP_NAME . ($folderName ? '/' . $folderName : ''); |
173
|
|
|
|
174
|
2 |
|
return rtrim($dataFolderPath, '/') . '/' . $baseAppDataFolderName . '/' . $appDataFolderPath . '/'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
public function syncAppDataFolder() |
181
|
|
|
{ |
182
|
|
|
$baseFolderName = 'appdata_' . $this->configService->getSystemValue('instanceid'); |
183
|
|
|
$appDataFolderName = Application::APP_NAME; |
184
|
|
|
|
185
|
|
|
$rootFolder = new StorageFolder($this->rootFolder); |
186
|
|
|
$rootFolder->sync(FolderInterface::SYNC_SHALLOW); |
187
|
|
|
|
188
|
|
|
try { |
189
|
|
|
$baseFolder = $rootFolder->getFolder($baseFolderName); |
190
|
|
|
$baseFolder->sync(FolderInterface::SYNC_SHALLOW); |
191
|
|
|
} catch (NotFoundException $e) { |
192
|
|
|
$baseFolder = $rootFolder->newFolder($baseFolderName); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
try { |
196
|
|
|
$appDataFolder = $baseFolder->getFolder($appDataFolderName); |
197
|
|
|
$appDataFolder->sync(FolderInterface::SYNC_RECURSIVE); |
198
|
|
|
} catch (NotFoundException $e) { |
199
|
|
|
$appDataFolder = $baseFolder->newFolder($appDataFolderName); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->appDataFolder = $appDataFolder->fakeRoot(); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|