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\Files; |
26
|
|
|
|
27
|
|
|
use OC\Files\Utils\Scanner; |
28
|
|
|
use OC\ForbiddenException; |
29
|
|
|
use OCP\EventDispatcher\IEventDispatcher; |
30
|
|
|
use OCP\Files\AlreadyExistsException; |
31
|
|
|
use OCP\Files\Folder as OCFolder; |
32
|
|
|
use OCP\Files\InvalidPathException; |
33
|
|
|
use OCP\Files\NotPermittedException; |
34
|
|
|
use OCP\IDBConnection; |
35
|
|
|
use OCP\ILogger; |
36
|
|
|
use OCP\ITempManager; |
37
|
|
|
|
38
|
|
|
class StorageFolder extends AbstractStorageNode implements FolderInterface |
39
|
|
|
{ |
40
|
|
|
use FolderTrait; |
41
|
|
|
|
42
|
|
|
/** @var array<string,string> */ |
43
|
|
|
private static $localPathCache = []; |
44
|
|
|
|
45
|
|
|
/** @var OCFolder */ |
46
|
|
|
protected $node; |
47
|
|
|
|
48
|
|
|
/** @var ITempManager */ |
49
|
|
|
private $tempManager; |
50
|
|
|
|
51
|
|
|
/** @var IDBConnection */ |
52
|
|
|
private $connection; |
53
|
|
|
|
54
|
|
|
/** @var ILogger */ |
55
|
|
|
private $logger; |
56
|
|
|
|
57
|
|
|
/** @var IEventDispatcher */ |
58
|
|
|
private $eventDispatcher; |
59
|
|
|
|
60
|
|
|
/** @var StorageFolder|null */ |
61
|
|
|
protected $rootFolder; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* StorageFolder constructor. |
65
|
|
|
* |
66
|
|
|
* @param OCFolder $folder |
67
|
|
|
* @param string|null $parentPath |
68
|
|
|
* |
69
|
|
|
* @throws InvalidPathException |
70
|
|
|
*/ |
71
|
29 |
|
public function __construct(OCFolder $folder, string $parentPath = null) |
72
|
|
|
{ |
73
|
29 |
|
$this->tempManager = \OC::$server->getTempManager(); |
|
|
|
|
74
|
29 |
|
$this->connection = \OC::$server->query(IDBConnection::class); |
|
|
|
|
75
|
29 |
|
$this->logger = \OC::$server->query(ILogger::class); |
|
|
|
|
76
|
29 |
|
$this->eventDispatcher = \OC::$server->query(IEventDispatcher::class); |
|
|
|
|
77
|
|
|
|
78
|
29 |
|
parent::__construct($folder, $parentPath); |
79
|
29 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
5 |
|
public function getLocalPath(): string |
85
|
|
|
{ |
86
|
5 |
|
if ($this->isLocal()) { |
87
|
5 |
|
return parent::getLocalPath(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$cachePath = $this->getOCNode()->getPath(); |
91
|
|
|
if (!isset(self::$localPathCache[$cachePath])) { |
92
|
|
|
self::$localPathCache[$cachePath] = $this->tempManager->getTemporaryFolder(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return self::$localPathCache[$cachePath]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
*/ |
101
|
|
|
public function listing(): array |
102
|
|
|
{ |
103
|
|
|
return iterator_to_array($this->getGenerator()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
18 |
|
protected function getGenerator(): \Generator |
110
|
|
|
{ |
111
|
18 |
|
$parentPath = $this->getPath(); |
112
|
18 |
|
foreach ($this->node->getDirectoryListing() as $node) { |
113
|
15 |
|
yield $this->repackNode($node, $parentPath); |
114
|
|
|
} |
115
|
18 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
5 |
|
public function exists(string $path): bool |
121
|
|
|
{ |
122
|
5 |
|
$path = $this->normalizePath($this->getPath() . '/' . $path); |
123
|
5 |
|
return $this->getRootFolder()->getOCNode()->nodeExists($path); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritDoc} |
128
|
|
|
*/ |
129
|
29 |
|
public function get(string $path): NodeInterface |
130
|
|
|
{ |
131
|
29 |
|
$path = $this->normalizePath($this->getPath() . '/' . $path); |
132
|
29 |
|
$parentPath = ($path !== '/') ? dirname($path) : null; |
133
|
29 |
|
return $this->repackNode($this->getRootFolder()->getOCNode()->get($path), $parentPath); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
5 |
|
public function newFolder(string $path): FolderInterface |
140
|
|
|
{ |
141
|
5 |
|
if ($this->exists($path)) { |
142
|
|
|
throw new AlreadyExistsException(); |
143
|
|
|
} |
144
|
|
|
|
145
|
5 |
|
$path = $this->normalizePath($this->getPath() . '/' . $path); |
146
|
|
|
|
147
|
5 |
|
$name = basename($path); |
148
|
5 |
|
$parentPath = dirname($path); |
149
|
|
|
|
150
|
|
|
/** @var StorageFolder $parentFolder */ |
151
|
5 |
|
$parentFolder = $this->newFolderRecursive($parentPath); |
152
|
|
|
|
153
|
5 |
|
if (!$parentFolder->isCreatable()) { |
154
|
|
|
throw new NotPermittedException(); |
155
|
|
|
} |
156
|
|
|
|
157
|
5 |
|
return new StorageFolder( |
158
|
5 |
|
$parentFolder->getOCNode()->newFolder($name), |
159
|
5 |
|
($path !== '/') ? $parentPath : null |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritDoc} |
165
|
|
|
*/ |
166
|
5 |
|
public function newFile(string $path): FileInterface |
167
|
|
|
{ |
168
|
5 |
|
if ($this->exists($path)) { |
169
|
|
|
throw new AlreadyExistsException(); |
170
|
|
|
} |
171
|
|
|
|
172
|
5 |
|
$path = $this->normalizePath($this->getPath() . '/' . $path); |
173
|
|
|
|
174
|
5 |
|
$name = basename($path); |
175
|
5 |
|
$parentPath = dirname($path); |
176
|
|
|
|
177
|
|
|
/** @var StorageFolder $parentFolder */ |
178
|
5 |
|
$parentFolder = $this->newFolderRecursive($parentPath); |
179
|
|
|
|
180
|
5 |
|
if (!$parentFolder->isCreatable()) { |
181
|
|
|
throw new NotPermittedException(); |
182
|
|
|
} |
183
|
|
|
|
184
|
5 |
|
return new StorageFile( |
185
|
5 |
|
$parentFolder->getOCNode()->newFile($name), |
186
|
5 |
|
($path !== '/') ? $parentPath : null |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritDoc} |
192
|
|
|
*/ |
193
|
9 |
|
public function fakeRoot(): FolderInterface |
194
|
|
|
{ |
195
|
9 |
|
return new StorageFolder($this->node); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritDoc} |
200
|
|
|
*/ |
201
|
21 |
|
public function sync(bool $recursive = FolderInterface::SYNC_RECURSIVE): void |
202
|
|
|
{ |
203
|
21 |
|
$scanner = new Scanner(null, $this->connection, $this->eventDispatcher, $this->logger); |
204
|
|
|
|
205
|
|
|
try { |
206
|
21 |
|
$scanner->scan($this->node->getPath(), $recursive); |
207
|
|
|
} catch (ForbiddenException $e) { |
208
|
|
|
throw new NotPermittedException(); |
209
|
|
|
} |
210
|
21 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritDoc} |
214
|
|
|
*/ |
215
|
5 |
|
public function isCreatable(): bool |
216
|
|
|
{ |
217
|
5 |
|
return $this->node->isCreatable(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritDoc} |
222
|
|
|
*/ |
223
|
29 |
|
protected function getRootFolder(): self |
224
|
|
|
{ |
225
|
29 |
|
if ($this->getPath() === '/') { |
226
|
29 |
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
12 |
|
if ($this->rootFolder === null) { |
230
|
12 |
|
$ocFolder = $this->node; |
231
|
12 |
|
for ($i = 0; $i < substr_count($this->getPath(), '/'); $i++) { |
232
|
12 |
|
$ocFolder = $ocFolder->getParent(); |
233
|
|
|
} |
234
|
|
|
|
235
|
12 |
|
$this->rootFolder = new StorageFolder($ocFolder); |
236
|
|
|
} |
237
|
|
|
|
238
|
12 |
|
return $this->rootFolder; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.