2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
use App\Events\LibraryChanged;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-12-20 12:17:35 +00:00
|
|
|
use Lastfm;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property string path
|
2016-03-16 07:16:48 +00:00
|
|
|
* @property string title
|
|
|
|
* @property Album album
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Song extends Model
|
|
|
|
{
|
|
|
|
protected $guarded = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes to be hidden from JSON outputs.
|
|
|
|
* Here we specify to hide lyrics as well to save some bandwidth (actually, lots of it).
|
|
|
|
* Lyrics can then be queried on demand.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $hidden = ['lyrics', 'created_at', 'updated_at', 'path', 'mtime'];
|
|
|
|
|
2015-12-20 18:09:34 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
2015-12-21 02:18:00 +00:00
|
|
|
'length' => 'float',
|
2016-03-22 08:22:39 +00:00
|
|
|
'mtime' => 'int',
|
2016-03-28 13:18:09 +00:00
|
|
|
'track' => 'int',
|
2016-04-17 15:38:06 +00:00
|
|
|
'contributing_artist_id' => 'int',
|
2015-12-20 18:09:34 +00:00
|
|
|
];
|
|
|
|
|
2016-01-05 02:54:27 +00:00
|
|
|
/**
|
|
|
|
* Indicates if the IDs are auto-incrementing.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $incrementing = false;
|
|
|
|
|
2016-04-17 15:38:06 +00:00
|
|
|
public function contributingArtist()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(ContributingArtist::class);
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
public function album()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Album::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function playlists()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
2015-12-20 12:17:35 +00:00
|
|
|
/**
|
|
|
|
* Scrobble the song using Last.fm service.
|
|
|
|
*
|
|
|
|
* @param string $timestamp The UNIX timestamp in which the song started playing.
|
2016-02-02 07:47:00 +00:00
|
|
|
*
|
2015-12-20 12:17:35 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-12-20 12:30:28 +00:00
|
|
|
public function scrobble($timestamp)
|
2015-12-20 12:17:35 +00:00
|
|
|
{
|
|
|
|
// Don't scrobble the unknown guys. No one knows them.
|
2016-04-17 15:38:06 +00:00
|
|
|
if ($this->artist->isUnknown()) {
|
2015-12-20 12:17:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current user hasn't connected to Last.fm, don't do shit.
|
2015-12-21 13:49:00 +00:00
|
|
|
if (!$sessionKey = auth()->user()->getLastfmSessionKey()) {
|
2015-12-20 12:17:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Lastfm::scrobble(
|
2016-04-17 15:38:06 +00:00
|
|
|
$this->artist->name,
|
2015-12-20 12:30:28 +00:00
|
|
|
$this->title,
|
2015-12-20 12:17:35 +00:00
|
|
|
$timestamp,
|
|
|
|
$this->album->name === Album::UNKNOWN_NAME ? '' : $this->album->name,
|
|
|
|
$sessionKey
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
/**
|
|
|
|
* Get a Song record using its path.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
*
|
|
|
|
* @return Song|null
|
|
|
|
*/
|
|
|
|
public static function byPath($path)
|
|
|
|
{
|
2016-03-22 08:22:39 +00:00
|
|
|
return self::find(File::getHash($path));
|
2016-02-02 07:47:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
/**
|
|
|
|
* Update song info.
|
|
|
|
*
|
|
|
|
* @param array $ids
|
|
|
|
* @param array $data The data array, with these supported fields:
|
|
|
|
* - title
|
|
|
|
* - artistName
|
|
|
|
* - albumName
|
|
|
|
* - lyrics
|
|
|
|
* All of these are optional, in which case the info will not be changed
|
|
|
|
* (except for lyrics, which will be emptied).
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static function updateInfo($ids, $data)
|
|
|
|
{
|
2016-03-06 04:11:28 +00:00
|
|
|
/*
|
2016-03-05 09:01:12 +00:00
|
|
|
* An array of the updated songs.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
$updatedSongs = [];
|
|
|
|
|
|
|
|
foreach ((array) $ids as $id) {
|
|
|
|
if (!$song = self::with('album', 'album.artist')->find($id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-28 13:18:09 +00:00
|
|
|
// If we're updating only one song, take into account the title, lyrics, and track number.
|
2016-04-24 04:37:04 +00:00
|
|
|
$single = count($ids) === 1;
|
|
|
|
|
|
|
|
$updatedSongs[] = $song->updateSingle(
|
|
|
|
trim($data['title']) ?: $song->title,
|
|
|
|
trim($data['albumName'] ?: $song->album->name),
|
|
|
|
trim($data['artistName']) ?: $song->artist->name,
|
|
|
|
$single ? trim($data['lyrics']) : $song->lyrics,
|
|
|
|
$single ? intval($data['track']) : $song->track,
|
|
|
|
intval($data['compilationState'])
|
|
|
|
);
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Our library may have been changed. Broadcast an event to tidy it up if need be.
|
|
|
|
if ($updatedSongs) {
|
|
|
|
event(new LibraryChanged());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $updatedSongs;
|
|
|
|
}
|
|
|
|
|
2016-04-24 04:37:04 +00:00
|
|
|
/**
|
|
|
|
* Update a single song's info.
|
|
|
|
*
|
|
|
|
* @param string $title
|
|
|
|
* @param string $albumName
|
|
|
|
* @param string $artistName
|
|
|
|
* @param string $lyrics
|
|
|
|
* @param int $track
|
|
|
|
* @param int $compilationState
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function updateSingle($title, $albumName, $artistName, $lyrics, $track, $compilationState)
|
|
|
|
{
|
|
|
|
// If the artist name is "Various Artists", it's a compilation song no matter what.
|
|
|
|
if ($artistName === Artist::VARIOUS_NAME) {
|
|
|
|
$compilationState = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the complitation state is "no change," we determine it via the current
|
|
|
|
// "contributing_artist_id" field value.
|
|
|
|
if ($compilationState === 2) {
|
|
|
|
$compilationState = $this->contributing_artist_id ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$album = null;
|
|
|
|
|
|
|
|
if ($compilationState === 0) {
|
|
|
|
// Not a compilation song
|
|
|
|
$this->contributing_artist_id = null;
|
|
|
|
$albumArtist = Artist::get($artistName);
|
|
|
|
$album = Album::get($albumArtist, $albumName, false);
|
|
|
|
} else {
|
|
|
|
$contributingArtist = Artist::get($artistName);
|
|
|
|
$this->contributing_artist_id = $contributingArtist->id;
|
|
|
|
$album = Album::get(Artist::getVarious(), $albumName, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->album_id = $album->id;
|
2016-05-27 02:41:46 +00:00
|
|
|
$this->title = $title;
|
2016-04-24 04:37:04 +00:00
|
|
|
$this->lyrics = $lyrics;
|
|
|
|
$this->track = $track;
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
// Get the updated record, with album and all.
|
2016-05-02 02:55:59 +00:00
|
|
|
$updatedSong = self::with('album', 'album.artist', 'contributingArtist')->find($this->id);
|
2016-04-24 04:37:04 +00:00
|
|
|
// Make sure lyrics is included in the returned JSON.
|
|
|
|
$updatedSong->makeVisible('lyrics');
|
|
|
|
|
|
|
|
return $updatedSong;
|
|
|
|
}
|
|
|
|
|
2016-02-02 07:47:00 +00:00
|
|
|
/**
|
|
|
|
* Scope a query to only include songs in a given directory.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @param string $path Full path of the directory
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeInDirectory($query, $path)
|
|
|
|
{
|
|
|
|
// Make sure the path ends with a directory separator.
|
|
|
|
$path = rtrim(trim($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
return $query->where('path', 'LIKE', "$path%");
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Sometimes the tags extracted from getID3 are HTML entity encoded.
|
|
|
|
* This makes sure they are always sane.
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setTitleAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['title'] = html_entity_decode($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some songs don't have a title.
|
|
|
|
* Fall back to the file name (without extension) for such.
|
2016-02-02 07:47:00 +00:00
|
|
|
*
|
2015-12-13 04:42:28 +00:00
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitleAttribute($value)
|
|
|
|
{
|
|
|
|
return $value ?: pathinfo($this->path, PATHINFO_FILENAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare the lyrics for displaying.
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLyricsAttribute($value)
|
|
|
|
{
|
2016-04-05 07:38:10 +00:00
|
|
|
// We don't use nl2br() here, because the function actually preserves linebreaks -
|
|
|
|
// it just _appends_ a "<br />" after each of them. This would cause our client
|
|
|
|
// implementation of br2nl to fail with duplicated linebreaks.
|
2016-03-06 07:44:38 +00:00
|
|
|
return str_replace(["\r\n", "\r", "\n"], '<br />', $value);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
2016-04-17 15:38:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the correct artist of the song.
|
|
|
|
* If it's part of a compilation, that would be the contributing artist.
|
|
|
|
* Otherwise, it's the album artist.
|
|
|
|
*
|
|
|
|
* @return Artist
|
|
|
|
*/
|
|
|
|
public function getArtistAttribute()
|
|
|
|
{
|
|
|
|
return $this->contributing_artist_id
|
|
|
|
? $this->contributingArtist
|
|
|
|
: $this->album->artist;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|