2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2015-12-19 16:36:44 +00:00
|
|
|
use App\Facades\Lastfm;
|
2016-08-03 10:42:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
/**
|
2016-04-17 15:38:06 +00:00
|
|
|
* @property string cover The path to the album's cover
|
|
|
|
* @property bool has_cover If the album has a cover image
|
2015-12-13 04:42:28 +00:00
|
|
|
* @property int id
|
2016-04-17 15:38:06 +00:00
|
|
|
* @property string name Name of the album
|
|
|
|
* @property bool is_compilation If the album is a compilation from multiple artists
|
|
|
|
* @property Artist artist The album's artist
|
2016-06-04 14:17:24 +00:00
|
|
|
* @property int artist_id
|
2016-08-03 10:42:11 +00:00
|
|
|
* @property Collection songs
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Album extends Model
|
|
|
|
{
|
|
|
|
const UNKNOWN_ID = 1;
|
|
|
|
const UNKNOWN_NAME = 'Unknown Album';
|
|
|
|
const UNKNOWN_COVER = 'unknown-album.png';
|
|
|
|
|
|
|
|
protected $guarded = ['id'];
|
2016-08-07 12:33:46 +00:00
|
|
|
protected $hidden = ['updated_at'];
|
2016-05-19 15:31:02 +00:00
|
|
|
protected $casts = ['artist_id' => 'integer'];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
public function artist()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Artist::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function songs()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Song::class);
|
|
|
|
}
|
|
|
|
|
2016-02-10 17:01:44 +00:00
|
|
|
public function isUnknown()
|
|
|
|
{
|
|
|
|
return $this->id === self::UNKNOWN_ID;
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Get an album using some provided information.
|
|
|
|
*
|
|
|
|
* @param Artist $artist
|
2016-04-17 15:38:06 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param bool $isCompilation
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2016-04-17 15:38:06 +00:00
|
|
|
public static function get(Artist $artist, $name, $isCompilation = false)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2016-04-17 15:38:06 +00:00
|
|
|
// If this is a compilation album, its artist must be "Various Artists"
|
|
|
|
if ($isCompilation) {
|
|
|
|
$artist = Artist::getVarious();
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-04-17 15:38:06 +00:00
|
|
|
return self::firstOrCreate([
|
2015-12-13 04:42:28 +00:00
|
|
|
'artist_id' => $artist->id,
|
2016-04-17 15:38:06 +00:00
|
|
|
'name' => $name ?: self::UNKNOWN_NAME,
|
2015-12-13 04:42:28 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:36:44 +00:00
|
|
|
/**
|
|
|
|
* Get extra information about the album from Last.fm.
|
2016-02-10 17:01:44 +00:00
|
|
|
*
|
2015-12-19 16:36:44 +00:00
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function getInfo()
|
|
|
|
{
|
2016-02-10 17:01:44 +00:00
|
|
|
if ($this->isUnknown()) {
|
2015-12-19 16:36:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$info = Lastfm::getAlbumInfo($this->name, $this->artist->name);
|
|
|
|
|
|
|
|
// If our current album has no cover, and Last.fm has one, why don't we steal it?
|
|
|
|
// Great artists steal for their great albums!
|
|
|
|
if (!$this->has_cover &&
|
|
|
|
is_string($image = array_get($info, 'image')) &&
|
|
|
|
ini_get('allow_url_fopen')
|
|
|
|
) {
|
|
|
|
$extension = explode('.', $image);
|
|
|
|
$this->writeCoverFile(file_get_contents($image), last($extension));
|
2015-12-30 06:03:47 +00:00
|
|
|
$info['cover'] = $this->cover;
|
2015-12-19 16:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Generate a cover from provided data.
|
|
|
|
*
|
|
|
|
* @param array $cover The cover data in array format, extracted by getID3.
|
|
|
|
* For example:
|
|
|
|
* [
|
|
|
|
* 'data' => '<binary data>',
|
|
|
|
* 'image_mime' => 'image/png',
|
|
|
|
* 'image_width' => 512,
|
|
|
|
* 'image_height' => 512,
|
|
|
|
* 'imagetype' => 'PNG', // not always present
|
|
|
|
* 'picturetype' => 'Other',
|
|
|
|
* 'description' => '',
|
|
|
|
* 'datalength' => 7627,
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
public function generateCover(array $cover)
|
|
|
|
{
|
|
|
|
$extension = explode('/', $cover['image_mime']);
|
2015-12-19 16:36:44 +00:00
|
|
|
$extension = empty($extension[1]) ? 'png' : $extension[1];
|
|
|
|
|
|
|
|
$this->writeCoverFile($cover['data'], $extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a cover image file with binary data and update the Album with the new cover file.
|
|
|
|
*
|
|
|
|
* @param string $binaryData
|
2015-12-19 17:08:03 +00:00
|
|
|
* @param string $extension The file extension
|
2015-12-19 16:36:44 +00:00
|
|
|
*/
|
2016-06-13 09:04:42 +00:00
|
|
|
public function writeCoverFile($binaryData, $extension)
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
|
|
|
$extension = trim(strtolower($extension), '. ');
|
2016-08-07 10:30:55 +00:00
|
|
|
$destPath = $this->generateRandomCoverPath($extension);
|
|
|
|
file_put_contents($destPath, $binaryData);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-08-07 10:30:55 +00:00
|
|
|
$this->update(['cover' => basename($destPath)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a cover file from an existing image on the system.
|
|
|
|
*
|
|
|
|
* @param string $srcPath The original image's full path.
|
|
|
|
*/
|
|
|
|
public function copyCoverFile($srcPath)
|
|
|
|
{
|
|
|
|
$extension = pathinfo($srcPath, PATHINFO_EXTENSION);
|
|
|
|
$destPath = $this->generateRandomCoverPath($extension);
|
|
|
|
copy($srcPath, $destPath);
|
|
|
|
|
|
|
|
$this->update(['cover' => basename($destPath)]);
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-08-07 10:30:55 +00:00
|
|
|
/**
|
|
|
|
* Generate a random path for the cover image.
|
|
|
|
*
|
|
|
|
* @param string $extension The extension of the cover (without dot)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function generateRandomCoverPath($extension)
|
|
|
|
{
|
|
|
|
return app()->publicPath().'/public/img/covers/'.uniqid('', true).".$extension";
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setCoverAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['cover'] = $value ?: self::UNKNOWN_COVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCoverAttribute($value)
|
|
|
|
{
|
2016-01-30 04:12:09 +00:00
|
|
|
return app()->staticUrl('public/img/covers/'.($value ?: self::UNKNOWN_COVER));
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the current album has a cover.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getHasCoverAttribute()
|
|
|
|
{
|
|
|
|
return $this->cover !== $this->getCoverAttribute(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sometimes the tags extracted from getID3 are HTML entity encoded.
|
|
|
|
* This makes sure they are always sane.
|
|
|
|
*
|
|
|
|
* @param $value
|
2015-12-19 16:36:44 +00:00
|
|
|
*
|
|
|
|
* @return string
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2015-12-16 05:03:48 +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);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2016-05-19 15:31:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the album is a compilation.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIsCompilationAttribute()
|
|
|
|
{
|
|
|
|
return $this->artist_id === Artist::VARIOUS_ID;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|