52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class MediaFile extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'client_id', 'name', 'original_name', 'path', 'disk', 'mime_type', 'size', 'folder', 'description',
|
|
];
|
|
|
|
public function client(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Client::class);
|
|
}
|
|
|
|
public function getUrlAttribute(): string
|
|
{
|
|
return route('files.serve', $this->id);
|
|
}
|
|
|
|
public function getHumanSizeAttribute(): string
|
|
{
|
|
$bytes = $this->size;
|
|
if ($bytes >= 1073741824) return round($bytes / 1073741824, 2) . ' GB';
|
|
if ($bytes >= 1048576) return round($bytes / 1048576, 2) . ' MB';
|
|
if ($bytes >= 1024) return round($bytes / 1024, 2) . ' KB';
|
|
return $bytes . ' B';
|
|
}
|
|
|
|
public function getIsImageAttribute(): bool
|
|
{
|
|
return str_starts_with($this->mime_type, 'image/');
|
|
}
|
|
|
|
public function getIconAttribute(): string
|
|
{
|
|
if ($this->is_image) return '🖼️';
|
|
if (str_starts_with($this->mime_type, 'video/')) return '🎬';
|
|
if (str_contains($this->mime_type, 'pdf')) return '📄';
|
|
return '📁';
|
|
}
|
|
}
|