MusicController::getAudioStream()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 18
rs 9.8333
c 2
b 0
f 0
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-2021 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 Psr\Log\LoggerInterface;
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
        LoggerInterface $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();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\IPreparedStatement::fetch() has been deprecated: 21.0.0 use \OCP\DB\IResult::fetch on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare ( Ignorable by Annotation )

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

94
            $row = /** @scrutinizer ignore-deprecated */ $stmt->fetch();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
95
96
            if (isset($row['title'])) {
97
                $artist = $this->DBController->loadArtistsToAlbum($row['album_id'], $row['albumArtist_id']);
98
                $row['albumartist'] = $artist;
99
100
                if ($row['year'] === '0') $row['year'] = $this->l10n->t('Unknown');
101
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