koel/app/Models/Song.php

357 lines
9.6 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-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;
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 contributingArtist
* @property Artist artist
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',
2016-04-17 15:38:06 +00:00
'contributing_artist_id' => 'int',
];
/**
* 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.
if (!$sessionKey = auth()->user()->lastfm_session_key) {
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).
*
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
/*
2016-03-05 09:01:12 +00:00
* An array of the updated songs.
*
* @var array
*/
$updatedSongs = [];
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;
}
2016-04-24 04:37:04 +00:00
$updatedSongs[] = $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,
$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;
}
2016-06-04 14:17:24 +00:00
// If the compilation state is "no change," we determine it via the current
2016-04-24 04:37:04 +00:00
// "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%");
}
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;
}
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
/**
* 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;
}
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
}