koel/app/Models/Artist.php

151 lines
3.7 KiB
PHP
Raw Normal View History

2015-12-13 12:42:28 +08:00
<?php
namespace App\Models;
use App\Facades\Lastfm;
2015-12-18 01:56:48 +08:00
use App\Facades\Util;
2016-09-26 15:32:16 +08:00
use App\Traits\SupportsDeleteWhereIDsNotIn;
2016-04-02 21:16:09 +08:00
use Exception;
2015-12-17 13:00:42 -05:00
use Illuminate\Database\Eloquent\Model;
2015-12-22 17:53:03 +08:00
use Log;
2015-12-13 12:42:28 +08:00
/**
* @property int id The model ID
* @property string name The artist name
2016-06-04 22:17:24 +08:00
* @property string image
2017-06-04 00:21:50 +01:00
* @property bool is_unknown
* @property bool is_various
2017-06-04 02:12:08 +01:00
* @property \Illuminate\Database\Eloquent\Collection songs
2015-12-13 12:42:28 +08:00
*/
class Artist extends Model
{
2016-09-26 15:32:16 +08:00
use SupportsDeleteWhereIDsNotIn;
2015-12-13 12:42:28 +08:00
const UNKNOWN_ID = 1;
const UNKNOWN_NAME = 'Unknown Artist';
2016-04-17 23:38:06 +08:00
const VARIOUS_ID = 2;
const VARIOUS_NAME = 'Various Artists';
2015-12-13 12:42:28 +08:00
protected $guarded = ['id'];
protected $hidden = ['created_at', 'updated_at'];
public function albums()
{
return $this->hasMany(Album::class);
}
2016-06-04 19:57:27 +08:00
public function songs()
{
return $this->hasManyThrough(Song::class, Album::class);
}
2017-06-04 00:21:50 +01:00
public function getIsUnknownAttribute()
2015-12-21 21:49:00 +08:00
{
return $this->id === self::UNKNOWN_ID;
}
2017-06-04 00:21:50 +01:00
public function getVariousAttribute()
2016-04-17 23:38:06 +08:00
{
return $this->id === self::VARIOUS_ID;
}
/**
* Get the "Various Artists" object.
*
* @return Artist
*/
2017-06-04 00:21:50 +01:00
public static function getVariousArtist()
2016-04-17 23:38:06 +08:00
{
return self::find(self::VARIOUS_ID);
}
2015-12-16 13:03:48 +08:00
/**
* Sometimes the tags extracted from getID3 are HTML entity encoded.
* This makes sure they are always sane.
*
* @param $value
2015-12-18 01:56:48 +08:00
*
* @return string
2015-12-16 13:03:48 +08:00
*/
2015-12-14 08:22:39 -05:00
public function getNameAttribute($value)
2015-12-13 12:42:28 +08:00
{
2015-12-16 13:03:48 +08:00
return html_entity_decode($value ?: self::UNKNOWN_NAME);
2015-12-13 12:42:28 +08: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-18 01:56:48 +08: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 10:11:32 +08:00
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
2015-12-18 01:56:48 +08:00
}
2015-12-13 12:42:28 +08: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 23:59:29 +07:00
*
* @return array|false
*/
public function getInfo()
{
2017-06-04 00:21:50 +01:00
if ($this->is_unknown) {
return false;
}
$info = Lastfm::getArtistInfo($this->name);
2017-06-04 00:21:50 +01:00
$image = array_get($info, 'image');
2015-12-22 17:53:03 +08:00
// If our current artist has no image, and Last.fm has one, copy the image for our local use.
2017-06-04 00:21:50 +01:00
if (!$this->image && is_string($image) && ini_get('allow_url_fopen')) {
2015-12-22 17:53:03 +08:00
try {
$extension = explode('.', $image);
$fileName = uniqid().'.'.trim(strtolower(last($extension)), '. ');
$coverPath = app()->publicPath().'/public/img/artists/'.$fileName;
file_put_contents($coverPath, file_get_contents($image));
$this->update(['image' => $fileName]);
$info['image'] = $this->image;
2016-04-02 21:16:09 +08:00
} catch (Exception $e) {
2015-12-22 17:53:03 +08:00
Log::error($e);
}
}
return $info;
}
2015-12-22 17:53:03 +08:00
2016-06-04 19:57:27 +08:00
/**
* Get songs *contributed* (in compilation albums) by the artist.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getContributedSongs()
{
return Song::whereContributingArtistId($this->id)->get();
}
2015-12-22 17:53:03 +08:00
/**
* Turn the image name into its absolute URL.
2016-02-10 23:59:29 +07:00
*
2015-12-22 04:54:19 -05:00
* @param mixed $value
2016-02-10 23:59:29 +07:00
*
2015-12-22 17:53:03 +08:00
* @return string|null
*/
public function getImageAttribute($value)
{
return $value ? app()->staticUrl("public/img/artists/$value") : null;
2015-12-22 17:53:03 +08:00
}
2015-12-13 12:42:28 +08:00
}