Passed
Push — master ( d53efc...71a75a )
by Marcel
01:51
created

CategoryController::getTracksDetails()   F

Complexity

Conditions 27
Paths 127

Size

Total Lines 120
Code Lines 97

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 27
eloc 97
c 4
b 0
f 0
nc 127
nop 2
dl 0
loc 120
rs 3.9416

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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' => 'X5', 'name' => $this->l10n->t('50 Random Tracks'));
105
            $aPlaylists[] = array('id' => '', 'name' => '');
106
107
            // Stream files are shown directly
108
            $SQL = 'SELECT  `file_id` AS `id`, `title` AS `name`, LOWER(`title`) AS `lower` 
109
						FROM `*PREFIX*audioplayer_streams`
110
			 			WHERE  `user_id` = ?
111
			 			ORDER BY LOWER(`title`) ASC
112
			 			';
113
            $stmt = $this->db->prepare($SQL);
114
            $stmt->execute(array($this->userId));
115
            $results = $stmt->fetchAll();
116
            foreach ($results as $row) {
117
                array_splice($row, 2, 1);
118
                $row['id'] = 'S' . $row['id'];
119
                $aPlaylists[] = $row;
120
            }
121
            $aPlaylists[] = array('id' => '', 'name' => '');
122
123
            // regular playlists are selected
124
            $SQL = 'SELECT  `id`,`name`, LOWER(`name`) AS `lower` 
125
						FROM `*PREFIX*audioplayer_playlists`
126
			 			WHERE  `user_id` = ?
127
			 			ORDER BY LOWER(`name`) ASC
128
			 			';
129
130
        } elseif ($category === 'Folder') {
131
            $SQL = 'SELECT  DISTINCT(`FC`.`fileid`) AS `id`, `FC`.`name`, LOWER(`FC`.`name`) AS `lower` 
132
						FROM `*PREFIX*audioplayer_tracks` `AT`
133
						JOIN `*PREFIX*filecache` `FC`
134
						ON `FC`.`fileid` = `AT`.`folder_id`
135
			 			WHERE `AT`.`user_id` = ?
136
			 			ORDER BY LOWER(`FC`.`name`) ASC
137
			 			';
138
        } elseif ($category === 'Album') {
139
            $SQL = 'SELECT  `AB`.`id` , `AB`.`name`, LOWER(`AB`.`name`) AS `lower`
140
						FROM `*PREFIX*audioplayer_albums` `AB`
141
						LEFT JOIN `*PREFIX*audioplayer_artists` `AA` 
142
						ON `AB`.`artist_id` = `AA`.`id`
143
			 			WHERE `AB`.`user_id` = ?
144
			 			ORDER BY LOWER(`AB`.`name`) ASC
145
			 			';
146
        } elseif ($category === 'Album Artist') {
147
            $SQL = 'SELECT  DISTINCT(`AB`.`artist_id`) AS `id`, `AA`.`name`, LOWER(`AA`.`name`) AS `lower` 
148
						FROM `*PREFIX*audioplayer_albums` `AB`
149
						JOIN `*PREFIX*audioplayer_artists` `AA`
150
						ON `AB`.`artist_id` = `AA`.`id`
151
			 			WHERE `AB`.`user_id` = ?
152
			 			ORDER BY LOWER(`AA`.`name`) ASC
153
			 			';
154
        }
155
156
        if (isset($SQL)) {
157
            $stmt = $this->db->prepare($SQL);
158
            $stmt->execute(array($this->userId));
159
            $results = $stmt->fetchAll();
160
            foreach ($results as $row) {
161
                array_splice($row, 2, 1);
162
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
163
                $row['cnt'] = $this->getTrackCount($category, $row['id']);
164
                $aPlaylists[] = $row;
165
            }
166
        }
167
168
        $result = empty($aPlaylists) ? [
169
            'status' => 'nodata'
170
        ] : [
171
            'status' => 'success',
172
            'data' => $aPlaylists
173
        ];
