PluginProbe
WPIDE – File Manager & Code Editor / trunk
WPIDE – File Manager & Code Editor vtrunk
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
← All changes | App/Services/Storage/Filesystem.php +41 -16 3.1trunk View file →
@@ -2,8 +2,9 @@
2 2
3 3 namespace WPIDE\App\Services\Storage;
4 4
5 5 use Exception;
6 +use League\Flysystem\FileNotFoundException;
6 7 use WPIDE\App\Services\Service;
7 8 use League\Flysystem\Filesystem as Flysystem;
8 9
9 10 class Filesystem implements Service
@@ -11,13 +12,15 @@
11 12 protected $root;
12 13 protected $separator;
13 14 protected $excluded_dirs;
14 15 protected $excluded_files;
16 + protected $path_prefix;
15 17
18 + /**
19 + * @var Flysystem
20 + */
16 21 protected $storage;
17 22
18 - protected $path_prefix;
19 -
20 23 public function init(array $config = [])
21 24 {
22 25 $this->separator = $config['separator'] ?? '/';
23 26 $this->root = $config['root'] ?? $this->separator;
@@ -30,9 +33,8 @@
30 33
31 34 $config = $config['config'] ?? [];
32 35
33 36 $this->storage = new Flysystem($adapter(), $config);
34 -
35 37 }
36 38
37 39 public function createDir(string $path, string $name)
38 40 {
@@ -66,9 +68,13 @@
66 68 public function isDir(string $path): bool
67 69 {
68 70 $path = $this->applyPathPrefix($path);
69 71
70 - return $this->storage->getSize($path) === false;
72 + try {
73 + return $this->storage->getSize($path) === false;
74 + }catch (\Exception $error) {
75 + return false;
76 + }
71 77 }
72 78
73 79 public function copyFile(string $source, string $destination)
74 80 {
@@ -194,8 +200,11 @@
194 200
195 201 return $this->storage->put($destination, $content);
196 202 }
197 203
204 + /**
205 + * @throws FileNotFoundException
206 + */
198 207 public function storeStream(string $path, string $name, $resource, bool $overwrite = false): bool
199 208 {
200 209 $destination = $this->joinPaths($this->applyPathPrefix($path), $name);
201 210
@@ -209,8 +218,11 @@
209 218
210 219 return $this->storage->putStream($destination, $resource);
211 220 }
212 221
222 + /**
223 + * @throws FileNotFoundException
224 + */
213 225 public function storeStreamFromContent(string $path, string $name, $content, bool $overwrite = false): bool
214 226 {
215 227 $stream = tmpfile();
216 228 fwrite($stream, $content);
@@ -239,9 +251,9 @@
239 251 {
240 252 return $this->path_prefix;
241 253 }
242 254
243 - public function listContents($path, $recursive = false): array
255 + public function listContents($path, $recursive = false, $filter = null): array
244 256 {
245 257
246 258 $results = $this->storage->listContents($path, $recursive);
247 259
@@ -264,8 +276,19 @@
264 276
265 277 });
266 278 }
267 279
280 + if(!empty($filter)) {
281 + if($filter === 'image') {
282 + $results = array_filter($results, function ($item) {
283 + if($item['type'] === 'file' && !in_array($item['extension'], ['jpg', 'jpeg', 'gif', 'png'])) {
284 + return false;
285 + }
286 + return true;
287 + });
288 + }
289 + }
290 +
268 291 return $results;
269 292 }
270 293
271 294 /**
@@ -270,13 +293,13 @@
270 293
271 294 /**
272 295 * @throws Exception
273 296 */
274 - public function getDirectoryCollection(string $path, bool $recursive = false): DirectoryCollection
297 + public function getDirectoryCollection(string $path, bool $recursive = false, $filter = null): DirectoryCollection
275 298 {
276 299 $collection = new DirectoryCollection($path);
277 300
278 - foreach ($this->listContents($this->applyPathPrefix($path), $recursive) as $entry) {
301 + foreach ($this->listContents($this->applyPathPrefix($path), $recursive, $filter) as $entry) {
279 302
280 303 // By default, only 'path' and 'type' is present
281 304
282 305 $is_dir = $entry['type'] === 'dir';
@@ -285,14 +308,14 @@
285 308 $size = isset($entry['size']) ? $entry['size'] : 0;
286 309 $timestamp = isset($entry['timestamp']) ? $entry['timestamp'] : 0;
287 310
288 311 $mime = $this->getMimetype($entry['path']);
289 - $dir_info = $is_dir ? $this->dirInfo($entry['path']) : [];
312 + $dir_info = $is_dir ? $this->dirInfo($entry['path'], $filter) : [];
290 313
291 314 $collection->addFile($entry['type'], $dir_info, $user_path, $path, $mime, $name, $size, $timestamp);
292 315 }
293 316
294 - if (! $recursive && $this->addSeparators($path) !== $this->separator) {
317 + if (empty($filter) && ! $recursive && $this->addSeparators($path) !== $this->separator) {
295 318 $collection->addFile('back', [], $this->getParent($path), $path, '', '..', 0, 0);
296 319 }
297 320
298 321 return $collection;
@@ -299,9 +322,10 @@
299 322 }
300 323
301 324 protected function isPathExcluded($path, $excluded): bool
302 325 {
303 -
326 + $path = wp_normalize_path($path);
327 +
304 328 foreach ($excluded as $exclude) {
305 329
306 330 $force_include = str_contains($exclude, "!");
307 331 $exclude = str_replace("!", "", $exclude);
@@ -319,13 +343,13 @@
319 343
320 344 return false;
321 345 }
322 346
323 - public function dirInfo($path): array
347 + public function dirInfo($path, $filter = null): array
324 348 {
325 349
326 350 $path = $this->applyPathPrefix($path);
327 - $content = $this->listContents($path);
351 + $content = $this->listContents($path, false, $filter);
328 352
329 353 $has_dirs = false;
330 354 $has_files = false;
331 355
@@ -333,9 +357,10 @@
333 357
334 358 if(!$has_dirs && $entry['type'] === 'dir') {
335 359 $has_dirs = true;
336 360 }
337 - if(!$has_files && $entry['type'] === 'files') {
361 +
362 + if(!$has_files && $entry['type'] === 'file') {
338 363 $has_files = true;
339 364 }
340 365
341 366 if($has_dirs && $has_files) {
@@ -352,20 +377,20 @@
352 377
353 378 /**
354 379 * @throws Exception
355 380 */
356 - public function getDirSize($path): int
381 + public function getDirSize($path, $filter = null): int
357 382 {
358 383
359 384 $size = 0;
360 385
361 - foreach ($this->getDirectoryCollection($path)->all() as $entry) {
386 + foreach ($this->getDirectoryCollection($path, false, $filter = null)->all() as $entry) {
362 387
363 388 if($entry['type'] === 'back') {
364 389 continue;
365 390 }
366 391
367 - $size += $entry['type'] === 'file' && isset($entry['size']) ? $entry['size'] : $this->getDirSize($entry['path']);
392 + $size += $entry['type'] === 'file' && isset($entry['size']) ? $entry['size'] : $this->getDirSize($entry['path'], $filter);
368 393 }
369 394
370 395 return $size;
371 396 }