CategoryController   F
last analyzed

Complexity

Total Complexity 69

Size/Duplication

Total Lines 460
Duplicated Lines 0 %

Importance

Changes 20
Bugs 1 Features 0
Metric Value
eloc 277
c 20
b 1
f 0
dl 0
loc 460
rs 2.88
wmc 69

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A getTrackCount() 0 20 2
C getCategoryItems() 0 104 15
B getCategoryItemCovers() 0 35 8
A getListViewHeaders() 0 10 4
D getTracksDetails() 0 114 25
C StreamParser() 0 89 14

How to fix   Complexity   

Complex Class

Complex classes like CategoryController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CategoryController, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Audio Player
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2016-2019 Marcel Scherello
10
 */
11
12
namespace OCA\audioplayer\Controller;
13
14
use OCP\AppFramework\Controller;
15
use OCP\AppFramework\Http\JSONResponse;
16
use OCP\Files\InvalidPathException;
17
use OCP\IRequest;
18
use OCP\IL10N;
19
use OCP\IDbConnection;
20
use OCP\ITagManager;
21
use OCP\Files\IRootFolder;
22
use OCP\ILogger;
23
use \OCP\Files\NotFoundException;
24
25
/**
26
 * Controller class for main page.
27
 */
28
class CategoryController extends Controller
29
{
30
31
    private $userId;
32
    private $l10n;
33
    private $db;
34
    private $tagger;
35
    private $tagManager;
36
    private $rootFolder;
37
    private $logger;
38
    private $DBController;
39
40
    public function __construct(
41
        $appName,
42
        IRequest $request,
43
        $userId,
44
        IL10N $l10n,
45
        IDBConnection $db,
46
        ITagManager $tagManager,
47
        IRootFolder $rootFolder,
48
        ILogger $logger,
49
        DbController $DBController
50
    )
51
    {
52
        parent::__construct($appName, $request);
53
        $this->userId = $userId;
54
        $this->l10n = $l10n;
55
        $this->db = $db;
56
        $this->tagManager = $tagManager;
57
        $this->tagger = null;
58
        $this->rootFolder = $rootFolder;
59
        $this->logger = $logger;
60
        $this->DBController = $DBController;
61
    }
62
63
    /**
64
     * Get the items for the selected category
65
     *
66
     * @NoAdminRequired
67
     * @param $category
68
     * @return JSONResponse
69
     */
70
    public function getCategoryItems($category)
71
    {
72
        $SQL = null;
73
        $aPlaylists = array();
74
        if ($category === 'Artist') {
75
            $SQL = 'SELECT  DISTINCT(`AT`.`artist_id`) AS `id`, `AA`.`name`, LOWER(`AA`.`name`) AS `lower` 
76
						FROM `*PREFIX*audioplayer_tracks` `AT`
77
						JOIN `*PREFIX*audioplayer_artists` `AA`
78
						ON `AA`.`id` = `AT`.`artist_id`
79
			 			WHERE  `AT`.`user_id` = ?
80
			 			ORDER BY LOWER(`AA`.`name`) ASC
81
			 			';
82
        } elseif ($category === 'Genre') {
83
            $SQL = 'SELECT  `id`, `name`, LOWER(`name`) AS `lower` 
84
						FROM `*PREFIX*audioplayer_genre`
85
			 			WHERE  `user_id` = ?
86
			 			ORDER BY LOWER(`name`) ASC
87
			 			';
88
        } elseif ($category === 'Year') {
89
            $SQL = 'SELECT DISTINCT(`year`) AS `id` ,`year` AS `name`  
90
						FROM `*PREFIX*audioplayer_tracks`
91
			 			WHERE  `user_id` = ?
92
			 			ORDER BY `id` ASC
93
			 			';
94
        } elseif ($category === 'Title') {
95
            $SQL = "SELECT distinct('0') as `id` ,'" . $this->l10n->t('All Titles') . "' as `name`  
96
						FROM `*PREFIX*audioplayer_tracks`
97
			 			WHERE  `user_id` = ?
98
			 			";
99
        } elseif ($category === 'Playlist') {
100
            $aPlaylists[] = array('id' => 'X1', 'name' => $this->l10n->t('Favorites'));
101
            $aPlaylists[] = array('id' => 'X2', 'name' => $this->l10n->t('Recently Added'));
102
            $aPlaylists[] = array('id' => 'X3', 'name' => $this->l10n->t('Recently Played'));
103
            $aPlaylists[] = array('id' => 'X4', 'name' => $this->l10n->t('Most Played'));
104
            $aPlaylists[] = array('id' => '', 'name' => '');
105
106
            // Stream files are shown directly
107
            $SQL = 'SELECT  `file_id` AS `id`, `title` AS `name`, LOWER(`title`) AS `lower` 
108
						FROM `*PREFIX*audioplayer_streams`
109
			 			WHERE  `user_id` = ?
110
			 			ORDER BY LOWER(`title`) ASC
111
			 			';
112
            $stmt = $this->db->prepare($SQL);
113
            $stmt->execute(array($this->userId));
114
            $results = $stmt->fetchAll();
115
            foreach ($results as $row) {
116
                array_splice($row, 2, 1);
117
                $row['id'] = 'S' . $row['id'];
118
                $aPlaylists[] = $row;
119
            }
120
            $aPlaylists[] = array('id' => '', 'name' => '');
121
122
            // regular playlists are selected
123
            $SQL = 'SELECT  `id`,`name`, LOWER(`name`) AS `lower` 
124
						FROM `*PREFIX*audioplayer_playlists`
125
			 			WHERE  `user_id` = ?
126
			 			ORDER BY LOWER(`name`) ASC
127
			 			';
128
129
        } elseif ($category === 'Folder') {
130
            $SQL = 'SELECT  DISTINCT(`FC`.`fileid`) AS `id`, `FC`.`name`, LOWER(`FC`.`name`) AS `lower` 
131
						FROM `*PREFIX*audioplayer_tracks` `AT`
132
						JOIN `*PREFIX*filecache` `FC`
133
						ON `FC`.`fileid` = `AT`.`folder_id`
134
			 			WHERE `AT`.`user_id` = ?
135
			 			ORDER BY LOWER(`FC`.`name`) ASC
136
			 			';
137
        } elseif ($category === 'Album') {
138
            $SQL = 'SELECT  `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower`
139
						FROM `*PREFIX*audioplayer_albums` `AB`
140
						LEFT JOIN `*PREFIX*audioplayer_artists` `AA` 
141
						ON `AB`.`artist_id` = `AA`.`id`
142
			 			WHERE `AB`.`user_id` = ?
143
			 			ORDER BY LOWER(`AB`.`name`) ASC
144
			 			';
145
        } elseif ($category === 'Album Artist') {
146
            $SQL = 'SELECT  DISTINCT(`AB`.`artist_id`) AS `id`, `AA`.`name`, LOWER(`AA`.`name`) AS `lower` 
147
						FROM `*PREFIX*audioplayer_albums` `AB`
148
						JOIN `*PREFIX*audioplayer_artists` `AA`
149
						ON `AB`.`artist_id` = `AA`.`id`
150
			 			WHERE `AB`.`user_id` = ?
151
			 			ORDER BY LOWER(`AA`.`name`) ASC
152
			 			';
153
        }
154
155
        if (isset($SQL)) {
156
            $stmt = $this->db->prepare($SQL);
157
            $stmt->execute(array($this->userId));
158
            $results = $stmt->fetchAll();
159
            foreach ($results as $row) {
160
                array_splice($row, 2, 1);
161
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
162
                $row['cnt'] = $this->getTrackCount($category, $row['id']);
163
                $aPlaylists[] = $row;
164
            }
165
        }
166
167
        $result = empty($aPlaylists) ? [
168
            'status' => 'nodata'
169
        ] : [
170
            'status' => 'success',
171
            'data' => $aPlaylists
172
        ];
173
        return new JSONResponse($result);
174
    }
175
176
    /**
177
     * Get the covers for the "Album Covers" view
178
     *
179
     * @NoAdminRequired
180
     * @param $category
181
     * @param $categoryId
182
     * @return JSONResponse
183
     */
184
    public function getCategoryItemCovers($category, $categoryId)
185
    {
186
        $whereMatching = array('Artist' => '`AT`.`artist_id`', 'Genre' => '`AT`.`genre_id`', 'Album' => '`AB`.`id`', 'Album Artist' => '`AB`.`artist_id`', 'Year' => '`AT`.`year`', 'Folder' => '`AT`.`folder_id`');
187
188
        $aPlaylists = array();
189
        $SQL = 'SELECT  `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower` , `AA`.`id` AS `art`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`';
190
        $SQL .= ' FROM `*PREFIX*audioplayer_tracks` `AT`';
191
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AB`.`id` = `AT`.`album_id`';
192
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AA`.`id` = `AB`.`artist_id`';
193
        $SQL .= ' WHERE `AT`.`user_id` = ? ';
194
        if ($categoryId) $SQL .= 'AND ' . $whereMatching[$category] . '= ?';
195
        $SQL .= ' GROUP BY `AB`.`id`, `AA`.`id` ORDER BY LOWER(`AB`.`name`) ASC';
196
197
        if (isset($SQL)) {
198
            $stmt = $this->db->prepare($SQL);
199
            if ($categoryId) {
200
                $stmt->execute(array($this->userId, $categoryId));
201
            } else {
202
                $stmt->execute(array($this->userId));
203
            }
204
            $results = $stmt->fetchAll();
205
            foreach ($results as $row) {
206
                $row['art'] = $this->DBController->loadArtistsToAlbum($row['id'], $row['art']);
207
                array_splice($row, 2, 1);
208
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
209
                $aPlaylists[] = $row;
210
            }
211
        }
212
        $result = empty($aPlaylists) ? [
213
            'status' => 'nodata'
214
        ] : [
215
            'status' => 'success',
216
            'data' => $aPlaylists
217
        ];
218
        return new JSONResponse($result);
219
    }
220
221
    /**
222
     * Get the number of tracks for a category item
223
     *
224
     * @param string $category
225
     * @param integer $categoryId
226
     * @return integer
227
     */
228
    private function getTrackCount($category, $categoryId)
229
    {
230
        $SQL = array();
231
        $SQL['Artist'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id` WHERE  `AT`.`artist_id` = ? AND `AT`.`user_id` = ?';
232
        $SQL['Genre'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`genre_id` = ? AND `AT`.`user_id` = ?';
233
        $SQL['Year'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`year` = ? AND `AT`.`user_id` = ?';
234
        $SQL['Title'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`id` > ? AND `AT`.`user_id` = ?';
235
        $SQL['Playlist'] = 'SELECT COUNT(`AP`.`track_id`) AS `count` FROM `*PREFIX*audioplayer_playlist_tracks` `AP` WHERE  `AP`.`playlist_id` = ?';
236
        $SQL['Folder'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`folder_id` = ? AND `AT`.`user_id` = ?';
237
        $SQL['Album'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`album_id` = ? AND `AT`.`user_id` = ?';
238
        $SQL['Album Artist'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_albums` `AB` JOIN `*PREFIX*audioplayer_tracks` `AT` ON `AB`.`id` = `AT`.`album_id` WHERE  `AB`.`artist_id` = ? AND `AB`.`user_id` = ?';
239
240
        $stmt = $this->db->prepare($SQL[$category]);
241
        if ($category === 'Playlist') {
242
            $stmt->execute(array($categoryId));
243
        } else {
244
            $stmt->execute(array($categoryId, $this->userId));
245
        }
246
        $results = $stmt->fetch();
247
        return $results['count'];
248
    }
249
250
    /**
251
     * Get the tracks for a selected category or album
252
     *
253
     * @param string $category
254
     * @param string $categoryId
255
     * @return array
256
     * @throws InvalidPathException
257
     */
258
    private function getTracksDetails($category, $categoryId)
0 ignored issues
show
Unused Code introduced by Rello
The method getTracksDetails() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
259
    {
260
        $SQL = null;
261
        $favorite = false;
262
        $aTracks = array();
263
        $SQL_select = 'SELECT  `AT`.`id`, `AT`.`title`  AS `cl1`, `AA`.`name` AS `cl2`, `AB`.`name` AS `cl3`, `AT`.`length` AS `len`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AB`.`name`) AS `lower`';
264
        $SQL_from = ' FROM `*PREFIX*audioplayer_tracks` `AT`
265
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
266
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`';
267
        $SQL_order = ' ORDER BY LOWER(`AB`.`name`) ASC, `AT`.`disc` ASC, `AT`.`number` ASC';
268
269
        if ($category === 'Artist') {
270
            $SQL_select = 'SELECT  `AT`.`id`, `AT`.`title`  AS `cl1`, `AB`.`name` AS `cl2`, `AT`.`year` AS `cl3`, `AT`.`length` AS `len`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AB`.`name`) AS `lower`';
271
            $SQL = $SQL_select . $SQL_from .
272
                'WHERE  `AT`.`artist_id` = ? AND `AT`.`user_id` = ?' .
273
                $SQL_order;
274
        } elseif ($category === 'Genre') {
275
            $SQL = $SQL_select . $SQL_from .
276
                'WHERE `AT`.`genre_id` = ? AND `AT`.`user_id` = ?' .
277
                $SQL_order;
278
        } elseif ($category === 'Year') {
279
            $SQL = $SQL_select . $SQL_from .
280
                'WHERE `AT`.`year` = ? AND `AT`.`user_id` = ?' .
281
                $SQL_order;
282
        } elseif ($category === 'Title') {
283
            $SQL = $SQL_select . $SQL_from .
284
                'WHERE `AT`.`id` > ? AND `AT`.`user_id` = ?' .
285
                $SQL_order;
286
        } elseif ($category === 'Playlist' AND $categoryId === 'X1') { // Favorites
287
            $SQL = 'SELECT  `AT`.`id` , `AT`.`title`  AS `cl1`,`AA`.`name` AS `cl2`, `AB`.`name` AS `cl3`,`AT`.`length` AS `len`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AT`.`title`) AS `lower`' .
288
                $SQL_from .
289
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ?' .
290
                ' ORDER BY LOWER(`AT`.`title`) ASC';
291
            $categoryId = 0; //overwrite to integer for PostgreSQL
292
            $favorite = true;
293
        } elseif ($category === 'Playlist' AND $categoryId === 'X2') { // Recently Added
294
            $SQL = $SQL_select . $SQL_from .
295
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ? 
296
			 		ORDER BY `AT`.`file_id` DESC
297
			 		Limit 100';
298
            $categoryId = 0;
299
        } elseif ($category === 'Playlist' AND $categoryId === 'X3') { // Recently Played
300
            $SQL = $SQL_select . $SQL_from .
301
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
302
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
303
			 		ORDER BY `AS`.`playtime` DESC
304
			 		Limit 50';
305
            $categoryId = 0;
306
        } elseif ($category === 'Playlist' AND $categoryId === 'X4') { // Most Played
307
            $SQL = $SQL_select . $SQL_from .
308
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
309
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
310
			 		ORDER BY `AS`.`playcount` DESC
311
			 		Limit 25';
312
            $categoryId = 0;
313
        } elseif ($category === 'Playlist') {
314
            $SQL = $SQL_select . ' , `AP`.`sortorder`' .
315
                'FROM `*PREFIX*audioplayer_playlist_tracks` `AP` 
316
					LEFT JOIN `*PREFIX*audioplayer_tracks` `AT` ON `AP`.`track_id` = `AT`.`id`
317
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
318
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`
319
			 		WHERE  `AP`.`playlist_id` = ?
320
					AND `AT`.`user_id` = ? 
321
			 		ORDER BY `AP`.`sortorder` ASC';
322
        } elseif ($category === 'Stream') {
323
            $aTracks = $this->StreamParser(intval(substr($categoryId, 1)));
324
            return $aTracks;
325
326
        } elseif ($category === 'Folder') {
327
            $SQL = $SQL_select . $SQL_from .
328
                'WHERE `AT`.`folder_id` = ? AND `AT`.`user_id` = ?' .
329
                $SQL_order;
330
        } elseif ($category === 'Album') {
331
            $SQL_select = 'SELECT  `AT`.`id`, `AT`.`title` AS `cl1`, `AA`.`name` AS `cl2`, `AT`.`length` AS `len`, `AT`.`disc` AS `dsc`, `AT`.`file_id` AS `fid`, `AT`.`mimetype` AS `mim`, (CASE  WHEN `AB`.`cover` IS NOT NULL THEN `AB`.`id` ELSE NULL END) AS `cid`, LOWER(`AT`.`title`) AS `lower`,`AT`.`number`  AS `num`';
332
            $SQL = $SQL_select . $SQL_from .
333
                'WHERE `AB`.`id` = ? AND `AB`.`user_id` = ?' .
334
                ' ORDER BY `AT`.`disc` ASC, `AT`.`number` ASC';
335
        } elseif ($category === 'Album Artist') {
336
            $SQL = $SQL_select . $SQL_from .
337
                'WHERE  `AB`.`artist_id` = ? AND `AT`.`user_id` = ?' .
338
                $SQL_order;
339
        }
340
341
        $this->tagger = $this->tagManager->load('files');
342
        $favorites = $this->tagger->getFavorites();
343
344
        $stmt = $this->db->prepare($SQL);
345
        $stmt->execute(array($categoryId, $this->userId));
346
        $results = $stmt->fetchAll();
347
        foreach ($results as $row) {
348
            if ($category === 'Album') {
349
                $row['cl3'] = $row['dsc'] . '-' . $row['num'];
350
            }
351
            array_splice($row, 8, 1);
352
            //$nodes = $this->rootFolder->getUserFolder($this->userId)->getById($row['fid']);
353
            //$file = array_shift($nodes);
354
            //if ($file === null) {
355
            //    $this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
356
            //    $this->DBController->deleteFromDB($row['fid'], $this->userId);
357
            //    continue;
358
            //}
359
            if (is_array($favorites) AND in_array($row['fid'], $favorites)) {
360
                $row['fav'] = 't';
361
            }
362
363
            if ($favorite AND is_array($favorites) AND !in_array($row['fid'], $favorites)) {
364
                //special handling for Favorites smart playlist;
365
                //do not display anything that is NOT a fav
366
            } else {
367
                array_splice($row, 5, 1);
368
                $aTracks[] = $row;
369
            }
370
        }
371
        return $aTracks;
372
    }
373
374
    /**
375
     * Extract steam urls from playlist files
376
     *
377
     * @param integer $fileId
378
     * @return array
379
     * @throws InvalidPathException
380
     */
381
    private function StreamParser($fileId)
382
    {
383
        $tracks = array();
384
        $x = 0;
385
        $title = null;
386
        $userView = $this->rootFolder->getUserFolder($this->userId);
387
        //$this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
388
389
        $streamfile = $userView->getById($fileId);
390
        $file_type = $streamfile[0]->getMimetype();
391
        $file_content = $streamfile[0]->getContent();
0 ignored issues
show
Bug introduced by Rello
The method getContent() does not exist on OCP\Files\Node. It seems like you code against a sub-type of OCP\Files\Node such as OCP\Files\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

391
        /** @scrutinizer ignore-call */ 
392
        $file_content = $streamfile[0]->getContent();
Loading history...
392
393
        if ($file_type === 'audio/x-scpls') {
394
            $stream_data = parse_ini_string($file_content, true, INI_SCANNER_RAW);
395
            $stream_rows = isset($stream_data['playlist']['NumberOfEntries']) ? $stream_data['playlist']['NumberOfEntries'] : $stream_data['playlist']['numberofentries'];
396
            for ($i = 1; $i <= $stream_rows; $i++) {
397
                $title = $stream_data['playlist']['Title' . $i];
398
                $file = $stream_data['playlist']['File' . $i];
399
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $file, $matches);
400
401
                if ($matches[0]) {
402
                    $row = array();
403
                    $row['id'] = $fileId . $i;
404
                    $row['cl1'] = $matches[0][0];
405
                    $row['cl2'] = '';
406
                    $row['cl3'] = '';
407
                    $row['len'] = '';
408
                    $row['mim'] = $file_type;
409
                    $row['cid'] = '';
410
                    $row['lin'] = $matches[0][0];
411
                    if ($title) $row['cl1'] = $title;
412
                    $tracks[] = $row;
413
                }
414
            }
415
        } else {
416
            foreach (preg_split("/((\r?\n)|(\r\n?))/", $file_content) as $line) {
417
                if (empty($line)) continue;
418
                if (substr($line, 0, 8) === '#EXTINF:') {
419
                    $extinf = explode(',', substr($line, 8));
420
                    $title = $extinf[1];
421
                }
422
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $line, $matches);
423
424
                if ($matches[0]) {
425
                    $x++;
426
                    $row = array();
427
                    $row['id'] = $fileId . $x;
428
                    $row['cl1'] = $matches[0][0];
429
                    $row['cl2'] = '';
430
                    $row['cl3'] = '';
431
                    $row['len'] = '';
432
                    $row['mim'] = $file_type;
433
                    $row['cid'] = '';
434
                    $row['lin'] = $matches[0][0];
435
                    if ($title) $row['cl1'] = $title;
436
                    $title = null;
437
                    $tracks[] = $row;
438
                } elseif (preg_match('/^[^"<>|:]*$/',$line)) {
439
                    $path = explode('/', $streamfile[0]->getInternalPath());
440
                    array_shift($path);
441
                    array_pop($path);
442
                    array_push($path, $line);
443
                    $path = implode('/', $path);
444
                    $x++;
445
446
                    try {
447
                        $fileId = $this->rootFolder->getUserFolder($this->userId)->get($path)->getId();
448
                        $track = $this->DBController->getTrackInfo(null,$fileId);
449
450
                        $row = array();
451
                        $row['id'] = $track['id'];
452
                        $row['cl1'] = $track['Title'];
453
                        $row['cl2'] = $track['Artist'];
454
                        $row['cl3'] = $track['Album'];
455
                        $row['len'] = $track['Length'];
456
                        $row['mim'] = $track['MIME type'];
457
                        $row['cid'] = '';
458
                        $row['lin'] = $track['id'];
459
                        $row['fav'] = $track['fav'];
460
                        if ($title) $row['cl1'] = $title;
461
                        $tracks[] = $row;
462
                        $title = null;
463
                    } catch (NotFoundException $e) {
464
                        //File is not known in the filecache and will be ignored;
465
                    }
466
                }
467
            }
468
        }
469
        return $tracks;
470
    }
471
472
    /**
473
     * Get selection dependend headers for the list view
474
     *
475
     * @param string $category
476
     * @return array
477
     */
478
    private function getListViewHeaders($category)
0 ignored issues
show
Unused Code introduced by Rello
The method getListViewHeaders() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
479
    {
480
        if ($category === 'Artist') {
481
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Album'), 'col3' => $this->l10n->t('Year'), 'col4' => $this->l10n->t('Length')];
482
        } elseif ($category === 'Album') {
483
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Artist'), 'col3' => $this->l10n->t('Disc') . '-' . $this->l10n->t('Track'), 'col4' => $this->l10n->t('Length')];
484
        } elseif ($category === 'x_Stream') { //temporary disabled; need to separate streams and playlists
485
            return ['col1' => $this->l10n->t('URL'), 'col2' => $this->l10n->t(''), 'col3' => $this->l10n->t(''), 'col4' => $this->l10n->t('')];
486
        } else {
487
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Artist'), 'col3' => $this->l10n->t('Album'), 'col4' => $this->l10n->t('Length')];
488
        }
489
    }
490
}