174
        return new JSONResponse($result);
175
    }
176
177
    /**
178
     * Get the covers for the "Album Covers" view
179
     *
180
     * @NoAdminRequired
181
     * @param $category
182
     * @param $categoryId
183
     * @return JSONResponse
184
     */
185
    public function getCategoryItemCovers($category, $categoryId)
186
    {
187
        $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`');
188
189
        $aPlaylists = array();
190
        $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`';
191
        $SQL .= ' FROM `*PREFIX*audioplayer_tracks` `AT`';
192
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AB`.`id` = `AT`.`album_id`';
193
        $SQL .= ' LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AA`.`id` = `AB`.`artist_id`';
194
        $SQL .= ' WHERE `AT`.`user_id` = ? ';
195
        if ($categoryId) $SQL .= 'AND ' . $whereMatching[$category] . '= ?';
196
        $SQL .= ' GROUP BY `AB`.`id`, `AA`.`id`, `AB`.`name` ORDER BY LOWER(`AB`.`name`) ASC';
197
198
        if (isset($SQL)) {
199
            $stmt = $this->db->prepare($SQL);
200
            if ($categoryId) {
201
                $stmt->execute(array($this->userId, $categoryId));
202
            } else {
203
                $stmt->execute(array($this->userId));
204
            }
205
            $results = $stmt->fetchAll();
206
            foreach ($results as $row) {
207
                $row['art'] = $this->DBController->loadArtistsToAlbum($row['id'], $row['art']);
208
                array_splice($row, 2, 1);
209
                if ($row['name'] === '0' OR $row['name'] === '') $row['name'] = $this->l10n->t('Unknown');
210
                $aPlaylists[] = $row;
211
            }
212
        }
213
        $result = empty($aPlaylists) ? [
214
            'status' => 'nodata'
215
        ] : [
216
            'status' => 'success',
217
            'data' => $aPlaylists
218
        ];
219
        return new JSONResponse($result);
220
    }
221
222
    /**
223
     * Get the number of tracks for a category item
224
     *
225
     * @param string $category
226
     * @param integer $categoryId
227
     * @return integer
228
     */
229
    private function getTrackCount($category, $categoryId)
230
    {
231
        $SQL = array();
232
        $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` = ?';
233
        $SQL['Genre'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`genre_id` = ? AND `AT`.`user_id` = ?';
234
        $SQL['Year'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`year` = ? AND `AT`.`user_id` = ?';
235
        $SQL['Title'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`id` > ? AND `AT`.`user_id` = ?';
236
        $SQL['Playlist'] = 'SELECT COUNT(`AP`.`track_id`) AS `count` FROM `*PREFIX*audioplayer_playlist_tracks` `AP` WHERE  `AP`.`playlist_id` = ?';
237
        $SQL['Folder'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`folder_id` = ? AND `AT`.`user_id` = ?';
238
        $SQL['Album'] = 'SELECT COUNT(`AT`.`id`) AS `count` FROM `*PREFIX*audioplayer_tracks` `AT` WHERE  `AT`.`album_id` = ? AND `AT`.`user_id` = ?';
239
        $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` = ?';
240
241
        $stmt = $this->db->prepare($SQL[$category]);
242
        if ($category === 'Playlist') {
243
            $stmt->execute(array($categoryId));
244
        } else {
245
            $stmt->execute(array($categoryId, $this->userId));
246
        }
247
        $results = $stmt->fetch();
248
        return $results['count'];
249
    }
250
251
    /**
252
     * get the tracks for a selected category or album
253
     *
254
     * @NoAdminRequired
255
     * @param string $category
256
     * @param string $categoryId
257
     * @return JSONResponse
258
     * @throws InvalidPathException
259
     * @throws NotFoundException
260
     */
261
    public function getTracks($category, $categoryId)
