koel/app/Services/MediaMetadataService.php

112 lines
3.5 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;
2018-08-31 13:47:15 +00:00
use Illuminate\Log\Logger;
2018-08-19 09:05:33 +00:00
class MediaMetadataService
{
2018-08-31 13:47:15 +00:00
private $logger;
public function __construct(Logger $logger)
{
$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));
}
/**
* Copy a cover file from an existing image on the system.
*
* @param string $source The original image's full path.
* @param string $destination The destination path. Automatically generated if empty.
*/
2018-08-24 15:27:19 +00:00
public function copyAlbumCover(Album $album, string $source, string $destination = ''): void
2018-08-19 09:05:33 +00:00
{
$extension = pathinfo($source, PATHINFO_EXTENSION);
$destination = $destination ?: $this->generateAlbumCoverPath($extension);
copy($source, $destination);
$album->update(['cover' => basename($destination)]);
}
/**
* 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.
*/
2018-08-24 15:27:19 +00:00
public function writeAlbumCover(Album $album, string $binaryData, string $extension, string $destination = ''): void
2018-08-19 09:05:33 +00:00
{
try {
$extension = trim(strtolower($extension), '. ');
$destination = $destination ?: $this->generateAlbumCoverPath($extension);
file_put_contents($destination, $binaryData);
$album->update(['cover' => 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
}
}
/**
* 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 = ''
): void {
2018-08-19 09:05:33 +00:00
try {
$extension = trim(strtolower($extension), '. ');
$destination = $destination ?: $this->generateArtistImagePath($extension);
file_put_contents($destination, $binaryData);
$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 a random path for an album cover image.
*
* @param string $extension The extension of the cover (without dot)
*/
2018-08-24 15:27:19 +00:00
private function generateAlbumCoverPath($extension): string
2018-08-19 09:05:33 +00:00
{
return sprintf('%s/public/img/covers/%s.%s', app()->publicPath(), uniqid('', true), $extension);
}
/**
* Generate a random path for an artist image.
*
* @param string $extension The extension of the cover (without dot)
*/
2018-08-24 15:27:19 +00:00
private function generateArtistImagePath($extension): string
2018-08-19 09:05:33 +00:00
{
return sprintf('%s/public/img/artists/%s.%s', app()->publicPath(), uniqid('', true), $extension);
}
}