2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
use App\Builders\SongBuilder;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Models\Concerns\SupportsDeleteWhereValueNotIn;
|
|
|
|
use App\Values\SongStorageMetadata\Contracts\SongStorageMetadata;
|
2024-02-05 11:50:06 +00:00
|
|
|
use App\Values\SongStorageMetadata\DropboxMetadata;
|
2024-02-05 13:27:17 +00:00
|
|
|
use App\Values\SongStorageMetadata\LegacyS3Metadata;
|
2024-02-04 20:31:01 +00:00
|
|
|
use App\Values\SongStorageMetadata\LocalMetadata;
|
|
|
|
use App\Values\SongStorageMetadata\S3CompatibleMetadata;
|
2024-02-05 21:17:41 +00:00
|
|
|
use App\Values\SongStorageTypes;
|
2022-07-27 15:32:36 +00:00
|
|
|
use Carbon\Carbon;
|
2022-07-06 11:05:21 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
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;
|
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-02-05 21:17:41 +00:00
|
|
|
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
|
2022-09-23 06:21:29 +00:00
|
|
|
* @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
|
2024-01-09 23:26:16 +00:00
|
|
|
* @property User $owner
|
2024-02-05 11:50:06 +00:00
|
|
|
* @property-read SongStorageMetadata $storage_metadata
|
2024-02-05 21:17:41 +00:00
|
|
|
* @property ?string $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
|
|
|
|
{
|
2020-11-14 16:57:25 +00:00
|
|
|
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 = [];
|
|
|
|
|
2022-07-06 11:05:21 +00:00
|
|
|
protected $hidden = ['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',
|
2024-01-03 17:02:18 +00:00
|
|
|
'is_public' => 'bool',
|
2015-12-20 18:09:34 +00:00
|
|
|
];
|
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
protected $keyType = 'string';
|
2018-11-03 23:25:08 +00:00
|
|
|
|
2022-08-01 10:42:33 +00:00
|
|
|
protected static function booted(): void
|
|
|
|
{
|
|
|
|
static::creating(static fn (self $song) => $song->id = Str::uuid()->toString());
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
static::saving(static function (self $song): void {
|
|
|
|
if ($song->storage === '') {
|
|
|
|
$song->storage = SongStorageTypes::LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SongStorageTypes::assertValidType($song->storage);
|
|
|
|
});
|
2022-08-01 10:42:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 18:45:11 +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);
|
|
|
|
}
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:05:21 +00:00
|
|
|
protected function title(): Attribute
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-07-06 11:05:21 +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-01-27 11:24:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-28 06:05:15 +00:00
|
|
|
protected function lyrics(): Attribute
|
|
|
|
{
|
2022-09-14 12:12:06 +00:00
|
|
|
$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);
|
|
|
|
};
|
2022-07-28 06:05:15 +00:00
|
|
|
|
|
|
|
return new Attribute(get: $normalizer, set: $normalizer);
|
|
|
|
}
|
|
|
|
|
2024-02-04 20:31:01 +00:00
|
|
|
protected function storageMetadata(): Attribute
|
|
|
|
{
|
|
|
|
return new Attribute(
|
2024-02-05 11:50:06 +00:00
|
|
|
get: function (): SongStorageMetadata {
|
2024-02-05 21:17:41 +00:00
|
|
|
try {
|
|
|
|
switch ($this->storage) {
|
|
|
|
case 's3':
|
2024-02-05 22:27:47 +00:00
|
|
|
preg_match('/^s3:\\/\\/(.*)\\/(.*)/', $this->path, $matches);
|
2024-02-05 21:17:41 +00:00
|
|
|
return S3CompatibleMetadata::make($matches[1], $matches[2]);
|
|
|
|
|
|
|
|
case 's3-legacy':
|
2024-02-05 22:27:47 +00:00
|
|
|
preg_match('/^s3:\\/\\/(.*)\\/(.*)/', $this->path, $matches);
|
2024-02-05 21:17:41 +00:00
|
|
|
return LegacyS3Metadata::make($matches[1], $matches[2]);
|
|
|
|
|
|
|
|
case '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
|
|
|
}
|
2024-02-04 20:31:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-09 18:45:11 +00:00
|
|
|
public static function getPathFromS3BucketAndKey(string $bucket, string $key): string
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-08-09 18:45:11 +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
|
|
|
}
|