1
|
|
|
<?php |
2
|
|
|
namespace Tartana\Event\Listener; |
3
|
|
|
use Joomla\Registry\Registry; |
4
|
|
|
use League\Flysystem\Adapter\Local; |
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Tartana\Component\Command\Command; |
7
|
|
|
use Tartana\Component\Command\Runner; |
8
|
|
|
use Tartana\Domain\Command\ChangeDownloadState; |
9
|
|
|
use Tartana\Entity\Download; |
10
|
|
|
use Tartana\Event\CommandEvent; |
11
|
|
|
use Tartana\Event\DownloadsCompletedEvent; |
12
|
|
|
use Tartana\Mixins\LoggerAwareTrait; |
13
|
|
|
use Tartana\Util; |
14
|
|
|
use League\Flysystem\Config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Converts downloads which are mp4 to mp3. |
18
|
|
|
*/ |
19
|
|
|
class ConvertSoundListener |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use LoggerAwareTrait; |
23
|
|
|
|
24
|
|
|
private $runner = null; |
25
|
|
|
|
26
|
|
|
private $configuration = null; |
27
|
|
|
|
28
|
23 |
|
public function __construct (Runner $runner, Registry $configuration) |
29
|
|
|
{ |
30
|
23 |
|
$this->runner = $runner; |
31
|
23 |
|
$this->configuration = $configuration; |
32
|
23 |
|
} |
33
|
|
|
|
34
|
5 |
|
public function onConvertDownloads (DownloadsCompletedEvent $event) |
35
|
|
|
{ |
36
|
5 |
|
$this->log('Started sound conversion', Logger::INFO); |
37
|
|
|
|
38
|
5 |
|
$destination = Util::realPath($this->configuration->get('sound.destination')); |
39
|
5 |
|
if (! empty($destination)) |
40
|
|
|
{ |
41
|
4 |
|
$destination = new Local($destination); |
42
|
|
|
} |
43
|
5 |
|
if (! $event->getDownloads() || ! $destination) |
44
|
|
|
{ |
45
|
2 |
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
$soundHostFilter = $this->configuration->get('sound.hostFilter'); |
49
|
3 |
|
$this->log('Host filter is: ' . $soundHostFilter); |
50
|
|
|
|
51
|
3 |
|
foreach ($event->getDownloads() as $download) |
52
|
|
|
{ |
53
|
3 |
|
if (! Util::endsWith($download->getFileName(), '.mp4')) |
54
|
|
|
{ |
55
|
1 |
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
if ($soundHostFilter && ! preg_match("/" . $soundHostFilter . "/", $download->getLink())) |
59
|
|
|
{ |
60
|
1 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
$destName = basename($download->getDestination()); |
64
|
3 |
|
if (! $destination->has($destName)) |
65
|
|
|
{ |
66
|
3 |
|
$destination->createDir($destName, new Config()); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
$this->log('Starting to convert the file ' . $download->getDestination() . '/' . $download->getFileName() . ' to mp3'); |
70
|
|
|
|
71
|
3 |
|
$command = new Command('ffmpeg'); |
72
|
3 |
|
$command->setAsync(false); |
73
|
3 |
|
$command->addArgument('-i'); |
74
|
3 |
|
$command->addArgument($download->getDestination() . '/' . $download->getFileName()); |
75
|
3 |
|
$command->addArgument('-f mp3', false); |
76
|
3 |
|
$command->addArgument('-ab 192k', false); |
77
|
3 |
|
$command->addArgument('-y', false); |
78
|
3 |
|
$command->addArgument('-vn'); |
79
|
3 |
|
$command->addArgument(str_replace('.mp4', '.mp3', $destination->applyPathPrefix($destName) . '/' . $download->getFileName())); |
80
|
|
|
|
81
|
3 |
|
$this->runner->execute($command); |
82
|
|
|
|
83
|
3 |
|
$this->log('Finished to convert the file ' . $download->getDestination() . '/' . $download->getFileName() . ' to mp3'); |
84
|
|
|
} |
85
|
3 |
|
$this->log('Finished sound conversion', Logger::INFO); |
86
|
3 |
|
} |
87
|
|
|
|
88
|
18 |
View Code Duplication |
public function onChangeDownloadStateAfter (CommandEvent $event) |
|
|
|
|
89
|
|
|
{ |
90
|
18 |
|
if (! $event->getCommand() instanceof ChangeDownloadState) |
91
|
|
|
{ |
92
|
14 |
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
if ($event->getCommand()->getToState() != Download::STATE_DOWNLOADING_NOT_STARTED && |
96
|
8 |
|
$event->getCommand()->getToState() != Download::STATE_DOWNLOADING_COMPLETED) |
97
|
|
|
{ |
98
|
1 |
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
$destination = Util::realPath($this->configuration->get('sound.destination')); |
102
|
7 |
|
if (empty($destination)) |
103
|
|
|
{ |
104
|
5 |
|
return; |
105
|
|
|
} |
106
|
|
|
else |
107
|
|
|
{ |
108
|
2 |
|
$destination = new Local($destination); |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
$this->log('Cleaning up the destination ' . $destination->getPathPrefix() . ' for sound conversion', Logger::INFO); |
112
|
|
|
|
113
|
2 |
|
$directories = []; |
114
|
2 |
|
foreach ($event->getCommand()->getDownloads() as $download) |
115
|
|
|
{ |
116
|
2 |
|
$directories[basename($download->getDestination())] = basename($download->getDestination()); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
foreach ($directories as $dirName) |
120
|
|
|
{ |
121
|
2 |
|
if (! $destination->has($dirName)) |
122
|
|
|
{ |
123
|
1 |
|
continue; |
124
|
|
|
} |
125
|
1 |
|
$this->log('Deleting the directory ' . $destination->applyPathPrefix($dirName)); |
126
|
1 |
|
$destination->deleteDir($dirName); |
127
|
|
|
} |
128
|
2 |
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.