koel/app/Models/Song.php

265 lines
7.4 KiB
PHP
Raw Normal View History

2015-12-13 12:42:28 +08:00
<?php
namespace App\Models;
2016-03-05 17:01:12 +08:00
use App\Events\LibraryChanged;
2016-09-26 15:32:16 +08:00
use App\Traits\SupportsDeleteWhereIDsNotIn;
2017-08-05 17:56:11 +01:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2015-12-13 12:42:28 +08:00
use Illuminate\Database\Eloquent\Model;
2017-08-05 17:56:11 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
2018-08-29 13:30:39 +07:00
use Illuminate\Support\Collection;
2020-12-23 11:53:00 +01:00
use Laravel\Scout\Searchable;
2015-12-13 12:42:28 +08:00
/**
2020-12-22 21:11:22 +01: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
2020-12-23 00:01:49 +01:00
* @property int $contributing_artist_id
2019-08-05 17:57:36 +07:00
*
2019-08-05 17:56:48 +07:00
* @method static self updateOrCreate(array $where, array $params)
* @method static Builder select(string $string)
* @method static Builder inDirectory(string $path)
* @method static self first()
2021-06-05 12:47:56 +02:00
* @method static Builder orderBy(...$args)
2020-09-06 20:21:39 +02:00
* @method static int count()
* @method static self|Collection|null find($id)
* @method static Builder take(int $count)
2015-12-13 12:42:28 +08:00
*/
class Song extends Model
{
use HasFactory;
2020-12-23 11:53:00 +01:00
use Searchable;
2016-09-26 15:32:16 +08:00
use SupportsDeleteWhereIDsNotIn;
2021-06-05 12:47:56 +02:00
public $incrementing = false;
2015-12-13 12:42:28 +08: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.
*/
protected $hidden = ['lyrics', 'updated_at', 'path', 'mtime'];
2015-12-13 12:42:28 +08:00
protected $casts = [
2015-12-20 21:18:00 -05:00
'length' => 'float',
2016-03-22 16:22:39 +08:00
'mtime' => 'int',
2016-03-28 21:18:09 +08:00
'track' => 'int',
'disc' => 'int',
];
2020-09-06 20:21:39 +02:00
protected $keyType = 'string';
2016-03-05 17:01:12 +08:00
/**
* Update song info.
*
2020-12-22 21:11:22 +01: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 17:01:12 +08:00
*/
2018-08-29 13:30:39 +07:00
public static function updateInfo(array $ids, array $data): Collection
2016-03-05 17:01:12 +08:00
{
2016-03-05 23:11:28 -05:00
/*
2017-04-24 00:01:02 +08:00
* A collection of the updated songs.
2016-03-05 17:01:12 +08:00
*
2017-08-05 17:56:11 +01:00
* @var Collection
2016-03-05 17:01:12 +08:00
*/
2017-04-24 00:01:02 +08:00
$updatedSongs = collect();
2016-03-05 17:01:12 +08:00
2016-05-27 11:32:52 +08: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) {
2020-12-23 00:01:49 +01:00
/** @var Song|null $song */
2019-06-30 16:22:53 +02:00
$song = self::with('album', 'album.artist')->find($id);
if (!$song) {
2016-03-05 17:01:12 +08:00
continue;
}
2017-04-24 00:01:02 +08:00
$updatedSongs->push($song->updateSingle(
2016-05-27 11:32:52 +08:00
$single ? trim($data['title']) : $song->title,
2016-04-24 12:37:04 +08:00
trim($data['albumName'] ?: $song->album->name),
trim($data['artistName']) ?: $song->artist->name,
$single ? trim($data['lyrics']) : $song->lyrics,
2016-08-16 23:12:35 +08:00
$single ? (int) $data['track'] : $song->track,
(int) $data['compilationState']
2017-04-24 00:01:02 +08:00
));
2016-03-05 17:01:12 +08:00
}
// Our library may have been changed. Broadcast an event to tidy it up if need be.
2017-04-24 00:01:02 +08:00
if ($updatedSongs->count()) {
2016-03-05 17:01:12 +08:00
event(new LibraryChanged());
}
2018-08-29 13:30:39 +07:00
return $updatedSongs;
2016-03-05 17:01:12 +08:00
}
2018-08-24 17:27:19 +02:00
public function updateSingle(
string $title,
string $albumName,
string $artistName,
string $lyrics,
int $track,
int $compilationState
): self {
2016-04-24 12:37:04 +08:00
if ($artistName === Artist::VARIOUS_NAME) {
2017-04-24 00:01:02 +08:00
// If the artist name is "Various Artists", it's a compilation song no matter what.
2016-04-24 12:37:04 +08:00
$compilationState = 1;
2017-04-24 00:01:02 +08:00
// and since we can't determine the real contributing artist, it's "Unknown"
$artistName = Artist::UNKNOWN_NAME;
2016-04-24 12:37:04 +08:00
}
$artist = Artist::getOrCreate($artistName);
2017-04-24 00:01:02 +08:00
switch ($compilationState) {
case 1: // ALL, or forcing compilation status to be Yes
$isCompilation = true;
break;
2020-12-22 21:11:22 +01:00
2017-04-24 00:01:02 +08:00
case 2: // Keep current compilation status
$isCompilation = $this->album->artist_id === Artist::VARIOUS_ID;
break;
2020-12-22 21:11:22 +01:00
2017-04-24 00:01:02 +08:00
default:
$isCompilation = false;
break;
2016-04-24 12:37:04 +08:00
}
$album = Album::getOrCreate($artist, $albumName, $isCompilation);
2016-04-24 12:37:04 +08:00
$this->artist_id = $artist->id;
2016-04-24 12:37:04 +08:00
$this->album_id = $album->id;
2016-05-27 10:41:46 +08:00
$this->title = $title;
2016-04-24 12:37:04 +08:00
$this->lyrics = $lyrics;
$this->track = $track;
$this->save();
2017-04-24 00:01:02 +08: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 12:37:04 +08:00
2017-04-24 00:01:02 +08:00
return $this;
2016-04-24 12:37:04 +08:00
}
2021-06-05 12:47:56 +02:00
public function artist(): BelongsTo
{
return $this->belongsTo(Artist::class);
}
public function album(): BelongsTo
{
return $this->belongsTo(Album::class);
}
public function playlists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class);
}
public function interactions(): HasMany
{
return $this->hasMany(Interaction::class);
}
2016-02-02 15:47:00 +08:00
/**
* Scope a query to only include songs in a given directory.
*/
2018-08-24 17:27:19 +02:00
public function scopeInDirectory(Builder $query, string $path): Builder
2016-02-02 15:47:00 +08:00
{
// Make sure the path ends with a directory separator.
2020-12-22 21:11:22 +01:00
$path = rtrim(trim($path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
2016-02-02 15:47:00 +08:00
return $query->where('path', 'LIKE', "$path%");
}
2015-12-13 12:42:28 +08:00
/**
* Sometimes the tags extracted from getID3 are HTML entity encoded.
* This makes sure they are always sane.
*/
2018-08-24 17:27:19 +02:00
public function setTitleAttribute(string $value): void
2015-12-13 12:42:28 +08: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 17:27:19 +02:00
public function getTitleAttribute(?string $value): string
2015-12-13 12:42:28 +08:00
{
return $value ?: pathinfo($this->path, PATHINFO_FILENAME);
}
/**
* Prepare the lyrics for displaying.
*/
2018-08-24 17:27:19 +02:00
public function getLyricsAttribute(string $value): string
2015-12-13 12:42:28 +08:00
{
2016-06-04 22:17:24 +08:00
// We don't use nl2br() here, because the function actually preserves line breaks -
2016-04-05 15:38:10 +08:00
// it just _appends_ a "<br />" after each of them. This would cause our client
2016-06-04 22:17:24 +08:00
// implementation of br2nl to fail with duplicated line breaks.
2016-03-06 15:44:38 +08:00
return str_replace(["\r\n", "\r", "\n"], '<br />', $value);
2015-12-13 12:42:28 +08:00
}
2016-04-17 23:38:06 +08:00
2016-06-13 17:04:42 +08:00
/**
2016-06-13 17:11:41 +08:00
* Get the bucket and key name of an S3 object.
2016-06-13 17:04:42 +08:00
*
2020-12-22 21:11:22 +01:00
* @return array<string>|null
2016-06-13 17:04:42 +08:00
*/
2018-08-24 17:27:19 +02:00
public function getS3ParamsAttribute(): ?array
2016-06-13 17:04:42 +08:00
{
if (!preg_match('/^s3:\\/\\/(.*)/', $this->path, $matches)) {
2018-08-24 17:27:19 +02:00
return null;
2016-06-13 17:04:42 +08:00
}
2020-09-06 20:21:39 +02:00
[$bucket, $key] = explode('/', $matches[1], 2);
2016-06-13 17:04:42 +08:00
return compact('bucket', 'key');
}
2017-06-24 21:46:55 +01:00
2020-12-23 11:53:00 +01:00
/** @return array<mixed> */
public function toSearchableArray(): array
{
$array = [
'id' => $this->id,
'title' => $this->title,
];
if (!$this->artist->is_unknown && !$this->artist->is_various) {
$array['artist'] = $this->artist->name;
}
return $array;
}
2020-12-22 21:11:22 +01:00
public function __toString(): string
2017-06-24 21:46:55 +01:00
{
return $this->id;
}
2015-12-13 12:42:28 +08:00
}