File.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Str;
  9. class File extends Model
  10. {
  11. use HasFactory;
  12. protected $fillable = [
  13. 'user_id',
  14. 'original_name',
  15. 'mime_type',
  16. 'path',
  17. 'link',
  18. 'is_generated',
  19. ];
  20. protected $appends = [
  21. 'name',
  22. ];
  23. protected $casts = [
  24. 'is_generated' => 'boolean',
  25. ];
  26. public function user(): BelongsTo
  27. {
  28. return $this->belongsTo(User::class);
  29. }
  30. public function name(): Attribute
  31. {
  32. return Attribute::make(
  33. get: fn () => $this->original_name ?? '',
  34. );
  35. }
  36. public function thumbnailPath(): Attribute
  37. {
  38. return Attribute::make(
  39. get: function () {
  40. if (!$this->path || Str::contains(pathinfo($this->path, PATHINFO_FILENAME), '.thumbnail')) {
  41. return $this->path;
  42. }
  43. $directory = pathinfo($this->path, PATHINFO_DIRNAME);
  44. $filename = pathinfo($this->path, PATHINFO_FILENAME);
  45. $extension = pathinfo($this->path, PATHINFO_EXTENSION);
  46. $thumbnailName = $filename . '.thumbnail' . ($extension !== '' ? '.' . $extension : '');
  47. return $directory === '.' ? $thumbnailName : $directory . '/' . $thumbnailName;
  48. },
  49. );
  50. }
  51. public function thumbnailLink(): Attribute
  52. {
  53. return Attribute::make(
  54. get: function () {
  55. $thumbnailPath = $this->thumbnail_path;
  56. if ($thumbnailPath && Storage::disk('public')->exists($thumbnailPath)) {
  57. return url('/storage/' . $thumbnailPath);
  58. }
  59. return $this->link;
  60. },
  61. );
  62. }
  63. public function isImage(): bool
  64. {
  65. return Str::startsWith(Str::lower((string) $this->mime_type), 'image/');
  66. }
  67. public function iconClass(): string
  68. {
  69. $mimeType = Str::lower((string) $this->mime_type);
  70. return match (true) {
  71. Str::startsWith($mimeType, 'image/') => 'bi bi-file-earmark-image',
  72. $mimeType === 'application/pdf' => 'bi bi-file-earmark-pdf',
  73. $mimeType === 'application/msword', Str::contains($mimeType, 'wordprocessingml') => 'bi bi-file-earmark-word',
  74. $mimeType === 'application/vnd.ms-excel', Str::contains($mimeType, 'spreadsheetml') => 'bi bi-file-earmark-excel',
  75. $mimeType === 'application/vnd.ms-powerpoint', Str::contains($mimeType, 'presentationml') => 'bi bi-file-earmark-ppt',
  76. Str::startsWith($mimeType, 'audio/') => 'bi bi-file-earmark-music',
  77. Str::startsWith($mimeType, 'video/') => 'bi bi-file-earmark-play',
  78. Str::startsWith($mimeType, 'text/'), Str::contains($mimeType, 'json'), Str::contains($mimeType, 'xml') => 'bi bi-file-earmark-code',
  79. Str::contains($mimeType, 'zip'), Str::contains($mimeType, 'rar'), Str::contains($mimeType, '7z'), Str::contains($mimeType, 'gzip') => 'bi bi-file-earmark-zip',
  80. default => 'bi bi-file-earmark',
  81. };
  82. }
  83. }