Passed
Push — master ( d0b3e5...6c6ee2 )
by Pauli
02:39
created

Track::getFileExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 - 2022
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 ?Album getAlbum()
39
 * @method void setAlbum(?Album $album)
40
 * @method ?int getLength()
41
 * @method void setLength(?int $length)
42
 * @method int getFileId()
43
 * @method void setFileId(int $fileId)
44
 * @method ?int getBitrate()
45
 * @method void setBitrate(?int $bitrate)
46
 * @method string getMimetype()
47
 * @method void setMimetype(string $mimetype)
48
 * @method ?string getMbid()
49
 * @method void setMbid(?string $mbid)
50
 * @method ?string getStarred()
51
 * @method void setStarred(?string $timestamp)
52
 * @method ?int getGenreId()
53
 * @method void setGenreId(?int $genreId)
54
 * @method ?string getGenreName()
55
 * @method void setGenreName(?string $genreName)
56
 * @method string getFilename()
57
 * @method void setFilename(string $filename)
58
 * @method int getSize()
59
 * @method void setSize(int $size)
60
 * @method int getFileModTime()
61
 * @method void setFileModTime(int $secsFromEpoch)
62
 * @method ?int getNumberOnPlaylist()
63
 * @method void setNumberOnPlaylist(?int $number)
64
 * @method int getPlayCount()
65
 * @method void setPlayCount(int $count)
66
 * @method ?string getLastPlayed()
67
 * @method void setLastPlayed(?string $timestamp)
68
 */
