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\Folder as OCFolder; |
31
|
|
|
use OCP\Files\InvalidPathException; |
32
|
|
|
use OCP\Files\NotPermittedException; |
33
|
|
|
use OCP\IDBConnection; |
34
|
|
|
use OCP\ILogger; |
35
|
|
|
use OCP\ITempManager; |
36
|
|
|
|
37
|
|
|
class StorageFolder extends AbstractStorageNode implements FolderInterface |
38
|
|
|
{ |
39
|
|
|
use FolderTrait; |
40
|
|
|
|
41
|
|
|
/** @var array<string,string> */ |
42
|
|
|
private static $localPathCache = []; |
43
|
|
|
|
44
|
|
|
/** @var OCFolder */ |
45
|
|
|
protected $node; |
46
|
|
|
|
47
|
|
|
/** @var ITempManager */ |
48
|
|
|
private $tempManager; |
49
|
|
|
|
50
|
|
|
/** @var IDBConnection */ |
51
|
|
|
private $connection; |
52
|
|
|
|
53
|
|
|
/** @var ILogger */ |
54
|
|
|
private $logger; |
55
|
|
|
|
56
|
|
|
/** @var IEventDispatcher|null */ |
57
|
|
|
private $eventDispatcher; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* StorageFolder constructor. |
61
|
|
|
* |
62
|
|
|
* @param OCFolder $folder |
63
|
|
|
* @param string|null $basePath |
64
|
|
|
* |
65
|
|
|
* @throws InvalidPathException |
66
|
|
|
*/ |
67
|
7 |
|
public function __construct(OCFolder $folder, string $basePath = null) |
68
|
|
|
{ |
69
|
7 |
|
$this->tempManager = \OC::$server->getTempManager(); |
70
|
7 |
|
$this->connection = \OC::$server->query(IDBConnection::class); |
71
|
7 |
|
$this->logger = \OC::$server->query(ILogger::class); |
72
|
|
|
|
73
|
7 |
|
parent::__construct($folder, $basePath); |
74
|
7 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
2 |
|
public function getLocalPath(): string |
80
|
|
|
{ |
81
|
2 |
|
if ($this->isLocal()) { |
82
|
2 |
|
return parent::getLocalPath(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$cachePath = $this->getOCNode()->getPath(); |
86
|
|
|
if (!isset(self::$localPathCache[$cachePath])) { |
87
|
|
|
self::$localPathCache[$cachePath] = $this->tempManager->getTemporaryFolder(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return self::$localPathCache[$cachePath]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
|
|
public function listing(): array |
97
|
|
|
{ |
98
|
|
|
return iterator_to_array($this->getGenerator()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
2 |
|
protected function getGenerator(): \Generator |
105
|
|
|
{ |
106
|
2 |
|
$basePath = $this->getPath(); |
107
|
2 |
|
foreach ($this->node->getDirectoryListing() as $node) { |
108
|
2 |
|
yield $this->repackNode($node, $basePath); |
109
|
|
|
} |
110
|
2 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function exists(string $path): bool |
116
|
|
|
{ |
117
|
|
|
// check for root path breakouts |
118
|
|
|
$this->getBasePath($path); |
119
|
|
|
|
120
|
|
|
return $this->node->nodeExists($path); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
7 |
|
public function get(string $path): NodeInterface |
127
|
|
|
{ |
128
|
7 |
|
$basePath = $this->getBasePath($path); |
129
|
7 |
|
return $this->repackNode($this->node->get($path), $basePath); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
5 |
|
public function newFolder(string $path): FolderInterface |
136
|
|
|
{ |
137
|
5 |
|
$basePath = $this->getBasePath($path); |
138
|
5 |
|
return new StorageFolder($this->node->newFolder($path), $basePath); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
4 |
|
public function newFile(string $path): FileInterface |
145
|
|
|
{ |
146
|
4 |
|
$basePath = $this->getBasePath($path); |
147
|
4 |
|
return new StorageFile($this->node->newFile($path), $basePath); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
3 |
|
public function fakeRoot(): FolderInterface |
154
|
|
|
{ |
155
|
3 |
|
return new StorageFolder($this->node); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
5 |
|
public function sync(bool $recursive = FolderInterface::SYNC_RECURSIVE) |
162
|
|
|
{ |
163
|
|
|
// TODO >= NC 18: Remove version switch |
164
|
5 |
|
list($majorVersion) = \OC_Util::getVersion(); |
165
|
5 |
|
if ($majorVersion >= 18) { |
166
|
5 |
|
if ($this->eventDispatcher === null) { |
167
|
5 |
|
$this->eventDispatcher = \OC::$server->query(IEventDispatcher::class); |
168
|
|
|
} |
169
|
|
|
|
170
|
5 |
|
$scanner = new Scanner(null, $this->connection, $this->eventDispatcher, $this->logger); |
171
|
|
|
} else { |
172
|
|
|
$scanner = new Scanner(null, $this->connection, $this->logger); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
try { |
176
|
5 |
|
$scanner->scan($this->node->getPath(), $recursive); |
177
|
|
|
} catch (ForbiddenException $e) { |
178
|
|
|
throw new NotPermittedException(); |
179
|
|
|
} |
180
|
5 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritDoc} |
184
|
|
|
*/ |
185
|
|
|
public function isCreatable(): bool |
186
|
|
|
{ |
187
|
|
|
return $this->node->isCreatable(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $path |
192
|
|
|
* |
193
|
|
|
* @return string|null |
194
|
|
|
* @throws InvalidPathException |
195
|
|
|
*/ |
196
|
7 |
|
private function getBasePath(string $path) |
197
|
|
|
{ |
198
|
7 |
|
$path = $this->normalizePath($this->getPath() . '/' . $path); |
199
|
7 |
|
return ($path !== '/') ? dirname($path) : null; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|