Implement Symfony finder to replace some scandirs.

This commit is contained in:
Buster "Silver Eagle" Neece 2018-07-06 21:41:07 -05:00
parent 3cb3f7bd30
commit 6be12cf98d
6 changed files with 98 additions and 88 deletions

View File

@ -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]]);
}
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Controller\Stations\Files;
use App\Cache;
use App\Csrf;
use App\Flash;
use App\Url;
@ -24,6 +25,9 @@ abstract class FilesControllerAbstract
/** @var string */
protected $csrf_namespace = 'stations_files';
/** @var Cache */
protected $cache;
/** @var array */
protected $form_config;
@ -40,12 +44,13 @@ abstract class FilesControllerAbstract
* @param Url $url
* @param array $form_config
*/
public function __construct(EntityManager $em, Flash $flash, Url $url, Csrf $csrf, array $form_config)
public function __construct(EntityManager $em, Flash $flash, Url $url, Csrf $csrf, Cache $cache, array $form_config)
{
$this->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];
}
}
}

View File

@ -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
);
};
}
}
}

View File

@ -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;
}
}
}

View File

@ -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",

100
composer.lock generated
View File

@ -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",