koel/app/Services/MediaInformationService.php

57 lines
1.3 KiB
PHP
Raw Normal View History

2018-08-18 13:19:40 +00:00
<?php
namespace App\Services;
use App\Models\Album;
use App\Models\Artist;
use App\Values\AlbumInformation;
use App\Values\ArtistInformation;
2022-07-16 22:42:29 +00:00
use Throwable;
2018-08-18 13:19:40 +00:00
class MediaInformationService
{
2022-06-10 10:47:46 +00:00
public function __construct(
private LastfmService $lastfmService,
2022-07-16 22:42:29 +00:00
private MediaMetadataService $mediaMetadataService
2022-06-10 10:47:46 +00:00
) {
2018-08-18 13:19:40 +00:00
}
public function getAlbumInformation(Album $album): ?AlbumInformation
2018-08-18 13:19:40 +00:00
{
if ($album->is_unknown) {
2018-08-24 15:27:19 +00:00
return null;
2018-08-18 13:19:40 +00:00
}
2022-07-18 11:00:37 +00:00
$info = $this->lastfmService->getAlbumInformation($album) ?: AlbumInformation::make();
2018-08-18 13:19:40 +00:00
2022-07-16 22:42:29 +00:00
if (!$album->has_cover) {
try {
2022-07-18 11:00:37 +00:00
$this->mediaMetadataService->tryDownloadAlbumCover($album);
$info->cover = $album->cover;
2022-07-16 22:42:29 +00:00
} catch (Throwable) {
}
}
2018-08-18 13:19:40 +00:00
return $info;
}
public function getArtistInformation(Artist $artist): ?ArtistInformation
2018-08-18 13:19:40 +00:00
{
if ($artist->is_unknown) {
2018-08-24 15:27:19 +00:00
return null;
2018-08-18 13:19:40 +00:00
}
2022-07-18 11:00:37 +00:00
$info = $this->lastfmService->getArtistInformation($artist) ?: ArtistInformation::make();
2022-07-16 22:42:29 +00:00
if (!$artist->has_image) {
try {
2022-07-18 11:00:37 +00:00
$this->mediaMetadataService->tryDownloadArtistImage($artist);
$info->image = $artist->image;
2022-07-16 22:42:29 +00:00
} catch (Throwable) {
}
}
2018-08-18 13:19:40 +00:00
return $info;
}
}