1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use App\Facades\Lastfm; |
6
|
|
|
use App\Traits\SupportsDeleteWhereIDsNotIn; |
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property string cover The path to the album's cover |
12
|
|
|
* @property bool has_cover If the album has a cover image |
13
|
|
|
* @property int id |
14
|
|
|
* @property string name Name of the album |
15
|
|
|
* @property bool is_compilation If the album is a compilation from multiple artists |
16
|
|
|
* @property Artist artist The album's artist |
17
|
|
|
* @property int artist_id |
18
|
|
|
* @property Collection songs |
19
|
|
|
* @property bool is_unknown |
20
|
|
|
*/ |
21
|
|
|
class Album extends Model |
22
|
|
|
{ |
23
|
|
|
use SupportsDeleteWhereIDsNotIn; |
24
|
|
|
|
25
|
|
|
const UNKNOWN_ID = 1; |
26
|
|
|
const UNKNOWN_NAME = 'Unknown Album'; |
27
|
|
|
const UNKNOWN_COVER = 'unknown-album.png'; |
28
|
|
|
|
29
|
|
|
protected $guarded = ['id']; |
30
|
|
|
protected $hidden = ['updated_at']; |
31
|
|
|
protected $casts = ['artist_id' => 'integer']; |
32
|
|
|
protected $appends = ['is_compilation']; |
33
|
|
|
|
34
|
|
|
public function artist() |
35
|
|
|
{ |
36
|
|
|
return $this->belongsTo(Artist::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function songs() |
40
|
|
|
{ |
41
|
|
|
return $this->hasMany(Song::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getIsUnknownAttribute() |
45
|
|
|
{ |
46
|
|
|
return $this->id === self::UNKNOWN_ID; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get an album using some provided information. |
51
|
|
|
* |
52
|
|
|
* @param Artist $artist |
53
|
|
|
* @param string $name |
54
|
|
|
* @param bool $isCompilation |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
|
|
public static function get(Artist $artist, $name, $isCompilation = false) |
59
|
|
|
{ |
60
|
|
|
// If this is a compilation album, its artist must be "Various Artists" |
61
|
|
|
if ($isCompilation) { |
62
|
|
|
$artist = Artist::getVariousArtist(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return self::firstOrCreate([ |
66
|
|
|
'artist_id' => $artist->id, |
67
|
|
|
'name' => $name ?: self::UNKNOWN_NAME, |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get extra information about the album from Last.fm. |
73
|
|
|
* |
74
|
|
|
* @return array|false |
75
|
|
|
*/ |
76
|
|
|
public function getInfo() |
77
|
|
|
{ |
78
|
|
|
if ($this->is_unknown) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$info = Lastfm::getAlbumInfo($this->name, $this->artist->name); |
83
|
|
|
$image = array_get($info, 'image'); |
84
|
|
|
|
85
|
|
|
// If our current album has no cover, and Last.fm has one, why don't we steal it? |
86
|
|
|
// Great artists steal for their great albums! |
87
|
|
|
if (!$this->has_cover && is_string($image) && ini_get('allow_url_fopen')) { |
88
|
|
|
$extension = explode('.', $image); |
89
|
|
|
$this->writeCoverFile(file_get_contents($image), last($extension)); |
90
|
|
|
$info['cover'] = $this->cover; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $info; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Generate a cover from provided data. |
98
|
|
|
* |
99
|
|
|
* @param array $cover The cover data in array format, extracted by getID3. |
100
|
|
|
* For example: |
101
|
|
|
* [ |
102
|
|
|
* 'data' => '<binary data>', |
103
|
|
|
* 'image_mime' => 'image/png', |
104
|
|
|
* 'image_width' => 512, |
105
|
|
|
* 'image_height' => 512, |
106
|
|
|
* 'imagetype' => 'PNG', // not always present |
107
|
|
|
* 'picturetype' => 'Other', |
108
|
|
|
* 'description' => '', |
109
|
|
|
* 'datalength' => 7627, |
110
|
|
|
* ] |
111
|
|
|
*/ |
112
|
|
|
public function generateCover(array $cover) |
113
|
|
|
{ |
114
|
|
|
$extension = explode('/', $cover['image_mime']); |
115
|
|
|
$extension = empty($extension[1]) ? 'png' : $extension[1]; |
116
|
|
|
|
117
|
|
|
$this->writeCoverFile($cover['data'], $extension); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Write a cover image file with binary data and update the Album with the new cover file. |
122
|
|
|
* |
123
|
|
|
* @param string $binaryData |
124
|
|
|
* @param string $extension The file extension |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function writeCoverFile($binaryData, $extension) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$extension = trim(strtolower($extension), '. '); |
129
|
|
|
$destination = $this->generateRandomCoverPath($extension); |
130
|
|
|
file_put_contents($destination, $binaryData); |
131
|
|
|
|
132
|
|
|
$this->update(['cover' => basename($destination)]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Copy a cover file from an existing image on the system. |
137
|
|
|
* |
138
|
|
|
* @param string $source The original image's full path. |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function copyCoverFile($source) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$extension = pathinfo($source, PATHINFO_EXTENSION); |
143
|
|
|
$destination = $this->generateRandomCoverPath($extension); |
144
|
|
|
copy($source, $destination); |
145
|
|
|
|
146
|
|
|
$this->update(['cover' => basename($destination)]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Generate a random path for the cover image. |
151
|
|
|
* |
152
|
|
|
* @param string $extension The extension of the cover (without dot) |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
private function generateRandomCoverPath($extension) |
157
|
|
|
{ |
158
|
|
|
return app()->publicPath().'/public/img/covers/'.uniqid('', true).".$extension"; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setCoverAttribute($value) |
162
|
|
|
{ |
163
|
|
|
$this->attributes['cover'] = $value ?: self::UNKNOWN_COVER; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getCoverAttribute($value) |
167
|
|
|
{ |
168
|
|
|
return app()->staticUrl('public/img/covers/'.($value ?: self::UNKNOWN_COVER)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Determine if the current album has a cover. |
173
|
|
|
* |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function getHasCoverAttribute() |
177
|
|
|
{ |
178
|
|
|
return $this->cover !== $this->getCoverAttribute(null); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Sometimes the tags extracted from getID3 are HTML entity encoded. |
183
|
|
|
* This makes sure they are always sane. |
184
|
|
|
* |
185
|
|
|
* @param $value |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getNameAttribute($value) |
190
|
|
|
{ |
191
|
|
|
return html_entity_decode($value); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Determine if the album is a compilation. |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function getIsCompilationAttribute() |
200
|
|
|
{ |
201
|
|
|
return $this->artist_id === Artist::VARIOUS_ID; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.