koel/app/Models/Song.php

370 lines
10 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;
2016-09-26 07:32:16 +00:00
use App\Traits\SupportsDeleteWhereIDsNotIn;
2016-07-11 07:26:39 +00:00
use AWS;
use Aws\AwsClient;
use Cache;
2015-12-13 04:42:28 +00:00
use Illuminate\Database\Eloquent\Model;
2015-12-20 12:17:35 +00:00
use Lastfm;
2016-09-26 06:49:30 +00:00
use YouTube;
2015-12-13 04:42:28 +00:00
/**
* @property string path
2016-03-16 07:16:48 +00:00
* @property string title
2016-06-04 14:17:24 +00:00
* @property Album album
* @property int contributing_artist_id
* @property Artist artist
2016-08-03 10:42:11 +00:00
* @property string s3_params
* @property float length
* @property string lyrics
2016-08-16 15:12:11 +00:00
* @property int track
* @property int album_id
* @property int id
2015-12-13 04:42:28 +00:00
*/
class Song extends Model
{
2016-09-26 07:32:16 +00:00
use SupportsDeleteWhereIDsNotIn;
2015-12-13 04:42:28 +00:00
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', 'updated_at', 'path', 'mtime'];
2015-12-13 04:42:28 +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',
];
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
2017-04-23 16:01:02 +00:00
public function artist()
2016-04-17 15:38:06 +00:00
{
2017-04-23 16:01:02 +00:00
return $this->belongsTo(ContributingArtist::class, 'contributing_artist_id');
2016-04-17 15:38:06 +00:00
}
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 User $user
2015-12-20 12:17:35 +00:00
* @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
*/
public function scrobble(User $user, $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.
2017-04-29 02:55:41 +00:00
if (!$user->connectedToLastfm()) {
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,
$user->lastfm_session_key
2015-12-20 12:17:35 +00:00
);
}
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).
*
2016-06-04 14:17:24 +00:00
* @return array
2016-03-05 09:01:12 +00:00
*/
public static function updateInfo($ids, $data)
{
2016-03-06 04:11:28 +00:00
/*
2017-04-23 16:01:02 +00:00
* A collection of the updated songs.
2016-03-05 09:01:12 +00:00
*
2017-04-23 16:01:02 +00:00
* @var \Illuminate\Support\Collection
2016-03-05 09:01:12 +00:00
*/
2017-04-23 16:01:02 +00:00
$updatedSongs = collect();
2016-03-05 09:01:12 +00:00
2016-05-27 03:32:52 +00:00
$ids = (array) $ids;
// If we're updating only one song, take into account the title, lyrics, and track number.
$single = count($ids) === 1;
foreach ($ids as $id) {
2016-03-05 09:01:12 +00:00
if (!$song = self::with('album', 'album.artist')->find($id)) {
continue;
}
2017-04-23 16:01:02 +00:00
$updatedSongs->push($song->updateSingle(
2016-05-27 03:32:52 +00:00
$single ? trim($data['title']) : $song->title,
2016-04-24 04:37:04 +00:00
trim($data['albumName'] ?: $song->album->name),
trim($data['artistName']) ?: $song->artist->name,
$single ? trim($data['lyrics']) : $song->lyrics,
2016-08-16 15:12:35 +00:00
$single ? (int) $data['track'] : $song->track,
(int) $data['compilationState']
2017-04-23 16:01:02 +00:00
));
2016-03-05 09:01:12 +00:00
}
// Our library may have been changed. Broadcast an event to tidy it up if need be.
2017-04-23 16:01:02 +00:00
if ($updatedSongs->count()) {
2016-03-05 09:01:12 +00:00
event(new LibraryChanged());
}
2017-04-23 16:01:02 +00:00
return [
'artists' => Artist::whereIn('id', $updatedSongs->pluck('contributing_artist_id'))->get(),
'albums' => Album::whereIn('id', $updatedSongs->pluck('album_id'))->get(),
'songs' => $updatedSongs,
];
2016-03-05 09:01:12 +00:00
}
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 ($artistName === Artist::VARIOUS_NAME) {
2017-04-23 16:01:02 +00:00
// If the artist name is "Various Artists", it's a compilation song no matter what.
2016-04-24 04:37:04 +00:00
$compilationState = 1;
2017-04-23 16:01:02 +00:00
// and since we can't determine the real contributing artist, it's "Unknown"
$artistName = Artist::UNKNOWN_NAME;
2016-04-24 04:37:04 +00:00
}
2017-04-23 16:01:02 +00:00
$artist = Artist::get($artistName);
switch ($compilationState) {
case 1: // ALL, or forcing compilation status to be Yes
$isCompilation = true;
break;
case 2: // Keep current compilation status
$isCompilation = $this->album->artist_id === Artist::VARIOUS_ID;
break;
default:
$isCompilation = false;
break;
2016-04-24 04:37:04 +00:00
}
2017-04-23 16:01:02 +00:00
$album = Album::get($artist, $albumName, $isCompilation);
2016-04-24 04:37:04 +00:00
2017-04-23 16:01:02 +00:00
$this->contributing_artist_id = $artist->id;
2016-04-24 04:37:04 +00:00
$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();
2017-04-23 16:01:02 +00:00
// Clean up unnecessary data from the object
unset($this->album);
unset($this->artist);
// and make sure the lyrics is shown
$this->makeVisible('lyrics');
2016-04-24 04:37:04 +00:00
2017-04-23 16:01:02 +00:00
return $this;
2016-04-24 04:37:04 +00:00
}
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%");
}
2016-06-04 13:42:12 +00:00
/**
2016-06-04 14:17:24 +00:00
* Get all songs favored by a user.
2016-06-04 13:42:12 +00:00
*
* @param User $user
* @param bool $toArray
*
2016-06-04 14:17:24 +00:00
* @return \Illuminate\Database\Eloquent\Collection|array
2016-06-04 13:42:12 +00:00
*/
public static function getFavorites(User $user, $toArray = false)
{
$songs = Interaction::where([
'user_id' => $user->id,
'liked' => true,
])
->with('song')
->get()
->pluck('song');
return $toArray ? $songs->toArray() : $songs;
}
2016-07-11 07:26:39 +00:00
/**
* Get the song's Object Storage url for streaming or downloading.
*
* @param AwsClient $s3
*
* @return string
*/
public function getObjectStoragePublicUrl(AwsClient $s3 = null)
{
// If we have a cached version, just return it.
if ($cached = Cache::get("OSUrl/{$this->id}")) {
return $cached;
}
// Otherwise, we query S3 for the presigned request.
if (!$s3) {
$s3 = AWS::createClient('s3');
}
$cmd = $s3->getCommand('GetObject', [
'Bucket' => $this->s3_params['bucket'],
'Key' => $this->s3_params['key'],
]);
// Here we specify that the request is valid for 1 hour.
// We'll also cache the public URL for future reuse.
$request = $s3->createPresignedRequest($cmd, '+1 hour');
$url = (string) $request->getUri();
Cache::put("OSUrl/{$this->id}", $url, 60);
return $url;
}
2016-09-26 06:49:30 +00:00
/**
* Get the YouTube videos related to this song.
*
* @param string $youTubePageToken The YouTube page token, for pagination purpose.
*
* @return @return object|false
*/
public function getRelatedYouTubeVideos($youTubePageToken = '')
{
return YouTube::searchVideosRelatedToSong($this, $youTubePageToken);
}
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-06-04 14:17:24 +00:00
// We don't use nl2br() here, because the function actually preserves line breaks -
2016-04-05 07:38:10 +00:00
// it just _appends_ a "<br />" after each of them. This would cause our client
2016-06-04 14:17:24 +00:00
// implementation of br2nl to fail with duplicated line breaks.
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
2016-06-13 09:04:42 +00:00
/**
* Determine if the song is an AWS S3 Object.
*
2016-06-13 09:11:41 +00:00
* @return bool
2016-06-13 09:04:42 +00:00
*/
public function isS3ObjectAttribute()
{
return strpos($this->path, 's3://') === 0;
}
/**
2016-06-13 09:11:41 +00:00
* Get the bucket and key name of an S3 object.
2016-06-13 09:04:42 +00:00
*
* @return bool|array
*/
public function getS3ParamsAttribute()
{
if (!preg_match('/^s3:\\/\\/(.*)/', $this->path, $matches)) {
return false;
}
list($bucket, $key) = explode('/', $matches[1], 2);
return compact('bucket', 'key');
}
2015-12-13 04:42:28 +00:00
}