| Total Complexity | 59 |
| Total Lines | 409 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
Complex classes like CoverHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CoverHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 32 | class CoverHelper { |
||
| 33 | private $extractor; |
||
| 34 | private $cache; |
||
| 35 | private $albumBusinessLayer; |
||
| 36 | private $coverSize; |
||
| 37 | private $logger; |
||
| 38 | |||
| 39 | const MAX_SIZE_TO_CACHE = 102400; |
||
| 40 | const DO_NOT_CROP_OR_SCALE = -1; |
||
| 41 | |||
| 42 | public function __construct( |
||
| 43 | Extractor $extractor, |
||
| 44 | Cache $cache, |
||
| 45 | AlbumBusinessLayer $albumBusinessLayer, |
||
| 46 | IConfig $config, |
||
| 47 | Logger $logger) { |
||
| 48 | $this->extractor = $extractor; |
||
| 49 | $this->cache = $cache; |
||
| 50 | $this->albumBusinessLayer = $albumBusinessLayer; |
||
| 51 | $this->logger = $logger; |
||
| 52 | |||
| 53 | // Read the cover size to use from config.php or use the default |
||
| 54 | $this->coverSize = \intval($config->getSystemValue('music.cover_size')) ?: 380; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get cover image of an album or and artist |
||
| 59 | * |
||
| 60 | * @param Album|Artist|PodcastChannel|Playlist $entity |
||
| 61 | * @param string $userId |
||
| 62 | * @param Folder $rootFolder |
||
| 63 | * @param int|null $size Desired (max) image size, null to use the default. |
||
| 64 | * Special value DO_NOT_CROP_OR_SCALE can be used to opt out of |
||
| 65 | * scaling and cropping altogether. |
||
| 66 | * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse |
||
| 67 | */ |
||
| 68 | public function getCover($entity, string $userId, Folder $rootFolder, int $size=null) : ?array { |
||
| 69 | if ($entity instanceof Playlist) { |
||
| 70 | $trackIds = $entity->getTrackIdsAsArray(); |
||
| 71 | $albums = $this->albumBusinessLayer->findAlbumsWithCoversForTracks($trackIds, $userId, 4); |
||
| 72 | return $this->getCoverMosaic($albums, $userId, $rootFolder); |
||
| 73 | } elseif ($size !== null) { |
||
| 74 | // Skip using cache in case the cover is requested in specific size |
||
| 75 | return $this->readCover($entity, $rootFolder, $size); |
||
| 76 | } else { |
||
| 77 | $dataAndHash = $this->getCoverAndHash($entity, $userId, $rootFolder); |
||
| 78 | return $dataAndHash['data']; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getCoverMosaic(array $entities, string $userId, Folder $rootFolder, int $size=null) : ?array { |
||
| 83 | if (\count($entities) === 0) { |
||
| 84 | return null; |
||
| 85 | } elseif (\count($entities) === 1) { |
||
| 86 | return $this->getCover($entities[0], $userId, $rootFolder, $size); |
||
| 87 | } else { |
||
| 88 | $covers = \array_map(function($entity) use ($userId, $rootFolder) { |
||
| 89 | return $this->getCover($entity, $userId, $rootFolder); |
||
| 90 | }, $entities); |
||
| 91 | |||
| 92 | return $this->createMosaic($covers, $size); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get cover image of an album or and artist along with the image's hash |
||
| 98 | * |
||
| 99 | * The hash is non-null only in case the cover is/was cached. |
||
| 100 | * |
||
| 101 | * @param Album|Artist|PodcastChannel $entity |
||
| 102 | * @param string $userId |
||
| 103 | * @param Folder $rootFolder |
||
| 104 | * @return array Dictionary with keys 'data' and 'hash' |
||
| 105 | */ |
||
| 106 | public function getCoverAndHash($entity, string $userId, Folder $rootFolder) : array { |
||
| 107 | $hash = $this->cache->get($userId, self::getHashKey($entity)); |
||
| 108 | $data = null; |
||
| 109 | |||
| 110 | if ($hash !== null) { |
||
| 111 | $data = $this->getCoverFromCache($hash, $userId); |
||
| 112 | } |
||
| 113 | if ($data === null) { |
||
| 114 | $hash = null; |
||
| 115 | $data = $this->readCover($entity, $rootFolder, $this->coverSize); |
||
| 116 | if ($data !== null) { |
||
| 117 | $hash = $this->addCoverToCache($entity, $userId, $data); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return ['data' => $data, 'hash' => $hash]; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get all album cover hashes for one user. |
||
| 126 | * @param string $userId |
||
| 127 | * @return array with album IDs as keys and hashes as values |
||
| 128 | */ |
||
| 129 | public function getAllCachedAlbumCoverHashes(string $userId) : array { |
||
| 130 | $rows = $this->cache->getAll($userId, 'album_cover_hash_'); |
||
| 131 | $hashes = []; |
||
| 132 | $prefixLen = \strlen('album_cover_hash_'); |
||
| 133 | foreach ($rows as $row) { |
||
| 134 | $albumId = \substr($row['key'], $prefixLen); |
||
| 135 | $hashes[$albumId] = $row['data']; |
||
| 136 | } |
||
| 137 | return $hashes; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get cover image with given hash from the cache |
||
| 142 | * |
||
| 143 | * @param string $hash |
||
| 144 | * @param string $userId |
||
| 145 | * @param bool $asBase64 |
||
| 146 | * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse |
||
| 147 | */ |
||
| 148 | public function getCoverFromCache(string $hash, string $userId, bool $asBase64 = false) : ?array { |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Cache the given cover image data |
||
| 164 | * @param Album|Artist|PodcastChannel $entity |
||
| 165 | * @param string $userId |
||
| 166 | * @param array $coverData |
||
| 167 | * @return string|null Hash of the cached cover |
||
| 168 | */ |
||
| 169 | private function addCoverToCache($entity, string $userId, array $coverData) : ?string { |
||
| 170 | $mime = $coverData['mimetype']; |
||
| 171 | $content = $coverData['content']; |
||
| 172 | $hash = null; |
||
| 173 | $hashKey = self::getHashKey($entity); |
||
| 174 | |||
| 175 | if ($mime && $content) { |
||
| 176 | $size = \strlen($content); |
||
| 177 | if ($size < self::MAX_SIZE_TO_CACHE) { |
||
| 178 | $hash = \hash('md5', $content); |
||
| 179 | // cache the data with hash as a key |
||
| 180 | try { |
||
| 181 | $this->cache->add($userId, 'cover_' . $hash, $mime . '|' . \base64_encode($content)); |
||
| 182 | } catch (UniqueConstraintViolationException $ex) { |
||
| 183 | $this->logger->log("Cover with hash $hash is already cached", 'debug'); |
||
| 184 | } |
||
| 185 | // cache the hash with hashKey as a key |
||
| 186 | try { |
||
| 187 | $this->cache->add($userId, $hashKey, $hash); |
||
| 188 | } catch (UniqueConstraintViolationException $ex) { |
||
| 189 | $this->logger->log("Cover hash with key $hashKey is already cached", 'debug'); |
||
| 190 | } |
||
| 191 | // collection.json needs to be regenrated the next time it's fetched |
||
| 192 | $this->cache->remove($userId, 'collection'); |
||
| 193 | } else { |
||
| 194 | $this->logger->log("Cover image of entity with key $hashKey is large ($size B), skip caching", 'debug'); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return $hash; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Remove album cover image from cache if it is there. Silently do nothing if there |
||
| 203 | * is no cached cover. All users are targeted if no $userId passed. |
||
| 204 | */ |
||
| 205 | public function removeAlbumCoverFromCache(int $albumId, string $userId=null) : void { |
||
| 206 | $this->cache->remove($userId, 'album_cover_hash_' . $albumId); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Remove artist cover image from cache if it is there. Silently do nothing if there |
||
| 211 | * is no cached cover. All users are targeted if no $userId passed. |
||
| 212 | */ |
||
| 213 | public function removeArtistCoverFromCache(int $artistId, string $userId=null) : void { |
||
| 214 | $this->cache->remove($userId, 'artist_cover_hash_' . $artistId); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Read cover image from the entity-specific file or URL and scale it unless the caller opts out of it |
||
| 219 | * @param Album|Artist|PodcastChannel $entity |
||
| 220 | * @param Folder $rootFolder |
||
| 221 | * @param int $size Maximum size for the image to read, larger images are scaled down. |
||
| 222 | * Special value DO_NOT_CROP_OR_SCALE can be used to opt out of |
||
| 223 | * scaling and cropping altogether. |
||
| 224 | * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse |
||
| 225 | */ |
||
| 226 | private function readCover($entity, Folder $rootFolder, int $size) : ?array { |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Read cover image from the file system |
||
| 246 | * @param Album|Artist $entity |
||
| 247 | * @param Folder $rootFolder |
||
| 248 | * @return array|null Image data in format accepted by \OCA\Music\Http\FileResponse |
||
| 249 | */ |
||
| 250 | private function readCoverFromLocalFile($entity, Folder $rootFolder) : ?array { |
||
| 251 | $response = null; |
||
| 252 | |||
| 253 | $coverId = $entity->getCoverFileId(); |
||
| 254 | if ($coverId > 0) { |
||
| 255 | $node = $rootFolder->getById($coverId)[0] ?? null; |
||
| 256 | if ($node instanceof File) { |
||
| 257 | $mime = $node->getMimeType(); |
||
| 258 | |||
| 259 | if (\strpos($mime, 'audio') === 0) { // embedded cover image |
||
| 260 | $cover = $this->extractor->parseEmbeddedCoverArt($node); // TODO: currently only album cover supported |
||
| 261 | |||
| 262 | if ($cover !== null) { |
||
| 263 | $response = ['mimetype' => $cover['image_mime'], 'content' => $cover['data']]; |
||
| 264 | } |
||
| 265 | } else { // separate image file |
||
| 266 | $response = ['mimetype' => $mime, 'content' => $node->getContent()]; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | if ($response === null) { |
||
| 271 | $class = \get_class($entity); |
||
| 272 | $this->logger->log("Requested cover not found for $class entity {$entity->getId()}, coverId=$coverId", 'error'); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | return $response; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Scale down images to reduce size and crop to square shape |
||
| 281 | * |
||
| 282 | * If one of the dimensions of the image is smaller than the maximum, then just |
||
| 283 | * crop to square shape but do not scale. |
||
| 284 | * @param array $image The image to be scaled down in format accepted by \OCA\Music\Http\FileResponse |
||
| 285 | * @param integer $maxSize The maximum size in pixels for the square shaped output |
||
| 286 | * @return array The processed image in format accepted by \OCA\Music\Http\FileResponse |
||
| 287 | */ |
||
| 288 | public function scaleDownAndCrop(array $image, int $maxSize) : array { |
||
| 340 | } |
||
| 341 | |||
| 342 | private function createMosaic(array $covers, ?int $size) : array { |
||
| 343 | $size = $size ?: $this->coverSize; |
||
| 344 | $pieceSize = $size/2; |
||
| 345 | $mosaicImg = \imagecreatetruecolor($size, $size); |
||
| 346 | if ($mosaicImg === false) { |
||
| 347 | $this->logger->log("Failed to create mosaic image of size $size x $size", 'warn'); |
||
| 348 | } |
||
| 349 | else { |
||
| 350 | $scaleAndCopyPiece = function($pieceData, $dstImage, $dstX, $dstY, $dstSize) { |
||
| 351 | $meta = \getimagesizefromstring($pieceData['content']); |
||
| 352 | $srcWidth = $meta[0]; |
||
| 353 | $srcHeight = $meta[1]; |
||
| 354 | |||
| 355 | $piece = imagecreatefromstring($pieceData['content']); |
||
| 356 | |||
| 357 | if ($piece === false) { |
||
| 358 | $this->logger->log('Failed to open cover image to create a mosaic', 'warn'); |
||
| 359 | } else { |
||
| 360 | \imagecopyresampled($dstImage, $piece, $dstX, $dstY, 0, 0, $dstSize, $dstSize, $srcWidth, $srcHeight); |
||
| 361 | \imagedestroy($piece); |
||
| 362 | } |
||
| 363 | }; |
||
| 364 | |||
| 365 | $coordinates = [ |
||
| 366 | ['x' => 0, 'y' => 0], // top-left |
||
| 367 | ['x' => $pieceSize, 'y' => $pieceSize], // bottom-right |
||
| 368 | ['x' => $pieceSize, 'y' => 0], // top-right |
||
| 369 | ['x' => 0, 'y' => $pieceSize], // bottom-left |
||
| 370 | ]; |
||
| 371 | |||
| 372 | $covers = \array_slice($covers, 0, 4); |
||
| 373 | foreach ($covers as $i => $cover) { |
||
| 374 | $scaleAndCopyPiece($cover, $mosaicImg, $coordinates[$i]['x'], $coordinates[$i]['y'], $pieceSize); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | $image = ['mimetype' => 'image/png']; |
||
| 379 | \ob_start(); |
||
| 380 | \ob_clean(); |
||
| 381 | imagepng($mosaicImg, null, 7, PNG_ALL_FILTERS); |
||
| 382 | $image['content'] = \ob_get_contents(); |
||
| 383 | \ob_end_clean(); |
||
| 384 | \imagedestroy($mosaicImg); |
||
| 385 | |||
| 386 | return $image; |
||
| 387 | } |
||
| 388 | |||
| 389 | private static function autoDetectMime(string $imageContent) : string { |
||
| 390 | return \getimagesizefromstring($imageContent)['mime']; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @throws \InvalidArgumentException if entity is not one of the expected types |
||
| 395 | */ |
||
| 396 | private static function getHashKey($entity) : string { |
||
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Create and store an access token which can be used to read cover images of a user. |
||
| 410 | * A user may have only one valid cover image access token at a time; the latest token |
||
| 411 | * always overwrites the previously obtained one. |
||
| 412 | * |
||
| 413 | * The reason this is needed is because the mediaSession in Firefox loads the cover images |
||
| 414 | * in a context where normal cookies and other standard request headers are not available. |
||
| 415 | * Hence, we need to provide the cover images as "public" resources, i.e. without requiring |
||
| 416 | * that the caller is logged in to the cloud. But still, we don't want to let just anyone |
||
| 417 | * load the user data. The solution is to use a temporary token which grants access just to |
||
| 418 | * the cover images. This token can be then sent as URL argument by the mediaSession. |
||
| 419 | */ |
||
| 420 | public function createAccessToken(string $userId) : string { |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @see CoverHelper::createAccessToken |
||
| 430 | * @throws \OutOfBoundsException if the token is not valid |
||
| 431 | */ |
||
| 432 | public function getUserForAccessToken(?string $token) : string { |
||
| 441 | } |
||
| 442 | } |
||
| 443 |