2019-10-10 02:13:04 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-10-10 02:13:04 +00:00
|
|
|
namespace App\Controller\Api\Stations\Files;
|
|
|
|
|
|
|
|
use App\Entity;
|
2021-03-31 16:42:24 +00:00
|
|
|
use App\Flysystem\StationFilesystems;
|
2019-10-10 02:13:04 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2021-03-31 16:42:24 +00:00
|
|
|
use League\Flysystem\StorageAttributes;
|
2019-10-10 02:13:04 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
class ListDirectoriesAction
|
|
|
|
{
|
|
|
|
public function __invoke(
|
|
|
|
ServerRequest $request,
|
2021-03-31 16:42:24 +00:00
|
|
|
Response $response
|
2019-10-10 02:13:04 +00:00
|
|
|
): ResponseInterface {
|
|
|
|
$station = $request->getStation();
|
|
|
|
|
2020-11-23 06:38:49 +00:00
|
|
|
$currentDir = $request->getParam('currentDirectory', '');
|
2021-03-31 16:42:24 +00:00
|
|
|
|
2021-06-08 06:40:49 +00:00
|
|
|
$fsMedia = (new StationFilesystems($station))->getMediaFilesystem();
|
2019-10-10 02:13:04 +00:00
|
|
|
|
2020-11-10 03:06:48 +00:00
|
|
|
$protectedPaths = [Entity\StationMedia::DIR_ALBUM_ART, Entity\StationMedia::DIR_WAVEFORMS];
|
|
|
|
|
2021-03-31 16:42:24 +00:00
|
|
|
$directoriesRaw = $fsMedia->listContents($currentDir, false)->filter(
|
|
|
|
function (StorageAttributes $attrs) use ($protectedPaths) {
|
|
|
|
if (!$attrs->isDir()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-10 02:13:04 +00:00
|
|
|
|
2021-03-31 16:42:24 +00:00
|
|
|
if (in_array($attrs->path(), $protectedPaths, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-11-10 03:06:48 +00:00
|
|
|
}
|
2021-03-31 16:42:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$directories = [];
|
|
|
|
foreach ($directoriesRaw as $directory) {
|
|
|
|
/** @var StorageAttributes $directory */
|
|
|
|
$path = $directory->path();
|
2020-11-10 03:06:48 +00:00
|
|
|
|
2021-03-31 16:42:24 +00:00
|
|
|
$directories[] = [
|
|
|
|
'name' => basename($path),
|
|
|
|
'path' => $path,
|
2019-10-10 02:13:04 +00:00
|
|
|
];
|
2021-03-31 16:42:24 +00:00
|
|
|
}
|
2019-10-10 02:13:04 +00:00
|
|
|
|
2021-03-31 16:42:24 +00:00
|
|
|
return $response->withJson(
|
|
|
|
[
|
|
|
|
'rows' => $directories,
|
|
|
|
]
|
|
|
|
);
|
2019-10-10 02:13:04 +00:00
|
|
|
}
|
2020-10-14 22:19:31 +00:00
|
|
|
}
|