Passed
Push — master ( 998e10...1efe83 )
by Marcel
06:10
created

lib/Controller/MusicController.php (1 issue)

Severity
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
            $response = new JSONResponse();
111
            $response->setData($result);
112
            return $response;
113
114
            \OC::$server->getSession()->close();
0 ignored issues
show
OC::server->getSession()->close() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
115
        }
116
    }
117
118
    /**
119
     * Stream files in OCP withing link-shared folder
120
     * @PublicPage
121
     * @NoCSRFRequired
122
     * @param $token
123
     * @param $file
124
     * @throws \OCP\Files\NotFoundException
125
     */
126
    public function getPublicAudioStream($token, $file)
127
    {
128
        if (!empty($token)) {
129
            $share = $this->shareManager->getShareByToken($token);
130
            $fileowner = $share->getShareOwner();
131
132
            // Setup filesystem
133
            $nodes = $this->rootFolder->getUserFolder($fileowner)->getById($share->getNodeId());
134
            $pfile = array_shift($nodes);
135
            $path = $pfile->getPath();
136
            $segments = explode('/', trim($path, '/'), 3);
137
            $startPath = $segments[2];
138
139
            $filenameAudio = $startPath . '/' . rawurldecode($file);
140
141
            \OC::$server->getSession()->close();
142
            $stream = new \OCA\audioplayer\Http\AudioStream($filenameAudio, $fileowner);
143
            $stream->start();
144
        }
145
    }
146
147
    /**
148
     * @NoAdminRequired
149
     * @NoCSRFRequired
150
     * @param $file
151
     */
152
    public function getAudioStream($file)
153
    {
154
        $filename = rawurldecode($file);
155
        $user = $this->userId;
156
        \OC::$server->getSession()->close();
157
        $stream = new \OCA\audioplayer\Http\AudioStream($filename, $user);
158
        $stream->start();
159
    }
160
}
161