From 6be12cf98d8c8204b9eadc44dd71cd89b4cf96e4 Mon Sep 17 00:00:00 2001 From: "Buster \"Silver Eagle\" Neece" Date: Fri, 6 Jul 2018 21:41:07 -0500 Subject: [PATCH] Implement Symfony finder to replace some scandirs. --- .../Stations/Files/FilesController.php | 62 +++++------ .../Files/FilesControllerAbstract.php | 9 +- .../Controller/Stations/StationsProvider.php | 4 +- .../Stations/Traits/SongHistoryFilters.php | 8 +- composer.json | 3 +- composer.lock | 100 +++++++++--------- 6 files changed, 98 insertions(+), 88 deletions(-) diff --git a/app/src/Controller/Stations/Files/FilesController.php b/app/src/Controller/Stations/Files/FilesController.php index dea0775c8..93bdfb325 100644 --- a/app/src/Controller/Stations/Files/FilesController.php +++ b/app/src/Controller/Stations/Files/FilesController.php @@ -7,6 +7,7 @@ use App\Http\Response; use App\Mvc\View; use App\Utilities; use AzuraCast\Radio\Backend\BackendAbstract; +use Symfony\Component\Finder\Finder; /** * Class FilesController @@ -131,13 +132,13 @@ class FilesController extends FilesControllerAbstract if (is_dir($file_path)) { $media_in_dir_raw = $this->em->createQuery('SELECT - partial sm.{id, unique_id, path, length, length_text, artist, title, album}, partial spm.{id}, partial sp.{id, name}, partial smcf.{id, field_id, value} - FROM Entity\StationMedia sm - LEFT JOIN sm.custom_fields smcf - LEFT JOIN sm.playlist_items spm - LEFT JOIN spm.playlist sp - WHERE sm.station_id = :station_id - AND sm.path LIKE :path') + partial sm.{id, unique_id, path, length, length_text, artist, title, album}, partial spm.{id}, partial sp.{id, name}, partial smcf.{id, field_id, value} + FROM Entity\StationMedia sm + LEFT JOIN sm.custom_fields smcf + LEFT JOIN sm.playlist_items spm + LEFT JOIN spm.playlist sp + WHERE sm.station_id = :station_id + AND sm.path LIKE :path') ->setParameter('station_id', $station_id) ->setParameter('path', $file . '%') ->getArrayResult(); @@ -173,11 +174,13 @@ class FilesController extends FilesControllerAbstract if (!empty($_REQUEST['searchPhrase'])) { $files = $this->_getMusicFiles($file_path); } else { - $files = array_diff(scandir($file_path), ['.', '..']); - foreach($files as &$file) - $file = $file_path.'/'.$file; + $finder = new Finder(); + $finder->in($file_path)->depth('== 0'); - unset($file); + $files = []; + foreach($finder as $finder_file) { + $files[] = $finder_file->getPathname(); + } } foreach ($files as $i) { @@ -321,7 +324,7 @@ class FilesController extends FilesControllerAbstract $music_files = $this->_getMusicFiles($files); $files_found = count($music_files); - foreach ($music_files as $i => $file) { + foreach ($music_files as $file) { try { $media = $this->media_repo->getOrCreate($station, $file); $this->em->remove($media); @@ -520,34 +523,33 @@ class FilesController extends FilesControllerAbstract ->withBody(new \Slim\Http\Stream($fh)); } - protected function _getMusicFiles($path) + protected function _getMusicFiles($path, $recursive = true) { if (is_array($path)) { $music_files = []; foreach ($path as $dir_file) { - $music_files = array_merge($music_files, $this->_getMusicFiles($dir_file)); + $music_files = array_merge($music_files, $this->_getMusicFiles($dir_file, $recursive)); } return $music_files; } - if (is_dir($path)) { - $music_files = []; - - $files = array_diff(scandir($path), ['.', '..']); - foreach ($files as $file) { - $file_path = $path . '/' . $file; - if (is_dir($file_path)) { - $music_files = array_merge($music_files, $this->_getMusicFiles($file_path)); - } else { - $music_files[] = $file_path; - } - } - - return $music_files; + if (!is_dir($path)) { + return [$path]; } - return [$path]; + $finder = new Finder(); + $finder = $finder->files()->in($path); + + if (!$recursive) { + $finder = $finder->depth('== 0'); + } + + $music_files = []; + foreach($finder as $file) { + $music_files[] = $file->getRelativePathname(); + } + return $music_files; } protected function _is_recursively_deleteable($d) @@ -582,4 +584,4 @@ class FilesController extends FilesControllerAbstract ->withStatus($code) ->withJson(['error' => ['code' => (int)$code, 'msg' => $msg]]); } -} \ No newline at end of file +} diff --git a/app/src/Controller/Stations/Files/FilesControllerAbstract.php b/app/src/Controller/Stations/Files/FilesControllerAbstract.php index a96856f11..0845ae676 100644 --- a/app/src/Controller/Stations/Files/FilesControllerAbstract.php +++ b/app/src/Controller/Stations/Files/FilesControllerAbstract.php @@ -1,6 +1,7 @@ em = $em; $this->flash = $flash; $this->url = $url; $this->csrf = $csrf; + $this->cache = $cache; $this->form_config = $form_config; $this->media_repo = $this->em->getRepository(Entity\StationMedia::class); @@ -70,4 +75,4 @@ abstract class FilesControllerAbstract return [$path, $full_path]; } -} \ No newline at end of file +} diff --git a/app/src/Controller/Stations/StationsProvider.php b/app/src/Controller/Stations/StationsProvider.php index 59cda8410..263b1701d 100644 --- a/app/src/Controller/Stations/StationsProvider.php +++ b/app/src/Controller/Stations/StationsProvider.php @@ -29,6 +29,7 @@ class StationsProvider implements ServiceProviderInterface $di[\App\Flash::class], $di[\App\Url::class], $di[\App\Csrf::class], + $di[\App\Cache::class], $config->get('forms/rename') ); }; @@ -44,6 +45,7 @@ class StationsProvider implements ServiceProviderInterface $di[\App\Flash::class], $url, $di[\App\Csrf::class], + $di[\App\Cache::class], $config->get('forms/media', [ 'url' => $url ]) @@ -148,4 +150,4 @@ class StationsProvider implements ServiceProviderInterface ); }; } -} \ No newline at end of file +} diff --git a/app/src/Controller/Stations/Traits/SongHistoryFilters.php b/app/src/Controller/Stations/Traits/SongHistoryFilters.php index 6e8acb4e2..46e1a8dc7 100644 --- a/app/src/Controller/Stations/Traits/SongHistoryFilters.php +++ b/app/src/Controller/Stations/Traits/SongHistoryFilters.php @@ -14,7 +14,7 @@ trait SongHistoryFilters protected function _getEligibleHistory($station_id) { - $cache_name = 'station_center_history_' . $station_id; + $cache_name = 'stations/'.$station_id.'/history'; $songs_played_raw = $this->cache->get($cache_name); @@ -60,7 +60,7 @@ trait SongHistoryFilters protected function _getIgnoredSongs() { - $song_hashes = $this->cache->get('station_center_ignored_songs'); + $song_hashes = $this->cache->get('stations/all/ignored_songs'); if (!$song_hashes) { $ignored_phrases = ['Offline', 'Sweeper', 'Bumper', 'Unknown']; @@ -80,9 +80,9 @@ trait SongHistoryFilters $song_hashes[$row['id']] = $row['id']; } - $this->cache->save($song_hashes, 'station_center_ignored_songs', 86400); + $this->cache->save($song_hashes, 'stations/all/ignored_songs', 86400); } return $song_hashes; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index b7860fb90..d3f87bc9d 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "gettext/gettext": "^4.4", "cakephp/chronos": "^1.1", "doctrine/data-fixtures": "^1.3", - "maxmind-db/reader": "~1.0" + "maxmind-db/reader": "~1.0", + "symfony/finder": "^4.1" }, "require-dev": { "codeception/codeception": "^2.2", diff --git a/composer.lock b/composer.lock index 5954f1263..7dba314cf 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f4a8f439fed1855416ed1d5f27ae6bdf", + "content-hash": "741784ff3d80786566bf78b9fabdc68a", "packages": [ { "name": "azuracast/azuraforms", @@ -3102,6 +3102,55 @@ "homepage": "https://symfony.com", "time": "2018-04-06T07:35:43+00:00" }, + { + "name": "symfony/finder", + "version": "v4.1.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/finder.git", + "reference": "84714b8417d19e4ba02ea78a41a975b3efaafddb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/finder/zipball/84714b8417d19e4ba02ea78a41a975b3efaafddb", + "reference": "84714b8417d19e4ba02ea78a41a975b3efaafddb", + "shasum": "" + }, + "require": { + "php": "^7.1.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Finder\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Finder Component", + "homepage": "https://symfony.com", + "time": "2018-06-19T21:38:16+00:00" + }, { "name": "symfony/options-resolver", "version": "v4.0.9", @@ -5407,55 +5456,6 @@ "homepage": "https://symfony.com", "time": "2018-03-19T22:35:49+00:00" }, - { - "name": "symfony/finder", - "version": "v4.0.9", - "source": { - "type": "git", - "url": "https://github.com/symfony/finder.git", - "reference": "ca27c02b7a3fef4828c998c2ff9ba7aae1641c49" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/finder/zipball/ca27c02b7a3fef4828c998c2ff9ba7aae1641c49", - "reference": "ca27c02b7a3fef4828c998c2ff9ba7aae1641c49", - "shasum": "" - }, - "require": { - "php": "^7.1.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "4.0-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\Finder\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony Finder Component", - "homepage": "https://symfony.com", - "time": "2018-04-04T05:10:37+00:00" - }, { "name": "symfony/process", "version": "v4.0.9",