koel/app/Services/MediaMetadataService.php

144 lines
4.2 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;
use Psr\Log\LoggerInterface;
2020-12-22 20:11:22 +00:00
use Throwable;
2018-08-19 09:05:33 +00:00
class MediaMetadataService
{
2021-06-05 10:47:56 +00:00
private ImageWriter $imageWriter;
private LoggerInterface $logger;
2018-08-31 13:47:15 +00:00
public function __construct(ImageWriter $imageWriter, LoggerInterface $logger)
2018-08-31 13:47:15 +00:00
{
$this->imageWriter = $imageWriter;
2018-08-31 13:47:15 +00:00
$this->logger = $logger;
}
2018-08-24 15:27:19 +00:00
public function downloadAlbumCover(Album $album, string $imageUrl): void
2018-08-19 09:05:33 +00:00
{
$extension = explode('.', $imageUrl);
$this->writeAlbumCover($album, file_get_contents($imageUrl), last($extension));
}
/**
* Write an album cover image file with binary data and update the Album with the new cover attribute.
*
* @param string $destination The destination path. Automatically generated if empty.
*/
public function writeAlbumCover(
Album $album,
string $binaryData,
string $extension,
string $destination = '',
bool $cleanUp = true
2020-09-06 21:20:42 +00:00
): void {
2018-08-19 09:05:33 +00:00
try {
$extension = trim(strtolower($extension), '. ');
$destination = $destination ?: $this->generateAlbumCoverPath($extension);
$this->imageWriter->writeFromBinaryData($destination, $binaryData);
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);
2020-12-22 20:11:22 +00:00
} catch (Throwable $e) {
2018-08-31 13:47:15 +00:00
$this->logger->error($e);
2018-08-19 09:05:33 +00:00
}
}
2018-08-24 15:27:19 +00:00
public function downloadArtistImage(Artist $artist, string $imageUrl): void
2018-08-19 09:05:33 +00:00
{
$extension = explode('.', $imageUrl);
$this->writeArtistImage($artist, file_get_contents($imageUrl), last($extension));
}
/**
* Write an artist image file with binary data and update the Artist with the new image attribute.
*
* @param string $destination The destination path. Automatically generated if empty.
*/
2018-08-24 15:27:19 +00:00
public function writeArtistImage(
Artist $artist,
string $binaryData,
string $extension,
string $destination = '',
bool $cleanUp = true
2018-08-24 15:27:19 +00:00
): void {
2018-08-19 09:05:33 +00:00
try {
$extension = trim(strtolower($extension), '. ');
$destination = $destination ?: $this->generateArtistImagePath($extension);
$this->imageWriter->writeFromBinaryData($destination, $binaryData);
2018-08-19 09:05:33 +00:00
if ($cleanUp && $artist->has_image) {
@unlink($artist->image_path);
}
2018-08-19 09:05:33 +00:00
$artist->update(['image' => basename($destination)]);
2020-12-22 20:11:22 +00:00
} catch (Throwable $e) {
2018-08-31 13:47:15 +00:00
$this->logger->error($e);
2018-08-19 09:05:33 +00:00
}
}
/**
* Generate the absolute path for an album cover image.
2018-08-19 09:05:33 +00:00
*
* @param string $extension The extension of the cover (without dot)
*/
private function generateAlbumCoverPath(string $extension): string
2018-08-19 09:05:33 +00:00
{
2020-06-13 12:19:24 +00:00
return album_cover_path(sprintf('%s.%s', sha1(uniqid()), $extension));
2018-08-19 09:05:33 +00:00
}
/**
* Generate the absolute path for an artist image.
2018-08-19 09:05:33 +00:00
*
* @param string $extension The extension of the cover (without dot)
*/
private function generateArtistImagePath($extension): string
2018-08-19 09:05:33 +00:00
{
2020-06-13 12:19:24 +00:00
return artist_image_path(sprintf('%s.%s', sha1(uniqid()), $extension));
2018-08-19 09:05:33 +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
{
$this->imageWriter->writeFromBinaryData(
$album->thumbnail_path,
file_get_contents($album->cover_path),
['max_width' => 48, 'blur' => 10]
);
}
private function deleteAlbumCoverFiles(Album $album): void
{
if (!$album->has_cover) {
return;
}
@unlink($album->cover_path);
@unlink($album->thumbnail_path);
}
2018-08-19 09:05:33 +00:00
}