Passed
Push — master ( f358a5...b5f949 )
by Pauli
03:17
created

CoverService::createAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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 Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2017 - 2025
11
 */
12
13
namespace OCA\Music\Service;
14
15
use OCA\Music\AppFramework\Core\Logger;
16
use OCA\Music\AppFramework\Db\UniqueConstraintViolationException;
17
use OCA\Music\BusinessLayer\AlbumBusinessLayer;
18
use OCA\Music\Db\Album;
19
use OCA\Music\Db\Artist;
20
use OCA\Music\Db\Cache;
21
use OCA\Music\Db\Entity;
22
use OCA\Music\Db\PodcastChannel;
23
use OCA\Music\Db\Playlist;
24
use OCA\Music\Db\RadioStation;
25
use OCA\Music\Utility\HttpUtil;
26
use OCA\Music\Utility\PlaceholderImage;
27
use OCA\Music\Utility\Random;
28
use OCP\Files\Folder;
29
use OCP\Files\File;
30
31
use OCP\IConfig;
32
use OCP\IL10N;
33
34
/**
35
 * utility to get cover image for album
36
 */
37
class CoverService {
38
	private Extractor $extractor;
39
	private Cache $cache;
40
	private AlbumBusinessLayer $albumBusinessLayer;
41
	private int $coverSize;
42
	private IL10N $l10n;
43
	private Logger $logger;
44
45
	const MAX_SIZE_TO_CACHE = 102400;
46
	const DO_NOT_CROP_OR_SCALE = -1;
47
48
	public function __construct(
49
			Extractor $extractor,
50
			Cache $cache,
51
			AlbumBusinessLayer $albumBusinessLayer,
52
			IConfig $config,
53
			IL10N $l10n,
54
			Logger $logger) {
55
		$this->extractor = $extractor;
56
		$this->cache = $cache;
57
		$this->albumBusinessLayer = $albumBusinessLayer;
58
		$this->l10n = $l10n;
59
		$this->logger = $logger;
60
61
		// Read the cover size to use from config.php or use the default
62
		$this->coverSize = \intval($config->getSystemValue('music.cover_size')) ?: 380;
63
	}
64
65
	/**
66
	 * Get cover image of an album, an artist, a podcast, or a playlist.
67
	 * Returns a generated placeholder in case there's no art set.
68
	 *
69
	 * @param int|null $size Desired (max) image size, null to use the default.
70
	 *                       Special value DO_NOT_CROP_OR_SCALE can be used to opt out of
71
	 *                       scaling and cropping altogether.
72
	 * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse
73
	 */
74
	public function getCover(Entity $entity, string $userId, Folder $rootFolder, ?int $size=null, bool $allowPlaceholder=true) : ?array {
75
		if ($entity instanceof Playlist) {
76
			$trackIds = $entity->getTrackIdsAsArray();
77
			$albums = $this->albumBusinessLayer->findAlbumsWithCoversForTracks($trackIds, $userId, 4);
78
			$result = $this->getCoverMosaic($albums, $userId, $rootFolder);
79
		} elseif ($entity instanceof RadioStation) {
80
			$result = null; // only placeholders supported for radio
81
		} elseif ($entity instanceof Album || $entity instanceof Artist || $entity instanceof PodcastChannel) {
82
			if ($size !== null) {
83
				// Skip using cache in case the cover is requested in specific size
84
				$result = $this->readCover($entity, $rootFolder, $size);
85
			} else {
86
				$dataAndHash = $this->getCoverAndHash($entity, $userId, $rootFolder);
87
				$result = $dataAndHash['data'];
88
			}
89
		} else {
90
			// only placeholder is supported for any other Entity type
91
			$result = null;
92
		}	
93
94
		if ($result === null && $allowPlaceholder) {
95
			$result = $this->getPlaceholder($entity, $size);
96
		}
97
98
		return $result;
99
	}
100
101
	public function getCoverMosaic(array $entities, string $userId, Folder $rootFolder, ?int $size=null) : ?array {
102
		if (\count($entities) === 0) {
103
			return null;
104
		} elseif (\count($entities) === 1) {
105
			return $this->getCover($entities[0], $userId, $rootFolder, $size);
106
		} else {
107
			$covers = \array_map(fn($entity) => $this->getCover($entity, $userId, $rootFolder), $entities);
108
			return $this->createMosaic($covers, $size);
109
		}
110
	}
111
112
	/**
113
	 * Get cover image of an album or and artist along with the image's hash
114
	 *
115
	 * The hash is non-null only in case the cover is/was cached.
116
	 *
117
	 * @param Album|Artist|PodcastChannel $entity
118
	 * @param string $userId
119
	 * @param Folder $rootFolder
120
	 * @return array Dictionary with keys 'data' and 'hash'
121
	 */
122
	public function getCoverAndHash($entity, string $userId, Folder $rootFolder) : array {
123
		$hash = $this->cache->get($userId, self::getHashKey($entity));
124
		$data = null;
125
126
		if ($hash !== null) {
127
			$data = $this->getCoverFromCache($hash, $userId);
128
		}
129
		if ($data === null) {
130
			$hash = null;
131
			$data = $this->readCover($entity, $rootFolder, $this->coverSize);
132
			if ($data !== null) {
133
				$hash = $this->addCoverToCache($entity, $userId, $data);
134
			}
135
		}
136
137
		return ['data' => $data, 'hash' => $hash];
138
	}
139
140
	/**
141
	 * Get all album cover hashes for one user.
142
	 * @param string $userId
143
	 * @return array with album IDs as keys and hashes as values
144
	 */
145
	public function getAllCachedAlbumCoverHashes(string $userId) : array {
146
		$rows = $this->cache->getAll($userId, 'album_cover_hash_');
147
		$hashes = [];
148
		$prefixLen = \strlen('album_cover_hash_');
149
		foreach ($rows as $row) {
150
			$albumId = \substr($row['key'], $prefixLen);
151
			$hashes[$albumId] = $row['data'];
152
		}
153
		return $hashes;
154
	}
155
156
	/**
157
	 * Get cover image with given hash from the cache
158
	 *
159
	 * @param string $hash
160
	 * @param string $userId
161
	 * @param bool $asBase64
162
	 * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse
163
	 */
164
	public function getCoverFromCache(string $hash, string $userId, bool $asBase64 = false) : ?array {
165
		$cached = $this->cache->get($userId, 'cover_' . $hash);
166
		if ($cached !== null) {
167
			$delimPos = \strpos($cached, '|');
168
			$mime = \substr($cached, 0, $delimPos);
169
			$content = \substr($cached, $delimPos + 1);
170
			if (!$asBase64) {
171
				$content = \base64_decode($content);
172
			}
173
			return ['mimetype' => $mime, 'content' => $content];
174
		}
175
		return null;
176
	}
177
178
	private function getPlaceholder(Entity $entity, ?int $size) : array {
179
		$name = $entity->getNameString($this->l10n);
180
		if (\method_exists($entity, 'getAlbumArtistNameString')) {
181
			$seed = $entity->/** @scrutinizer ignore-call */getAlbumArtistNameString($this->l10n) . $name;
182
		} else {
183
			$seed = $name;
184
		}
185
		$size = $size > 0 ? (int)$size : $this->coverSize;
186
		return PlaceholderImage::generateForResponse($name, $seed, $size);
187
	}
188
189
	/**
190
	 * Cache the given cover image data
191
	 * @param Album|Artist|PodcastChannel $entity
192
	 * @param string $userId
193
	 * @param array $coverData
194
	 * @return string|null Hash of the cached cover
195
	 */
196
	private function addCoverToCache(Entity $entity, string $userId, array $coverData) : ?string {
197
		$mime = $coverData['mimetype'];
198
		$content = $coverData['content'];
199
		$hash = null;
200
		$hashKey = self::getHashKey($entity);
201
202
		if ($mime && $content) {
203
			$size = \strlen($content);
204
			if ($size < self::MAX_SIZE_TO_CACHE) {
205
				$hash = \hash('md5', $content);
206
				// cache the data with hash as a key
207
				try {
208
					$this->cache->add($userId, 'cover_' . $hash, $mime . '|' . \base64_encode($content));
209
				} catch (UniqueConstraintViolationException $ex) {
210
					$this->logger->log("Cover with hash $hash is already cached", 'debug');
211
				}
212
				// cache the hash with hashKey as a key
213
				try {
214
					$this->cache->add($userId, $hashKey, $hash);
215
				} catch (UniqueConstraintViolationException $ex) {
216
					$this->logger->log("Cover hash with key $hashKey is already cached", 'debug');
217
				}
218
				// collection.json needs to be regenerated the next time it's fetched
219
				$this->cache->remove($userId, 'collection');
220
			} else {
221
				$this->logger->log("Cover image of entity with key $hashKey is large ($size B), skip caching", 'debug');
222
			}
223
		}
224
225
		return $hash;
226
	}
227
228
	/**
229
	 * Remove album cover image from cache if it is there. Silently do nothing if there
230
	 * is no cached cover. All users are targeted if no $userId passed.
231
	 */
232
	public function removeAlbumCoverFromCache(int $albumId, ?string $userId=null) : void {
233
		$this->cache->remove($userId, 'album_cover_hash_' . $albumId);
234
	}
235
236
	/**
237
	 * Remove artist cover image from cache if it is there. Silently do nothing if there
238
	 * is no cached cover. All users are targeted if no $userId passed.
239
	 */
240
	public function removeArtistCoverFromCache(int $artistId, ?string $userId=null) : void {
241
		$this->cache->remove($userId, 'artist_cover_hash_' . $artistId);
242
	}
243
244
	/**
245
	 * Read cover image from the entity-specific file or URL and scale it unless the caller opts out of it
246
	 * @param Album|Artist|PodcastChannel $entity
247
	 * @param Folder $rootFolder
248
	 * @param int $size Maximum size for the image to read, larger images are scaled down.
249
	 *                  Special value DO_NOT_CROP_OR_SCALE can be used to opt out of
250
	 *                  scaling and cropping altogether.
251
	 * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse
252
	 */
253
	private function readCover($entity, Folder $rootFolder, int $size) : ?array {
254
		if ($entity instanceof PodcastChannel) {
255
			list('content' => $image, 'content_type' => $mime) = HttpUtil::loadFromUrl($entity->getImageUrl());
256
			if ($image !== false) {
257
				$response = ['mimetype' => $mime, 'content' => $image];
258
			} else {
259
				$response = null;
260
			}
261
		} else {
262
			$response = $this->readCoverFromLocalFile($entity, $rootFolder);
263
		}
264
265
		if ($response !== null) {
266
			if ($size !== self::DO_NOT_CROP_OR_SCALE) {
267
				$response = $this->scaleDownAndCrop($response, $size);
268
			} elseif ($response['mimetype'] === null) {
269
				$response['mimetype'] = self::autoDetectMime($response['content']);
270
			}
271
		}
272
273
		return $response;
274
	}
275
276
	/**
277
	 * Read cover image from the file system
278
	 * @param Album|Artist $entity
279
	 * @param Folder $rootFolder
280
	 * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse
281
	 */
282
	private function readCoverFromLocalFile($entity, Folder $rootFolder) : ?array {
283
		$response = null;
284
285
		$coverId = $entity->getCoverFileId();
286
		if ($coverId > 0) {
287
			$node = $rootFolder->getById($coverId)[0] ?? null;
288
			if ($node instanceof File) {
289
				$mime = $node->getMimeType();
290
291
				if (\strpos($mime, 'audio') === 0) { // embedded cover image
292
					$cover = $this->extractor->parseEmbeddedCoverArt($node); // TODO: currently only album cover supported
293
294
					if ($cover !== null) {
295
						$response = ['mimetype' => $cover['image_mime'], 'content' => $cover['data']];
296
					}
297
				} else { // separate image file
298
					$response = ['mimetype' => $mime, 'content' => $node->getContent()];
299
				}
300
			}
301
302
			if ($response === null) {
303
				$class = \get_class($entity);
304
				$this->logger->log("Requested cover not found for $class entity {$entity->getId()}, coverId=$coverId", 'error');
305
			}
306
		}
307
308
		return $response;
309
	}
310
311
	/**
312
	 * Scale down images to reduce size and crop to square shape
313
	 *
314
	 * If one of the dimensions of the image is smaller than the maximum, then just
315
	 * crop to square shape but do not scale.
316
	 * @param array $image The image to be scaled down in format accepted by \OCA\Music\Http\FileResponse
317
	 * @param integer $maxSize The maximum size in pixels for the square shaped output
318
	 * @return array The processed image in format accepted by \OCA\Music\Http\FileResponse
319
	 */
320
	public function scaleDownAndCrop(array $image, int $maxSize) : array {
321
		$meta = \getimagesizefromstring($image['content']);
322
323
		if ($meta !== false) {
324
			$srcWidth = $meta[0];
325
			$srcHeight = $meta[1];
326
		} else {
327
			$srcWidth = $srcHeight = 0;
328
			$this->logger->log('Failed to extract size of the image, skip downscaling', 'warn');
329
		}
330
331
		// only process picture if it's larger than target size or not perfect square
332
		if ($srcWidth > $maxSize || $srcHeight > $maxSize || $srcWidth != $srcHeight) {
333
			$img = \imagecreatefromstring($image['content']);
334
335
			if ($img === false) {
336
				$this->logger->log('Failed to open cover image for downscaling', 'warn');
337
			} else {
338
				$srcCropSize = \min($srcWidth, $srcHeight);
339
				$srcX = (int)(($srcWidth - $srcCropSize) / 2);
340
				$srcY = (int)(($srcHeight - $srcCropSize) / 2);
341
342
				$dstSize = \min($maxSize, $srcCropSize);
343
				$scaledImg = \imagecreatetruecolor($dstSize, $dstSize);
344
345
				if ($scaledImg === false) {
346
					$this->logger->log("Failed to create scaled image of size $dstSize x $dstSize", 'warn');
347
					\imagedestroy($img);
348
				} else {
349
					\imagecopyresampled($scaledImg, $img, 0, 0, $srcX, $srcY, $dstSize, $dstSize, $srcCropSize, $srcCropSize);
350
					\imagedestroy($img);
351
352
					\ob_start();
353
					\ob_clean();
354
					$image['mimetype'] = $meta['mime']; // override the supplied mime with the auto-detected one
355
					switch ($image['mimetype']) {
356
						case 'image/jpeg':
357
							imagejpeg($scaledImg, null, 75);
358
							$image['content'] = \ob_get_contents();
359
							break;
360
						case 'image/png':
361
							imagepng($scaledImg, null, 7, PNG_ALL_FILTERS);
362
							$image['content'] = \ob_get_contents();
363
							break;
364
						case 'image/gif':
365
							imagegif($scaledImg, null);
366
							$image['content'] = \ob_get_contents();
367
							break;
368
						default:
369
							$this->logger->log("Cover image type {$image['mimetype']} not supported for downscaling", 'warn');
370
							break;
371
					}
372
					\ob_end_clean();
373
					\imagedestroy($scaledImg);
374
				}
375
			}
376
		}
377
		return $image;
378
	}
379
380
	private function createMosaic(array $covers, ?int $size) : array {
381
		$size = $size ?: $this->coverSize;
382
		$pieceSize = $size/2;
383
		$mosaicImg = \imagecreatetruecolor($size, $size);
384
		if ($mosaicImg === false) {
385
			$this->logger->log("Failed to create mosaic image of size $size x $size", 'warn');
386
		}
387
		else {
388
			$scaleAndCopyPiece = function($pieceData, $dstImage, $dstX, $dstY, $dstSize) {
389
				$meta = \getimagesizefromstring($pieceData['content']);
390
				$srcWidth = $meta[0];
391
				$srcHeight = $meta[1];
392
393
				$piece = imagecreatefromstring($pieceData['content']);
394
395
				if ($piece === false) {
396
					$this->logger->log('Failed to open cover image to create a mosaic', 'warn');
397
				} else {
398
					\imagecopyresampled($dstImage, $piece, $dstX, $dstY, 0, 0, $dstSize, $dstSize, $srcWidth, $srcHeight);
399
					\imagedestroy($piece);
400
				}
401
			};
402
403
			$coordinates = [
404
				['x' => 0,			'y' => 0],			// top-left
405
				['x' => $pieceSize,	'y' => $pieceSize],	// bottom-right
406
				['x' => $pieceSize,	'y' => 0],			// top-right
407
				['x' => 0,			'y' => $pieceSize],	// bottom-left
408
			];
409
410
			$covers = \array_slice($covers, 0, 4);
411
			foreach ($covers as $i => $cover) {
412
				$scaleAndCopyPiece($cover, $mosaicImg, $coordinates[$i]['x'], $coordinates[$i]['y'], $pieceSize);
413
			}
414
		}
415
416
		$image = ['mimetype' => 'image/png'];
417
		\ob_start();
418
		\ob_clean();
419
		imagepng($mosaicImg, null, 7, PNG_ALL_FILTERS);
420
		$image['content'] = \ob_get_contents();
421
		\ob_end_clean();
422
		\imagedestroy($mosaicImg);
423
424
		return $image;
425
	}
426
427
	private static function autoDetectMime(string $imageContent) : ?string {
428
		$meta = \getimagesizefromstring($imageContent);
429
		return ($meta === false) ? null : $meta['mime'];
430
	}
431
432
	/**
433
	 * @throws \InvalidArgumentException if entity is not one of the expected types
434
	 */
435
	private static function getHashKey(Entity $entity) : string {
436
		if ($entity instanceof Album) {
437
			return 'album_cover_hash_' . $entity->getId();
438
		} elseif ($entity instanceof Artist) {
439
			return 'artist_cover_hash_' . $entity->getId();
440
		} elseif ($entity instanceof PodcastChannel) {
441
			return 'podcast_cover_hash' . $entity->getId();
442
		} else {
443
			throw new \InvalidArgumentException('Unexpected entity type');
444
		}
445
	}
446
447
	/**
448
	 * Create and store an access token which can be used to read cover images of a user.
449
	 * A user may have only one valid cover image access token at a time; the latest token
450
	 * always overwrites the previously obtained one.
451
	 *
452
	 * The reason this is needed is because the mediaSession in Firefox loads the cover images
453
	 * in a context where normal cookies and other standard request headers are not available.
454
	 * Hence, we need to provide the cover images as "public" resources, i.e. without requiring
455
	 * that the caller is logged in to the cloud. But still, we don't want to let just anyone
456
	 * load the user data. The solution is to use a temporary token which grants access just to
457
	 * the cover images. This token can be then sent as URL argument by the mediaSession.
458
	 */
459
	public function createAccessToken(string $userId) : string {
460
		$token = Random::secure(32);
461
		// It might be neater to use a dedicated DB table for this, but the generic cache table
462
		// will do, at least for now.
463
		$this->cache->set($userId, 'cover_access_token', $token);
464
		return $token;
465
	}
466
467
	/**
468
	 * @see CoverService::createAccessToken
469
	 * @throws \OutOfBoundsException if the token is not valid
470
	 */
471
	public function getUserForAccessToken(?string $token) : string {
472
		if ($token === null) {
473
			throw new \OutOfBoundsException('Cannot get user for a null token');
474
		}
475
		$userId = $this->cache->getOwner('cover_access_token', $token);
476
		if ($userId === null) {
477
			throw new \OutOfBoundsException('No userId found for the given token');
478
		}
479
		return $userId;
480
	}
481
}
482