1
|
|
|
<?php |
2
|
|
|
namespace Tartana\Console\Command; |
3
|
|
|
|
4
|
|
|
use League\Flysystem\Adapter\Local; |
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Tartana\Component\Command\Command; |
11
|
|
|
use Tartana\Mixins\LoggerAwareTrait; |
12
|
|
|
use Tartana\Util; |
13
|
|
|
|
14
|
|
|
class ProcessDiscFolderCommand extends SymfonyCommand |
15
|
|
|
{ |
16
|
|
|
use LoggerAwareTrait; |
17
|
|
|
|
18
|
6 |
|
public function __construct() |
19
|
|
|
{ |
20
|
6 |
|
parent::__construct('process:disc'); |
21
|
6 |
|
} |
22
|
|
|
|
23
|
6 |
|
protected function configure() |
24
|
|
|
{ |
25
|
6 |
|
$this->setDescription( |
26
|
6 |
|
'Converts folders with the name CD and which contains mp3 files to a flat structure. This command is running in foreground!' |
27
|
|
|
); |
28
|
|
|
|
29
|
6 |
|
$this->addArgument('source', InputArgument::REQUIRED, 'The folder to process.'); |
30
|
6 |
|
} |
31
|
|
|
|
32
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
4 |
|
$source = Util::realPath($input->getArgument('source')); |
35
|
4 |
|
if (empty($source)) { |
36
|
1 |
|
$this->log('Source directory no found to convert.', Logger::ERROR); |
37
|
1 |
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
$source = new Local($source); |
41
|
|
|
|
42
|
3 |
|
foreach ($source->listContents('', true) as $directory) { |
43
|
3 |
|
if ($directory['type'] != 'dir') { |
44
|
|
|
// Not a directory |
45
|
3 |
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
$dirName = basename($directory['path']); |
49
|
3 |
|
if ($dirName != 'CD' && $dirName != 'Cover') { |
50
|
|
|
// Not meant to be processed |
51
|
3 |
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
foreach ($source->listContents($directory['path'], false) as $file) { |
55
|
3 |
|
if ($file['type'] != 'file') { |
56
|
2 |
|
continue; |
57
|
|
|
} |
58
|
3 |
|
$source->rename($file['path'], dirname(dirname($file['path'])) . '/' . basename($file['path'])); |
59
|
|
|
} |
60
|
3 |
|
if (empty($source->listContents($directory['path'], false))) { |
61
|
3 |
|
$source->deleteDir($directory['path']); |
62
|
|
|
} |
63
|
|
|
} |
64
|
3 |
|
} |
65
|
|
|
} |
66
|
|
|
|