2018-08-19 09:05:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use Exception;
|
2019-01-01 11:53:20 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-08-19 09:05:33 +00:00
|
|
|
|
|
|
|
class MediaMetadataService
|
|
|
|
{
|
2020-04-27 20:32:24 +00:00
|
|
|
private $imageWriter;
|
2018-08-31 13:47:15 +00:00
|
|
|
private $logger;
|
|
|
|
|
2020-04-27 20:32:24 +00:00
|
|
|
public function __construct(ImageWriter $imageWriter, LoggerInterface $logger)
|
2018-08-31 13:47:15 +00:00
|
|
|
{
|
2020-04-27 20:32:24 +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.
|
|
|
|
*/
|
2020-06-13 11:00:51 +00:00
|
|
|
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), '. ');
|
2020-04-26 19:09:43 +00:00
|
|
|
$destination = $destination ?: $this->generateAlbumCoverPath($extension);
|
2020-04-27 20:32:24 +00:00
|
|
|
$this->imageWriter->writeFromBinaryData($destination, $binaryData);
|
2018-08-19 09:05:33 +00:00
|
|
|
|
2020-06-13 11:00:51 +00:00
|
|
|
if ($cleanUp) {
|
|
|
|
$this->deleteAlbumCoverFiles($album);
|
|
|
|
}
|
|
|
|
|
2018-08-19 09:05:33 +00:00
|
|
|
$album->update(['cover' => basename($destination)]);
|
2020-06-13 11:00:51 +00:00
|
|
|
$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,
|
2020-06-13 11:00:51 +00:00
|
|
|
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), '. ');
|
2020-04-26 19:09:43 +00:00
|
|
|
$destination = $destination ?: $this->generateArtistImagePath($extension);
|
2020-04-27 20:32:24 +00:00
|
|
|
$this->imageWriter->writeFromBinaryData($destination, $binaryData);
|
2018-08-19 09:05:33 +00:00
|
|
|
|
2020-06-13 11:00:51 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-30 10:51:04 +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)
|
|
|
|
*/
|
2020-04-26 19:09:43 +00:00
|
|
|
private function generateAlbumCoverPath(string $extension): string
|
2018-08-19 09:05:33 +00:00
|
|
|
{
|
2020-06-13 11:00:51 +00:00
|
|
|
return public_path(sprintf('public/img/covers/%s.%s', sha1(uniqid()), $extension));
|
2018-08-19 09:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-30 10:51:04 +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)
|
|
|
|
*/
|
2020-04-26 19:09:43 +00:00
|
|
|
private function generateArtistImagePath($extension): string
|
2018-08-19 09:05:33 +00:00
|
|
|
{
|
2020-06-13 11:00:51 +00:00
|
|
|
return public_path(sprintf('public/img/artists/%s.%s', sha1(uniqid()), $extension));
|
2018-08-19 09:05:33 +00:00
|
|
|
}
|
2020-06-12 13:55:45 +00:00
|
|
|
|
2020-06-13 11:00:51 +00:00
|
|
|
/**
|
|
|
|
* Get the URL of an album's thumbnail.
|
|
|
|
* Auto-generate the thumbnail when possible, if one doesn't exist yet.
|
|
|
|
*/
|
2020-06-12 13:55:45 +00:00
|
|
|
public function getAlbumThumbnailUrl(Album $album): ?string
|
|
|
|
{
|
|
|
|
if (!$album->has_cover) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-13 11:00:51 +00:00
|
|
|
if (!file_exists($album->thumbnail_path)) {
|
|
|
|
$this->createThumbnailForAlbum($album);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $album->thumbnail;
|
|
|
|
}
|
2020-06-12 13:55:45 +00:00
|
|
|
|
2020-06-13 11:00:51 +00:00
|
|
|
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;
|
2020-06-12 13:55:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 11:00:51 +00:00
|
|
|
@unlink($album->cover_path);
|
|
|
|
@unlink($album->thumbnail_path);
|
2020-06-12 13:55:45 +00:00
|
|
|
}
|
2018-08-19 09:05:33 +00:00
|
|
|
}
|