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;
|
2017-08-05 16:56:11 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2020-09-06 18:21:39 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
2020-11-14 16:57:25 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
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;
|
2018-11-03 23:25:08 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2018-08-29 06:30:39 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-22 20:11:22 +00:00
|
|
|
* @property string $path
|
|
|
|
* @property string $title
|
|
|
|
* @property Album $album
|
|
|
|
* @property Artist $artist
|
|
|
|
* @property array<string> $s3_params
|
|
|
|
* @property float $length
|
|
|
|
* @property string $lyrics
|
|
|
|
* @property int $track
|
|
|
|
* @property int $disc
|
|
|
|
* @property int $album_id
|
|
|
|
* @property string $id
|
|
|
|
* @property int $artist_id
|
|
|
|
* @property int $mtime
|
2019-08-05 10:57:36 +00:00
|
|
|
*
|
2019-08-05 10:56:48 +00:00
|
|
|
* @method static self updateOrCreate(array $where, array $params)
|
|
|
|
* @method static Builder select(string $string)
|
|
|
|
* @method static Builder inDirectory(string $path)
|
2019-10-23 10:45:57 +00:00
|
|
|
* @method static self first()
|
2020-09-06 18:21:39 +00:00
|
|
|
* @method static EloquentCollection orderBy(...$args)
|
|
|
|
* @method static int count()
|
|
|
|
* @method static self|null find($id)
|
2020-09-07 20:43:23 +00:00
|
|
|
* @method static Builder take(int $count)
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
class Song extends Model
|
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
use HasFactory;
|
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.
|
|
|
|
*/
|
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
|
|
|
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
|
|
|
];
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected $keyType = 'string';
|
2016-01-05 02:54:27 +00:00
|
|
|
public $incrementing = false;
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function artist(): BelongsTo
|
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
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function album(): BelongsTo
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->belongsTo(Album::class);
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function playlists(): BelongsToMany
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $this->belongsToMany(Playlist::class);
|
|
|
|
}
|
|
|
|
|
2018-11-03 23:25:08 +00:00
|
|
|
public function interactions(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Interaction::class);
|
|
|
|
}
|
|
|
|
|
2016-03-05 09:01:12 +00:00
|
|
|
/**
|
|
|
|
* Update song info.
|
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param array<string> $ids
|
|
|
|
* @param array<string> $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 Collection|array<Song>
|
2016-03-05 09:01:12 +00:00
|
|
|
*/
|
2018-08-29 06:30:39 +00:00
|
|
|
public static function updateInfo(array $ids, array $data): Collection
|
2016-03-05 09:01:12 +00:00
|
|
|
{
|
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) {
|
2019-06-30 14:22:53 +00:00
|
|
|
/** @var Song $song */
|
|
|
|
$song = self::with('album', 'album.artist')->find($id);
|
|
|
|
|
|
|
|
if (!$song) {
|
2016-03-05 09:01:12 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2018-08-29 06:30:39 +00:00
|
|
|
return $updatedSongs;
|
2016-03-05 09:01:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:27:19 +00:00
|
|
|
public function updateSingle(
|
|
|
|
string $title,
|
|
|
|
string $albumName,
|
|
|
|
string $artistName,
|
|
|
|
string $lyrics,
|
|
|
|
int $track,
|
|
|
|
int $compilationState
|
|
|
|
): self {
|
2016-04-24 04:37:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
$artist = Artist::getOrCreate($artistName);
|
2017-04-23 16:01:02 +00:00
|
|
|
|
|
|
|
switch ($compilationState) {
|
|
|
|
case 1: // ALL, or forcing compilation status to be Yes
|
|
|
|
$isCompilation = true;
|
|
|
|
break;
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2017-04-23 16:01:02 +00:00
|
|
|
case 2: // Keep current compilation status
|
|
|
|
$isCompilation = $this->album->artist_id === Artist::VARIOUS_ID;
|
|
|
|
break;
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2017-04-23 16:01:02 +00:00
|
|
|
default:
|
|
|
|
$isCompilation = false;
|
|
|
|
break;
|
2016-04-24 04:37:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 16:57:25 +00:00
|
|
|
$album = Album::getOrCreate($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.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function scopeInDirectory(Builder $query, string $path): Builder
|
2016-02-02 07:47:00 +00:00
|
|
|
{
|
|
|
|
// Make sure the path ends with a directory separator.
|
2020-12-22 20:11:22 +00:00
|
|
|
$path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
2016-02-02 07:47:00 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function setTitleAttribute(string $value): void
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
$this->attributes['title'] = html_entity_decode($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some songs don't have a title.
|
|
|
|
* Fall back to the file name (without extension) for such.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function getTitleAttribute(?string $value): string
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
|
|
|
return $value ?: pathinfo($this->path, PATHINFO_FILENAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare the lyrics for displaying.
|
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function getLyricsAttribute(string $value): string
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
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
|
|
|
*
|
2020-12-22 20:11:22 +00:00
|
|
|
* @return array<string>|null
|
2016-06-13 09:04:42 +00:00
|
|
|
*/
|
2018-08-24 15:27:19 +00:00
|
|
|
public function getS3ParamsAttribute(): ?array
|
2016-06-13 09:04:42 +00:00
|
|
|
{
|
|
|
|
if (!preg_match('/^s3:\\/\\/(.*)/', $this->path, $matches)) {
|
2018-08-24 15:27:19 +00:00
|
|
|
return null;
|
2016-06-13 09:04:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
[$bucket, $key] = explode('/', $matches[1], 2);
|
2016-06-13 09:04:42 +00:00
|
|
|
|
|
|
|
return compact('bucket', 'key');
|
|
|
|
}
|
2017-06-24 20:46:55 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
public function __toString(): string
|
2017-06-24 20:46:55 +00:00
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|