koel/app/Models/Song.php

264 lines
8.1 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
use App\Builders\SongBuilder;
2024-05-19 05:49:42 +00:00
use App\Casts\Podcast\EpisodeMetadataCast;
use App\Enums\PlayableType;
2024-04-18 17:20:14 +00:00
use App\Enums\SongStorageType;
use App\Models\Concerns\SupportsDeleteWhereValueNotIn;
2024-02-05 11:50:06 +00:00
use App\Values\SongStorageMetadata\DropboxMetadata;
use App\Values\SongStorageMetadata\LocalMetadata;
use App\Values\SongStorageMetadata\S3CompatibleMetadata;
2024-04-26 13:35:26 +00:00
use App\Values\SongStorageMetadata\S3LambdaMetadata;
use App\Values\SongStorageMetadata\SftpMetadata;
use App\Values\SongStorageMetadata\SongStorageMetadata;
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;
2024-05-19 05:49:42 +00:00
use PhanAn\Poddle\Values\EpisodeMetadata;
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
2024-05-19 05:49:42 +00:00
* @property ?Artist $artist
* @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
* @property-read PlayableType $type
2024-05-19 05:49:42 +00:00
*
* // Podcast episode properties
* @property ?EpisodeMetadata $episode_metadata
* @property ?string $episode_guid
* @property ?string $podcast_id
2024-05-19 05:49:42 +00:00
* @property ?Podcast $podcast
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',
2024-05-19 05:49:42 +00:00
'episode_metadata' => EpisodeMetadataCast::class,
];
2024-05-19 05:49:42 +00:00
protected $with = ['album', 'artist', 'podcast'];
2020-09-06 18:21:39 +00:00
protected $keyType = 'string';
2022-08-01 10:42:33 +00:00
protected static function booted(): void
{
2024-05-19 05:49:42 +00:00
static::creating(static function (self $song): void {
$song->id ??= Str::uuid()->toString();
});
2022-08-01 10:42:33 +00:00
}
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);
}
2024-05-19 05:49:42 +00:00
protected function albumArtist(): Attribute
{
return Attribute::get(fn () => $this->album?->artist);
}
public function podcast(): BelongsTo
2022-10-12 09:27:35 +00:00
{
2024-05-19 05:49:42 +00:00
return $this->belongsTo(Podcast::class);
2022-10-12 09:27:35 +00:00
}
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 type(): Attribute
{
return Attribute::get(fn () => $this->podcast_id ? PlayableType::PODCAST_EPISODE : PlayableType::SONG);
}
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
{
2024-05-19 05:49:42 +00:00
if ($this->isEpisode()) {
return $user->subscribedToPodcast($this->podcast);
}
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-26 13:35:26 +00:00
case SongStorageType::SFTP:
preg_match('/^sftp:\\/\\/(.*)/', $this->path, $matches);
return SftpMetadata::make($matches[1]);
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);
2024-04-26 13:35:26 +00:00
return S3LambdaMetadata::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,
2024-05-19 05:49:42 +00:00
'type' => $this->type->value,
2020-12-23 10:53:00 +00:00
];
2024-05-19 05:49:42 +00:00
if ($this->episode_metadata?->description) {
$array['episode_description'] = $this->episode_metadata->description;
}
if ($this->artist && !$this->artist->is_unknown && !$this->artist->is_various) {
2020-12-23 10:53:00 +00:00
$array['artist'] = $this->artist->name;
}
return $array;
}
2024-05-19 05:49:42 +00:00
public function isEpisode(): bool
{
return $this->type === PlayableType::PODCAST_EPISODE;
2024-05-19 05:49:42 +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
}