69
class Track extends Entity {
70
	public $title;
71
	public $number;
72
	public $disk;
73
	public $year;
74
	public $artistId;
75
	public $albumId;
76
	public $length;
77
	public $fileId;
78
	public $bitrate;
79
	public $uri;
80
	public $mimetype;
81
	public $mbid;
82
	public $starred;
83
	public $genreId;
84
	public $playCount;
85
	public $lastPlayed;
86
87
	// not from the music_tracks table but still part of the standard content of this entity:
88
	public $filename;
89
	public $size;
90
	public $fileModTime;
91
	public $albumName;
92
	public $artistName;
93
	public $genreName;
94
95
	// the rest of the variables are injected separately when needed
96
	public $album;
97
	public $numberOnPlaylist;
98
99
	public function __construct() {
100
		$this->addType('number', 'int');
101
		$this->addType('disk', 'int');
102
		$this->addType('year', 'int');
103
		$this->addType('artistId', 'int');
104
		$this->addType('albumId', 'int');
105
		$this->addType('length', 'int');
106
		$this->addType('bitrate', 'int');
107
		$this->addType('fileId', 'int');
108
		$this->addType('genreId', 'int');
109
		$this->addType('size', 'int');
110
		$this->addType('fileModTime', 'int');
111
		$this->addType('playCount', 'int');
112
	}
113
114
	public function getUri(IURLGenerator $urlGenerator) : string {
115
		return $urlGenerator->linkToRoute(
116
			'music.shivaApi.track',
117
			['trackId' => $this->id]
118
		);
119
	}
120
121
	public function getArtistWithUri(IURLGenerator $urlGenerator) : array {
122
		return [
123
			'id' => $this->artistId,
124
			'uri' => $urlGenerator->linkToRoute(
125
				'music.shivaApi.artist',
126
				['artistId' => $this->artistId]
127
			)
128
		];
129
	}
130
131
	public function getAlbumWithUri(IURLGenerator $urlGenerator) : array {
132
		return [
133
			'id' => $this->albumId,
134
			'uri' => $urlGenerator->linkToRoute(
135
				'music.shivaApi.album',
136
				['albumId' => $this->albumId]
137
			)
138
		];
139
	}
140
141
	public function getArtistNameString(IL10N $l10n) : string {
142
		return $this->getArtistName() ?: Artist::unknownNameString($l10n);
143
	}
144
145
	public function getAlbumNameString(IL10N $l10n) : string {
146
		return $this->getAlbumName() ?: Album::unknownNameString($l10n);
147
	}
148
149
	public function getGenreNameString(IL10N $l10n) : string {
150
		return $this->getGenreName() ?: Genre::unknownNameString($l10n);
151
	}
152
153
	public function toCollection(IL10N $l10n) : array {
154
		return [
155
			'title' => $this->getTitle(),
156
			'number' => $this->getNumber(),
157
			'disk' => $this->getDisk(),
158
			'artistName' => $this->getArtistNameString($l10n),
159
			'artistId' => $this->getArtistId(),
160
			'length' => $this->getLength(),
161
			'files' => [$this->getMimetype() => $this->getFileId()],
162
			'id' => $this->getId(),
163
		];
164
	}
165
166
	public function toAPI(IURLGenerator $urlGenerator) : array {
167
		return [
168
			'title' => $this->getTitle(),
169
			'ordinal' => $this->getAdjustedTrackNumber(),
170
			'artist' => $this->getArtistWithUri($urlGenerator),
171
			'album' => $this->getAlbumWithUri($urlGenerator),
172
			'length' => $this->getLength(),
173
			'files' => [$this->getMimetype() => $urlGenerator->linkToRoute(
174
				'music.api.download',
175
				['fileId' => $this->getFileId()]
176
			)],
177
			'bitrate' => $this->getBitrate(),
178
			'id' => $this->getId(),
179
			'slug' => $this->slugify('title'),
180
			'uri' => $this->getUri($urlGenerator)
181
		];
182
	}
183
184
	public function toAmpacheApi(IL10N $l10n, callable $createPlayUrl, callable $createImageUrl) : array {
185
		$album = $this->getAlbum();
186
187
		$result = [
188
			'id' => (string)$this->getId(),
189
			'title' => $this->getTitle() ?: '',
190
			'name' => $this->getTitle() ?: '',
191
			'artist' => [
192
				'id' => (string)$this->getArtistId() ?: '0',
193
				'value' => $this->getArtistNameString($l10n)
194
			],
195
			'albumartist' => [
196
				'id' => (string)$album->getAlbumArtistId() ?: '0',
197
				'value' => $album->getAlbumArtistNameString($l10n)
198
			],
199
			'album' => [
200
				'id' => (string)$album->getId() ?: '0',
201
				'value' => $album->getNameString($l10n)
202
			],
203
			'url' => $createPlayUrl($this),
204
			'time' => $this->getLength(),
205
			'year' => $this->getYear(),
206
			'track' => $this->getAdjustedTrackNumber(),
207
			'filename' => $this->getFilename(),
208
			'bitrate' => $this->getBitrate(),
209
			'mime' => $this->getMimetype(),
210
			'size' => $this->getSize(),
211
			'art' => $createImageUrl($this),
212
			'rating' => 0,
213
			'preciserating' => 0,
214
			'playcount' => $this->getPlayCount(),
215
			'flag' => empty($this->getStarred()) ? 0 : 1,
216
		];
217
218
		$genreId = $this->getGenreId();
219
		if ($genreId !== null) {
220
			$result['tag'] = [[
221
				'id' => (string)$genreId,
222
				'value' => $this->getGenreNameString($l10n),
223
				'count' => 1
224
			]];
225
		}
226
227
		return $result;
228
	}
229
230
	/**
231
	 * The same API format is used both on "old" and "new" API methods. The "new" API adds some
232
	 * new fields for the songs, but providing some extra fields shouldn't be a problem for the
233
	 * older clients. The $track entity must have the Album reference injected prior to calling this.
234
	 */
235
	public function toSubsonicApi(IL10N $l10n) : array {
236
		$albumId = $this->getAlbumId();
237
		$album = $this->getAlbum();
238
		$hasCoverArt = ($album !== null && !empty($album->getCoverFileId()));
239
240
		return [
241
			'id' => 'track-' . $this->getId(),
242
			'parent' => 'album-' . $albumId,
243
			//'discNumber' => $this->getDisk(), // not supported on any of the tested clients => adjust track number instead
244
			'title' => $this->getTitle() ?? '',
245
			'artist' => $this->getArtistNameString($l10n),
246
			'isDir' => false,
247
			'album' => $this->getAlbumNameString($l10n),
248
			'year' => $this->getYear(),
249
			'size' => $this->getSize(),
250
			'contentType' => $this->getMimetype(),
251
			'suffix' => $this->getFileExtension(),
252
			'duration' => $this->getLength() ?? 0,
253
			'bitRate' => empty($this->getBitrate()) ? null : (int)\round($this->getBitrate()/1000), // convert bps to kbps
254
			//'path' => '',
255
			'isVideo' => false,
256
			'albumId' => 'album-' . $albumId,
257
			'artistId' => 'artist-' . $this->getArtistId(),
258
			'type' => 'music',
259
			'created' => Util::formatZuluDateTime($this->getCreated()),
260
			'track' => $this->getAdjustedTrackNumber(false), // DSub would get confused of playlist numbering, https://github.com/owncloud/music/issues/994
261
			'starred' => Util::formatZuluDateTime($this->getStarred()),
262
			'genre' => empty($this->getGenreId()) ? null : $this->getGenreNameString($l10n),
263
			'coverArt' => !$hasCoverArt ? null : 'album-' . $albumId
264
		];
265
	}
266
267
	public function getAdjustedTrackNumber(bool $enablePlaylistNumbering=true) : ?int {
268
		// Unless disabled, the number on playlist overrides the track number if it is set.
269
		if ($enablePlaylistNumbering && $this->numberOnPlaylist !== null) {
270
			$trackNumber = $this->numberOnPlaylist;
271
		} else {
272
			// On single-disk albums, the track number is given as-is.
273
			// On multi-disk albums, the disk-number is applied to the track number.
274
			// In case we have no Album reference, the best we can do is to apply the
275
			// disk number if it is greater than 1. For disk 1, we don't know if this
276
			// is a multi-disk album or not.
277
			$numberOfDisks = ($this->album) ? $this->album->getNumberOfDisks() : null;
278
			$trackNumber = $this->getNumber();
279
280
			if ($this->disk > 1 || $numberOfDisks > 1) {
281
				$trackNumber = $trackNumber ?: 0;
282
				$trackNumber += (100 * $this->disk);
283
			}
284
		}
285
286
		return $trackNumber;
287
	}
288
289
	public function getFileExtension() : string {
290
		$parts = \explode('.', $this->getFilename() ?? '');
291
		return \end($parts);
292
	}
293
294
}
295