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;
|
2017-08-05 16:56:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-08-05 16:56:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
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 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
|
2017-12-03 10:02:31 +00:00
|
|
|
* @property int disc
|
2016-08-16 15:12:11 +00:00
|
|
|
* @property int album_id
|
|
|
|
* @property int id
|
2017-06-03 23:21:50 +00:00
|
|
|
* @property int artist_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
|
|
|
|
*/
|
2016-08-07 12:33:46 +00:00
|
|
|
protected $hidden = ['lyrics', 'updated_at', 'path', 'mtime'];
|
2015-12-13 04:42:28 +00:00
|
|
|
|
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',
|
2017-12-03 10:02:31 +00:00
|
|
|
'disc' => '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;
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
/**
|
|
|
|
* A song belongs to an artist.
|
|
|
|
*
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
2017-04-23 16:01:02 +00:00
|
|
|
public function artist()
|
2016-04-17 15:38:06 +00:00
|
|
|
{
|
2017-04-29 03:49:14 +00:00
|
|
|
return $this->belongsTo(Artist::class);
|
2016-04-17 15:38:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
/**
|
|
|
|
* A song belongs to a album.
|
|
|
|
*
|
|
|
|
* @return BelongsTo
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
public function album()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Album::class);
|
|
|
|
}
|
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
/**
|
|
|
|
* A song can belong to many playlists.
|
|
|
|
*
|
|
|
|
* @return BelongsToMany
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
public function playlists()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
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-08-05 16:56:11 +00:00
|
|
|
* @var 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 [
|
2017-04-29 03:49:14 +00:00
|
|
|
'artists' => Artist::whereIn('id', $updatedSongs->pluck('artist_id'))->get(),
|
2017-04-23 16:01:02 +00:00
|
|
|
'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-29 03:49:14 +00:00
|
|
|
$this->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.
|
|
|
|
*
|
2017-08-05 16:56:11 +00:00
|
|
|
* @param Builder $query
|
|
|
|
* @param string $path Full path of the directory
|
2016-02-02 07:47:00 +00:00
|
|
|
*
|
2017-08-05 16:56:11 +00:00
|
|
|
* @return Builder
|
2016-02-02 07:47:00 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2017-08-05 16:56:11 +00:00
|
|
|
* @return Collection|array
|
2016-06-04 13:42:12 +00:00
|
|
|
*/
|
|
|
|
public static function getFavorites(User $user, $toArray = false)
|
|
|
|
{
|
2018-08-19 11:08:16 +00:00
|
|
|
/** @var Collection $songs */
|
2017-05-01 17:36:42 +00:00
|
|
|
$songs = Interaction::whereUserIdAndLike($user->id, true)
|
2016-06-04 13:42:12 +00:00
|
|
|
->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)
|
|
|
|
{
|
2017-08-05 22:27:26 +00:00
|
|
|
return Cache::remember("OSUrl/{$this->id}", 60, function () use ($s3) {
|
|
|
|
if (!$s3) {
|
|
|
|
$s3 = AWS::createClient('s3');
|
|
|
|
}
|
2016-07-11 07:26:39 +00:00
|
|
|
|
2017-08-05 22:27:26 +00:00
|
|
|
$cmd = $s3->getCommand('GetObject', [
|
|
|
|
'Bucket' => $this->s3_params['bucket'],
|
|
|
|
'Key' => $this->s3_params['key'],
|
|
|
|
]);
|
2016-07-11 07:26:39 +00:00
|
|
|
|
2017-08-05 22:27:26 +00:00
|
|
|
// 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();
|
2016-07-11 07:26:39 +00:00
|
|
|
|
2017-08-05 22:27:26 +00:00
|
|
|
return $url;
|
|
|
|
});
|
2016-07-11 07:26:39 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2017-08-05 16:56:11 +00:00
|
|
|
* @param string $value
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
*
|
2017-08-05 18:55:53 +00:00
|
|
|
* @param string $value
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
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');
|
|
|
|
}
|
2017-06-24 20:46:55 +00:00
|
|
|
|
2017-08-05 16:56:11 +00:00
|
|
|
/**
|
|
|
|
* Return the ID of the song when it's converted to string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-06-24 20:46:55 +00:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|