MusicController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 61
c 7
b 0
f 1
dl 0
loc 139
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 1
A getPublicAudioStream() 0 18 2
A getPublicAudioInfo() 0 42 4
A getAudioStream() 0 18 2
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
 * @author Sebastian Doell <[email protected]>
10
 * @copyright 2016-2019 Marcel Scherello
11
 * @copyright 2015 Sebastian Doell
12
 */
13
14
namespace OCA\audioplayer\Controller;
15
16
use OCP\AppFramework\Controller;
17
use OCP\AppFramework\Http\JSONResponse;
18
use OCP\IRequest;
19
use OCP\IL10N;
20
use OCP\IDbConnection;
21
use OCP\Share\IManager;
22
use OCP\Files\IRootFolder;
23
use OCP\ILogger;
24
25
/**
26
 * Controller class for main page.
27
 */
28
class MusicController extends Controller
29
{
30
31
    private $userId;
32
    private $l10n;
33
    private $db;
34
    private $shareManager;
35
    private $logger;
36
    private $DBController;
37
    private $rootFolder;
38
39
    public function __construct(
40
        $appName,
41
        IRequest $request,
42
        $userId,
43
        IL10N $l10n,
44
        IDbConnection $db,
45
        IManager $shareManager,
46
        ILogger $logger,
47
        DbController $DBController,
48
        IRootFolder $rootFolder
49
    )
50
    {
51
        parent::__construct($appName, $request);
52
        $this->userId = $userId;
53
        $this->l10n = $l10n;
54
        $this->db = $db;
55
        $this->shareManager = $shareManager;
56
        $this->logger = $logger;
57
        $this->DBController = $DBController;
58
        $this->rootFolder = $rootFolder;
59
    }
60
61
    /**
62
     * @PublicPage
63
     * @NoCSRFRequired
64
     * @param $token
65
     * @return JSONResponse
66
     * @throws \OCP\Files\NotFoundException
67
     * @throws \OCP\Share\Exceptions\ShareNotFound
68
     */
69
    public function getPublicAudioInfo($token)
70
    {
71
        if (!empty($token)) {
72
            $share = $this->shareManager->getShareByToken($token);
73
            $fileid = $share->getNodeId();
74
            $fileowner = $share->getShareOwner();
75
76
            //\OCP\Util::writeLog('audioplayer', 'fileid: '.$fileid, \OCP\Util::DEBUG);
77
            //\OCP\Util::writeLog('audioplayer', 'fileowner: '.$fileowner, \OCP\Util::DEBUG);
78
79
            $SQL = "SELECT `AT`.`title`,`AG`.`name` AS `genre`,`AB`.`name` AS `album`,`AT`.`artist_id`,
80
					`AT`.`length`,`AT`.`bitrate`,`AT`.`year`,`AA`.`name` AS `artist`,
81
					ROUND((`AT`.`bitrate` / 1000 ),0) AS `bitrate`, `AT`.`disc`,
82
					`AT`.`number`, `AT`.`composer`, `AT`.`subtitle`, `AT`.`mimetype`, `AB`.`id` AS `album_id` , `AB`.`artist_id` AS `albumArtist_id`
83
					, `AT`.`isrc` , `AT`.`copyright`
84
						FROM `*PREFIX*audioplayer_tracks` `AT`
85
						LEFT JOIN `*PREFIX*audioplayer_artists` `AA` ON `AT`.`artist_id` = `AA`.`id`
86
						LEFT JOIN `*PREFIX*audioplayer_genre` `AG` ON `AT`.`genre_id` = `AG`.`id`
87
						LEFT JOIN `*PREFIX*audioplayer_albums` `AB` ON `AT`.`album_id` = `AB`.`id`
88
			 			WHERE  `AT`.`user_id` = ? AND `AT`.`file_id` = ?
89
			 			ORDER BY `AT`.`album_id` ASC,`AT`.`number` ASC
90
			 			";
91
92
            $stmt = $this->db->prepare($SQL);
93
            $stmt->execute(array($fileowner, $fileid));
94
            $row = $stmt->fetch();
95
96
            $artist = $this->DBController->loadArtistsToAlbum($row['album_id'], $row['albumArtist_id']);
97
            $row['albumartist'] = $artist;
98
99
            if ($row['year'] === '0') $row['year'] = $this->l10n->t('Unknown');
100
101
            if ($row['title']) {
102
                $result = [
103
                    'status' => 'success',
104
                    'data' => $row];
105
            } else {
106
                $result = [
107
                    'status' => 'error',
108
                    'data' => 'nodata'];
109
            }
110
            return new JSONResponse($result);
111
        }
112
    }
113
114
    /**
115
     * Stream files in OCP withing link-shared folder
116
     * @PublicPage
117
     * @NoCSRFRequired
118
     * @param $token
119
     * @param $file
120
     * @throws \OCP\Files\NotFoundException
121
     */
122
    public function getPublicAudioStream($token, $file)
123
    {
124
        if (!empty($token)) {
125
            $share = $this->shareManager->getShareByToken($token);
126
            $fileowner = $share->getShareOwner();
127
128
            // Setup filesystem
129
            $nodes = $this->rootFolder->getUserFolder($fileowner)->getById($share->getNodeId());
130
            $pfile = array_shift($nodes);
131
            $path = $pfile->getPath();
132
            $segments = explode('/', trim($path, '/'), 3);
133
            $startPath = $segments[2];
134
135
            $filenameAudio = $startPath . '/' . rawurldecode($file);
136
137
            \OC::$server->getSession()->close();
138
            $stream = new \OCA\audioplayer\Http\AudioStream($filenameAudio, $fileowner);
139
            $stream->start();
140
        }
141
    }
142
143
    /**
144
     * @NoAdminRequired
145
     * @NoCSRFRequired
146
     * @param $file
147
     * @param $t
148
     */
149
    public function getAudioStream($file, $t)
150
    {
151
152
        if ($t) {
153
            $fileId = $this->DBController->getFileId($t);
154
            $nodes = $this->rootFolder->getUserFolder($this->userId)->getById($fileId);
155
            $file = array_shift($nodes);
156
            $path = $file->getPath();
157
            $segments = explode('/', trim($path, '/'), 3);
158
            $filename = $segments[2];
159
        } else {
160
            $filename = rawurldecode($file);
161
        }
162
163
        $user = $this->userId;
164
        \OC::$server->getSession()->close();
165
        $stream = new \OCA\audioplayer\Http\AudioStream($filename, $user);
166
        $stream->start();
167
    }
168
}
169