| @@ -2,9 +2,8 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace WPIDE\App\Services\Storage; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | -use League\Flysystem\FileNotFoundException; | |
| 7 | 6 | use WPIDE\App\Services\Service; |
| 8 | 7 | use League\Flysystem\Filesystem as Flysystem; |
| 9 | 8 | |
| 10 | 9 | class Filesystem implements Service |
| @@ -12,15 +11,13 @@ | ||
| 12 | 11 | protected $root; |
| 13 | 12 | protected $separator; |
| 14 | 13 | protected $excluded_dirs; |
| 15 | 14 | protected $excluded_files; |
| 16 | - protected $path_prefix; | |
| 17 | 15 | |
| 18 | - /** | |
| 19 | - * @var Flysystem | |
| 20 | - */ | |
| 21 | 16 | protected $storage; |
| 22 | 17 | |
| 18 | + protected $path_prefix; | |
| 19 | + | |
| 23 | 20 | public function init(array $config = []) |
| 24 | 21 | { |
| 25 | 22 | $this->separator = $config['separator'] ?? '/'; |
| 26 | 23 | $this->root = $config['root'] ?? $this->separator; |
| @@ -33,8 +30,9 @@ | ||
| 33 | 30 | |
| 34 | 31 | $config = $config['config'] ?? []; |
| 35 | 32 | |
| 36 | 33 | $this->storage = new Flysystem($adapter(), $config); |
| 34 | + | |
| 37 | 35 | } |
| 38 | 36 | |
| 39 | 37 | public function createDir(string $path, string $name) |
| 40 | 38 | { |
| @@ -200,11 +198,8 @@ | ||
| 200 | 198 | |
| 201 | 199 | return $this->storage->put($destination, $content); |
| 202 | 200 | } |
| 203 | 201 | |
| 204 | - /** | |
| 205 | - * @throws FileNotFoundException | |
| 206 | - */ | |
| 207 | 202 | public function storeStream(string $path, string $name, $resource, bool $overwrite = false): bool |
| 208 | 203 | { |
| 209 | 204 | $destination = $this->joinPaths($this->applyPathPrefix($path), $name); |
| 210 | 205 | |
| @@ -218,11 +213,8 @@ | ||
| 218 | 213 | |
| 219 | 214 | return $this->storage->putStream($destination, $resource); |
| 220 | 215 | } |
| 221 | 216 | |
| 222 | - /** | |
| 223 | - * @throws FileNotFoundException | |
| 224 | - */ | |
| 225 | 217 | public function storeStreamFromContent(string $path, string $name, $content, bool $overwrite = false): bool |
| 226 | 218 | { |
| 227 | 219 | $stream = tmpfile(); |
| 228 | 220 | fwrite($stream, $content); |
| @@ -251,9 +243,9 @@ | ||
| 251 | 243 | { |
| 252 | 244 | return $this->path_prefix; |
| 253 | 245 | } |
| 254 | 246 | |
| 255 | - public function listContents($path, $recursive = false, $filter = null): array | |
| 247 | + public function listContents($path, $recursive = false): array | |
| 256 | 248 | { |
| 257 | 249 | |
| 258 | 250 | $results = $this->storage->listContents($path, $recursive); |
| 259 | 251 | |
| @@ -276,19 +268,8 @@ | ||
| 276 | 268 | |
| 277 | 269 | }); |
| 278 | 270 | } |
| 279 | 271 | |
| 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 | - | |
| 291 | 272 | return $results; |
| 292 | 273 | } |
| 293 | 274 | |
| 294 | 275 | /** |
| @@ -293,13 +274,13 @@ | ||
| 293 | 274 | |
| 294 | 275 | /** |
| 295 | 276 | * @throws Exception |
| 296 | 277 | */ |
| 297 | - public function getDirectoryCollection(string $path, bool $recursive = false, $filter = null): DirectoryCollection | |
| 278 | + public function getDirectoryCollection(string $path, bool $recursive = false): DirectoryCollection | |
| 298 | 279 | { |
| 299 | 280 | $collection = new DirectoryCollection($path); |
| 300 | 281 | |
| 301 | - foreach ($this->listContents($this->applyPathPrefix($path), $recursive, $filter) as $entry) { | |
| 282 | + foreach ($this->listContents($this->applyPathPrefix($path), $recursive) as $entry) { | |
| 302 | 283 | |
| 303 | 284 | // By default, only 'path' and 'type' is present |
| 304 | 285 | |
| 305 | 286 | $is_dir = $entry['type'] === 'dir'; |
| @@ -308,14 +289,14 @@ | ||
| 308 | 289 | $size = isset($entry['size']) ? $entry['size'] : 0; |
| 309 | 290 | $timestamp = isset($entry['timestamp']) ? $entry['timestamp'] : 0; |
| 310 | 291 | |
| 311 | 292 | $mime = $this->getMimetype($entry['path']); |
| 312 | - $dir_info = $is_dir ? $this->dirInfo($entry['path'], $filter) : []; | |
| 293 | + $dir_info = $is_dir ? $this->dirInfo($entry['path']) : []; | |
| 313 | 294 | |
| 314 | 295 | $collection->addFile($entry['type'], $dir_info, $user_path, $path, $mime, $name, $size, $timestamp); |
| 315 | 296 | } |
| 316 | 297 | |
| 317 | - if (empty($filter) && ! $recursive && $this->addSeparators($path) !== $this->separator) { | |
| 298 | + if (! $recursive && $this->addSeparators($path) !== $this->separator) { | |
| 318 | 299 | $collection->addFile('back', [], $this->getParent($path), $path, '', '..', 0, 0); |
| 319 | 300 | } |
| 320 | 301 | |
| 321 | 302 | return $collection; |
| @@ -322,10 +303,9 @@ | ||
| 322 | 303 | } |
| 323 | 304 | |
| 324 | 305 | protected function isPathExcluded($path, $excluded): bool |
| 325 | 306 | { |
| 326 | - $path = wp_normalize_path($path); | |
| 327 | - | |
| 307 | + | |
| 328 | 308 | foreach ($excluded as $exclude) { |
| 329 | 309 | |
| 330 | 310 | $force_include = str_contains($exclude, "!"); |
| 331 | 311 | $exclude = str_replace("!", "", $exclude); |
| @@ -343,13 +323,13 @@ | ||
| 343 | 323 | |
| 344 | 324 | return false; |
| 345 | 325 | } |
| 346 | 326 | |
| 347 | - public function dirInfo($path, $filter = null): array | |
| 327 | + public function dirInfo($path): array | |
| 348 | 328 | { |
| 349 | 329 | |
| 350 | 330 | $path = $this->applyPathPrefix($path); |
| 351 | - $content = $this->listContents($path, false, $filter); | |
| 331 | + $content = $this->listContents($path); | |
| 352 | 332 | |
| 353 | 333 | $has_dirs = false; |
| 354 | 334 | $has_files = false; |
| 355 | 335 | |
| @@ -357,10 +337,9 @@ | ||
| 357 | 337 | |
| 358 | 338 | if(!$has_dirs && $entry['type'] === 'dir') { |
| 359 | 339 | $has_dirs = true; |
| 360 | 340 | } |
| 361 | - | |
| 362 | - if(!$has_files && $entry['type'] === 'file') { | |
| 341 | + if(!$has_files && $entry['type'] === 'files') { | |
| 363 | 342 | $has_files = true; |
| 364 | 343 | } |
| 365 | 344 | |
| 366 | 345 | if($has_dirs && $has_files) { |
| @@ -377,20 +356,20 @@ | ||
| 377 | 356 | |
| 378 | 357 | /** |
| 379 | 358 | * @throws Exception |
| 380 | 359 | */ |
| 381 | - public function getDirSize($path, $filter = null): int | |
| 360 | + public function getDirSize($path): int | |
| 382 | 361 | { |
| 383 | 362 | |
| 384 | 363 | $size = 0; |
| 385 | 364 | |
| 386 | - foreach ($this->getDirectoryCollection($path, false, $filter = null)->all() as $entry) { | |
| 365 | + foreach ($this->getDirectoryCollection($path)->all() as $entry) { | |
| 387 | 366 | |
| 388 | 367 | if($entry['type'] === 'back') { |
| 389 | 368 | continue; |
| 390 | 369 | } |
| 391 | 370 | |
| 392 | - $size += $entry['type'] === 'file' && isset($entry['size']) ? $entry['size'] : $this->getDirSize($entry['path'], $filter); | |
| 371 | + $size += $entry['type'] === 'file' && isset($entry['size']) ? $entry['size'] : $this->getDirSize($entry['path']); | |
| 393 | 372 | } |
| 394 | 373 | |
| 395 | 374 | return $size; |
| 396 | 375 | } |