Passed
Push — master ( f9c9de...599a1b )
by Pauli
01:59
created

Track::toSubsonicApi()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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