koel/app/Models/Song.php

220 lines
6.7 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
use App\Builders\SongBuilder;
2024-04-18 17:20:14 +00:00
use App\Enums\SongStorageType;
use App\Models\Concerns\SupportsDeleteWhereValueNotIn;
use App\Values\SongStorageMetadata\Contracts\SongStorageMetadata;
2024-02-05 11:50:06 +00:00
use App\Values\SongStorageMetadata\DropboxMetadata;
use App\Values\SongStorageMetadata\LegacyS3Metadata;
use App\Values\SongStorageMetadata\LocalMetadata;
use App\Values\SongStorageMetadata\S3CompatibleMetadata;
2022-07-27 15:32:36 +00:00
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
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;
use Illuminate\Database\Eloquent\Relations\HasMany;
2022-08-01 10:42:33 +00:00
use Illuminate\Support\Str;
2020-12-23 10:53:00 +00:00
use Laravel\Scout\Searchable;
use Throwable;
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
2024-01-03 17:02:18 +00:00
* @property User $uploader
2020-12-22 20:11:22 +00:00
* @property Artist $artist
2022-10-12 09:27:35 +00:00
* @property Artist $album_artist
2020-12-22 20:11:22 +00:00
* @property float $length
* @property string $lyrics
* @property int $track
* @property int $disc
* @property int $album_id
* @property int|null $year
* @property string $genre
2020-12-22 20:11:22 +00:00
* @property string $id
* @property int $artist_id
* @property int $mtime
2022-07-27 15:32:36 +00:00
* @property ?bool $liked Whether the song is liked by the current user (dynamically calculated)
* @property ?int $play_count The number of times the song has been played by the current user (dynamically calculated)
* @property Carbon $created_at
2024-01-03 17:02:18 +00:00
* @property int $owner_id
* @property bool $is_public
* @property User $owner
2024-02-05 11:50:06 +00:00
* @property-read SongStorageMetadata $storage_metadata
2024-04-18 17:20:14 +00:00
* @property SongStorageType $storage
2024-01-18 11:13:05 +00:00
*
* // The following are only available for collaborative playlists
* @property-read ?string $collaborator_email The email of the user who added the song to the playlist
* @property-read ?string $collaborator_name The name of the user who added the song to the playlist
2024-01-24 22:39:47 +00:00
* @property-read ?int $collaborator_id The ID of the user who added the song to the playlist
2024-01-18 11:13:05 +00:00
* @property-read ?string $added_at The date the song was added to the playlist
2015-12-13 04:42:28 +00:00
*/
class Song extends Model
{
use HasFactory;
2020-12-23 10:53:00 +00:00
use Searchable;
2022-08-01 10:42:33 +00:00
use SupportsDeleteWhereValueNotIn;
2016-09-26 07:32:16 +00:00
2022-08-01 10:42:33 +00:00
public const ID_REGEX = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
2021-06-05 10:47:56 +00:00
public $incrementing = false;
2015-12-13 04:42:28 +00:00
protected $guarded = [];
protected $hidden = ['updated_at', 'path', 'mtime'];
2015-12-13 04:42:28 +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',
'disc' => 'int',
2024-01-03 17:02:18 +00:00
'is_public' => 'bool',
];
2020-09-06 18:21:39 +00:00
protected $keyType = 'string';
2022-08-01 10:42:33 +00:00
protected static function booted(): void
{
static::creating(static fn (self $song) => $song->id = Str::uuid()->toString());
}
public static function query(): SongBuilder
{
return parent::query();
}
2024-01-03 17:02:18 +00:00
public function owner(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function newEloquentBuilder($query): SongBuilder
{
return new SongBuilder($query);
}
2021-06-05 10:47:56 +00:00
public function artist(): BelongsTo
{
return $this->belongsTo(Artist::class);
}
public function album(): BelongsTo
{
return $this->belongsTo(Album::class);
}
2022-10-12 09:27:35 +00:00
public function album_artist(): BelongsTo // @phpcs:ignore
{
return $this->album->artist();
}
2021-06-05 10:47:56 +00:00
public function playlists(): BelongsToMany
{
return $this->belongsToMany(Playlist::class);
}
public function interactions(): HasMany
{
return $this->hasMany(Interaction::class);
}
protected function title(): Attribute
2015-12-13 04:42:28 +00:00
{
return new Attribute(
get: fn (?string $value) => $value ?: pathinfo($this->path, PATHINFO_FILENAME),
set: static fn (string $value) => html_entity_decode($value)
);
2015-12-13 04:42:28 +00:00
}
2024-01-03 17:02:18 +00:00
public function accessibleBy(User $user): bool
{
return $this->is_public || $this->ownedBy($user);
}
public function ownedBy(User $user): bool
{
return $this->owner_id === $user->id;
2024-01-03 17:02:18 +00:00
}
protected function lyrics(): Attribute
{
$normalizer = static function (?string $value): string {
// Since we're displaying the lyrics using <pre>, replace breaks with newlines and strip all tags.
$value = strip_tags(preg_replace('#<br\s*/?>#i', PHP_EOL, $value));
// also remove the timestamps that often come with LRC files
return preg_replace('/\[\d{2}:\d{2}.\d{2}]\s*/m', '', $value);
};
return new Attribute(get: $normalizer, set: $normalizer);
}
2024-04-18 17:20:14 +00:00
protected function storage(): Attribute
{
return new Attribute(
get: static fn (?string $raw) => SongStorageType::tryFrom($raw) ?? SongStorageType::LOCAL,
set: static function (SongStorageType|string|null $type) {
$type = $type instanceof SongStorageType ? $type : SongStorageType::tryFrom($type);
return $type->value;
}
);
}
protected function storageMetadata(): Attribute
{
return new Attribute(
2024-02-05 11:50:06 +00:00
get: function (): SongStorageMetadata {
try {
switch ($this->storage) {
2024-04-18 17:20:14 +00:00
case SongStorageType::S3:
preg_match('/^s3:\\/\\/(.*)\\/(.*)/', $this->path, $matches);
return S3CompatibleMetadata::make($matches[1], $matches[2]);
2024-04-18 17:20:14 +00:00
case SongStorageType::S3_LAMBDA:
preg_match('/^s3:\\/\\/(.*)\\/(.*)/', $this->path, $matches);
return LegacyS3Metadata::make($matches[1], $matches[2]);
2024-04-18 17:20:14 +00:00
case SongStorageType::DROPBOX:
preg_match('/^dropbox:\\/\\/(.*)/', $this->path, $matches);
return DropboxMetadata::make($matches[1]);
default:
return LocalMetadata::make($this->path);
}
} catch (Throwable) {
return LocalMetadata::make($this->path);
2024-02-05 11:50:06 +00:00
}
}
);
}
public static function getPathFromS3BucketAndKey(string $bucket, string $key): string
2015-12-13 04:42:28 +00:00
{
return "s3://$bucket/$key";
2015-12-13 04:42:28 +00:00
}
2016-04-17 15:38:06 +00:00
2020-12-23 10:53:00 +00: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 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
}