Просмотр файла app/Models/File.php

Размер файла: 1.75Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Exception;
  8. use Illuminate\Database\Eloquent\Relations\MorphTo;
  9.  
  10. /**
  11. * Class File
  12. *
  13. * @property int id
  14. * @property string relate_type
  15. * @property int relate_id
  16. * @property string hash
  17. * @property string name
  18. * @property int size
  19. * @property int user_id
  20. * @property int created_at
  21. * @property string extension
  22. * @property BaseModel relate
  23. */
  24. class File extends BaseModel
  25. {
  26. /**
  27. * Indicates if the model should be timestamped.
  28. *
  29. * @var bool
  30. */
  31. public $timestamps = false;
  32.  
  33. /**
  34. * The attributes that aren't mass assignable.
  35. *
  36. * @var array
  37. */
  38. protected $guarded = [];
  39.  
  40. /**
  41. * Возвращает связанные объекты
  42. *
  43. * @return MorphTo
  44. */
  45. public function relate(): MorphTo
  46. {
  47. return $this->morphTo('relate');
  48. }
  49.  
  50. /**
  51. * Возвращает расширение файла
  52. *
  53. * @return string
  54. */
  55. public function getExtensionAttribute(): string
  56. {
  57. return getExtension($this->hash);
  58. }
  59.  
  60. /**
  61. * Является ли файл картинкой
  62. *
  63. * @return bool
  64. */
  65. public function isImage(): bool
  66. {
  67. return in_array($this->extension, ['jpg', 'jpeg', 'gif', 'png'], true);
  68. }
  69.  
  70. /**
  71. * Является ли файл аудио
  72. *
  73. * @return bool
  74. */
  75. public function isAudio(): bool
  76. {
  77. return $this->extension === 'mp3';
  78. }
  79.  
  80. /**
  81. * Удаление записи и загруженных файлов
  82. *
  83. * @return bool|null
  84. * @throws Exception
  85. */
  86. public function delete(): ?bool
  87. {
  88. deleteFile(public_path($this->hash));
  89.  
  90. return parent::delete();
  91. }
  92. }