262
    {
263
        if ($categoryId[0] === 'S') $category = 'Stream';
264
        if ($categoryId[0] === 'P') $category = 'Playlist';
265
        $items = $this->getTracksDetails($category, $categoryId);
266
        $headers = $this->getListViewHeaders($category);
267
268
        $result = !empty($items) ? [
269
            'status' => 'success',
270
            'data' => $items,
271
            'header' => $headers,
272
        ] : [
273
            'status' => 'nodata',
274
        ];
275
        return new JSONResponse($result);
276
    }
277
278
    /**
279
     * Get the tracks for a selected category or album
280
     *
281
     * @param string $category
282
     * @param string $categoryId
283
     * @return array
284
     * @throws InvalidPathException
285
     */
286
    private function getTracksDetails($category, $categoryId)
287
    {
288
        $SQL = null;
289
        $favorite = false;
290
        $aTracks = array();
291
        $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`';
292
        $SQL_from = ' FROM `*PREFIX*audioplayer_tracks` `AT`
293
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
294
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`';
295
        $SQL_order = ' ORDER BY LOWER(`AB`.`name`) ASC, `AT`.`disc` ASC, `AT`.`number` ASC';
296
297
        if ($category === 'Artist') {
298
            $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`';
299
            $SQL = $SQL_select . $SQL_from .
300
                'WHERE  `AT`.`artist_id` = ? AND `AT`.`user_id` = ?' .
301
                $SQL_order;
302
        } elseif ($category === 'Genre') {
303
            $SQL = $SQL_select . $SQL_from .
304
                'WHERE `AT`.`genre_id` = ? AND `AT`.`user_id` = ?' .
305
                $SQL_order;
306
        } elseif ($category === 'Year') {
307
            $SQL = $SQL_select . $SQL_from .
308
                'WHERE `AT`.`year` = ? AND `AT`.`user_id` = ?' .
309
                $SQL_order;
310
        } elseif ($category === 'Title') {
311
            $SQL = $SQL_select . $SQL_from .
312
                'WHERE `AT`.`id` > ? AND `AT`.`user_id` = ?' .
313
                $SQL_order;
314
        } elseif ($category === 'Playlist' AND $categoryId === 'X1') { // Favorites
315
            $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`' .
316
                $SQL_from .
317
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ?' .
318
                ' ORDER BY LOWER(`AT`.`title`) ASC';
319
            $categoryId = 0; //overwrite to integer for PostgreSQL
320
            $favorite = true;
321
        } elseif ($category === 'Playlist' AND $categoryId === 'X2') { // Recently Added
322
            $SQL = $SQL_select . $SQL_from .
323
                'WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ? 
324
			 		ORDER BY `AT`.`file_id` DESC
325
			 		Limit 100';
326
            $categoryId = 0;
327
        } elseif ($category === 'Playlist' AND $categoryId === 'X3') { // Recently Played
328
            $SQL = $SQL_select . $SQL_from .
329
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
330
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
331
			 		ORDER BY `AS`.`playtime` DESC
332
			 		Limit 50';
333
            $categoryId = 0;
334
        } elseif ($category === 'Playlist' AND $categoryId === 'X4') { // Most Played
335
            $SQL = $SQL_select . $SQL_from .
336
                'LEFT JOIN `*PREFIX*audioplayer_stats` `AS` ON `AT`.`id` = `AS`.`track_id`
337
			 		WHERE `AS`.`id` <> ? AND `AT`.`user_id` = ? 
338
			 		ORDER BY `AS`.`playcount` DESC
339
			 		Limit 25';
340
            $categoryId = 0;
341
        } elseif ($category === 'Playlist' AND $categoryId === "X5") { // 50 Random Tracks
342
            $SQL = $SQL_select . $SQL_from .
343
                "WHERE `AT`.`id` <> ? AND `AT`.`user_id` = ? 
344
			 		ORDER BY RAND()
345
			 		Limit 50";
346
            $categoryId = 0;
347
        } elseif ($category === 'Playlist') {
348
            $SQL = $SQL_select . ' , `AP`.`sortorder`' .
349
                'FROM `*PREFIX*audioplayer_playlist_tracks` `AP` 
350
					LEFT JOIN `*PREFIX*audioplayer_tracks` `AT` ON `AP`.`track_id` = `AT`.`id`
351
					LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
352
					LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`
353
			 		WHERE  `AP`.`playlist_id` = ?
354
					AND `AT`.`user_id` = ? 
355
			 		ORDER BY `AP`.`sortorder` ASC';
356
        } elseif ($category === 'Stream') {
357
            $aTracks = $this->StreamParser(intval(substr($categoryId, 1)));
358
            return $aTracks;
359
360
        } elseif ($category === 'Folder') {
361
            $SQL = $SQL_select . $SQL_from .
362
                'WHERE `AT`.`folder_id` = ? AND `AT`.`user_id` = ?' .
363
                $SQL_order;
364
        } elseif ($category === 'Album') {
365
            $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`';
366
            $SQL = $SQL_select . $SQL_from .
367
                'WHERE `AB`.`id` = ? AND `AB`.`user_id` = ?' .
368
                ' ORDER BY `AT`.`disc` ASC, `AT`.`number` ASC';
369
        } elseif ($category === 'Album Artist') {
370
            $SQL = $SQL_select . $SQL_from .
371
                'WHERE  `AB`.`artist_id` = ? AND `AT`.`user_id` = ?' .
372
                $SQL_order;
373
        }
374
375
        $this->tagger = $this->tagManager->load('files');
376
        $favorites = $this->tagger->getFavorites();
377
378
        $stmt = $this->db->prepare($SQL);
379
        $stmt->execute(array($categoryId, $this->userId));
380
        $results = $stmt->fetchAll();
381
        foreach ($results as $row) {
382
            if ($category === 'Album') {
383
                $row['cl3'] = $row['dsc'] . '-' . $row['num'];
384
            }
385
            array_splice($row, 8, 1);
386
            //$nodes = $this->rootFolder->getUserFolder($this->userId)->getById($row['fid']);
387
            //$file = array_shift($nodes);
388
            //if ($file === null) {
389
            //    $this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
390
            //    $this->DBController->deleteFromDB($row['fid'], $this->userId);
391
            //    continue;
392
            //}
393
            if (is_array($favorites) AND in_array($row['fid'], $favorites)) {
394
                $row['fav'] = 't';
395
            }
396
397
            if ($favorite AND is_array($favorites) AND !in_array($row['fid'], $favorites)) {
398
                //special handling for Favorites smart playlist;
399
                //do not display anything that is NOT a fav
400
            } else {
401
                array_splice($row, 5, 1);
402
                $aTracks[] = $row;
403
            }
404
        }
405
        return $aTracks;
406
    }
