koel/app/Services/MediaMetadataService.php

151 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;
use Exception;
use Psr\Log\LoggerInterface;
2018-08-19 09:05:33 +00:00
class MediaMetadataService
{
private $imageWriter;
2018-08-31 13:47:15 +00:00
private $logger;
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-19 09:05:33 +00:00
/**
* Download a copy of the album cover.
*/
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
): 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);
2018-08-19 09:05:33 +00:00
} catch (Exception $e) {
2018-08-31 13:47:15 +00:00
$this->logger->error($e);
2018-08-19 09:05:33 +00:00
}
}
/**
* Download a copy of the artist image.
*/
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)]);
} catch (Exception $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
{
return public_path(sprintf('public/img/covers/%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
{
return public_path(sprintf('public/img/artists/%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
}