| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- class File extends Model
- {
- use HasFactory;
- protected $fillable = [
- 'user_id',
- 'original_name',
- 'mime_type',
- 'path',
- 'link',
- 'is_generated',
- ];
- protected $appends = [
- 'name',
- ];
- protected $casts = [
- 'is_generated' => 'boolean',
- ];
- public function user(): BelongsTo
- {
- return $this->belongsTo(User::class);
- }
- public function name(): Attribute
- {
- return Attribute::make(
- get: fn () => $this->original_name ?? '',
- );
- }
- public function thumbnailPath(): Attribute
- {
- return Attribute::make(
- get: function () {
- if (!$this->path || Str::contains(pathinfo($this->path, PATHINFO_FILENAME), '.thumbnail')) {
- return $this->path;
- }
- $directory = pathinfo($this->path, PATHINFO_DIRNAME);
- $filename = pathinfo($this->path, PATHINFO_FILENAME);
- $extension = pathinfo($this->path, PATHINFO_EXTENSION);
- $thumbnailName = $filename . '.thumbnail' . ($extension !== '' ? '.' . $extension : '');
- return $directory === '.' ? $thumbnailName : $directory . '/' . $thumbnailName;
- },
- );
- }
- public function thumbnailLink(): Attribute
- {
- return Attribute::make(
- get: function () {
- $thumbnailPath = $this->thumbnail_path;
- if ($thumbnailPath && Storage::disk('public')->exists($thumbnailPath)) {
- return url('/storage/' . $thumbnailPath);
- }
- return $this->link;
- },
- );
- }
- public function isImage(): bool
- {
- return Str::startsWith(Str::lower((string) $this->mime_type), 'image/');
- }
- public function iconClass(): string
- {
- $mimeType = Str::lower((string) $this->mime_type);
- return match (true) {
- Str::startsWith($mimeType, 'image/') => 'bi bi-file-earmark-image',
- $mimeType === 'application/pdf' => 'bi bi-file-earmark-pdf',
- $mimeType === 'application/msword', Str::contains($mimeType, 'wordprocessingml') => 'bi bi-file-earmark-word',
- $mimeType === 'application/vnd.ms-excel', Str::contains($mimeType, 'spreadsheetml') => 'bi bi-file-earmark-excel',
- $mimeType === 'application/vnd.ms-powerpoint', Str::contains($mimeType, 'presentationml') => 'bi bi-file-earmark-ppt',
- Str::startsWith($mimeType, 'audio/') => 'bi bi-file-earmark-music',
- Str::startsWith($mimeType, 'video/') => 'bi bi-file-earmark-play',
- Str::startsWith($mimeType, 'text/'), Str::contains($mimeType, 'json'), Str::contains($mimeType, 'xml') => 'bi bi-file-earmark-code',
- Str::contains($mimeType, 'zip'), Str::contains($mimeType, 'rar'), Str::contains($mimeType, '7z'), Str::contains($mimeType, 'gzip') => 'bi bi-file-earmark-zip',
- default => 'bi bi-file-earmark',
- };
- }
- }
|