koel/app/Models/Artist.php

188 lines
4.7 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
use App\Facades\Lastfm;
2015-12-17 17:56:48 +00:00
use App\Facades\Util;
2016-09-26 07:32:16 +00:00
use App\Traits\SupportsDeleteWhereIDsNotIn;
2016-04-02 13:16:09 +00:00
use Exception;
2017-08-05 18:55:02 +00:00
use Illuminate\Database\Eloquent\Collection;
2015-12-17 18:00:42 +00:00
use Illuminate\Database\Eloquent\Model;
2017-08-05 18:55:02 +00:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
2015-12-22 09:53:03 +00:00
use Log;
2015-12-13 04:42:28 +00:00
/**
* @property int id The model ID
* @property string name The artist name
2016-06-04 14:17:24 +00:00
* @property string image
2017-06-03 23:21:50 +00:00
* @property bool is_unknown
* @property bool is_various
2017-08-05 18:55:02 +00:00
* @property Collection songs
2015-12-13 04:42:28 +00:00
*/
class Artist extends Model
{
2016-09-26 07:32:16 +00:00
use SupportsDeleteWhereIDsNotIn;
2015-12-13 04:42:28 +00:00
const UNKNOWN_ID = 1;
const UNKNOWN_NAME = 'Unknown Artist';
2016-04-17 15:38:06 +00:00
const VARIOUS_ID = 2;
const VARIOUS_NAME = 'Various Artists';
2015-12-13 04:42:28 +00:00
protected $guarded = ['id'];
protected $hidden = ['created_at', 'updated_at'];
2017-08-05 18:55:02 +00:00
/**
* An artist can have many albums.
*
* @return HasMany
*/
2015-12-13 04:42:28 +00:00
public function albums()
{
return $this->hasMany(Album::class);
}
2017-08-05 18:55:02 +00:00
/**
* An artist can have many songs.
* Unless he is Rick Astley.
*
* @return HasManyThrough
*/
2016-06-04 11:57:27 +00:00
public function songs()
{
return $this->hasManyThrough(Song::class, Album::class);
}
2017-08-05 18:55:02 +00:00
/**
* Indicate if the artist is unknown.
*
* @return bool
*/
2017-06-03 23:21:50 +00:00
public function getIsUnknownAttribute()
2015-12-21 13:49:00 +00:00
{
return $this->id === self::UNKNOWN_ID;
}
2017-08-05 18:55:02 +00:00
/**
* Indicate if the artist is the special "Various Artists"
*
* @return bool
*/
public function getIsVariousAttribute()
2016-04-17 15:38:06 +00:00
{
return $this->id === self::VARIOUS_ID;
}
/**
* Get the "Various Artists" object.
*
* @return Artist
*/
2017-06-03 23:21:50 +00:00
public static function getVariousArtist()
2016-04-17 15:38:06 +00:00
{
return self::find(self::VARIOUS_ID);
}
2015-12-16 05:03:48 +00:00
/**
* Sometimes the tags extracted from getID3 are HTML entity encoded.
* This makes sure they are always sane.
*
* @param $value
2015-12-17 17:56:48 +00:00
*
* @return string
2015-12-16 05:03:48 +00:00
*/
2015-12-14 13:22:39 +00:00
public function getNameAttribute($value)
2015-12-13 04:42:28 +00:00
{
2015-12-16 05:03:48 +00:00
return html_entity_decode($value ?: self::UNKNOWN_NAME);
2015-12-13 04:42:28 +00:00
}
/**
* Get an Artist object from their name.
* If such is not found, a new artist will be created.
*
* @param string $name
*
* @return Artist
*/
public static function get($name)
{
2015-12-17 17:56:48 +00:00
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
if ($encoding = Util::detectUTFEncoding($name)) {
2015-12-18 02:11:32 +00:00
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
2015-12-17 17:56:48 +00:00
}
2015-12-13 04:42:28 +00:00
$name = trim($name) ?: self::UNKNOWN_NAME;
return self::firstOrCreate(compact('name'), compact('name'));
}
/**
* Get extra information about the artist from Last.fm.
2016-02-10 16:59:29 +00:00
*
* @return array|false
*/
public function getInfo()
{
2017-06-03 23:21:50 +00:00
if ($this->is_unknown) {
return false;
}
$info = Lastfm::getArtistInfo($this->name);
2017-06-03 23:21:50 +00:00
$image = array_get($info, 'image');
2015-12-22 09:53:03 +00:00
// If our current artist has no image, and Last.fm has one, copy the image for our local use.
2017-06-03 23:21:50 +00:00
if (!$this->image && is_string($image) && ini_get('allow_url_fopen')) {
2015-12-22 09:53:03 +00:00
try {
$extension = explode('.', $image);
2017-06-10 13:25:30 +00:00
$this->writeImageFile(file_get_contents($image), last($extension));
$info['image'] = $this->image;
2016-04-02 13:16:09 +00:00
} catch (Exception $e) {
2015-12-22 09:53:03 +00:00
Log::error($e);
}
}
return $info;
}
2015-12-22 09:53:03 +00:00
2017-06-10 13:25:30 +00:00
/**
* Write an artist image file with binary data and update the Artist with the new cover file.
*
* @param string $binaryData
* @param string $extension The file extension
* @param string $destination The destination path. Automatically generated if empty.
*/
public function writeImageFile($binaryData, $extension, $destination = '')
{
$extension = trim(strtolower($extension), '. ');
$destination = $destination ?: $this->generateRandomImagePath($extension);
file_put_contents($destination, $binaryData);
$this->update(['image' => basename($destination)]);
}
/**
* Generate a random path for the artist's image.
*
* @param string $extension The extension of the cover (without dot)
*
* @return string
*/
private function generateRandomImagePath($extension)
{
return app()->publicPath().'/public/img/artists/'.uniqid('', true).".$extension";
}
2015-12-22 09:53:03 +00:00
/**
* Turn the image name into its absolute URL.
2016-02-10 16:59:29 +00:00
*
2015-12-22 09:54:19 +00:00
* @param mixed $value
2016-02-10 16:59:29 +00:00
*
2015-12-22 09:53:03 +00:00
* @return string|null
*/
public function getImageAttribute($value)
{
return $value ? app()->staticUrl("public/img/artists/$value") : null;
2015-12-22 09:53:03 +00:00
}
2015-12-13 04:42:28 +00:00
}