Completed
Pull Request — master (#777)
by Pauli
16:48 queued 14:24
created

ApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 17
dl 0
loc 33
ccs 17
cts 17
cp 1
crap 1
rs 9.7333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
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 2017 - 2020
13
 */
14
15
namespace OCA\Music\Controller;
16
17
use \OCP\AppFramework\Controller;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use \OCP\AppFramework\Http;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use \OCP\AppFramework\Http\DataDisplayResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\DataDisplayResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use \OCP\AppFramework\Http\JSONResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\JSONResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use \OCP\AppFramework\Http\RedirectResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use \OCP\Files\Folder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Folder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use \OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use \OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use \OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
27
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
28
use \OCA\Music\AppFramework\Core\Logger;
29
use \OCA\Music\BusinessLayer\AlbumBusinessLayer;
30
use \OCA\Music\BusinessLayer\ArtistBusinessLayer;
31
use \OCA\Music\BusinessLayer\GenreBusinessLayer;
32
use \OCA\Music\BusinessLayer\TrackBusinessLayer;
33
use \OCA\Music\Db\Album;
34
use \OCA\Music\Db\Artist;
35
use \OCA\Music\Db\Maintenance;
36
use \OCA\Music\Db\Track;
37
use \OCA\Music\Http\ErrorResponse;
38
use \OCA\Music\Http\FileResponse;
39
use \OCA\Music\Utility\CollectionHelper;
40
use \OCA\Music\Utility\CoverHelper;
41
use \OCA\Music\Utility\DetailsHelper;
42
use \OCA\Music\Utility\LastfmService;
43
use \OCA\Music\Utility\Scanner;
44
use \OCA\Music\Utility\Util;
45
46
class ApiController extends Controller {
47
48
	/** @var IL10N */
49
	private $l10n;
50
	/** @var TrackBusinessLayer */
51
	private $trackBusinessLayer;
52
	/** @var ArtistBusinessLayer */
53
	private $artistBusinessLayer;
54
	/** @var AlbumBusinessLayer */
55
	private $albumBusinessLayer;
56
	/** @var GenreBusinessLayer */
57
	private $genreBusinessLayer;
58
	/** @var Scanner */
59
	private $scanner;
60
	/** @var CollectionHelper */
61
	private $collectionHelper;
62
	/** @var CoverHelper */
63
	private $coverHelper;
64
	/** @var DetailsHelper */
65
	private $detailsHelper;
66
	/** @var LastfmService */
67
	private $lastfmService;
68
	/** @var Maintenance */
69
	private $maintenance;
70
	/** @var string */
71
	private $userId;
72
	/** @var IURLGenerator */
73
	private $urlGenerator;
74
	/** @var Folder */
75
	private $userFolder;
76
	/** @var Logger */
77
	private $logger;
78
79 16
	public function __construct($appname,
80
								IRequest $request,
81
								IURLGenerator $urlGenerator,
82
								TrackBusinessLayer $trackbusinesslayer,
83
								ArtistBusinessLayer $artistbusinesslayer,
84
								AlbumBusinessLayer $albumbusinesslayer,
85
								GenreBusinessLayer $genreBusinessLayer,
86
								Scanner $scanner,
87
								CollectionHelper $collectionHelper,
88
								CoverHelper $coverHelper,
89
								DetailsHelper $detailsHelper,
90
								LastfmService $lastfmService,
91
								Maintenance $maintenance,
92
								$userId,
93
								IL10N $l10n,
94
								/*Folder*/ $userFolder, // no type-hint as this may sometimes be null
95
								Logger $logger) {
96 16
		parent::__construct($appname, $request);
97 16
		$this->l10n = $l10n;
98 16
		$this->trackBusinessLayer = $trackbusinesslayer;
99 16
		$this->artistBusinessLayer = $artistbusinesslayer;
100 16
		$this->albumBusinessLayer = $albumbusinesslayer;
101 16
		$this->genreBusinessLayer = $genreBusinessLayer;
102 16
		$this->scanner = $scanner;
103 16
		$this->collectionHelper = $collectionHelper;
104 16
		$this->coverHelper = $coverHelper;
105 16
		$this->detailsHelper = $detailsHelper;
106 16
		$this->lastfmService = $lastfmService;
107 16
		$this->maintenance = $maintenance;
108 16
		$this->userId = $userId;
109 16
		$this->urlGenerator = $urlGenerator;
110 16
		$this->userFolder = $userFolder;
111 16
		$this->logger = $logger;
112 16
	}
113
114
	/**
115
	 * Extracts the id from an unique slug (id-slug)
116
	 * @param string $slug the slug
117
	 * @return integer the id
118
	 */
119 5
	protected static function getIdFromSlug($slug) {
120 5
		$split = \explode('-', $slug, 2);
121
122 5
		return (int)$split[0];
123
	}
124
125
	/**
126
	 * @NoAdminRequired
127
	 * @NoCSRFRequired
128
	 */
129
	public function prepareCollection() {
130
		$hash = $this->collectionHelper->getCachedJsonHash();
131
		if ($hash === null) {
132
			// build the collection but ignore the data for now
133
			$this->collectionHelper->getJson();
134
			$hash = $this->collectionHelper->getCachedJsonHash();
135
		}
136
		return new JSONResponse(['hash' => $hash]);
137
	}
138
139
	/**
140
	 * @NoAdminRequired
141
	 * @NoCSRFRequired
142
	 */
143
	public function collection() {
144
145
		$collectionJson = $this->collectionHelper->getJson();
146
		$response = new DataDisplayResponse($collectionJson);
147
		$response->addHeader('Content-Type', 'application/json; charset=utf-8');
148
149
		// Instruct the client to cache the result in case it requested the collection with
150
		// the correct hash. The hash could be incorrect if the collection would have changed
151
		// between calls to prepareCollection() and colletion().
152
		$requestHash = $this->request->getParam('hash');
153
		$actualHash = $this->collectionHelper->getCachedJsonHash();
154
		if (!empty($actualHash) && $requestHash === $actualHash) {
155
			self::setClientCaching($response, 90); // cache for 3 months
156
		}
157
158
		return $response;
159
	}
160
161
	/**
162
	 * @NoAdminRequired
163
	 * @NoCSRFRequired
164
	 */
165
	public function folders() {
166
		$folders = $this->trackBusinessLayer->findAllFolders($this->userId, $this->userFolder);
167
		return new JSONResponse($folders);
168
	}
169
170
	/**
171
	 * @NoAdminRequired
172
	 * @NoCSRFRequired
173
	 */
174
	public function genres() {
175
		$genres = $this->genreBusinessLayer->findAllWithTrackIds($this->userId);
176
		$unscanned =  $this->trackBusinessLayer->findFilesWithoutScannedGenre($this->userId);
177
		return new JSONResponse([
178
			'genres' => \array_map(function($g) {return $g->toApi();}, $genres),
179
			'unscanned' => $unscanned
180
		]);
181
	}
182
183
	/**
184
	 * @NoAdminRequired
185
	 * @NoCSRFRequired
186
	 */
187 3
	public function artists($fulltree, $albums) {
188 3
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
189 3
		$includeAlbums = \filter_var($albums, FILTER_VALIDATE_BOOLEAN);
190
		/** @var Artist[] $artists */
191 3
		$artists = $this->artistBusinessLayer->findAll($this->userId);
192
193
		$artists = \array_map(function($a) use ($fulltree, $includeAlbums) {
194 3
			return $this->artistToApi($a, $includeAlbums || $fulltree, $fulltree);
195 3
		}, $artists);
196
197 3
		return new JSONResponse($artists);
198
	}
199
200
	/**
201
	 * @NoAdminRequired
202
	 * @NoCSRFRequired
203
	 */
204 2
	public function artist($artistIdOrSlug, $fulltree) {
205 2
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
206 2
		$artistId = $this->getIdFromSlug($artistIdOrSlug);
207
		/** @var Artist $artist */
208 2
		$artist = $this->artistBusinessLayer->find($artistId, $this->userId);
209 2
		$artist = $this->artistToApi($artist, $fulltree, $fulltree);
210 2
		return new JSONResponse($artist);
211
	}
212
213
	/**
214
	 * Return given artist in Shia API format
215
	 * @param Artist $artist
216
	 * @param boolean $includeAlbums
217
	 * @param boolean $includeTracks (ignored if $includeAlbums==false)
218
	 * @return array
219
	 */
220 5
	private function artistToApi($artist, $includeAlbums, $includeTracks) {
221 5
		$artistInApi = $artist->toAPI($this->urlGenerator, $this->l10n);
222 5
		if ($includeAlbums) {
223 3
			$artistId = $artist->getId();
224 3
			$albums = $this->albumBusinessLayer->findAllByArtist($artistId, $this->userId);
225
226
			$artistInApi['albums'] = \array_map(function($a) use ($includeTracks) {
227 3
				return $this->albumToApi($a, $includeTracks, false);
228 3
			}, $albums);
229
		}
230 5
		return $artistInApi;
231
	}
232
233
	/**
234
	 * @NoAdminRequired
235
	 * @NoCSRFRequired
236
	 */
237 2
	public function albums($artist, $fulltree) {
238 2
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
239 2
		if ($artist) {
240
			$albums = $this->albumBusinessLayer->findAllByArtist($artist, $this->userId);
241
		} else {
242 2
			$albums = $this->albumBusinessLayer->findAll($this->userId);
243
		}
244
245
		$albums = \array_map(function($a) use ($fulltree) {
246 2
			return $this->albumToApi($a, $fulltree, $fulltree);
247 2
		}, $albums);
248
249 2
		return new JSONResponse($albums);
250
	}
251
252
	/**
253
	 * @NoAdminRequired
254
	 * @NoCSRFRequired
255
	 */
256 2
	public function album($albumIdOrSlug, $fulltree) {
257 2
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
258 2
		$albumId = $this->getIdFromSlug($albumIdOrSlug);
259 2
		$album = $this->albumBusinessLayer->find($albumId, $this->userId);
260 2
		$album = $this->albumToApi($album, $fulltree, $fulltree);
261 2
		return new JSONResponse($album);
262
	}
263
264
	/**
265
	 * Return given album in the Shiva API format
266
	 * @param Album $album
267
	 * @param boolean $includeTracks
268
	 * @param boolean $includeAlbums
269
	 * @return array
270
	 */
271 7
	private function albumToApi($album, $includeTracks, $includeArtists) {
272 7
		$albumInApi = $album->toAPI($this->urlGenerator, $this->l10n);
273
274 7
		if ($includeTracks) {
275 4
			$albumId = $album->getId();
276 4
			$tracks = $this->trackBusinessLayer->findAllByAlbum($albumId, $this->userId);
277
			$albumInApi['tracks'] = \array_map(function($t) {
278 4
				return $t->toAPI($this->urlGenerator);
279 4
			}, $tracks);
280
		}
281
282 7
		if ($includeArtists) {
283 2
			$artistIds = $album->getArtistIds();
284 2
			$artists = $this->artistBusinessLayer->findById($artistIds, $this->userId);
285 2
			$albumInApi['artists'] = \array_map(function($a) {
286 2
				return $a->toAPI($this->urlGenerator, $this->l10n);
287 2
			}, $artists);
288
		}
289
290 7
		return $albumInApi;
291
	}
292
293
	/**
294
	 * @NoAdminRequired
295
	 * @NoCSRFRequired
296
	 */
297 4
	public function tracks($artist, $album, $fulltree) {
298 4
		$fulltree = \filter_var($fulltree, FILTER_VALIDATE_BOOLEAN);
299 4
		if ($artist) {
300 1
			$tracks = $this->trackBusinessLayer->findAllByArtist($artist, $this->userId);
301 3
		} elseif ($album) {
302 1
			$tracks = $this->trackBusinessLayer->findAllByAlbum($album, $this->userId);
303
		} else {
304 2
			$tracks = $this->trackBusinessLayer->findAll($this->userId);
305
		}
306 4
		foreach ($tracks as &$track) {
307 4
			$artistId = $track->getArtistId();
308 4
			$albumId = $track->getAlbumId();
309 4
			$track = $track->toAPI($this->urlGenerator);
310 4
			if ($fulltree) {
311
				/** @var Artist $artist */
312 1
				$artist = $this->artistBusinessLayer->find($artistId, $this->userId);
313 1
				$track['artist'] = $artist->toAPI($this->urlGenerator, $this->l10n);
314 1
				$album = $this->albumBusinessLayer->find($albumId, $this->userId);
315 1
				$track['album'] = $album->toAPI($this->urlGenerator, $this->l10n);
316
			}
317
		}
318 4
		return new JSONResponse($tracks);
319
	}
320
321
	/**
322
	 * @NoAdminRequired
323
	 * @NoCSRFRequired
324
	 */
325 1
	public function track($trackIdOrSlug) {
326 1
		$trackId = $this->getIdFromSlug($trackIdOrSlug);
327
		/** @var Track $track */
328 1
		$track = $this->trackBusinessLayer->find($trackId, $this->userId);
329 1
		return new JSONResponse($track->toAPI($this->urlGenerator));
330
	}
331
332
	/**
333
	 * @NoAdminRequired
334
	 * @NoCSRFRequired
335
	 */
336 1
	public function trackByFileId($fileId) {
337 1
		$track = $this->trackBusinessLayer->findByFileId($fileId, $this->userId);
338 1
		if ($track !== null) {
339 1
			$track->setAlbum($this->albumBusinessLayer->find($track->getAlbumId(), $this->userId));
340 1
			$track->setArtist($this->artistBusinessLayer->find($track->getArtistId(), $this->userId));
341 1
			return new JSONResponse($track->toCollection($this->l10n));
342
		} else {
343
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
344
		}
345
	}
346
347
	/**
348
	 * @NoAdminRequired
349
	 * @NoCSRFRequired
350
	 */
351
	public function getScanState() {
352
		return new JSONResponse([
353
			'unscannedFiles' => $this->scanner->getUnscannedMusicFileIds($this->userId),
354
			'scannedCount' => $this->trackBusinessLayer->count($this->userId)
355
		]);
356
	}
357
358
	/**
359
	 * @NoAdminRequired
360
	 * @UseSession to keep the session reserved while execution in progress
361
	 */
362
	public function scan($files, $finalize) {
363
		// extract the parameters
364
		$fileIds = \array_map('intval', \explode(',', $files));
365
		$finalize = \filter_var($finalize, FILTER_VALIDATE_BOOLEAN);
366
367
		$filesScanned = $this->scanner->scanFiles($this->userId, $this->userFolder, $fileIds);
368
369
		$coversUpdated = false;
370
		if ($finalize) {
371
			$coversUpdated = $this->scanner->findAlbumCovers($this->userId)
372
							|| $this->scanner->findArtistCovers($this->userId);
373
			$totalCount = $this->trackBusinessLayer->count($this->userId);
374
			$this->logger->log("Scanning finished, user $this->userId has $totalCount scanned tracks in total", 'info');
375
		}
376
377
		return new JSONResponse([
378
			'filesScanned' => $filesScanned,
379
			'coversUpdated' => $coversUpdated
380
		]);
381
	}
382
383
	/**
384
	 * @NoAdminRequired
385
	 * @UseSession to keep the session reserved while execution in progress
386
	 */
387
	public function resetScanned() {
388
		$this->maintenance->resetDb($this->userId);
389
		return new JSONResponse(['success' => true]);
390
	}
391
392
	/**
393
	 * @NoAdminRequired
394
	 * @NoCSRFRequired
395
	 */
396
	public function download($fileId) {
397
		$track = $this->trackBusinessLayer->findByFileId($fileId, $this->userId);
398
		if ($track === null) {
399
			return new ErrorResponse(Http::STATUS_NOT_FOUND, 'track not found');
400
		}
401
402
		$nodes = $this->userFolder->getById($track->getFileId());
403
		if (\count($nodes) > 0) {
404
			// get the first valid node
405
			$node = $nodes[0];
406
407
			$mime = $node->getMimeType();
408
			$content = $node->getContent();
409
			return new FileResponse(['mimetype' => $mime, 'content' => $content]);
410
		}
411
412
		return new ErrorResponse(Http::STATUS_NOT_FOUND, 'file not found');
413
	}
414
415
	/**
416
	 * @NoAdminRequired
417
	 * @NoCSRFRequired
418
	 */
419
	public function filePath($fileId) {
420
		$nodes = $this->userFolder->getById($fileId);
421
		if (\count($nodes) == 0) {
422
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
423
		} else {
424
			$node = $nodes[0];
425
			$path = $this->userFolder->getRelativePath($node->getPath());
426
			return new JSONResponse(['path' => Util::urlEncodePath($path)]);
427
		}
428
	}
429
430
	/**
431
	 * @NoAdminRequired
432
	 * @NoCSRFRequired
433
	 */
434
	public function fileInfo($fileId) {
435
		$info = $this->scanner->getFileInfo($fileId, $this->userId, $this->userFolder);
436
		if ($info) {
437
			return new JSONResponse($info);
438
		} else {
439
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
440
		}
441
	}
442
443
	/**
444
	 * @NoAdminRequired
445
	 * @NoCSRFRequired
446
	 */
447
	public function fileDetails($fileId) {
448
		$details = $this->detailsHelper->getDetails($fileId, $this->userFolder);
449
		if ($details) {
450
			return new JSONResponse($details);
451
		} else {
452
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
453
		}
454
	}
455
456
	/**
457
	 * @NoAdminRequired
458
	 * @NoCSRFRequired
459
	 */
460
	public function artistDetails($artistIdOrSlug) {
461
		try {
462
			$artistId = $this->getIdFromSlug($artistIdOrSlug);
463
			$info = $this->lastfmService->getArtistInfo($artistId, $this->userId);
464
			return new JSONResponse($info);
465
		}
466
		catch (BusinessLayerException $e) {
467
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
468
		}
469
	}
470
471
	/**
472
	 * @NoAdminRequired
473
	 * @NoCSRFRequired
474
	 */
475
	public function albumCover($albumIdOrSlug) {
476
		$albumId = $this->getIdFromSlug($albumIdOrSlug);
477
		$album = $this->albumBusinessLayer->find($albumId, $this->userId);
478
		return $this->cover($album);
479
	}
480
481
	/**
482
	 * @NoAdminRequired
483
	 * @NoCSRFRequired
484
	 */
485
	public function artistCover($artistIdOrSlug, $originalSize) {
486
		$originalSize = \filter_var($originalSize, FILTER_VALIDATE_BOOLEAN);
487
488
		$artistId = $this->getIdFromSlug($artistIdOrSlug);
489
		$artist = $this->artistBusinessLayer->find($artistId, $this->userId);
490
491
		if ($originalSize) {
492
			$cover = $this->coverHelper->getCover(
493
					$artist, $this->userId, $this->userFolder, CoverHelper::DO_NOT_CROP_OR_SCALE);
494
			if ($cover !== null) {
495
				return new FileResponse($cover);
496
			} else {
497
				return new ErrorResponse(Http::STATUS_NOT_FOUND);
498
			}
499
		}
500
		else {
501
			return $this->cover($artist);
502
		}
503
	}
504
505
	private function cover($entity) {
506
		$coverAndHash = $this->coverHelper->getCoverAndHash($entity, $this->userId, $this->userFolder);
507
508
		if ($coverAndHash['hash'] !== null) {
509
			// Cover is in cache. Return a redirection response so that the client
510
			// will fetch the content through a cacheable route.
511
			$link = $this->urlGenerator->linkToRoute('music.api.cachedCover', ['hash' => $coverAndHash['hash']]);
512
			return new RedirectResponse($link);
513
		} else if ($coverAndHash['data'] !== null) {
514
			return new FileResponse($coverAndHash['data']);
515
		} else {
516
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
517
		}
518
	}
519
520
	/**
521
	 * @NoAdminRequired
522
	 * @NoCSRFRequired
523
	 */
524
	public function cachedCover($hash) {
525
		$coverData = $this->coverHelper->getCoverFromCache($hash, $this->userId);
526
527
		if ($coverData !== null) {
528
			$response =  new FileResponse($coverData);
529
			// instruct also the client-side to cache the result, this is safe
530
			// as the resource URI contains the image hash
531
			self::setClientCaching($response);
532
			return $response;
533
		} else {
534
			return new ErrorResponse(Http::STATUS_NOT_FOUND);
535
		}
536
	}
537
538
	private static function setClientCaching(&$httpResponse, $days=365) {
539
		$httpResponse->cacheFor($days * 24 * 60 * 60);
540
		$httpResponse->addHeader('Pragma', 'cache');
541
	}
542
}
543