|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* System Controller |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace App\System; |
|
6
|
|
|
|
|
7
|
|
|
use App\Downloader; |
|
8
|
|
|
use App\Utils\Utils; |
|
9
|
|
|
use League\Flysystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Controller |
|
13
|
|
|
* @package App\System |
|
14
|
|
|
*/ |
|
15
|
|
|
class Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Flysystem lib |
|
19
|
|
|
* @var Filesystem |
|
20
|
|
|
*/ |
|
21
|
|
|
private $system; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Receives dependencies |
|
25
|
|
|
* |
|
26
|
|
|
* @param Filesystem $system |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Filesystem $system) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->system = $system; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Gets the array of the local lessons & series. |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getAllLessons() |
|
39
|
|
|
{ |
|
40
|
|
|
$array = []; |
|
41
|
|
|
$array['lessons'] = $this->getLessons(true); |
|
42
|
|
|
$array['series'] = $this->getSeries(true); |
|
43
|
|
|
|
|
44
|
|
|
Downloader::$totalLocalLessons = count($array['lessons']); |
|
45
|
|
|
|
|
46
|
|
|
return $array; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the series |
|
51
|
|
|
* |
|
52
|
|
|
* @param bool $skip |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
private function getSeries($skip = false) |
|
57
|
|
|
{ |
|
58
|
|
|
$list = $this->system->listContents(SERIES_FOLDER, true); |
|
59
|
|
|
$array = []; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($list as $entry) { |
|
62
|
|
|
if ($entry['type'] != 'file') { |
|
63
|
|
|
continue; |
|
64
|
|
|
} //skip folder, we only want the files |
|
65
|
|
|
|
|
66
|
|
|
$serie = substr($entry['dirname'], strlen(SERIES_FOLDER) + 1); |
|
67
|
|
|
$episode = (int)substr($entry['filename'], 0, strpos($entry['filename'], '-')); |
|
68
|
|
|
|
|
69
|
|
|
$array[$serie][] = $episode; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if($skip) { |
|
73
|
|
|
foreach($this->getSkipSeries() as $skipSerie => $episodes) { |
|
74
|
|
|
if(!isset($array[$skipSerie])) { |
|
75
|
|
|
$array[$skipSerie] = $episodes; |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$array[$skipSerie] = array_merge($array[$skipSerie], $episodes); |
|
80
|
|
|
$array[$skipSerie] = array_filter(array_unique($array[$skipSerie])); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $array; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Gets the lessons in the folder. |
|
89
|
|
|
* |
|
90
|
|
|
* @param bool $skip |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getLessons($skip = false) |
|
95
|
|
|
{ |
|
96
|
|
|
$list = $this->system->listContents(LESSONS_FOLDER); |
|
97
|
|
|
$array = []; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($list as $entry) { |
|
100
|
|
|
if ($entry['type'] != 'file') { |
|
101
|
|
|
continue; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$originalName = $entry['filename']; |
|
105
|
|
|
|
|
106
|
|
|
$array[] = substr($originalName, strpos($originalName, '-') + 1); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($skip) { |
|
110
|
|
|
$array = array_merge($this->getSkipLessons(), $array); |
|
111
|
|
|
$array = array_filter(array_unique($array)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $array; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Create skip file to lessons |
|
119
|
|
|
*/ |
|
120
|
|
|
public function writeSkipLessons() |
|
121
|
|
|
{ |
|
122
|
|
|
$file = LESSONS_FOLDER . '/.skip'; |
|
123
|
|
|
|
|
124
|
|
|
$lessons = serialize($this->getLessons(true)); |
|
125
|
|
|
|
|
126
|
|
|
if($this->system->has($file)) { |
|
127
|
|
|
$this->system->delete($file); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$this->system->write($file, $lessons); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* run write commands |
|
135
|
|
|
*/ |
|
136
|
|
|
public function writeSkipFiles() |
|
137
|
|
|
{ |
|
138
|
|
|
Utils::box('Creating skip files'); |
|
139
|
|
|
|
|
140
|
|
|
$this->writeSkipSeries(); |
|
141
|
|
|
Utils::write('Skip files for series created'); |
|
142
|
|
|
|
|
143
|
|
|
$this->writeSkipLessons(); |
|
144
|
|
|
Utils::write('Skip files for lesson created'); |
|
145
|
|
|
|
|
146
|
|
|
Utils::box('Finished'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Create skip file to lessons |
|
151
|
|
|
*/ |
|
152
|
|
|
public function writeSkipSeries() |
|
153
|
|
|
{ |
|
154
|
|
|
$file = SERIES_FOLDER . '/.skip'; |
|
155
|
|
|
|
|
156
|
|
|
$series = serialize($this->getSeries(true)); |
|
157
|
|
|
|
|
158
|
|
|
if($this->system->has($file)) { |
|
159
|
|
|
$this->system->delete($file); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$this->system->write($file, $series); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get skiped lessons |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getSkipLessons() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->getSkipedData(LESSONS_FOLDER . '/.skip'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Get skiped series |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
public function getSkipSeries() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->getSkipedData(SERIES_FOLDER . '/.skip'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Read skip file |
|
185
|
|
|
* |
|
186
|
|
|
* @param $pathToSkipFile |
|
187
|
|
|
* @return array|mixed |
|
188
|
|
|
*/ |
|
189
|
|
|
private function getSkipedData($pathToSkipFile) { |
|
190
|
|
|
|
|
191
|
|
|
if ($this->system->has($pathToSkipFile)) { |
|
192
|
|
|
$content = $this->system->read($pathToSkipFile); |
|
193
|
|
|
|
|
194
|
|
|
return unserialize($content); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return []; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Rename lessons, adding 0 padding to the number. |
|
202
|
|
|
*/ |
|
203
|
|
|
public function renameLessonsWithRightPadding() |
|
204
|
|
|
{ |
|
205
|
|
|
$list = $this->system->listContents(LESSONS_FOLDER); |
|
206
|
|
|
|
|
207
|
|
|
foreach ($list as $entry) { |
|
208
|
|
|
if ($entry['type'] != 'file') { |
|
209
|
|
|
continue; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$originalName = $entry['basename']; |
|
213
|
|
|
$oldNumber = substr($originalName, 0, strpos($originalName, '-')); |
|
214
|
|
|
|
|
215
|
|
|
if (strlen($oldNumber) == 4) { |
|
216
|
|
|
continue; |
|
217
|
|
|
} // already correct |
|
218
|
|
|
|
|
219
|
|
|
$newNumber = sprintf("%04d", $oldNumber); |
|
220
|
|
|
$nameWithoutNumber = substr($originalName, strpos($originalName, '-') + 1); |
|
221
|
|
|
$newName = $newNumber . '-' . $nameWithoutNumber; |
|
222
|
|
|
|
|
223
|
|
|
$this->system->rename(LESSONS_FOLDER . '/' . $originalName, LESSONS_FOLDER . '/' . $newName); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Create series folder if not exists. |
|
229
|
|
|
* |
|
230
|
|
|
* @param $serie |
|
231
|
|
|
*/ |
|
232
|
|
|
public function createSerieFolderIfNotExists($serie) |
|
233
|
|
|
{ |
|
234
|
|
|
$this->createFolderIfNotExists(SERIES_FOLDER . '/' . $serie); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Create folder if not exists. |
|
239
|
|
|
* |
|
240
|
|
|
* @param $folder |
|
241
|
|
|
*/ |
|
242
|
|
|
public function createFolderIfNotExists($folder) |
|
243
|
|
|
{ |
|
244
|
|
|
if ($this->system->has($folder) === false) { |
|
245
|
|
|
$this->system->createDir($folder); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|