1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ownCloud - Music app |
5
|
|
|
* |
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
7
|
|
|
* later. See the COPYING file. |
8
|
|
|
* |
9
|
|
|
* @author Morris Jobke <[email protected]> |
10
|
|
|
* @author Pauli Järvinen <[email protected]> |
11
|
|
|
* @copyright Morris Jobke 2013, 2014 |
12
|
|
|
* @copyright Pauli Järvinen 2016 - 2023 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace OCA\Music\Db; |
16
|
|
|
|
17
|
|
|
use OCA\Music\Utility\Util; |
18
|
|
|
use OCP\IL10N; |
19
|
|
|
use OCP\IURLGenerator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @method string getTitle() |
23
|
|
|
* @method void setTitle(string $title) |
24
|
|
|
* @method ?int getNumber() |
25
|
|
|
* @method void setNumber(?int $number) |
26
|
|
|
* @method ?int getDisk() |
27
|
|
|
* @method void setDisk(?int $disk) |
28
|
|
|
* @method ?int getYear() |
29
|
|
|
* @method void setYear(?int $year) |
30
|
|
|
* @method int getArtistId() |
31
|
|
|
* @method void setArtistId(int $artistId) |
32
|
|
|
* @method ?string getArtistName() |
33
|
|
|
* @method void setArtistName(?string $artistName) |
34
|
|
|
* @method int getAlbumId() |
35
|
|
|
* @method void setAlbumId(int $albumId) |
36
|
|
|
* @method ?string getAlbumName() |
37
|
|
|
* @method void setAlbumName(?string $albumName) |
38
|
|
|
* @method ?int getLength() |
39
|
|
|
* @method void setLength(?int $length) |
40
|
|
|
* @method int getFileId() |
41
|
|
|
* @method void setFileId(int $fileId) |
42
|
|
|
* @method ?int getBitrate() |
43
|
|
|
* @method void setBitrate(?int $bitrate) |
44
|
|
|
* @method string getMimetype() |
45
|
|
|
* @method void setMimetype(string $mimetype) |
46
|
|
|
* @method ?string getMbid() |
47
|
|
|
* @method void setMbid(?string $mbid) |
48
|
|
|
* @method ?string getStarred() |
49
|
|
|
* @method void setStarred(?string $timestamp) |
50
|
|
|
* @method ?int getRating() |
51
|
|
|
* @method setRating(?int $rating) |
52
|
|
|
* @method ?int getGenreId() |
53
|
|
|
* @method void setGenreId(?int $genreId) |
54
|
|
|
* @method int getPlayCount() |
55
|
|
|
* @method void setPlayCount(int $count) |
56
|
|
|
* @method ?string getLastPlayed() |
57
|
|
|
* @method void setLastPlayed(?string $timestamp) |
58
|
|
|
*/ |
59
|
|
|
class Track extends Entity { |
60
|
|
|
public $title; |
61
|
|
|
public $number; |
62
|
|
|
public $disk; |
63
|
|
|
public $year; |
64
|
|
|
public $artistId; |
65
|
|
|
public $albumId; |
66
|
|
|
public $length; |
67
|
|
|
public $fileId; |
68
|
|
|
public $bitrate; |
69
|
|
|
public $uri; |
70
|
|
|
public $mimetype; |
71
|
|
|
public $mbid; |
72
|
|
|
public $starred; |
73
|
|
|
public $rating; |
74
|
|
|
public $genreId; |
75
|
|
|
public $playCount; |
76
|
|
|
public $lastPlayed; |
77
|
|
|
|
78
|
|
|
// not from the music_tracks table but still part of the standard content of this entity: |
79
|
|
|
public $filename; |
80
|
|
|
public $size; |
81
|
|
|
public $fileModTime; |
82
|
|
|
public $albumName; |
83
|
|
|
public $artistName; |
84
|
|
|
public $genreName; |
85
|
|
|
|
86
|
|
|
// the rest of the variables are injected separately when needed |
87
|
|
|
private $album; |
88
|
|
|
private $numberOnPlaylist; |
89
|
|
|
|
90
|
|
|
public function __construct() { |
91
|
|
|
$this->addType('number', 'int'); |
92
|
|
|
$this->addType('disk', 'int'); |
93
|
|
|
$this->addType('year', 'int'); |
94
|
|
|
$this->addType('artistId', 'int'); |
95
|
|
|
$this->addType('albumId', 'int'); |
96
|
|
|
$this->addType('length', 'int'); |
97
|
|
|
$this->addType('bitrate', 'int'); |
98
|
|
|
$this->addType('fileId', 'int'); |
99
|
|
|
$this->addType('genreId', 'int'); |
100
|
|
|
$this->addType('playCount', 'int'); |
101
|
|
|
$this->addType('rating', 'int'); |
102
|
|
|
$this->addType('size', 'int'); |
103
|
|
|
$this->addType('fileModTime', 'int'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getAlbum() : ?Album { |
107
|
|
|
return $this->album; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setAlbum(?Album $album) : void { |
111
|
|
|
$this->album = $album; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getNumberOnPlaylist() : ?int { |
115
|
|
|
return $this->numberOnPlaylist; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setNumberOnPlaylist(int $number) { |
119
|
|
|
$this->numberOnPlaylist = $number; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getUri(IURLGenerator $urlGenerator) : string { |
123
|
|
|
return $urlGenerator->linkToRoute( |
124
|
|
|
'music.shivaApi.track', |
125
|
|
|
['trackId' => $this->id] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getArtistWithUri(IURLGenerator $urlGenerator) : array { |
130
|
|
|
return [ |
131
|
|
|
'id' => $this->artistId, |
132
|
|
|
'uri' => $urlGenerator->linkToRoute( |
133
|
|
|
'music.shivaApi.artist', |
134
|
|
|
['artistId' => $this->artistId] |
135
|
|
|
) |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getAlbumWithUri(IURLGenerator $urlGenerator) : array { |
140
|
|
|
return [ |
141
|
|
|
'id' => $this->albumId, |
142
|
|
|
'uri' => $urlGenerator->linkToRoute( |
143
|
|
|
'music.shivaApi.album', |
144
|
|
|
['albumId' => $this->albumId] |
145
|
|
|
) |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getArtistNameString(IL10N $l10n) : string { |
150
|
|
|
return $this->getArtistName() ?: Artist::unknownNameString($l10n); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getAlbumNameString(IL10N $l10n) : string { |
154
|
|
|
return $this->getAlbumName() ?: Album::unknownNameString($l10n); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getGenreNameString(IL10N $l10n) : string { |
158
|
|
|
return $this->getGenreName() ?: Genre::unknownNameString($l10n); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function toCollection() : array { |
162
|
|
|
return [ |
163
|
|
|
'title' => $this->getTitle(), |
164
|
|
|
'number' => $this->getNumber(), |
165
|
|
|
'disk' => $this->getDisk(), |
166
|
|
|
'artistId' => $this->getArtistId(), |
167
|
|
|
'length' => $this->getLength(), |
168
|
|
|
'files' => [$this->getMimetype() => $this->getFileId()], |
169
|
|
|
'id' => $this->getId(), |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function toAPI(IURLGenerator $urlGenerator) : array { |
174
|
|
|
return [ |
175
|
|
|
'title' => $this->getTitle(), |
176
|
|
|
'ordinal' => $this->getAdjustedTrackNumber(), |
177
|
|
|
'artist' => $this->getArtistWithUri($urlGenerator), |
178
|
|
|
'album' => $this->getAlbumWithUri($urlGenerator), |
179
|
|
|
'length' => $this->getLength(), |
180
|
|
|
'files' => [$this->getMimetype() => $urlGenerator->linkToRoute( |
181
|
|
|
'music.api.download', |
182
|
|
|
['fileId' => $this->getFileId()] |
183
|
|
|
)], |
184
|
|
|
'bitrate' => $this->getBitrate(), |
185
|
|
|
'id' => $this->getId(), |
186
|
|
|
'slug' => $this->slugify('title'), |
187
|
|
|
'uri' => $this->getUri($urlGenerator) |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function toAmpacheApi( |
192
|
|
|
IL10N $l10n, |
193
|
|
|
callable $createPlayUrl, |
194
|
|
|
callable $createImageUrl, |
195
|
|
|
callable $renderAlbumOrArtistRef, |
196
|
|
|
string $genreKey, |
197
|
|
|
bool $includeArtists) : array { |
198
|
|
|
$album = $this->getAlbum(); |
199
|
|
|
|
200
|
|
|
$result = [ |
201
|
|
|
'id' => (string)$this->getId(), |
202
|
|
|
'title' => $this->getTitle() ?: '', |
203
|
|
|
'name' => $this->getTitle() ?: '', |
204
|
|
|
'artist' => $renderAlbumOrArtistRef($this->getArtistId() ?: 0, $this->getArtistNameString($l10n)), |
205
|
|
|
'albumartist' => $renderAlbumOrArtistRef($album->getAlbumArtistId() ?: 0, $album->getAlbumArtistNameString($l10n)), |
206
|
|
|
'album' => $renderAlbumOrArtistRef($album->getId() ?: 0, $album->getNameString($l10n)), |
207
|
|
|
'url' => $createPlayUrl($this), |
208
|
|
|
'time' => $this->getLength(), |
209
|
|
|
'year' => $this->getYear(), |
210
|
|
|
'track' => $this->getAdjustedTrackNumber(), // TODO: maybe there should be a user setting to select plain or adjusted number |
211
|
|
|
'playlisttrack' => $this->getAdjustedTrackNumber(), |
212
|
|
|
'disk' => $this->getDisk(), |
213
|
|
|
'filename' => $this->getFilename(), |
|
|
|
|
214
|
|
|
'format' => $this->getFileExtension(), |
215
|
|
|
'stream_format' => $this->getFileExtension(), |
216
|
|
|
'bitrate' => $this->getBitrate(), |
217
|
|
|
'stream_bitrate' => $this->getBitrate(), |
218
|
|
|
'mime' => $this->getMimetype(), |
219
|
|
|
'stream_mime' => $this->getMimetype(), |
220
|
|
|
'size' => $this->getSize(), |
|
|
|
|
221
|
|
|
'art' => $createImageUrl($this), |
222
|
|
|
'rating' => $this->getRating() ?? 0, |
223
|
|
|
'preciserating' => $this->getRating() ?? 0, |
224
|
|
|
'playcount' => $this->getPlayCount(), |
225
|
|
|
'flag' => !empty($this->getStarred()), |
226
|
|
|
'language' => null, |
227
|
|
|
'lyrics' => null, |
228
|
|
|
'mode' => null, // cbr/vbr |
229
|
|
|
'rate' => null, // sample rate [Hz] |
230
|
|
|
'replaygain_album_gain' => null, |
231
|
|
|
'replaygain_album_peak' => null, |
232
|
|
|
'replaygain_track_gain' => null, |
233
|
|
|
'replaygain_track_peak' => null, |
234
|
|
|
'r128_album_gain' => null, |
235
|
|
|
'r128_track_gain' => null, |
236
|
|
|
]; |
237
|
|
|
|
238
|
|
|
$genreId = $this->getGenreId(); |
239
|
|
|
if ($genreId !== null) { |
240
|
|
|
$result[$genreKey] = [[ |
241
|
|
|
'id' => (string)$genreId, |
242
|
|
|
'value' => $this->getGenreNameString($l10n), |
243
|
|
|
'count' => 1 |
244
|
|
|
]]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if ($includeArtists) { |
248
|
|
|
// Add another property `artists`. Apparently, it exists to support mulitple artists per song |
249
|
|
|
// but we don't have such possibility and this is always just a 1-item array. |
250
|
|
|
$result['artists'] = [$result['artist']]; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $result; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* The same API format is used both on "old" and "new" API methods. The "new" API adds some |
258
|
|
|
* new fields for the songs, but providing some extra fields shouldn't be a problem for the |
259
|
|
|
* older clients. The $track entity must have the Album reference injected prior to calling this. |
260
|
|
|
*/ |
261
|
|
|
public function toSubsonicApi(IL10N $l10n) : array { |
262
|
|
|
$albumId = $this->getAlbumId(); |
263
|
|
|
$album = $this->getAlbum(); |
264
|
|
|
$hasCoverArt = ($album !== null && !empty($album->getCoverFileId())); |
265
|
|
|
|
266
|
|
|
return [ |
267
|
|
|
'id' => 'track-' . $this->getId(), |
268
|
|
|
'parent' => 'album-' . $albumId, |
269
|
|
|
'discNumber' => $this->getDisk(), |
270
|
|
|
'title' => $this->getTitle(), |
271
|
|
|
'artist' => $this->getArtistNameString($l10n), |
272
|
|
|
'isDir' => false, |
273
|
|
|
'album' => $this->getAlbumNameString($l10n), |
274
|
|
|
'year' => $this->getYear(), |
275
|
|
|
'size' => $this->getSize(), |
276
|
|
|
'contentType' => $this->getMimetype(), |
277
|
|
|
'suffix' => $this->getFileExtension(), |
278
|
|
|
'duration' => $this->getLength() ?? 0, |
279
|
|
|
'bitRate' => empty($this->getBitrate()) ? null : (int)\round($this->getBitrate()/1000), // convert bps to kbps |
280
|
|
|
//'path' => '', |
281
|
|
|
'isVideo' => false, |
282
|
|
|
'albumId' => 'album-' . $albumId, |
283
|
|
|
'artistId' => 'artist-' . $this->getArtistId(), |
284
|
|
|
'type' => 'music', |
285
|
|
|
'created' => Util::formatZuluDateTime($this->getCreated()), |
286
|
|
|
'track' => $this->getAdjustedTrackNumber(false), // DSub would get confused of playlist numbering, https://github.com/owncloud/music/issues/994 |
287
|
|
|
'starred' => Util::formatZuluDateTime($this->getStarred()), |
288
|
|
|
'genre' => empty($this->getGenreId()) ? null : $this->getGenreNameString($l10n), |
289
|
|
|
'coverArt' => !$hasCoverArt ? null : 'album-' . $albumId |
290
|
|
|
]; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function getAdjustedTrackNumber(bool $enablePlaylistNumbering=true) : ?int { |
294
|
|
|
// Unless disabled, the number on playlist overrides the track number if it is set. |
295
|
|
|
if ($enablePlaylistNumbering && $this->numberOnPlaylist !== null) { |
296
|
|
|
$trackNumber = $this->numberOnPlaylist; |
297
|
|
|
} else { |
298
|
|
|
// On single-disk albums, the track number is given as-is. |
299
|
|
|
// On multi-disk albums, the disk-number is applied to the track number. |
300
|
|
|
// In case we have no Album reference, the best we can do is to apply the |
301
|
|
|
// disk number if it is greater than 1. For disk 1, we don't know if this |
302
|
|
|
// is a multi-disk album or not. |
303
|
|
|
$numberOfDisks = ($this->album) ? $this->album->getNumberOfDisks() : null; |
304
|
|
|
$trackNumber = $this->getNumber(); |
305
|
|
|
|
306
|
|
|
if ($this->disk > 1 || $numberOfDisks > 1) { |
307
|
|
|
$trackNumber = $trackNumber ?: 0; |
308
|
|
|
$trackNumber += (100 * $this->disk); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $trackNumber; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getFileExtension() : string { |
316
|
|
|
$parts = \explode('.', $this->getFilename()); |
|
|
|
|
317
|
|
|
return \end($parts); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
} |
321
|
|
|
|