koel/app/Models/Song.php

246 lines
7 KiB
PHP
Raw Normal View History

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'];
/**
* @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',
];
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
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.
2015-12-21 13:49:00 +00:00
if ($this->album->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(
2015-12-20 12:30:28 +00:00
$this->album->artist->name,
$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
* The artist that our songs will be associated to.
* If they are not existing yet, we will create the object.
*
* @var Artist
*/
$targetArtist = null;
2016-03-06 04:11:28 +00:00
/*
2016-03-05 09:01:12 +00:00
* The album that our songs will be associated to.
* If it can't be found, we'll create it.
*
* @var Album
*/
$targetAlbum = null;
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-03-05 09:01:12 +00:00
if (count($ids) === 1) {
$song->title = trim($data['title']) ?: $song->title;
$song->lyrics = trim($data['lyrics']);
2016-03-28 13:18:09 +00:00
$song->track = (int) trim($data['track']);
2016-03-05 09:01:12 +00:00
}
// If "newArtist" is provided, we'll see if such an artist name is found in our database.
// If negative, we create a new record into $targetArtist.
if ($artistName = trim($data['artistName'])) {
$targetArtist = Artist::get($artistName);
} else {
$targetArtist = $song->album->artist;
}
// Here it gets a little tricky.
// If "newAlbum" is provided, we find the album OF THE ARTIST.
// If none is found, create it as $targetAlbum, which is also populated just once.
if ($albumName = trim($data['albumName'])) {
$targetAlbum = Album::get($targetArtist, $albumName);
$song->album_id = $targetAlbum->id;
} else {
// The albumName is empty.
// However, if the artist has changed, it's not the same album anymore.
// Instead, the song now belongs to another album WITH THE SAME NAME, but under the new artist.
//
// See? I told you, it's tricky.
// Or maybe it's not.
// Whatever.
if ($targetArtist->id !== $song->album->artist->id) {
$song->album_id = Album::get($targetArtist, $song->album->name)->id;
}
}
$song->save();
// Get the updated record, with album and all.
$updatedSong = self::with('album', 'album.artist')->find($id);
// Make sure lyrics is included in the returned JSON.
$updatedSong->makeVisible('lyrics');
$updatedSongs[] = $updatedSong;
}
// Our library may have been changed. Broadcast an event to tidy it up if need be.
if ($updatedSongs) {
event(new LibraryChanged());
}
return $updatedSongs;
}
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-03-06 07:44:38 +00:00
// We don't use nl2br() here, because the function actually preserve linebreaks -
// it just _appends_ a "<br />" after each of them. This would case our client
// implementation of br2nl fails with duplicated linebreaks.
return str_replace(["\r\n", "\r", "\n"], '<br />', $value);
2015-12-13 04:42:28 +00:00
}
}