initial commit
This commit is contained in:
51
app/Models/MediaFile.php
Normal file
51
app/Models/MediaFile.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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 '📁';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user