koel/app/Services/MediaMetadataService.php

139 lines
4.3 KiB
PHP
Raw Normal View History

2018-08-19 09:05:33 +00:00
<?php
namespace App\Services;
use App\Models\Album;
use App\Models\Artist;
2024-02-24 15:37:01 +00:00
use App\Models\Playlist;
use Illuminate\Support\Facades\File;
2022-07-16 23:11:24 +00:00
use Illuminate\Support\Str;
2020-12-22 20:11:22 +00:00
2018-08-19 09:05:33 +00:00
class MediaMetadataService
{
2024-04-18 14:36:28 +00:00
public function __construct(
private readonly SpotifyService $spotifyService,
private readonly ImageWriter $imageWriter
) {
2018-08-31 13:47:15 +00:00
}
2022-07-18 11:00:37 +00:00
public function tryDownloadAlbumCover(Album $album): void
2018-08-19 09:05:33 +00:00
{
2022-07-18 11:00:37 +00:00
optional($this->spotifyService->tryGetAlbumCover($album), function (string $coverUrl) use ($album): void {
$this->writeAlbumCover($album, $coverUrl);
});
2018-08-19 09:05:33 +00:00
}
/**
2022-07-16 23:11:24 +00:00
* Write an album cover image file and update the Album with the new cover attribute.
2018-08-19 09:05:33 +00:00
*
2022-07-16 22:42:29 +00:00
* @param string $source Path, URL, or even binary data. See https://image.intervention.io/v2/api/make.
* @param string|null $destination The destination path. Automatically generated if empty.
2018-08-19 09:05:33 +00:00
*/
2024-03-19 22:48:12 +00:00
public function writeAlbumCover(Album $album, string $source, ?string $destination = '', bool $cleanUp = true): void
{
attempt(function () use ($album, $source, $destination, $cleanUp): void {
$destination = $destination ?: $this->generateAlbumCoverPath();
2022-07-16 22:42:29 +00:00
$this->imageWriter->write($destination, $source);
2018-08-19 09:05:33 +00:00
if ($cleanUp) {
$this->deleteAlbumCoverFiles($album);
}
2018-08-19 09:05:33 +00:00
$album->update(['cover' => basename($destination)]);
$this->createThumbnailForAlbum($album);
2022-08-08 16:00:59 +00:00
});
2018-08-19 09:05:33 +00:00
}
2022-07-18 11:00:37 +00:00
public function tryDownloadArtistImage(Artist $artist): void
2018-08-19 09:05:33 +00:00
{
2022-07-18 11:00:37 +00:00
optional($this->spotifyService->tryGetArtistImage($artist), function (string $imageUrl) use ($artist): void {
$this->writeArtistImage($artist, $imageUrl);
});
2018-08-19 09:05:33 +00:00
}
/**
2022-07-16 23:11:24 +00:00
* Write an artist image file update the Artist with the new image attribute.
2018-08-19 09:05:33 +00:00
*
2022-07-16 22:42:29 +00:00
* @param string $source Path, URL, or even binary data. See https://image.intervention.io/v2/api/make.
* @param string|null $destination The destination path. Automatically generated if empty.
2018-08-19 09:05:33 +00:00
*/
2018-08-24 15:27:19 +00:00
public function writeArtistImage(
Artist $artist,
2022-07-16 22:42:29 +00:00
string $source,
2022-07-16 23:11:24 +00:00
?string $destination = '',
bool $cleanUp = true
2018-08-24 15:27:19 +00:00
): void {
2024-03-19 22:48:12 +00:00
attempt(function () use ($artist, $source, $destination, $cleanUp): void {
$destination = $destination ?: $this->generateArtistImagePath();
2022-07-16 22:42:29 +00:00
$this->imageWriter->write($destination, $source);
2018-08-19 09:05:33 +00:00
if ($cleanUp && $artist->has_image) {
File::delete($artist->image_path);
}
2018-08-19 09:05:33 +00:00
$artist->update(['image' => basename($destination)]);
2022-08-08 16:00:59 +00:00
});
2018-08-19 09:05:33 +00:00
}
2024-03-19 22:48:12 +00:00
public function writePlaylistCover(Playlist $playlist, string $source): void
2024-02-24 15:37:01 +00:00
{
2024-03-19 22:48:12 +00:00
attempt(function () use ($playlist, $source): void {
$destination = $this->generatePlaylistCoverPath();
2024-02-24 15:37:01 +00:00
$this->imageWriter->write($destination, $source);
if ($playlist->cover_path) {
File::delete($playlist->cover_path);
}
$playlist->update(['cover' => basename($destination)]);
});
}
2024-03-19 22:48:12 +00:00
private function generateAlbumCoverPath(): string
2018-08-19 09:05:33 +00:00
{
2024-03-19 22:48:12 +00:00
return album_cover_path(sprintf('%s.webp', sha1(Str::uuid())));
2018-08-19 09:05:33 +00:00
}
2024-03-19 22:48:12 +00:00
private function generateArtistImagePath(): string
2018-08-19 09:05:33 +00:00
{
2024-03-19 22:48:12 +00:00
return artist_image_path(sprintf('%s.webp', sha1(Str::uuid())));
2018-08-19 09:05:33 +00:00
}
2024-03-19 22:48:12 +00:00
private function generatePlaylistCoverPath(): string
2024-02-24 15:37:01 +00:00
{
2024-03-19 22:48:12 +00:00
return playlist_cover_path(sprintf('%s.webp', sha1(Str::uuid())));
2024-02-24 15:37:01 +00:00
}
/**
* Get the URL of an album's thumbnail.
* Auto-generate the thumbnail when possible, if one doesn't exist yet.
*/
public function getAlbumThumbnailUrl(Album $album): ?string
{
if (!$album->has_cover) {
return null;
}
if (!File::exists($album->thumbnail_path)) {
$this->createThumbnailForAlbum($album);
}
return $album->thumbnail;
}
private function createThumbnailForAlbum(Album $album): void
{
2022-07-16 22:42:29 +00:00
$this->imageWriter->write($album->thumbnail_path, $album->cover_path, ['max_width' => 48, 'blur' => 10]);
}
private function deleteAlbumCoverFiles(Album $album): void
{
if (!$album->has_cover) {
return;
}
File::delete($album->cover_path);
File::delete($album->thumbnail_path);
}
2018-08-19 09:05:33 +00:00
}