Media-type detection was duplicated across Media, MediaItem (byte-identical copies), HasMedia::getMediaType, the ContentTypeCompatibleWithMedia rule, and six publishers — each hardcoding MIME prefixes and divergent extension lists (Media's video list even had avi/webm/mkv, contradicting the Type enum's mp4/mov). Add classify(), fromExtension(), and isGif() to Media\Type as the single source for 'what kind is this?' (broad classification), distinct from fromMime()/allowedMimeTypes() (the strict upload allow-list). Every detector now delegates to the enum; the broad extension lists and MIME prefixes live only there. Behavior-preserving (full suite green); adds direct tests for classify/fromExtension/isGif.
98 lines
2.3 KiB
PHP
98 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Media\Type as MediaType;
|
|
use Database\Factories\MediaFactory;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Concerns\HasUuids;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class Media extends Model
|
|
{
|
|
/** @use HasFactory<MediaFactory> */
|
|
use HasFactory, HasUuids;
|
|
|
|
protected $table = 'medias';
|
|
|
|
protected $appends = ['url'];
|
|
|
|
protected $fillable = [
|
|
'mediable_id',
|
|
'mediable_type',
|
|
'group_id',
|
|
'collection',
|
|
'type',
|
|
'path',
|
|
'original_filename',
|
|
'mime_type',
|
|
'size',
|
|
'order',
|
|
'meta',
|
|
'upload_token',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'type' => MediaType::class,
|
|
'size' => 'integer',
|
|
'order' => 'integer',
|
|
'meta' => 'array',
|
|
];
|
|
}
|
|
|
|
public function mediable(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
protected function url(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => Storage::url($this->path),
|
|
);
|
|
}
|
|
|
|
public function isVideo(): bool
|
|
{
|
|
return MediaType::classify($this->mime_type, $this->path) === MediaType::Video;
|
|
}
|
|
|
|
public function isImage(): bool
|
|
{
|
|
return MediaType::classify($this->mime_type, $this->path) === MediaType::Image;
|
|
}
|
|
|
|
public function isDocument(): bool
|
|
{
|
|
return MediaType::classify($this->mime_type, $this->path) === MediaType::Document;
|
|
}
|
|
|
|
public function getTemporaryUrl(int $expirationMinutes = 60): string
|
|
{
|
|
return Storage::temporaryUrl(
|
|
$this->path,
|
|
now()->addMinutes($expirationMinutes)
|
|
);
|
|
}
|
|
|
|
public function delete(): bool
|
|
{
|
|
// Only delete the file if no other media records use the same path
|
|
$otherMediaWithSamePath = static::where('path', $this->path)
|
|
->where('id', '!=', $this->id)
|
|
->exists();
|
|
|
|
if (! $otherMediaWithSamePath) {
|
|
Storage::delete($this->path);
|
|
}
|
|
|
|
return parent::delete();
|
|
}
|
|
}
|