407
408
    /**
409
     * Extract steam urls from playlist files
410
     *
411
     * @param integer $fileId
412
     * @return array
413
     * @throws InvalidPathException
414
     */
415
    private function StreamParser($fileId)
416
    {
417
        $tracks = array();
418
        $x = 0;
419
        $title = null;
420
        $userView = $this->rootFolder->getUserFolder($this->userId);
421
        //$this->logger->debug('removed/unshared file found => remove '.$row['fid'], array('app' => 'audioplayer'));
422
423
        $streamfile = $userView->getById($fileId);
424
        $file_type = $streamfile[0]->getMimetype();
425
        $file_content = $streamfile[0]->getContent();
0 ignored issues
show
Bug introduced by
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

425
        /** @scrutinizer ignore-call */ 
426
        $file_content = $streamfile[0]->getContent();
Loading history...
426
427
        if ($file_type === 'audio/x-scpls') {
428
            $stream_data = parse_ini_string($file_content, true, INI_SCANNER_RAW);
429
            $stream_rows = isset($stream_data['playlist']['NumberOfEntries']) ? $stream_data['playlist']['NumberOfEntries'] : $stream_data['playlist']['numberofentries'];
430
            for ($i = 1; $i <= $stream_rows; $i++) {
431
                $title = $stream_data['playlist']['Title' . $i];
432
                $file = $stream_data['playlist']['File' . $i];
433
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $file, $matches);
434
435
                if ($matches[0]) {
436
                    $row = array();
437
                    $row['id'] = $fileId . $i;
438
                    $row['cl1'] = $matches[0][0];
439
                    $row['cl2'] = '';
440
                    $row['cl3'] = '';
441
                    $row['len'] = '';
442
                    $row['mim'] = $file_type;
443
                    $row['cid'] = '';
444
                    $row['lin'] = $matches[0][0];
445
                    if ($title) $row['cl1'] = $title;
446
                    $tracks[] = $row;
447
                }
448
            }
449
        } else {
450
            foreach (preg_split("/((\r?\n)|(\r\n?))/", $file_content) as $line) {
451
                if (empty($line)) continue;
452
                if (substr($line, 0, 8) === '#EXTINF:') {
453
                    $extinf = explode(',', substr($line, 8));
454
                    $title = $extinf[1];
455
                }
456
                preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $line, $matches);
457
458
                if ($matches[0]) {
459
                    $x++;
460
                    $row = array();
461
                    $row['id'] = $fileId . $x;
462
                    $row['cl1'] = $matches[0][0];
463
                    $row['cl2'] = '';
464
                    $row['cl3'] = '';
465
                    $row['len'] = '';
466
                    $row['mim'] = $file_type;
467
                    $row['cid'] = '';
468
                    $row['lin'] = $matches[0][0];
469
                    if ($title) $row['cl1'] = $title;
470
                    $title = null;
471
                    $tracks[] = $row;
472
                } elseif (preg_match('/^[^"<>|:]*$/',$line)) {
473
                    $path = explode('/', $streamfile[0]->getInternalPath());
474
                    array_shift($path);
475
                    array_pop($path);
476
                    array_push($path, $line);
477
                    $path = implode('/', $path);
478
                    $x++;
479
480
                    try {
481
                        $fileId = $this->rootFolder->getUserFolder($this->userId)->get($path)->getId();
482
                        $track = $this->DBController->getTrackInfo(null,$fileId);
483
484
                        $row = array();
485
                        $row['id'] = $track['id'];
486
                        $row['cl1'] = $track['Title'];
487
                        $row['cl2'] = $track['Artist'];
488
                        $row['cl3'] = $track['Album'];
489
                        $row['len'] = $track['Length'];
490
                        $row['mim'] = $track['MIME type'];
491
                        $row['cid'] = '';
492
                        $row['lin'] = $track['id'];
493
                        $row['fav'] = $track['fav'];
494
                        if ($title) $row['cl1'] = $title;
495
                        $tracks[] = $row;
496
                        $title = null;
497
                    } catch (NotFoundException $e) {
498
                        //File is not known in the filecache and will be ignored;
499
                    }
500
                }
501
            }
502
        }
503
        return $tracks;
504
    }
505
506
    /**
507
     * Get selection dependend headers for the list view
508
     *
509
     * @param string $category
510
     * @return array
511
     */
512
    private function getListViewHeaders($category)
513
    {
514
        if ($category === 'Artist') {
515
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Album'), 'col3' => $this->l10n->t('Year'), 'col4' => $this->l10n->t('Length')];
516
        } elseif ($category === 'Album') {
517
            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')];
518
        } elseif ($category === 'x_Stream') { //temporary disabled; need to separate streams and playlists
519
            return ['col1' => $this->l10n->t('URL'), 'col2' => $this->l10n->t(''), 'col3' => $this->l10n->t(''), 'col4' => $this->l10n->t('')];
520
        } else {
521
            return ['col1' => $this->l10n->t('Title'), 'col2' => $this->l10n->t('Artist'), 'col3' => $this->l10n->t('Album'), 'col4' => $this->l10n->t('Length')];
522
        }
523
    }
524
}
525